From fcc785847a9c21712aa16a7e76cf47324bb25435 Mon Sep 17 00:00:00 2001 From: Brian Flowers Date: Thu, 15 Sep 2016 14:06:53 -0400 Subject: [PATCH] Adding sensors plugin and completing df plugin --- bdsm.d/df.generic | 2 +- bdsm.d/sensors.generic | 153 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) mode change 100644 => 100755 bdsm.d/df.generic create mode 100755 bdsm.d/sensors.generic diff --git a/bdsm.d/df.generic b/bdsm.d/df.generic old mode 100644 new mode 100755 index 09648de..5ccb905 --- a/bdsm.d/df.generic +++ b/bdsm.d/df.generic @@ -67,7 +67,7 @@ start() ssh -q $HOSTNAME <> $OUTPATH sleep $DELAY diff --git a/bdsm.d/sensors.generic b/bdsm.d/sensors.generic new file mode 100755 index 0000000..f8f20d3 --- /dev/null +++ b/bdsm.d/sensors.generic @@ -0,0 +1,153 @@ +#!/bin/bash +################################################################################ +# +# sensors.generic +# Bash Daemon for System Monitoring +# Generic sensors plugin for monitoring temperature through lm_sensors +# +# +# +# +############################################################################### + +# 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 +DELAY=$3 +LABEL=$4 +PATH=$5 +OUTPATH=/home/bdsm/out.fifo + +# MAIN FUNCTIONS +############################################################################### +help() +{ + echo "Generic temperature monitoring through lm_sensors" + echo "USAGE: $0 [FUNCTION] [HOSTNAME] [DELAY]" + echo "FUNCTION -- start/stop/status/help" + echo "HOSTNAME -- the hostname to monitor" + echo "::DELAY -- seconds between measurements" + exit 0 +} + +start() +{ + running=`ssh $HOSTNAME "cat /home/bdsm/.sensors.pid | \ + xargs ps -T | \ + grep -v PID | \ + wc -l"` 2>/dev/null + + if [ $running -eq 0 ] || [ $running -gt 5 ]; then + # Check if sensors is installed + installed=`ssh -q $HOSTNAME "sensors > /dev/null 2>&1; echo $?"` + if [ ! $installed ]; then + echo "" > $OUTPATH + return 1 + fi + + ssh -q $HOSTNAME < 2) { + FS=":" + printf ts "|" host "|" device " " adapter " " $0 "\n" + FS=" " + } line=line+1 + }' | tr ':' '|' + sleep $DELAY + done & +EOF + ssh -q $HOSTNAME "ps aux | \ + grep bdsm | \ + grep sensors | \ + grep -v grep | \ + awk '{print \$2}' \ + > /home/bdsm/.sensors.pid &" + else + echo "Already Running" + fi +} + +stop() +{ + ssh $HOSTNAME "kill `cat /home/bdsm/.sensors.pid`" + ssh $HOSTNAME "rm /home/bdsm/.sensors.pid" +} + +status() +{ + running=`ssh $HOSTNAME "cat /home/bdsm/.sensors.pid | \ + xargs ps -T | \ + grep -v PID | \ + wc -l"` + + if [ $running -eq 0 ]; then + echo "OFF" + else + echo "ON" + 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 +############################################################################### + +# 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 + formatConfig + start +elif [ $1 == "stop" ]; then + formatConfig + stop +elif [ $1 == "status" ]; then + formatConfig + status +elif [ $1 == "help" ]; then + help +fi -- 1.8.3.1