InfluxDB Installation/Collectors

From r00tedvw.com wiki
(Difference between revisions)
Jump to: navigation, search
(Service)
Line 142: Line 142:
 
exit 0
 
exit 0
 
</nowiki>
 
</nowiki>
 +
 
  <nowiki>
 
  <nowiki>
 
# sudo chmod +x /etc/init.d/statcollector
 
# sudo chmod +x /etc/init.d/statcollector
 
# sudo chkconfig --add /etc/init.d/statcollector
 
# sudo chkconfig --add /etc/init.d/statcollector
 
</nowiki>
 
</nowiki>

Revision as of 04:01, 3 October 2016

InfluxDB Installation | Collector Scripts

vmstat collector

script

place in /etc/statcollector/statcollector.sh

#!/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

Service

place in /etc/init.d/statcollector

# chkconfig: 2345 90 60

 ## BEGIN INIT INFO
 # Provides: statcollector
 # Required-Start: $remote_fs $syslog
 # Required-Stop: $remote_fs $syslog
 # Default-Start: 3 4 5
 # Default-Stop: 90
 # Short-Description: Start statcollector at boot time
 # Description: Load statcollector at startup to report vmstat values to InfluxDB
 ### END INIT INFO

#startup function
start_stat_collector() {
        printf "Starting Stat Collector ...\n"
        /etc/statcollector/statcollector.sh > /dev/null 2>&1 &
        printf "... started.\n"
}

#stopping function
stop_stat_collector () {
        printf "Stopping Stat Collector ...\n"
        killall -9 statcollector.sh > /dev/null 2>&1 &
        printf "... stopped.\n"
}

#restarting function that stops and starts stat collector
restart_stat_collector () {
        printf "Restarting Stat Collector ..."
        stop_stat_collector
        start_stat_collector
}

#status determination
status_stat_collector () {
        if [ $(ps -ef | grep -v grep | grep 'statcollector.sh' | wc -l) = 1 ]; then
                printf "Stat Collector is running.\n"
        else
                printf "Stat Collector is not running.\n"
        fi
}

case "$1" in
        start)
                start_stat_collector
                ;;
        stop)
                stop_stat_collector
                ;;
        restart)
                restart_stat_collector
                ;;
        status)
                status_stat_collector
                ;;
        *)
                printf "Usage: /etc/init.d/statcollector {start|stop|restart|status}\n"
                exit 1
                ;;

esac
exit 0

# sudo chmod +x /etc/init.d/statcollector
# sudo chkconfig --add /etc/init.d/statcollector

Personal tools
Namespaces

Variants
Actions
Navigation
Mediawiki
Confluence
DevOps Tools
Ubuntu
Ubuntu 22
Mac OSX
Oracle Linux
AWS
Windows
OpenVPN
Grafana
InfluxDB2
TrueNas
OwnCloud
Pivotal
osTicket
OTRS
phpBB
WordPress
VmWare ESXI 5.1
Crypto currencies
HTML
CSS
Python
Java Script
PHP
Raspberry Pi
Canvas LMS
Kaltura Media Server
Plex Media Server
MetaSploit
Zoneminder
ShinobiCE
Photoshop CS2
Fortinet
Uploaded
Certifications
General Info
Games
Meal Plans
NC Statutes
2020 Election
Volkswagen
Covid
NCDMV
Toolbox