From cea692f9c09b6b3d28176e10427803e1a41c22fb Mon Sep 17 00:00:00 2001 From: Brian Flowers Date: Tue, 20 Sep 2016 02:13:46 -0400 Subject: [PATCH] Added logging plugin and misc fixes to others --- bdsm.d/df.generic | 9 ++-- bdsm.d/file.logger | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ bdsm.d/sensors.generic | 5 +-- bdsm.d/vmstat.generic | 7 ++- 4 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 bdsm.d/file.logger diff --git a/bdsm.d/df.generic b/bdsm.d/df.generic index cec9da9..ea3d8fc 100755 --- a/bdsm.d/df.generic +++ b/bdsm.d/df.generic @@ -69,7 +69,7 @@ start() while [ 1 ]; do df=`df --output=pcent "$DRIVE" | tail -1 | sed 's/[^0-9]//g'` ts=`date '+%s'` - stdbuf -o0 echo "\$ts|$HOSTNAME|DISK-$LABEL-USED|\$df" + stdbuf -oL echo "\$ts|$HOSTNAME|DISK-$LABEL-USED|\$df" sleep $DELAY done >> $OUTPATH & echo \$! >/home/bdsm/.df.pid @@ -81,7 +81,7 @@ EOF stop() { - ssh $HOSTNAME "kill `cat /home/bdsm/.df.pid`" + ssh $HOSTNAME 'kill `cat /home/bdsm/.df.pid`' ssh $HOSTNAME "rm /home/bdsm/.df.pid" } @@ -107,10 +107,9 @@ compatible() compatible="`ssh -q -n $HOSTNAME 'df --output=pcent /' 2>&1 1>/dev/null`" if [ "$compatible" != "" ]; then echo 0 - return + else + echo 1 fi - echo 1 - return } # UTILITY FUNCTIONS diff --git a/bdsm.d/file.logger b/bdsm.d/file.logger new file mode 100644 index 0000000..f76f4e6 --- /dev/null +++ b/bdsm.d/file.logger @@ -0,0 +1,116 @@ +#!/bin/bash +################################################################################ +# +# file.logger +# Bash Daemon for System Monitoring +# Logging to a text file +# +############################################################################### + +# TRAP UNKNOWN ERRORS +############################################################################### +trap failure SIGTERM SIGINT SIGFPE + +failure() +{ + error "Something has gone SERIOUSLY wrong! (Received signal $?)" + return 255 +} + +error() +{ + printf "\e[41m" 1>&2 + printf "ERROR: $1" 1>&2 + printf "\e[0m\n" 1>&2 +} + +# VARIABLES +############################################################################### +SCRIPT=$0 +HOSTNAME=$2 +FILE=$3 + +# MAIN FUNCTIONS +############################################################################### +help() +{ + echo "Logging to a flat file" + echo "USAGE: $0 [FUNCTION] [HOSTNAME] [FILENAME]" + echo "FUNCTION -- start/stop/status/help" + echo "HOSTNAME -- the hostname to log on" + echo "::FILENAME -- the file to write logs to" + exit 0 +} + +start() +{ + ssh -qn $HOSTNAME "cat /home/bdsm/in.fifo >> $FILE; echo $! > /home/bdsm/.file-logger.pid &" +} + +stop() +{ + ssh -qn $HOSTNAME 'kill `cat /home/bdsm/.file-logger.pid`; rm /home/bdsm/.file-logger.pid' +} + +status() +{ + running=`ssh $HOSTNAME "cat /home/bdsm/.file-logger.pid 2>/dev/null | \ + xargs ps -T | \ + grep -v PID | \ + wc -l"` + + if [ -z "$running" ]; then + running=0 + fi + if [ $running -eq 0 ]; then + echo "OFF" + else + echo "ON" + fi +} + +compatible() +{ + compatible="`ssh -q -n $HOSTNAME 'ls /home/bdsm/in.fifo' 2>&1 1>/dev/null`" + if [ "$compatible" != "" ]; then + echo 0 + else + echo 1 + fi +} + +# UTILITY FUNCTIONS +############################################################################### + +formatConfig() +{ + # Reformat config file to be more machine-readable + # Removes whitespace and adds semicolons after property values + cat /home/bdsm/bdsm.conf | tr '\n' ' ' | sed 's/}/}\ + /g' | sed 's/^;*//g' | sed "s/[[:space:]]//g"| \ + sed 's/;\([{}]\)/\1/g' | sed 's/\([{}]\);/\1/g' \ + > /home/bdsm/.bdsm.conf.tmp +} + +# SCRIPT START +############################################################################### +if [ ! -f /home/bdsm/.bdsm.conf.tmp ]; then + formatConfig +fi + +# Get a list of configured hosts +hosts=($(cat /home/bdsm/.bdsm.conf.tmp | cut -d'{' -f1)) + +if [ -z $1 ]; then + help +elif [ $1 == "start" ]; then + start +elif [ $1 == "stop" ]; then + stop +elif [ $1 == "status" ]; then + status +elif [ $1 == "compatible" ]; then + compatible +elif [ $1 == "help" ]; then + help +fi diff --git a/bdsm.d/sensors.generic b/bdsm.d/sensors.generic index 6dd9ef1..d39fa9b 100755 --- a/bdsm.d/sensors.generic +++ b/bdsm.d/sensors.generic @@ -119,10 +119,9 @@ compatible() compatible="`ssh -q -n $HOSTNAME 'sensors' 2>&1 1>/dev/null`" if [ "$compatible" != "" ]; then echo 0 - return + else + echo 1 fi - echo 1 - return } # UTILITY FUNCTIONS diff --git a/bdsm.d/vmstat.generic b/bdsm.d/vmstat.generic index 34a9122..8fa70bb 100755 --- a/bdsm.d/vmstat.generic +++ b/bdsm.d/vmstat.generic @@ -76,7 +76,7 @@ start() fi ssh -q $HOSTNAME <