From: Brian Flowers Date: Mon, 31 Jul 2017 17:11:49 +0000 (-0400) Subject: bugfixes in confugration, README, file/sqlite3 processors; and added DS18B20.raspi... X-Git-Url: http://git.slightlycyberpunk.com%2C%20git.slightlycyberpunk.com/git/?a=commitdiff_plain;h=7ab99e8c2f2e297a9a63dd6ab227f710cda1dd68;p=bdsm.git bugfixes in confugration, README, file/sqlite3 processors; and added DS18B20.raspi plugin --- diff --git a/README b/README index 05d42fd..9fc06ce 100644 --- a/README +++ b/README @@ -15,7 +15,8 @@ series of plugin module scripts. Doing this kind of monitoring through a shell environment does create significant overhead, and bdsm is not recommended any production uses. It may be useful for certain niche hardware and software applications, -and as an experiment in shell scripting. +and as an experiment in shell scripting. It also isn't quite functional +yet.... 0) PREREQUISITES @@ -25,18 +26,26 @@ c) (additional requirements based on the monitoring plugins selected) 1) INSTALLATION INSTRUCTIONS -Create a new user named bdsm and clone the repository into this user's home -directory. +Create a new user named 'bdsm' and extract the latest bdsm snapshot to this +user's home directory: + +useradd bdsm +sudo su - bdsm +wget -O bdsm.tar.gz "https://git.slightlycyberpunk.com/?p=bdsm.git;a=snapshot;h=HEAD;sf=tgz" + +mv bdsm*/* . +rmdir bdsm* + 2) CONFIGURATION Login as the bdsm user Configure your infrastructure with: -~/bdsm.sh configure +~/bin/bdsm.sh configure Then deploy the required components to all downstream hosts with: -~/bdsm.sh deploy +~/bin/bdsm.sh deploy 3) USAGE diff --git a/bin/bdsm.sh b/bin/bdsm.sh index 4959315..ee7e473 100755 --- a/bin/bdsm.sh +++ b/bin/bdsm.sh @@ -314,7 +314,7 @@ status() configure() { configured=0 - if [ -f $config_path/bdsm.conf ]; then + if [ -s $config_path/bdsm.conf ]; then prompt "Configuration already exists! [K]eep, [U]pdate, or [D]elete?" read selection if [ $selection == "K" ]; then diff --git a/etc/bdsm/monitors/DS18B20.raspi b/etc/bdsm/monitors/DS18B20.raspi new file mode 100644 index 0000000..0147ff2 --- /dev/null +++ b/etc/bdsm/monitors/DS18B20.raspi @@ -0,0 +1,131 @@ +#!/bin/bash +################################################################################ +# +# DS18B20.raspi +# Bash Daemon for System Monitoring +# Plugin for monitoring DS18B20 temperature sensors connected to a Raspberry Pi +# Hardware should be connected and configured as described at: +# https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/overview +# +# +############################################################################### + +# 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 +SENSOR-ID="${4}" +OUTPATH=/home/bdsm/out.fifo + +# MAIN FUNCTIONS +############################################################################### +help() +{ + echo "DS18B20 temperature monitoring for Raspberry Pi systems" + echo "USAGE: $0 [FUNCTION] [HOSTNAME] [DELAY] [SENSOR-ID]" + echo "FUNCTION -- start/stop/status/help" + echo "HOSTNAME -- the hostname to monitor" + echo "::DELAY -- seconds between measurements" + echo "::SENSOR-ID -- the serial number of the sensor to read" + exit 0 +} + +start() +{ + running=`ssh -q $HOSTNAME "cat /home/bdsm/pids/DS18B20.pid 2>/dev/null | \ + xargs ps -p 2>/dev/null | \ + grep -v PID | \ + wc -l"` 2>/dev/null + + if [ $running -eq 0 ] || [ $running -gt 5 ]; then + # Check if the hardware is installed + installed=`ssh -q $HOSTNAME "cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves | grep '^28' | wc -l"` + if [ $installed == 0 ]; then + echo "" > $OUTPATH + return 1 + fi + + ssh -q $HOSTNAME <> $OUTPATH & + echo \$! > /home/bdsm/pids/DS18B20.pid +EOF + else + echo "Already Running" + fi +} + +stop() +{ + ssh $HOSTNAME 'kill `cat /home/bdsm/pids/DS18B20.pid`' + ssh $HOSTNAME 'rm /home/bdsm/pids/DS18B20.pid' +} + +status() +{ + running=`ssh $HOSTNAME "cat /home/bdsm/pids/DS18B20.pid 2>/dev/null | \ + xargs ps -p 2>/dev/null | \ + 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 'cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves | grep \"^28\" | wc -l' 2>/dev/null`" + if [ "$compatible" -gt 0 ]; then + echo 0 + else + echo 1 + fi +} + +# SCRIPT START +############################################################################### + +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/etc/bdsm/processors/file.processor b/etc/bdsm/processors/file.processor index 316776e..fe634bd 100755 --- a/etc/bdsm/processors/file.processor +++ b/etc/bdsm/processors/file.processor @@ -41,8 +41,20 @@ help() start() { - cat /home/bdsm/in.fifo | grep '.*|.*|.*|.*' >> /home/bdsm/bdsm.out & + cat /home/bdsm/in.fifo | grep '.*|.*|.*|.*' --line-buffered >> /home/bdsm/bdsm.out & echo $! > /home/bdsm/pids/file.processor.pid + + # Check if it failed to write the pid... + if [ ! -s /home/bdsm/pids/file.processor.pid ]; then + ps -ef | grep in.fifo | grep -v grep | awk '{print $2}' > /home/bdsm/pids/file.processor.pid + fi + + # Check if it failed to start... + if [ ! -s /home/bdsm/pids/file.processor.pid ]; then + error "File processor was unable to start." + return 1 + fi + nohhup logrotation & } stop() @@ -68,6 +80,24 @@ status() fi } +logrotation() +{ + hours=`date +%H` + minutes=`date +%M` + hourdiff=`expr 23 - $hours` + mindiff=`expr 59 - $minutes` + diff=`expr $hourdiff \* 3600 + $mindiff \* 60` + + filedate=`date +%Y%m%d` + + if [ -s /home/bdsm/bdsm.out ]; then + mv /home/bdsm/bdsm.out /home/bdsm/bdsm.$filedate.out + touch /home/bdsm/bdsm.out + fi + + sleep $diff && logrotation +} + # SCRIPT START ############################################################################### if [ -z $1 ]; then diff --git a/etc/bdsm/processors/sqlite3.processor b/etc/bdsm/processors/sqlite3.processor index 97ff850..e62ea76 100755 --- a/etc/bdsm/processors/sqlite3.processor +++ b/etc/bdsm/processors/sqlite3.processor @@ -58,7 +58,7 @@ start() value varchar(128))" fi - cat /home/bdsm/in.fifo | grep '.*|.*|.*|.*' | awk -F'|' '{ \ + cat /home/bdsm/in.fifo | grep '.*|.*|.*|.*' --line-buffered | awk -F'|' '{ \ printf("INSERT INTO MEASUREMENTS(timestamp,host,attribute,value) \ VALUES (%s,\"%s\", \"%s\", \"%s\");\n",$1,$2, $3, $4) }' | \ sqlite3 $CONFIG_PATH/bdsm.db &