We need to monitor the size of a directory (for example the data directory of InfluxDB) to set up alerts in Grafana. As mentioned here: How to configure telegraf to send a folder-size to influxDB , there is no built-in plugin for this.
We don't mind using the inputs.exec
section of Telegraf. The directories are not huge (low filecount + dircount), so deep scanning (like the use of du
) is fine by us.
One of the directories we need to monitor is /var/lib/influxdb/data
.
What would be a simple script to execute, and what are the caveats?
Telegraf is InfluxData's data collection agent for collecting and reporting metrics. Its vast library of input plugins and “plug-and-play” architecture lets you quickly and easily collect metrics from many different sources. This article describes how to use Telegraf to collect and store data in InfluxDB v2.
# logtarget = "file" ## Name of the file to be logged to when using the "file" logtarget. If set to ## the empty string then logs are written to stderr. # logfile = "" You can specify debug = true in the agent config to print the debug logs. If you don't specify any log file, the logs will be printed on terminal.
The Telegraf configuration file ( telegraf. conf ) lists all available Telegraf plugins. See the current version here: telegraf.
Telegraf is a plugin-driven agent that collects, processes, aggregates, and writes metrics. It supports four categories of plugins including input, output, aggregator, processor, and external.
You could create a simple bash script metrics-exec_du.sh
with the following content (chmod 755):
#!/usr/bin/env bash
du -bs "${1}" | awk '{print "[ { \"bytes\": "$1", \"dudir\": \""$2"\" } ]";}'
And activate it by putting the following in the Telegraf config file:
[[inputs.exec]]
commands = [ "YOUR_PATH/metrics-exec_du.sh /var/lib/influxdb/data" ]
timeout = "5s"
name_override = "du"
name_suffix = ""
data_format = "json"
tag_keys = [ "dudir" ]
Caveats:
du
command can stress your server, so use with caretelegraf
must be able to scan the dirs. There are several options, but since InfluxDB's directory mask is a bit unspecified (see: https://github.com/influxdata/influxdb/issues/5171#issuecomment-306419800), we applied a rather crude workaround (examples are for Ubuntu 16.04.2 LTS
):
influxdb
group to the user telegraf
: sudo usermod --groups influxdb --append telegraf
10 * * * * chmod -R g+rX /var/lib/influxdb/data > /var/log/influxdb/chmodfix.log 2>&1
Result, configured in Grafana (data source: InfluxDB):
Cheers, TW
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With