Glances REST API for Multiple Disks

I’m trying to find a sane way to parse file system information for multiple disks using the Glances REST API. I’ve found this thread, but the accepted solution seemed content to rely on using an array index to assign metrics to a disk. Unfortunately Glances can’t be trusted to always report disk information in the same order (and some disks are not always present).

I’m currently using Glances 2.7.1 because my servers all run Debian Stretch. If this is easily solved by upgrading to Glances 3.x I may pursue that, but I haven’t found any indication that’s the case.

Here is the JSON output of …/api/2/fs:

[{"device_name": "/dev/sdb2", "fs_type": "ext4", "free": 222757384192, "mnt_point": "/", "used": 1574334464, "size": 236412149760, "percent": 0.7, "key": "mnt_point"}, {"device_name": "/dev/sdb1", "fs_type": "vfat", "free": 535670784, "mnt_point": "/boot/efi", "used": 135168, "size": 535805952, "percent": 0.0, "key": "mnt_point"}, {"device_name": "/dev/mapper/sdc", "fs_type": "xfs", "free": 6199598092288, "mnt_point": "/mnt/sdc", "used": 3799094161408, "size": 9998692253696, "percent": 38.0, "key": "mnt_point"}, {"device_name": "/dev/mapper/sdd", "fs_type": "xfs", "free": 4961232416768, "mnt_point": "/mnt/sdd", "used": 5037459836928, "size": 9998692253696, "percent": 50.4, "key": "mnt_point"}]

Technically the yaml below works, but it’s keyed on the array index, which is not reliable. Today /dev/sdb2 is index[0] but tomorrow it might be index[1]:

  - platform: rest
    resource: http://1.2.3.4:61208/api/2/fs
    name: sdb2
    value_template: '{{ value_json[0].used| multiply(0.000000001)| round(1) }}'
    unit_of_measurement: GB
  - platform: rest
    resource: http://1.2.3.4:61208/api/2/fs
    name: sdb1
    value_template: '{{ value_json[1].used| multiply(0.000000001)| round(1) }}'
    unit_of_measurement: GB

This solution also (I believe) polls the API twice, which is not efficient. I have to believe there’s a way to grab the full REST info above, parse the array, and pull out values based on some key other than the index. Unfortunately I’m just not smart or caffeinated enough to figure it out. I did try many permutations of the final example on the RESTful Sensor API doc page but I think it wasn’t working because Glances doesn’t assign a parent for each disk.

Thanks! I’m hoping to use any solution from this to maybe make use of the …/api/2/all output to key in on other data more efficiently as well.

2 Likes

I think your separate calls was the way I went. Tho I also remember editing the config on the machine with glances to only include the disks I wanted, as it kept listing “snap” disks from some snaps I have installed.

did you find a solution to this. I’m trying to do the same thing with dockers containers and as you say an array of container.[0].name is fine if you know how many containers are running rather than [n] containers.

EDIT: i’m trying to pull the json_attributes ‘containers’ then I believe that stores the attributes to that sensor. Same uber problem of not knowing how many containers are (which will change over time)

Unfortunately no, though I haven’t had much time to research it lately. I have to imagine there’s a way to key on an attribute other than the index position - I just haven’t found syntax that HA will tolerate yet.

Good day, So I’ve tried this for a while and I finally found a solution a few weeks ago.
The developer is introducing or has introduced an mqtt module. So you can export all the data via mqtt.
I believe this is already available in the dev branch.
I use docker, so I had to create my own image. I basically pulled his dockerfile and added the bits I needed.

1 Like

good find - although what bits specifically in the dev build? It looks like MQTT is in the normal release in 3.0+
https://glances.readthedocs.io/en/stable/gw/mqtt.html?highlight=mqtt