Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor the size of a directory via Telegraf

Tags:

du

telegraf

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?

like image 200
Tw Bert Avatar asked Jun 06 '17 09:06

Tw Bert


People also ask

What is Telegraf InfluxDB?

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.

How do I enable Telegraf logs?

# 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.

What is Telegraf conf?

The Telegraf configuration file ( telegraf. conf ) lists all available Telegraf plugins. See the current version here: telegraf.

What is a plugin in 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.


1 Answers

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:

  1. The du command can stress your server, so use with care
  2. The user telegraf 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):
    • Add the influxdb group to the user telegraf : sudo usermod --groups influxdb --append telegraf
    • Put the following in the crontab, run for example each 10 minutes: 10 * * * * chmod -R g+rX /var/lib/influxdb/data > /var/log/influxdb/chmodfix.log 2>&1

Result, configured in Grafana (data source: InfluxDB): Grafana dirsize monitoring

Cheers, TW

like image 56
Tw Bert Avatar answered Sep 28 '22 06:09

Tw Bert