Last active
March 4, 2022 16:23
-
-
Save LuisPalacios/9597178db4c4c4b357dd0700d61c1835 to your computer and use it in GitHub Desktop.
Script compatible con el Plugin [[input.exec]] de Telegraf para mandar datos a InfluxDB sobre el tamaño de sus Buckets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# By Luispa (2022) | |
# | |
# Script para ser usado desde un cliente Telegraf, que permite averiguar el tamaño de los | |
# buckets (bases de datos) de InfluxDB. El típico caso de uso es cuando tenemos un servidor | |
# linux con e instalamos el cliente Telegraf para moniorizar precisamente cuanto ocupan | |
# dichos buckets en el disco duro. | |
# | |
# Este script debe mostrar los datos que monitoriza por su standard output y la | |
# salida esperada son varias líneas, una por cada bucket, del tipo: | |
# | |
# root@almacenix:/etc/telegraf/scripts# ./bucket_sizes.sh | |
# influxdb,bucket=home_assistant kbytes=121016 1645026907517000000 | |
# influxdb,bucket=_monitoring kbytes=244 1645026907517000000 | |
# influxdb,bucket=telegraf kbytes=106416 1645026907517000000 | |
# | --------------- ------------- | | |
# | | | | | |
# | | | | | |
# +-----------+--------+-+---------+-+------------+ | |
# |measurement|,tag_set| |field_set| |timestamp | (timestamp es opcional) | |
# +-----------+--------+-+---------+-+------------+ | |
# | |
# Referencia : https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_reference/ | |
# Tutorial : https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_tutorial/ | |
# | |
# INSTALACIÓN: | |
# | |
# 1. Dar permiso al usuario Telegraf para que lea el tamaño de los directorios donde están los buckets | |
# y que no supongan muchas lineas nuevas en syslog por hacerlo... | |
# | |
# /etc/sudoers | |
# Defaults:telegraf !logfile, !syslog # Para que no te llene el syslog cada 30s | |
# telegraf ALL = (influxdb) NOPASSWD: /usr/bin/du # Para que telegraf pueda hacer un 'du' como influxdb | |
# | |
# /etc/pam.d/sudo | |
# : | |
# @include common-account | |
# session [success=1 default=ignore] pam_succeed_if.so quiet ruser = telegraf # <== AÑADIR esta línea AQUI !! | |
# @include common-session-noninteractive | |
# | |
# | |
# 2. Crear /etc/telegraf/scripts y copiar este script con permisos de ejecución | |
# | |
# chmod 755 /etc/telegraf/scripts/bucket_sizes.sh | |
# | |
# 3. Modificar el fichero de configuración de telgraf: | |
# | |
# Opción 1: /etc/telegraf/telegraf.conf | |
# Opción 2: Si lo cargas directamente desde InfluxDB, modificarlo en InfluxDB | |
# http://almacenix.parchis.org:8086 --> Load Data > Telegraf | |
# | |
# [[inputs.exec]] | |
# commands = ["/etc/telegraf/scripts/bucket_sizes.sh"] | |
# data_format = "influx" | |
# interval = "30s" | |
# | |
# 4. Adecúa las variables siguientes para tu instalación. Este script va averiguar | |
# el nombre de los Buckets para no guardar la información con buckets-id's | |
# | |
# 5. Rearranca el servicio | |
# | |
# systemctl restart telegraf | |
# | |
# 6. Compruebo que funciona | |
# | |
# http://a.b.c.d:8086 | |
# Data Explorer | |
# telegraf | |
# _measurement: influxdb | |
# | |
DATADIR="/var/lib/influxdb/engine/data" | |
HOST="http://localhost:8086" | |
ORG="parchis.org" | |
TOKEN="nC9xF8uKJCJMd1mbeZ0pejEM4MFFj-YjsceYHltqs3pt6kHUdhaiZA9vJnzujWqbUJqwWXWDxB9sUm77CJbfOw==" | |
# Array clave/valor bucket-id -> bucket-name | |
declare -A arr | |
arrdef=`influx bucket list --host ${HOST} --org ${ORG} --token ${TOKEN} | tail -n +2 | awk 'BEGIN { printf "arr+=( " } {printf "[\"%s\"]=\"%s\" ",$1,$2} END { print ")" }'` | |
eval ${arrdef} | |
# Timestamp: fecha actual, en milisegundos y le añado 6 ceros (descarto los nanosegundos) | |
timestamp=`date +%s%3N000000` | |
# Recorro los buckets ID's | |
for key in ${!arr[@]}; do | |
# (debug only): echo "Clave: ${key} - Valor: ${arr[${key}]}" | |
# Si el bucket tiene datos, | |
du_output=`sudo -u influxdb du -s ${DATADIR}/${key} 2>/dev/null` | |
if [ "${?}" == 0 ]; then | |
# Muestro una línea en formato line_protocol | |
lineprotocol=`echo ${du_output} | sed "s/\t//g" | sed "s/${key}/${arr[${key}]}/g" | awk -v TIMESTAMP=${timestamp} -F'/' '{printf "influxdb,bucket=%s kbytes=%s %s\n",$7,$1,TIMESTAMP}'` | |
echo "${lineprotocol}" | |
fi | |
done | |
#EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment