如何通过 Telegraf 监控目录的大小

How to monitor the size of a directory via Telegraf

我们需要监控一个目录(例如InfluxDB的数据目录)的大小,以便在Grafana中设置告警。如此处所述: How to configure telegraf to send a folder-size to influxDB ,没有内置插件。

我们不介意使用 Telegraf 的 inputs.exec 部分。目录不是很大(低文件数 + 目录数),所以我们可以进行深度扫描(如使用 du)。

我们需要监控的目录之一是/var/lib/influxdb/data

要执行的简单脚本是什么,注意事项是什么?

您可以使用以下内容 (chmod 755) 创建一个简单的 bash 脚本 metrics-exec_du.sh

#!/usr/bin/env bash
du -bs "" | awk '{print "[ { \"bytes\": "", \"dudir\": \"""\" } ]";}'

并通过在 Telegraf 配置文件中添加以下内容来激活它:

[[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" ]

注意事项:

  1. du 命令会给您的服务器带来压力,因此请谨慎使用
  2. 用户telegraf必须能够扫描目录。有几个选项,但由于 InfluxDB 的目录掩码有点未指定(参见:https://github.com/influxdata/influxdb/issues/5171#issuecomment-306419800),我们应用了一个相当粗略的解决方法(示例用于 Ubuntu 16.04.2 LTS):
    • influxdb 组添加到用户 telegraf : sudo usermod --groups influxdb --append telegraf
    • 将以下内容放入 crontab,例如每 10 分钟 运行:10 * * * * chmod -R g+rX /var/lib/influxdb/data > /var/log/influxdb/chmodfix.log 2>&1

结果,在Grafana中配置(数据来源:InfluxDB):

干杯,TW

如果您需要监视多个目录,我通过 Tw Bert 更新了答案并将其扩展为允许您在一个命令行上传递它们。这样您就不必在 telegraf.conf 文件中添加多个 [[input.exec]] 条目。

创建文件 /etc/telegraf/scripts/disk-usage.sh 包含:

#!/bin/bash

echo "["
du -ks "$@" | awk '{if (NR!=1) {printf ",\n"};printf "  { \"directory_size_kilobytes\": "", \"path\": \"""\" }";}'
echo
echo "]"

我想监控两个目录:/mnt/user/appdata/influxdb/mnt/user/appdata/grafana。我可以这样做:

# Get disk usage for multiple directories
[[inputs.exec]]
  commands = [ "/etc/telegraf/scripts/disk-usage.sh /mnt/user/appdata/influxdb /mnt/user/appdata/grafana" ]
  timeout = "5s"
  name_override = "du"
  name_suffix = ""
  data_format = "json"
  tag_keys = [ "path" ]

更新配置后,您可以使用以下方法进行测试:

telegraf --debug --config /etc/telegraf/telegraf.conf --input-filter exec --test

哪个应该向您展示 Telegraf 将推送哪些内容:

bash-4.3# telegraf --debug --config /etc/telegraf/telegraf.conf --input-filter exec --test
> du,host=SomeHost,path=/mnt/user/appdata/influxdb directory_size_kilobytes=80928 1536297559000000000
> du,host=SomeHost,path=/mnt/user/appdata/grafana directory_size_kilobytes=596 1536297559000000000

已经提供的解决方案对我来说很好,并强调警告,这样的读取权限很棒。另一种值得一提的方法是使用 Telegraf 收集 monitor diskspace on influxdb with telegraf.

中提出的数据
[[outputs.influxdb]]
  urls = ["udp://your_host:8089"]
  database = "telegraf_metrics"

  ## Retention policy to write to. Empty string writes to the default rp.
  retention_policy = ""
  ## Write consistency (clusters only), can be: "any", "one", "quorum", "all"
  write_consistency = "any"

  ## Write timeout (for the InfluxDB client), formatted as a string.
  ## If not provided, will default to 5s. 0s means no timeout (not recommended).
  timeout = "5s" 

# Read metrics about disk usage by mount point
[[inputs.disk]]
  ## By default, telegraf gather stats for all mountpoints.
  ## Setting mountpoints will restrict the stats to the specified mountpoints.
  # mount_points = ["/"]

  ## Ignore some mountpoints by filesystem type. For example (dev)tmpfs (usually
  ## present on /run, /var/run, /dev/shm or /dev).
  ignore_fs = ["tmpfs", "devtmpfs"]

注意:超时要慎重考虑。也许每小时阅读一次就足以避免日志记录耗尽。

使用 filecount 插件

本身是可能的
[[inputs.filecount]]
directories = ["/var/lib/influxdb/engine/data"]

输出:

> filecount,directory=/var/lib/influxdb/engine/data,host=psg count=424i,size_bytes=387980393i 1652195855000000000