bugfixes in confugration, README, file/sqlite3 processors; and added DS18B20.raspi...
authorBrian Flowers <git-admn@bsflowers.net>
Mon, 31 Jul 2017 17:11:49 +0000 (13:11 -0400)
committerBrian Flowers <git-admn@bsflowers.net>
Mon, 31 Jul 2017 17:11:49 +0000 (13:11 -0400)
README
bin/bdsm.sh
etc/bdsm/monitors/DS18B20.raspi [new file with mode: 0644]
etc/bdsm/processors/file.processor
etc/bdsm/processors/sqlite3.processor

diff --git a/README b/README
index 05d42fd..9fc06ce 100644 (file)
--- 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
 
index 4959315..ee7e473 100755 (executable)
@@ -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 (file)
index 0000000..0147ff2
--- /dev/null
@@ -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 <<EOF
+    while [ 1 ]; do
+      ts=\`date '+%s'\`
+      
+      awk -v ts="\$ts" -v host="\$HOSTNAME" \
+        '/t=/ { split(FILENAME, filearr, "[/-]"); split(\$10, temparr, "");
+        print ts "|" host "|" filearr[7] "|" temparr[3] temparr[4] "." temparr[5] }' \
+        /sys/bus/w1/devices/28-*/w1_slave
+    sleep $DELAY
+    done >> $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
index 316776e..fe634bd 100755 (executable)
@@ -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
index 97ff850..e62ea76 100755 (executable)
@@ -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 &