Monitor InfluxDB database size

Hello all,
Little example on how to monitor the size of your influxDB database size:
This will work on a strandard linux debian installation (inc. openHABian)

Item:

Number InfluxDB_Size

Rule:

rule "Influx DB Size Data Collection"
when
    Time cron "0 0 0/1 * * ?" // On the hour
then
    var String databaseQueryString = executeCommandLine("sudo du -sh /var/lib/influxdb/data/openhab_db", 1000)
    // logInfo("TEST1", databaseQueryString) // eg. 29M	/var/lib/influxdb/data/openhab_db
    var String databaseSizeString = databaseQueryString.split("\t").get(0) // Extract first part of returned string
    databaseSizeString = databaseSizeString.replace("M", "") // Removes the "M"
    databaseSizeString = databaseSizeString.replace("K", "") // Removes the "K"
    logInfo("TEST1", databaseSizeString) // eg: 29
    var Number databaseSize = Float::parseFloat(databaseSizeString) as Number // Cast into a Number
    logInfo("TEST1", databaseSize.toString) // eg: 29.0
    InfluxDB_Size.postUpdate(databaseSize)
end
5 Likes

Nice one, thx.
If you want to make it more general: I played with it on my test system and only have kByte db size there. So the “K” needs to be removed also :rofl: (Don’t know how to do that, sorry)
And the cron is currently in minutes, not in // On the hour
Thanks again, all your work and answers are much appreciated :+1:

1 Like

Thanks for pointing it out.
I have added the “K” removal

1 Like

this is great - thank you!

Has anybody an idea how to handle this if InfluxDB and Openhab are running as Docker Containers?

Very helpful - thank you! :grinning:
I just needed to tweak it to work on OH3

Hi,
is there a smart way if the Database is running on an other machine?

Thanks Christian