Unreliable InfluxDB size sensor

Is there a way to use this script:

#!/bin/bash

docker exec influxdb du -sh /var/lib/influxdb/data/home_assistant | awk '{print $1; }' | cut -d'M' -f1 > /docker/homeassistant/file_sensors/ha_influx_db_size.txt

With the command line sensor?

Or maybe:

This shell command does not work:

get_influxdb_size: "bash docker exec influxdb du -sh /var/lib/influxdb/data/homeassistant | awk '{print $1; }' | cut -d'M' -f1 > /config/file_sensors/ha_influx_db_size.txt"

No errors in the log.

Some progress.

This works in the influx container:

du -s /data/influxdb/data/homeassistant

It gives me a nice machine readable number of KB (no unit as occurs with the human readable -h option).

According to portainer the container name is

addon_a0d7b954_influxdb

However running this from the CLI

bash docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant

Gives me a bunch of command not found errors. So it was more than a bit doubtful that this would work:

- platform: command_line
  name: InfluxDB Size Test
  command: "bash docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant"
  unit_of_measurement: "MB"
  value_template: '{{ value / 1024 }}'

And as expected, it does not.

Command failed: bash docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant

This works from the SSH ad Web Terminal Addon with protection mode disabled:

docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant

However according to Frenk there is no way yo access that from within home assistant. So dead end and the only option for Home assistant users is the unreliable and (and frankly incorrect) shard method. Which is also not an option if you disable the internal database - as recommended by InfluxDB themselves.

Only solution: If you want a reliable sensor choose another install method that allows access to docker container files.

1 Like

It has stopped working altogether after updating to HassOS 4.14

EDIT: restarting the addon rectified it.

Just a backlink as I was looking for the same.

In case anyone got a better solution… let us know :slight_smile:

Abusing the init_commands in the Web Terminal SSH addon (with protection mode off) I have added the config:

init_commands:
  - >-
    watch -n 10 docker exec addon_a0d7b954_influxdb du -s
    /data/influxdb/data/homeassistant >
    /config/file_sensors/ha_influx_db_size.txt

Which is logging to the file!

It’s a mess but it is logging.

e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:55:12

352808	/data/influxdb/data/homeassistant
e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:55:22

352808	/data/influxdb/data/homeassistant
e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:55:32

352808	/data/influxdb/data/homeassistant
e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:55:42

352808	/data/influxdb/data/homeassistant
e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:55:52

352808	/data/influxdb/data/homeassistant
e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:56:02

352808	/data/influxdb/data/homeassistant
e[He[JEvery 10.0s: docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant                                                                                                                                                          2021-01-25 18:56:12

352808	/data/influxdb/data/homeassistant

Using the file sensor I should be able to get this into home assistant as a sensor. I’m going to increase the watch time to 3000 seconds, 10 was just to test. The only downside is that the web terminal no longer works with ingress. Guess I’ll have to use the core SSH addon for access.

The confusing thing is that redirecting the command output with > should overwrite the file but instead it is appending to the file.

Any Linux gurus know why?

@tom_l have fun :slight_smile:

init_commands:
  - 'while [ 1 = 1 ]; do du -s /etc > /backup/test.txt && sleep 10; done'

Thanks but while this works:

docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant > /config/file_sensors/ha_influx_db_size.txt

This:

while [ 1 = 1 ]; do docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant > /config/file_sensors/ha_influx_db_size.txt sleep 10; done

Returns:

du: cannot access '10' : No such file or directory

EDIT: I see my missing &&.

Edit 2: So the configuration is changed to this on saving:

init_commands:
  - >-
    while [ 1 = 1 ]; do docker exec addon_a0d7b954_influxdb du -s
    /data/influxdb/data/homeassistant >
    /config/file_sensors/ha_influx_db_size.txt && sleep 300; done

And while it is overwriting this 352808 /data/influxdb/data/homeassistant to the file, I still get 502: Bad Gateway when attempting to open the web terminal. Seems an awful waste of this addon.

@tom_l don’t give up hope on that amazing plugin… there’s a simple solution to your issue. Just throw ampersand at the end which turns the Linux command into a “background” task. It should just look like the below just pay attention to that last ampersand.

init_commands:
  - 'while [ 1 = 1 ]; do du -s /etc > /backup/test.txt && sleep 10; done &'
1 Like

For a moment there I thought we had cracked it. It was logging to file every 300 sec and I could still log into the SSH terminal.

Then I noticed my system temps.

It seems the backgrounded sleep function caused my processor use to spike and my relatively stable cpu temp to go from 50°C to above 80°.

Is it possible to set a cron job instead?

Would that be more efficient?

That’s kinda sad… sleep is not supposed to do that. Mine did just like yours when I tried the read -t 10 command as the pause (verified with htop in the shell). Sleep does not seem to do what it’s doing to you… works like a charm for me.

I just tried it again. This time it seems to be behaving. Not sure what I did wrong last time.

Will post up the full solution after testing for a few days.

Interesting :+1:t3:

Update after 3 days? Worth trying or still side effects (performance/resource consumption, non-working SSH addon, …)?

No performance issues but the value has not changed since it started. I need to investigate why when I get some time, later today or tomorrow.

Ah okay. Keep us posted :+1:t3:

1 Like

If you’re using Linux and InfluxDB addon in HASSIO make some_file.sh with content:

#!/bin/bash


mqttuser='your_user'
mqttpassword='your_password'
mqttbroker='your_broker_ip_or_localhost'
mqttport='your_port'

influxdb_size=$(docker exec YOUR_INFLUXDB_DOCKER du -shm "/data/influxdb/data/homeassistant" | cut -f1 )
mosquitto_pub -h $mqttbroker -p $mqttport -u $mqttuser -P $mqttpassword -t influxdb/influxdb_size -m $influxdb_size -r

This file will output size of folder in MB and publish it in mqtt: influxdb/influxdb_size

Next create the mqtt sensor:

  - platform: mqtt
    name: influxdb_size
    state_topic: "influxdb/influxdb_size"
    unit_of_measurement: "MB"

And in the end add to cron file ‘some_file.sh’ with interval whatever you want.
Hope it will help you

Thanks but I’m using HassOS. So that is not possible. My workaround using the SSH web terminal is working.

1 Like

Using HASS OS too. And not using MQTT.

So what about

?

Any smaller issues, side effects or inconveniences? Or “bullet proof” to use?

All going swimmingly.

So, to wrap it all up. Use this init_command in the SSH & Web Terminal addon with protection mode disabled:

init_commands:
  - >-
    while [ 1 = 1 ]; do docker exec addon_a0d7b954_influxdb du -s
    /data/influxdb/data/homeassistant >
    /config/file_sensors/ha_influx_db_size.txt && sleep 300; done &

Then create this file sensor:

- platform: file
  name: 'InfluxDB HA DB Size'
  file_path: '/config/file_sensors/ha_influx_db_size.txt'
  unit_of_measurement: 'MB'
  value_template: "{{ (value.split('\t')[0]|int/1000)|round(3) }}"

You can then disable the InfluxDB internal database if you wish (recommended by InfluxDB).

To do so, in the Influxdb addon configuration add this:

envvars:
  - name: INFLUXDB_MONITOR_STORE_ENABLED
    value: "false"

Don’t expect it to be as active as the shard method. I only see an increase every day or two.

2 Likes