When getting channel values for a sensor where the channel has both a volume and speed value using the API, the output is ambiguous and problematic for programmatic usage.
When getting XML output, using /api/table.xml?content=channels&output=xml&columns=name,lastvalue,lastvalue_&id=<SENSORID>
, the output is as follows
<?xml version="1.0" encoding="UTF-8"?> <channels totalcount="0" listend="1"> <prtg-version>18.2.41.1652</prtg-version> <item> <name>Downtime</name> <lastvalue/> </item> <item> <name>Traffic In</name> <lastvalue>6,522 MByte</lastvalue> <lastvalue_raw>6838698632.0000</lastvalue_raw> <lastvalue>912 Mbit/s</lastvalue> <lastvalue_raw>113978310.5333</lastvalue_raw> </item> <item> <name>Traffic Out</name> <lastvalue>1,072 MByte</lastvalue> <lastvalue_raw>1123960141.0000</lastvalue_raw> <lastvalue>150 Mbit/s</lastvalue> <lastvalue_raw>18732669.0167</lastvalue_raw> </item> <item> <name>Traffic Total</name> <lastvalue>7,594 MByte</lastvalue> <lastvalue_raw>7962658773.0000</lastvalue_raw> <lastvalue>1,062 Mbit/s</lastvalue> <lastvalue_raw>132710979.5500</lastvalue_raw> </item> </channels>
The XML nodes are ambiguous, and except for string processing, there is no way of knowing which node is speed and which is volume.
Things get worse when pulling the data in JSON with /api/table.xml?content=channels&output=json&columns=name,lastvalue,lastvalue_&id=<SENSORID>
This is the output:
{ "prtg-version": "18.2.41.1652", "treesize": 0, "channels": [{ "name": "Downtime", "lastvalue": "", "lastvalue": "", "lastvalue_raw": "" }, { "name": "Traffic In", "lastvalue": "6,522 MByte", "lastvalue_raw": 6838698632.0000, "lastvalue": "912 Mbit/s", "lastvalue_raw": 113978310.5333 }, { "name": "Traffic Out", "lastvalue": "1,072 MByte", "lastvalue_raw": 1123960141.0000, "lastvalue": "150 Mbit/s", "lastvalue_raw": 18732669.0167 }, { "name": "Traffic Total", "lastvalue": "7,594 MByte", "lastvalue_raw": 7962658773.0000, "lastvalue": "1,062 Mbit/s", "lastvalue_raw": 132710979.5500 }] }
With JSON, having multiple keys in a hash with the same key value is frowned on (see this SO answer), and most parsing libraries will only use the last instance of the key.
Further, this ambiguous nature of the results combined with the fact that node/value order is not guaranteed by either XML specs or JSON specs, there is no way to ever know for sure which raw value is which in a programmatic way, effectively making those values useless for further processing.
Only CSV (/api/table.xml?content=channels&output=csvtable&columns=name,lastvalue_&id=<SENSORID>
) output will definitively mark the data point as either volume or speed, as shown below.
"Channel","Channel(RAW)","Last Value (volume)","Last Value (volume)(RAW)","Last Value (speed)","Last Value (speed)(RAW)" "Downtime","Downtime","","","","" "Traffic In","Traffic In","3,879 MByte","4067636651.0000","542 Mbit/s","67805245.0575" "Traffic Out","Traffic Out","1,026 MByte","1076308276.0000","144 Mbit/s","17941461.5103" "Traffic Total","Traffic Total","4,906 MByte","5143944927.0000","686 Mbit/s","85746706.5678"
I would like to avoid converting CSV into XML or JSON for further processing, especially if I need to the raw values. Am I missing something in the API docs on how to get around this (outside of using CSV), or does this need to go in as a bug fix/feature request?
Add comment