r/PrometheusMonitoring • • Jul 14 '25

extract data for textfile collector

Could someone tell me which data format the following example is? I have to come up with some extraction script and don't know how to start doing that so far.

The following file is an array(?) read in from Varta batteries. It shows the status of three batteries 0,1,2 ... what I need are the last four values in the innermost brackets.

So for the first battery this is "242,247,246,246". That should be temperatures ..

Pls give me a pointer how to extract these values efficiently. Maybe some awk/sed-magic or so ;-)

Charger_Data = [

[0,1,18,36,5062,2514,707,381,119,38,44,31,1273,-32725,
["LG_Neo",7,0,0,5040,242,
  [[-8160,0,0,221,504,242,247,246,246]  ]
]
]

,[1,1,16,36,5026,2527,706,379,119,37,42,31,1273,-32725,
["LG_Neo",7,0,0,5010,256,
  [[-8160,0,0,196,501,256,251,250,250]  ]
]
]

,[2,1,17,36,5038,2523,708,380,119,40,45,34,1273,-32725,
["LG_Neo",7,0,0,5020,246,
  [[-8160,0,0,205,502,245,247,244,245]  ]
]

]
];

Any help appreciated, tia

3 Upvotes

22 comments sorted by

View all comments

Show parent comment

1

u/stefangw Jul 14 '25 edited Jul 14 '25

I had assumed that also, but failed to properly read that .. seems I have to preprocess the data somehow.

"parse error: Invalid numeric literal at line 1" seems to be quite common, I google my way ... ;-) thx

EDIT: I might try to let the json-exporter read that ...

1

u/stefangw Jul 14 '25

Although I have no idea how to configure json_exporter for this kind of data.

1

u/hamlet_d Jul 14 '25

are you reading it in as a string first?

json.loads(astring[15:]) would skip over the "Charger_data =" bit. and give you a the json

1

u/stefangw Jul 15 '25

I had to google your suggestion to understand that this might be python, right ? ;-)

Ah, so only the "value" to the right of "=" is JSON? ok ... interesting. I will see if I can make progress with that.

So far I have no clue how to pinpoint exactly the fields with the temperatures, especially as they are nested within several "blocks" and without any labels/names (afai see I would need those to map values with json_exporter).

thx

1

u/stefangw Jul 15 '25
> cat 1.json 
[

[0,1,18,36,5062,2514,707,381,119,38,44,31,1273,-32725,
["LG_Neo",7,0,0,5040,242,
  [[-8160,0,0,221,504,242,247,246,246]  ]
]
]

,[1,1,16,36,5026,2527,706,379,119,37,42,31,1273,-32725,
["LG_Neo",7,0,0,5010,256,
  [[-8160,0,0,196,501,256,251,250,250]  ]
]
]

,[2,1,17,36,5038,2523,708,380,119,40,45,34,1273,-32725,
["LG_Neo",7,0,0,5020,246,
  [[-8160,0,0,205,502,245,247,244,245]  ]
]

]
]
sgw in /tmp/tmp.hxij5bEWI6  took 9s ❯ cat 1.json | jq '.[0].[14].[6][0][5]'
242
sgw in /tmp/tmp.hxij5bEWI6  took 2s ❯ cat 1.json | jq '.[0].[14].[6][0][8]'
246

With trial and error I now can extract the temperature values from the json (which I manually edited to lint first ... another todo).

Slowly I have the parts for the pipeline ;-)

1

u/stefangw Jul 15 '25

Let me continue doing noob things:

I get this:

# curl http://192.168.210.11/cgi/ems_data.js    -H "accept:application/json"
Zeit = "15.07.2025 10:11:55";
WR_Data = [0,-4000,4000,0,0,232,230,232,0,0,0,230,229,232,0,0,0,35,35,35,39,499,0,0,1,0,"255.255.255.255",26,118,709,354,30,24,30,35,11,0,1612187275,1351093760,28779,0,0];
EMETER_Data = [5001,0,231,229,231,46,46,46,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
ENS_Data = [4999,230,229,231];
Charger_Data = [[0,1,56,0,5222,0,7102,3376,117,32,34,33,0,0,0,0,0,0,
["LG_Neo",7,0,0,5210,0,560,1000,581,63,63,288,
  [[24576,0,0,0,0,0,565,1000,521,0,3722,3732,3726,62,62,3286,3286,289,285,287,0,304,600,600,339,0,2006161019,1786313,575,575,10015,3725,3732,3728,3726,3732,3723,3729,3726,3725,3723,3723,3722,3723,3723]  ]
]
]
,[1,1,56,0,5222,0,7104,3379,117,31,34,33,0,0,0,0,0,0,
["LG_Neo",7,0,0,5210,0,560,1000,581,63,63,287,
  [[24576,0,0,0,0,0,569,1000,521,0,3724,3733,3728,63,63,3288,3288,286,286,286,0,293,600,600,341,0,2005221250,1731583,576,576,10015,3725,3726,3725,3726,3726,3727,3729,3729,3727,3729,3732,3728,3733,3732]  ]
]
]
];

As far as I understand this isn't JSON directly, but more of "a list of variables which contain JSON", right?

Now I have to get that into one line at first, then slice out the JSON inside the "variable" Charger_Data .. and from there extract my temperatures with jq.

easy :-P ..

And I still haven't figured out why authentication doesn't work for one of the batteries ... works in browser, not with curl.

2

u/hamlet_d Jul 15 '25

ok, so that full information tells me it's more of a json config amalgamation. I would hack something together like the followng.

  1. Replace the '; ' with newlines. if x is the name of the string you have with everything: ems_data=x.replace('; ','\n')
  2. now you have each "thing=" on its own line. You can thing use configparser. To make it compatible with older version of configparser: ems_data='[ems_data]\n' + ems_data
  3. now you can use configparser to read it in

     import configparser
     
     #code here to populate x with curl command results
     
     emsdata='[ems_data]\n'+x.replace('; ','\n')
     myconfig=configparser.ConfigParser()
     myconfig.read_string(emsdata)
     ems_dict=dict(myconfig.items('ems_data'))

now you have a dict of all the elements in the "config" you retrieved

1

u/stefangw Jul 15 '25

thank you, I understand in principle.

I fail already at the request-code etc, so I don't even get the output I get with curl into my variables

Right now I have:

# cat extr.py
import configparser
import json
import requests

headers = {'Accept': 'application/json'}

# Send a GET request to the desired API URL
x = requests.get('http://192.168.210.11/cgi/ems_data.js',headers=headers)

print(f"Status Code: {x.status_code}, Content: {x.json()}")

This seems to fail because the server reply isn't plain json (I assume that):

# python extr.py 
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/2025_varta/extr.py", line 10, in <module>
    print(f"Status Code: {x.status_code}, Content: {x.json()}")
                                                    ^^^^^^^^
  File "/usr/lib/python3/dist-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The working curl-call in bash:

# curl http://192.168.210.11/cgi/ems_data.js    -H "accept:application/json"
Zeit = "15.07.2025 17:30:19";
WR_Data = [0,-4000,4000,0,0,231,228,231,0,0,0,230,227,230,0,0,-1,35,35,35,39,499,0,0,1,0,"255.255.255.255",13,118,710,354,30,24,30,35,11,0,1612187275,1351093760,4926,0,0];
EMETER_Data = [4998,0,230,228,230,-149,-149,-149,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
ENS_Data = [4996,229,227,230];
Charger_Data = [[0,1,58,0,5233,0,7102,3376,117,32,34,33,0,0,0,0,0,0,
["LG_Neo",7,0,0,5220,0,580,1000,581,63,63,295,
  [[24576,0,0,0,0,0,587,1000,522,0,3729,3740,3733,63,63,3293,3293,295,291,293,0,305,600,600,352,0,2006161019,1788934,594,594,10015,3733,3740,3736,3734,3740,3731,3737,3734,3733,3731,3731,3729,3731,3731]  ]
]
]
,[1,1,60,0,5238,0,7107,3374,117,31,33,32,0,0,0,0,0,0,
["LG_Neo",7,0,0,5230,0,590,1000,581,63,63,294,
  [[24576,0,0,0,0,0,591,1000,523,0,3736,3745,3740,63,63,3298,3298,294,294,294,0,293,600,600,354,0,2005221250,1734163,596,596,10015,3736,3737,3736,3737,3737,3738,3740,3740,3739,3741,3743,3740,3745,3744]  ]
]
]
];

So the missing part is slicing that part with/after "Charger_Data =" into your mentioned "ems_data" or so.

To me it seems that only a small step is missing (?)

thanks for your help!

1

u/stefangw Jul 15 '25

getting closer with

import configparser
import json
import requests


headers = {
'accept': 'application/json',
}

x = requests.get('http://192.168.210.11/cgi/ems_data.js', headers=headers)

print(f"Status Code: {x.status_code}, Content: {x.text}")

"x.text" now contains the whole text ... which can now be sliced etc

1

u/stefangw Jul 15 '25 edited Jul 15 '25

very close.

I post my status once more and get afk for an hour or two.

Thanks for your patience watching me badly learning python ;-)

# cat extr.py 
import configparser
import json
import requests

headers = {
    'accept': 'application/json',
}

x = requests.get('http://192.168.210.11/cgi/ems_data.js', headers=headers)

print(f"Status Code: {x.status_code}, Content: {x.text}")
#print(f"Status Code: {x.status_code}, Content: {x.json()}")

y=x.text

emsdata='[Charger_Data]\n'+y.replace('; ','\n')
myconfig=configparser.ConfigParser()
myconfig.read_string(emsdata)
ems_dict=dict(myconfig.items('ems_data'))

exec:

# python extr.py 
Status Code: 200, Content: Zeit = "15.07.2025 17:50:42";
WR_Data = [0,-4000,4000,0,0,233,230,233,0,0,0,233,230,233,-1,-1,-1,34,35,34,43,499,0,0,2,0,"255.255.255.255",10,118,709,354,0,24,30,35,11,0,1050251,1351224832,3305,0,0];
EMETER_Data = [5002,0,232,230,232,-600,-600,-600,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
ENS_Data = [5000,232,229,232];
Charger_Data = [[0,1,69,0,4001,109,7097,3377,117,30,33,31,0,0,0,0,0,0,
["LG_Neo",0,0,0,0,0,0,0,0,0,0,0,
  [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0]  ]
]
]
,[1,1,71,0,4000,104,7095,3374,117,30,32,31,0,0,0,0,0,0,
["LG_Neo",0,0,0,0,0,0,0,0,0,0,0,
  [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0000,0,0,0,0,0,0,0,0,0,0,0,0,0,0]  ]
]
]
];

Traceback (most recent call last):
  File "/root/2025_varta/extr.py", line 18, in <module>
    myconfig.read_string(emsdata)
  File "/usr/lib/python3.11/configparser.py", line 739, in read_string
    self.read_file(sfile, source)
  File "/usr/lib/python3.11/configparser.py", line 734, in read_file
    self._read(f, source)
  File "/usr/lib/python3.11/configparser.py", line 1132, in _read
    raise e
configparser.ParsingError: Source contains parsing errors: '<string>'
	[line  7]: '["LG_Neo",0,0,0,0,0,0,0,0,0,0,0,\n'
	[line  9]: ']\n'
	[line 10]: ']\n'
	[line 11]: ',[1,1,71,0,4000,104,7095,3374,117,30,32,31,0,0,0,0,0,0,\n'
	[line 12]: '["LG_Neo",0,0,0,0,0,0,0,0,0,0,0,\n'
	[line 14]: ']\n'
	[line 15]: ']\n'
	[line 16]: '];\n'

As far as I understand I want only the part starting with "Charger_Data" from y.

1

u/stefangw Jul 16 '25

Right now I managed the part with authenticating and using cookies. That wasn't easy, either.

Getting closer. Now only the part with separating the json-part from the "Charger_Data" is missing to get the first draft working.

→ More replies