Telegraf
Custom Shell
Samples #1
#/bin/bash
devname=(`lsblk| grep 'disk'|awk '{print $1}'`)
dirname=(`lsblk| grep 'disk'|awk '{if ($7=="") print "/";else print $7}'`)
#At that time, I wanted to store these directory names in dictionary format, and later changed to variable mode, shell Of[ ] { } * @ $Special characters will drive you crazy
#declare -A devdict
devnum=`expr ${#devname[@]} - 1`
for i in `seq 0 $devnum`;do
if [-z "${dirname[$i]}" ];then
eval ${devname[$i]}="/"
else
eval ${devname[$i]}="${dirname[$i]}"
fi
#devdict+=([${devname[$i]}]="${dirname[$i]}")
done
#echo ${!devdict[*]}
#echo ${devdict[*]}
ioarry=`iostat -x | grep sd|awk '{print "datadir=${"$1"}@r="$4",w="$5",await="$10",svctm="$11",util="$12}'`
for i in ${ioarry[@]};do
eval temp="${i}"
#Replace the special character @, and the space in the shell will be truncated to two elements
temp=${temp/@/ }
echo "exec,${temp}"
#Ensure that the final output is in the following format. The first character is the measurement name. If the input.exec plug-in has the configuration name "suffix", the suffix will be added automatically
#The output format is measurement name, comma, tag keys (comma separated), space, filed keys (comma separated)
#The data format output mismatch will lead to the failure of telegraf to parse the data and go to the influxdb. It took a long time to debug and didn't look at the hole dug by the official website
#exec,datadir=/data/data11 r=4.1,w=6.1,await=0.83,svctm=1.35,util=1.46"
done
#echo ${devdict[@]}
[[inputs.exec]]
##Commands array
commands = ["bash /appcom/telegraf/collect_iostat.sh",]
timeout='5s'
##Suffix for measurements
name_suffix="_collectiostat"
data_format="influx"
Sample #2
#!/bin/sh
hostname=`hostname`
uptime=`awk '{print $1}' /proc/uptime`
if uptime |grep -q user ; then
load1=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $7}'`
load5=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $8}'`
load15=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $9}'`
else
load1=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $5}'`
load5=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $6}'`
load15=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $7}'`
fi
echo "uptime,host=$hostname uptime=$uptime,load1=$load1,load5=$load5,load15=$load15"
[agent]
interval = "5s"
round_interval = true
[[inputs.swap]]
[inputs.swap.tags]
metrics_source="telegraf_demo"
[[inputs.exec]]
commands = ["/etc/telegraf/uptime.sh"]
data_format = "influx"
[inputs.exec.tags]
metrics_source="telegraf_demo"
[[outputs.influxdb]]
url = "https://influxdemo:8086"
database = "telegraf"
Sample #3
#! /bin/bash
/usr/bin/speedtest --format json | jq '.download.bandwidth = .download.bandwidth / 125000 | .upload.bandwidth = .upload.bandwidth / 125000'
[[inputs.exec]]
commands = [
"/home/rock64/speedtest.sh"
]
interval = "300s"
timeout = "60s"