InfluxDB Installation/Collectors
From r00tedvw.com wiki
(Difference between revisions)
Line 6: | Line 6: | ||
#user defined variables | #user defined variables | ||
− | dbhostname=('db fqdn') | + | dbhostname=('db hostname fqdn') |
dbport=('8086') | dbport=('8086') | ||
dbtable=('influxdb') | dbtable=('influxdb') | ||
Line 19: | Line 19: | ||
DATE=$(date +%F"|"%R:%S) | DATE=$(date +%F"|"%R:%S) | ||
+ | #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 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | vmstatvalues | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
#function that checks for connectivity to db host and continues if found. | #function that checks for connectivity to db host and continues if found. |
Revision as of 02:06, 3 October 2016
InfluxDB Installation | Collector Scripts
vmstat collector
script
#!/bin/bash -x #user defined variables dbhostname=('db hostname fqdn') dbport=('8086') dbtable=('influxdb') dbuser=('influx') dbpassword=('influxdb') logdir=("/var/log/vmstatcollector/") #define variables HOSTNAME=$(hostname) DOMAIN=$(hostname | awk -F "." '/1/ { printf $2"."$3"."$4};') log=("vmstatcollector-$(date +%F).txt") DATE=$(date +%F"|"%R:%S) #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 connectivity to db host and continues if found. function inet_test { #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 \ && influx_post else is=('is NOT') \ && log_dir fi } inet_test