InfluxDB Installation/Collectors
From r00tedvw.com wiki
(Difference between revisions)
Line 3: | Line 3: | ||
===script=== | ===script=== | ||
<nowiki> | <nowiki> | ||
− | #!/bin/bash | + | #!/bin/bash -x |
+ | |||
+ | #user defined variables | ||
+ | dbhostname=('db fqdn') | ||
+ | dbport=('8086') | ||
+ | dbtable=('influxdb') | ||
+ | dbuser=('influx') | ||
+ | dbpassword=('influxdb') | ||
+ | logdir=("/var/log/vmstatcollector/") | ||
#define variables | #define variables | ||
HOSTNAME=$(hostname) | HOSTNAME=$(hostname) | ||
DOMAIN=$(hostname | awk -F "." '/1/ { printf $2"."$3"."$4};') | DOMAIN=$(hostname | awk -F "." '/1/ { printf $2"."$3"."$4};') | ||
− | + | log=("vmstatcollector-$(date +%F).txt") | |
− | + | DATE=$(date +%F"|"%R:%S) | |
− | + | ||
− | + | ||
− | + | ||
#vmstat 10 sec avg, output and create variables matching each | #vmstat 10 sec avg, output and create variables matching each | ||
Line 36: | Line 42: | ||
#array of vmstat variable values | #array of vmstat variable values | ||
vmstatvalues=("$r_process_normal" "$b_process_uninterruptible" "$swapd_virtual_mem_used" "$free_idle_mem" "$buf_mem_kernel" "$cache_mem_kernel" "$si_mem_from_swap" "$so_mem_to_swap" "$bi_io_from" "$bo_io_to" "$in_interrupts" "$cs_context_switch" "$us_cpu_non_kernel_code" "$sy_cpu_kernel_code" "$id_cpu_idle" "$wa_cpu_wait" "$st_cpu_stolen") | vmstatvalues=("$r_process_normal" "$b_process_uninterruptible" "$swapd_virtual_mem_used" "$free_idle_mem" "$buf_mem_kernel" "$cache_mem_kernel" "$si_mem_from_swap" "$so_mem_to_swap" "$bi_io_from" "$bo_io_to" "$in_interrupts" "$cs_context_switch" "$us_cpu_non_kernel_code" "$sy_cpu_kernel_code" "$id_cpu_idle" "$wa_cpu_wait" "$st_cpu_stolen") | ||
+ | #printf "%s\n" "${arr[@]}" | ||
#array of vmstat variable names | #array of vmstat variable names | ||
vmstatdef=('r_process_normal' 'b_process_uninterruptible' 'swapd_virtual_mem_used' 'free_idle_mem' 'buf_mem_kernel' 'cache_mem_kernel' 'si_mem_from_swap' 'so_mem_to_swap' 'bi_io_from' 'bo_io_to' 'in_interrupts' 'cs_context_switch' 'us_cpu_non_kernel_code' 'sy_cpu_kernel_code' 'id_cpu_idle' 'wa_cpu_wait' 'st_cpu_stolen') | vmstatdef=('r_process_normal' 'b_process_uninterruptible' 'swapd_virtual_mem_used' 'free_idle_mem' 'buf_mem_kernel' 'cache_mem_kernel' 'si_mem_from_swap' 'so_mem_to_swap' 'bi_io_from' 'bo_io_to' 'in_interrupts' 'cs_context_switch' 'us_cpu_non_kernel_code' 'sy_cpu_kernel_code' 'id_cpu_idle' 'wa_cpu_wait' 'st_cpu_stolen') | ||
− | #loop that brings everything together and outputs to db via http post | + | #function that checks for connectivity to db host and continues if found. |
− | function influx_post { | + | 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 | |
</nowiki> | </nowiki> |
Revision as of 04:03, 2 October 2016
InfluxDB Installation | Collector Scripts
vmstat collector
script
#!/bin/bash -x #user defined variables dbhostname=('db 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) #vmstat 10 sec avg, output and create variables matching each vmstatout=$(vmstat 10 2 -S k | awk 'NR==4') r_process_normal=$(printf "$vmstatout" | awk '{ printf $1 }') b_process_uninterruptible=$(printf "$vmstatout" | awk '{ printf $2 }') swapd_virtual_mem_used=$(printf "$vmstatout" | awk '{ printf $3 }') free_idle_mem=$(printf "$vmstatout" | awk '{ printf $4 }') buf_mem_kernel=$(printf "$vmstatout" | awk '{ printf $5 }') cache_mem_kernel=$(printf "$vmstatout" | awk '{ printf $6 }') si_mem_from_swap=$(printf "$vmstatout" | awk '{ printf $7 }') so_mem_to_swap=$(printf "$vmstatout" | awk '{ printf $8 }') bi_io_from=$(printf "$vmstatout" | awk '{ printf $9 }') bo_io_to=$(printf "$vmstatout" | awk '{ printf $10 }') in_interrupts=$(printf "$vmstatout" | awk '{ printf $11 }') cs_context_switch=$(printf "$vmstatout" | awk '{ printf $12 }') us_cpu_non_kernel_code=$(printf "$vmstatout" | awk '{ printf $13 }') sy_cpu_kernel_code=$(printf "$vmstatout" | awk '{ printf $14 }') id_cpu_idle=$(printf "$vmstatout" | awk '{ printf $15 }') wa_cpu_wait=$(printf "$vmstatout" | awk '{ printf $16 }') st_cpu_stolen=$(printf "$vmstatout" | awk '{ printf $17 }') #array of vmstat variable values vmstatvalues=("$r_process_normal" "$b_process_uninterruptible" "$swapd_virtual_mem_used" "$free_idle_mem" "$buf_mem_kernel" "$cache_mem_kernel" "$si_mem_from_swap" "$so_mem_to_swap" "$bi_io_from" "$bo_io_to" "$in_interrupts" "$cs_context_switch" "$us_cpu_non_kernel_code" "$sy_cpu_kernel_code" "$id_cpu_idle" "$wa_cpu_wait" "$st_cpu_stolen") #printf "%s\n" "${arr[@]}" #array of vmstat variable names vmstatdef=('r_process_normal' 'b_process_uninterruptible' 'swapd_virtual_mem_used' 'free_idle_mem' 'buf_mem_kernel' 'cache_mem_kernel' 'si_mem_from_swap' 'so_mem_to_swap' 'bi_io_from' 'bo_io_to' 'in_interrupts' 'cs_context_switch' 'us_cpu_non_kernel_code' 'sy_cpu_kernel_code' 'id_cpu_idle' 'wa_cpu_wait' 'st_cpu_stolen') #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