InfluxDB Installation/Collectors
From r00tedvw.com wiki
InfluxDB Installation | Collector Scripts
vmstat collector
script
#!/bin/bash -x
#user defined variables
dbhostname=('dbhost fqdn')
dbport=('8086')
dbtable=('influxdb')
dbuser=('influx')
dbpassword=('influxdb')
logdir=("/var/log/statcollector/")
#define variables
HOSTNAME=$(hostname)
DOMAIN=$(hostname | awk -F "." '/1/ { printf $2"."$3"."$4};')
log=("statcollector-$(date +%F).txt")
DATE=$(date +%F"|"%R:%S)
#function that checks for connectivity to db host and continues if found.
function inet_test {
#function that extracts values and names from vmstat
function vmstatvar {
#set word boundary as a space, read the line and insert each variable into an array.
#this reads the value names from vmstat
string=$(vmstat 1 1 -S k | awk 'NR==2')
IFS=' ' read -r -a vmstatdef <<<$string
#same as above, but this reads the 10 second average values from vmstat and saves them into an array.
string2=$(vmstat 10 2 -S k | awk 'NR==4')
IFS=' ' read -r -a vmstatvalues <<<$string2
}
#function that checks for access to log dir and creates log file either in log directory or in /var/tmp/
function log_dir {
if ls "$logdir" >/dev/null 2>&1; then
if [ -e "$logdir""$log" ] >/dev/null 2>&1; then
printf "$DATE"" -- ""$dbhostname"" ""$is"" accessible\n" >> "$logdir""$log"
else
printf "Start New Log\n""$DATE"" -- ""$dbhostname"" ""$is"" accessible\n" > "$logdir""$log"
fi
elif [ -e "/var/tmp/""$log" ] >/dev/null 2>&1; then
printf "$DATE"" -- ""$logdir"" is NOT accessible\n""$DATE"" -- ""$dbhostname"" ""$is"" accessible\n" >> "/var/tmp/""$log"
else
printf "Start New Log\n""$DATE"" -- ""$logdir"" is NOT accessible\n""$DATE"" -- ""$dbhostname"" ""$is"" accessible\n" > "/var/tmp/""$log"
fi
}
#loop that brings everything together and outputs to db via http post
function influx_post {
for ((i=0;i<=(${#vmstatvalues[@]}-1);i++))
do
curl -i --max-time 10\
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST "http://""$dbhostname"":""$dbport""/write?db=""$dbtable" -u "$dbuser":"$dbpassword" --data-binary "${vmstatdef[i]}"",host=""$HOSTNAME"",domain=""$DOMAIN"" value=""${vmstatvalues[i]}"
done
}
#portion of code that checks db connectivity and then calls nested functions
if ping -q -c 1 -W 1 "$dbhostname" >/dev/null 2>&1; then
is=("is") \
&& log_dir \
&& vmstatvar \
&& influx_post
else
is=('is NOT') \
&& log_dir
fi
}
while true; do
inet_test
sleep 5
done