From: Brian Flowers Date: Fri, 4 Nov 2016 01:18:23 +0000 (-0400) Subject: Bugfixes for updated folder structure X-Git-Url: http://git.slightlycyberpunk.com%2C%20git.slightlycyberpunk.com/git/?a=commitdiff_plain;h=b56241ce7842d5e18e0039857c3c07eb857db28e;p=bdsm.git Bugfixes for updated folder structure --- diff --git a/bdsm.conf b/bdsm.conf deleted file mode 100644 index f7ecb56..0000000 --- a/bdsm.conf +++ /dev/null @@ -1,4 +0,0 @@ -generic:bdsm -{ - vmstat(5); -} diff --git a/bdsm.d/df.generic b/bdsm.d/df.generic deleted file mode 100755 index 93f9790..0000000 --- a/bdsm.d/df.generic +++ /dev/null @@ -1,149 +0,0 @@ -#!/bin/bash -################################################################################ -# -# df.generic -# Bash Daemon for System Monitoring -# Generic df plugin for monitoring disk utilization -# -# -# -# -############################################################################### - -# 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 -DRIVE=$5 -OUTPATH=/home/bdsm/out.fifo - -# MAIN FUNCTIONS -############################################################################### -help() -{ - echo "Generic df disk utilization monitoring" - echo "USAGE: $0 [FUNCTION] [HOSTNAME] [DELAY]" - echo "FUNCTION -- start/stop/status/help" - echo "HOSTNAME -- the hostname to monitor" - echo "::DELAY -- seconds between measurements" - echo "::LABEL -- label for this drive" - echo "::DRIVE -- filesystem path to monitor" - exit 0 -} - -start() -{ - running=`ssh -q $HOSTNAME "cat /home/bdsm/.df.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 df is installed - installed=`ssh -q $HOSTNAME "df > /dev/null 2>&1; echo $?"` - if [ ! $installed ]; then - echo "" > $OUTPATH - return 1 - fi - - ssh -q $HOSTNAME <> $OUTPATH & - echo \$! >/home/bdsm/.df.pid -EOF - else - echo "Already Running" - fi -} - -stop() -{ - ssh $HOSTNAME 'kill `cat /home/bdsm/.df.pid`' - ssh $HOSTNAME 'rm /home/bdsm/.df.pid' -} - -status() -{ - running=`ssh $HOSTNAME "cat /home/bdsm/.df.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 'df --output=pcent /' 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/file.processor b/bdsm.d/file.processor deleted file mode 100644 index f4dd03f..0000000 --- a/bdsm.d/file.processor +++ /dev/null @@ -1,103 +0,0 @@ -#!/bin/bash -################################################################################ -# -# file.processor -# 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 - -# MAIN FUNCTIONS -############################################################################### -help() -{ - echo "Logging to a flat file" - echo "USAGE: $0 [FUNCTION]" - echo "FUNCTION -- start/stop/status/help" - exit 0 -} - -start() -{ - cat /home/bdsm/in.fifo >> /home/bdsm/bdsm.log & - echo $! > /home/bdsm/.processor.pid -} - -stop() -{ - kill `cat /home/bdsm/.processor.pid` - rm /home/bdsm/.file-logger.pid -} - -status() -{ - running=`cat /home/bdsm/.processor.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 -} - -# 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 == "help" ]; then - help -fi diff --git a/bdsm.d/sensors.generic b/bdsm.d/sensors.generic deleted file mode 100755 index 852ab1b..0000000 --- a/bdsm.d/sensors.generic +++ /dev/null @@ -1,161 +0,0 @@ -#!/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 -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 -q $HOSTNAME "cat /home/bdsm/.sensors.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 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 >> $OUTPATH & - echo \$! > /home/bdsm/.sensors.pid -EOF - 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 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 'sensors' 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/sqlite3.processor b/bdsm.d/sqlite3.processor deleted file mode 100644 index f86e7a4..0000000 --- a/bdsm.d/sqlite3.processor +++ /dev/null @@ -1,117 +0,0 @@ -#!/bin/bash -################################################################################ -# -# sqlite3.processor -# Bash Daemon for System Monitoring -# Logging to an sqlite3 database 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 - -# MAIN FUNCTIONS -############################################################################### -help() -{ - echo "Logging to an sqlite3 database file" - echo "USAGE: $0 [FUNCTION]" - echo "FUNCTION -- start/stop/status/help" - exit 0 -} - -start() -{ - sqlite3 /home/bdsm/bdsm.sqlite3 "SELECT 1 FROM MEASUREMENTS" 2>/dev/null 1>&2 - if [ $? -eq 1 ]; then - sqlite3 /home/bdsm/bdsm.sqlite3 \ - "CREATE TABLE MEASUREMENTS(\ - id integer primary key, - timestamp integer, - host varchar(128), - attribute varchar(128), - value varchar(128))" - fi - - cat /home/bdsm/in.fifo | awk -F'|' '{ \ - printf("INSERT INTO MEASUREMENTS(timestamp,host,attribute,value) \ - VALUES (%s,\"%s\", \"%s\", \"%s\");\n",$1,$2, $3, $4) }' | \ - sqlite3 bdsm.sqlite3 & - echo $! > /home/bdsm/.processor.pid -} - -stop() -{ - kill `cat /home/bdsm/.processor.pid` - rm /home/bdsm/.processor.pid -} - -status() -{ - running=`cat /home/bdsm/.processor.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 -} - -# 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 == "help" ]; then - help -fi diff --git a/bdsm.d/template.generic b/bdsm.d/template.generic deleted file mode 100644 index 3740fd1..0000000 --- a/bdsm.d/template.generic +++ /dev/null @@ -1,126 +0,0 @@ -#!/bin/bash -################################################################################ -# -# template.generic -# Bash Daemon for System Monitoring -# Generic plugin template -# -############################################################################### - -# 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 - -# FIFO pipe to be used for output -OUTPATH=/home/bdsm/out.fifo - -# Optional Parameters -PARAM1=$3 -PARAM2=$4 - -# MAIN FUNCTIONS -############################################################################### -help() -{ - # Print usage information - # Formatting is important here, as this is processed by bdsm.sh! - # First two parameters should be function and hostname - # Other parameters are optional and will be stored in bdsm.conf - # Any parameters to be stored in BSDM.conf should output as: - # ::[PARAM NAME] -- [PARAM DESCRIPTION] - # Parameters NOT stored in bsfc.conf must NOT start with ::! - echo "PLUGIN DESCRIPTION GOES HERE" - echo "USAGE: $0 [FUNCTION] [HOSTNAME] [PARAMETER 1] [PARAMETER 2]" - echo "FUNCTION -- start, stop, status or help" - echo "HOSTNAME -- host to run the monitor on" - echo "::PARAM1 -- description of the first parameter" - echo "::PARAM2 -- description of the second parameter" - exit 0 -} - -start() -{ - # Start the monitor and write output to $OUTPATH - # Output should be pipe-delimited - # One line per attribute - # Format: - # $TIMESTAMP|$HOST|$ATTRIBUTE|$VALUE - # - # TIMESTAMP format is date +%s - # HOSTNAME is a global variable - # ATTRIBUTE is a string describing what is being measure - # ex. "CPU-SY" for system CPU utilization - # VALUE is the value measured (ie, percentage) - # : Don't include units -- ie, 59 not 59% - # : UP/DOWN/??? for service up/down checks - # : If those don't make sense, use whatever does, but keep it concise! -} - -stop() -{ - # Stop the monitor -} - -status() -{ - # Check if the monitor is running and echo ON or OFF -} - -compatible() -{ - # Check if this plugin is compatible with the given host -} - -# 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/vmstat.generic b/bdsm.d/vmstat.generic deleted file mode 100755 index 2779ae7..0000000 --- a/bdsm.d/vmstat.generic +++ /dev/null @@ -1,184 +0,0 @@ -#!/bin/bash -################################################################################ -# -# vmstat.generic -# Bash Daemon for System Monitoring -# Generic vmstat plugin -# -# -# -# -############################################################################### - -# 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 -OUTPATH=/home/bdsm/out.fifo - -# MAIN FUNCTIONS -############################################################################### -help() -{ - echo "Generic vmstat monitoring" - 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 -q $HOSTNAME "cat /home/bdsm/.vmstat.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 vmstat is installed - installed=`ssh -q $HOSTNAME "vmstat > /dev/null 2>&1; echo $?"` - if [ ! $installed ]; then - echo "" > $OUTPATH - return 1 - fi - - # Check what options vmstat supports - if [ `ssh -q $HOSTNAME "vmstat -n >/dev/null 2>&1; echo $?"` ]; then - vmopt="-n" - else - vmopt="" - fi - - # Check what options awk supports - if [ "`ssh -qn $HOSTNAME \"echo | awk -W interactive '{print 1}' 2>&1 1>/dev/null\"`" != "" ]; then - awkopt="" - else - awkopt="-W interactive" - fi - - ssh -q $HOSTNAME 2>/dev/null < uscol) - sycol=i - if(\$i == "wa" && i > uscol) - wacol=i - if(\$i == "id" && i > uscol) - idcol=i - } - } - !/[a-z-]/ { - "date '+%s'" | getline ts - if(uscol != 99 && uscol != 0) - print ts,host,"CPU-US",\$uscol - if(sycol != 0) - print ts,host,"CPU-SY",\$sycol - if(wacol != 0) - print ts,host,"CPU-WA",\$wacol - if(idcol != 0) - print ts,host,"CPU-ID",\$idcol - close("date '+%s'") - }' >> $OUTPATH 2>/home/bdsm/err.log & -EOF - ssh -q $HOSTNAME 2>/dev/null "ps aux | \ - grep bdsm | \ - grep vmstat | \ - grep -v grep | \ - awk '{print \$2}' \ - > /home/bdsm/.vmstat.pid &" - else - echo "Already Running" - fi -} - -stop() -{ - ssh $HOSTNAME 'kill `cat /home/bdsm/.vmstat.pid`' - ssh $HOSTNAME 'rm /home/bdsm/.vmstat.pid' -} - -status() -{ - running=`ssh $HOSTNAME "cat /home/bdsm/.vmstat.pid 2>/dev/null | \ - xargs ps -p 2>/dev/null | \ - grep -v PID | \ - wc -l"` - - if [ $running -eq 0 ]; then - echo "OFF" - else - echo "ON" - fi -} - -compatible() -{ - compatible="`ssh -q -n $HOSTNAME 'stdbuf -oL vmstat' 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.sh b/bdsm.sh deleted file mode 100755 index 0592ee2..0000000 --- a/bdsm.sh +++ /dev/null @@ -1,487 +0,0 @@ -#!/bin/bash -################################################################################ -# -# bdsm.sh -# Bash Daemon for System Monitoring -# -# TODO: -# 1) Continuing checks (confirm start/stop/status/config of all plugins) -# 2) Fix stop -- doesn't stop df, doesn't stop remote sessions, doesn't stop processor -# -# ) Plugins: -# o) disk utilization - iostat -# b) ram utilization - free or vmstat -# c) network utilization - natstat and netstat -D -# d) uptime -# e) IP address - ifconfig -# f) updates - native package manager -# g) active users - ps -ef -# h) active processes - ps -ef -# -############################################################################### - -# TRAP UNKNOWN ERRORS -############################################################################### - -trap failure SIGFPE SIGHUP SIGABRT SIGALRM SIGQUIT -trap term SIGTERM SIGINT - -term() -{ - echo - echo - error "BDSM TERMINATED." - exit 1 -} - -failure() -{ - echo - echo - error "Something has gone SERIOUSLY wrong! (Received signal $?)" - exit 255 -} - - -# VARIABLES -############################################################################### - -if [ -f "/home/bdsm/bdsm.d/$2.processor" ]; then - processor="${2}" -else - processor="" -fi - -# UTILITY FUNCTIONS -############################################################################### -error() -{ - printf "\e[41m" 1>&2 - printf "ERROR: $1" 1>&2 - printf "\e[0m\n" 1>&2 -} - -warn() -{ - printf "\e[30;43m" 1>&2 - printf "WARNING: $1" 1>&2 - printf "\e[0m\n" 1>&2 - -} - -header() -{ - echo - printf "\e[44m" 1>&2 - printf " $1" 1>&2 - printf "\e[0m\n" 1>&2 -} - -option() -{ - printf "\e[36m" 1>&2 - printf " $1" 1>&2 - printf "\e[0m\n" 1>&2 -} - -prompt() -{ - read -t .25 -n 1000 discard &2 - printf "\r$1" 1>&2 - printf "\e[0;0m " 1>&2 -} - -info() -{ - printf "\e[33m" - printf "INFO: $1" - printf "\e[0m\n" -} - -selector() -{ - prmpt=${1} - shift - - argarr=( "$@" ) - i=0 - while [ ! -z "$1" ]; do - option "$i. $1" - i=`expr $i + 1` - shift - done - - selection=-1 - while [ "$selection" -lt 0 ] || \ - [ "$selection" -gt $i ] || \ - [ "`echo $selection | sed 's/[^0-9]//g'`" != "$selection" ]; do - prompt "$prmpt: " - read selection - done - - echo ${argarr[$selection]} -} - -confirm() -{ - prmpt=${1} - - prompt "CONFIRM: $prmpt Y/[N]" - read selection - if [ "$selection" == "Y" ] || [ "$selection" == "y" ]; then - echo 0 - else - echo 1 - fi -} - -# ACTION FUNCTIONS -############################################################################### - -help() -{ - echo - echo "Bash Daemon for System Monitoring (bdsm.sh)" - echo "USAGE: $0 [start|stop|status|deploy|configure|help]" - echo - echo "If this is your first time executing $0, start with:" - echo " $0 configure" - echo - echo "Once configuration is complete, you must deploy with:" - echo " $0 deploy" - echo - echo "During deployment you may be asked for the root password" - echo " for these systems. This is required for configuring user" - echo " setup. To avoid this, create a user 'bdsm' on the remote" - echo " server and configure passwordless SSH access to this user." - echo " This user will need very limited permissions." - echo - echo " After deployment is completed, you can start, check, or stop:" - echo " $0 start" - echo " $0 status" - echo " $0 stop" - echo - echo "Returns:" - echo " 0: Success!" - echo " 1: PEBKAC" - echo " 2: Could not connect to remote host" - echo " 255: You're on your own. Good luck!" -} - -start() -{ - # Start the processor - processors=($(ls /home/bdsm/bdsm.d/*.processor | sed 's|.*/\(.*\).processor|\1|g')) - if [ ${#processors[@]} -eq 0 ]; then - error "No processors available!" - error "Please install a .processor script to /home/bdsm/bdsm.d" - elif [ ${#processors} -eq 1 ]; then - processor=${processors[0]} - else - processor=`selector "Select a processor" ${processors[@]}` - fi - /home/bdsm/bdsm.d/$processor.processor start - - i=0 - while [ $i -lt ${#hosts[@]} ]; do - type=`echo ${hosts[$i]} | cut -d':' -f1` - host=`echo ${hosts[$i]} | cut -d':' -f2-` - info "Starting $type monitors on $host..." - functions=`cat /home/bdsm/.bdsm.conf.tmp | grep "$type:$host" | sed 's/.*{\(.*\)}.*/\1/'` - - ssh -q $host "cat /home/bdsm/out.fifo" > /home/bdsm/in.fifo & - echo $! > /home/bdsm/pids/$host.in - - - j=0 - while read -u10 line; do - command=`echo $line | cut -d' ' -f1` - if [ "$command" == "" ]; then - continue - fi - args="`echo $line | cut -d' ' -f2-`" - if [ -f /home/bdsm/bdsm.d/$command.$type ]; then - /home/bdsm/bdsm.d/$command.$type start $host $args - elif [ -f /home/bdsm/bdsm.d/$command.$type ]; then - /home/bdsm/bdsm.d/$command.generic start $host $args - else - error "Could not find plugin for $command on $type:$host!" - fi - done 10< <(echo $functions | tr ';' '\n' | sed 's/[(),]/ /g') - - i=`expr $i + 1` - done -} - -stop() -{ - i=0 - while [ $i -lt ${#hosts[@]} ]; do - type=`echo ${hosts[$i]} | cut -d':' -f1` - host=`echo ${hosts[$i]} | cut -d':' -f2-` - info "Stopping $type monitors on $host..." - functions=`cat /home/bdsm/.bdsm.conf.tmp | grep "$type:$host" | sed 's/.*{\(.*\)}.*/\1/'` - - j=0 - echo $functions | tr ';' '\n' | sed 's/[(),]/ /g' | while read line; do - command=`echo $line | cut -d' ' -f1` - if [ "$command" == "" ]; then - continue - fi - args="`echo $line | cut -d' ' -f2-`" - if [ -f /home/bdsm/bdsm.d/$command.$type ]; then - /home/bdsm/bdsm.d/$command.$type stop $host $args - elif [ -f /home/bdsm/bdsm.d/$command.$type ]; then - /home/bdsm/bdsm.d/$command.generic stop $host $args - else - error "Could not find plugin for $command on $type:$host!" - fi - done - - # These should terminate once all writing processes are killed - # Might need some error handling here -- this command SHOULD always fail - # If the processes exist, something is wrong! - kill `cat /home/bdsm/pids/$host.in` 2>/dev/null - rm /home/bdsm/pids/$host.in - - /home/bdsm/bdsm.d/$processor.processor stop - - i=`expr $i + 1` - done -} - -status() -{ - i=0 - while [ $i -lt ${#hosts[@]} ]; do - type=`echo ${hosts[$i]} | cut -d':' -f1` - host=`echo ${hosts[$i]} | cut -d':' -f2-` - info "Checking status of $type monitors on $host..." - functions=`cat /home/bdsm/.bdsm.conf.tmp | grep "$type:$host" | sed 's/.*{\(.*\)}.*/\1/'` - - j=0 - while read -u10 line; do - command=`echo $line | cut -d' ' -f1` - if [ "$command" == "" ]; then - continue - fi - prefix="$type:$host|$command|" - if [ -f /home/bdsm/bdsm.d/$command.$type ]; then - /home/bdsm/bdsm.d/$command.$type status $host | sed "s/^/$prefix/g" - elif [ -f /home/bdsm/bdsm.d/$command.$type ]; then - /home/bdsm/bdsm.d/$command.generic status $host | sed "s/^/$prefix/g" - else - error "Could not find plugin for $command on $type:$host!" - fi - done 10< <(echo $functions | tr ';' '\n' | sed 's/[(),]/ /g') - - pid=`cat /home/bdsm/pids/$host.in 2>/dev/null` - if [ ! -f /home/bdsm/pids/$host.in ]; then - echo "$type:$host:MONITOR|???" - elif [ `ps -p $pid | wc -l` -ge 2 ]; then - echo "$type:$host:MONITOR|UP" - else - echo "$type:$host:MONITOR|DOWN" - fi - - i=`expr $i + 1` - done -} - -configure() -{ - configured=0 - if [ -f /home/bdsm/bdsm.conf ]; then - prompt "Configuration already exists! [K]eep, [U]pdate, or [D]elete?" - read selection - if [ $selection == "K" ]; then - info "Proceeding with existing configuration..." - configured=1 - elif [ $selection == "U" ]; then - info "Updating existing configuration..." - elif [ $selection == "D" ]; then - info "Purging existing configuration..." - rm /home/bdsm/bdsm.conf - touch /home/bdsm/bdsm.conf - else - error "Invalid selection." - return 1 - fi - else - touch /home/bdsm/bdsm.conf - fi - - while [ $configured -ne 1 ]; do - # Prompt which host to configure - header "Select a host to configure:" - - host=`selector "Select a host" ${hosts[@]} "New..." "Done"` - if [ "$host" == "New..." ]; then - host="" - elif [ "$host" == "Done" ]; then - configured=1 - break - else - type=`echo $host | cut -d':' -f1` - host=`echo $host | cut -d':' -f2-` - fi - while [ -z $host ]; do - header "Configure a new host" - - types=$(ls /home/bdsm/bdsm.d/ | cut -d'.' -f2 | sort -u) - type=`selector "Host type" ${types[@]}` - - prompt "Hostname: " - read host - - if [ `confirm "Confirm creation of $type:$host?"` -eq 0 ]; then - echo "$type:$host{}" >> /home/bdsm/.bdsm.conf.tmp - else - host="" - fi - done - - header "Configuring $host" - availServices=() - while read service; do - if [ -f /home/bdsm/bdsm.d/$service.$type ]; then - serviceFile="$service.$type" - elif [ -f /home/bdsm/bdsm.d/$service.generic ]; then - serviceFile="$service.generic" - else - error "$service plugin not found!" - fi - - compatible=($(/home/bdsm/bdsm.d/$serviceFile compatible $host)) - if [ "$compatible" -eq "1" ]; then - availServices+=($service) - fi - done < <(find /home/bdsm/bdsm.d -executable -type f | \ - sed 's|/home/bdsm/bdsm.d/||g' | \ - cut -d'.' -f1 | \ - sort -u) - - services=$(cat /home/bdsm/.bdsm.conf.tmp | grep ":$host{" | \ - sed 's/.*{\(.*\)}/\1/g' | tr ';' ' ') - service=`selector "Select a service to configure" ${services[@]} "Add a service..." "Done"` - - if [ "$service" == "Add a service..." ]; then - header "Add a new service" - service=`selector "Service type: " ${availServices[@]}` - elif [ "$service" == "Done" ]; then - continue - fi - - header "Configuring $service" - if [ `confirm "Disable $service?"` -eq 0 ]; then - cat /home/bdsm/.bdsm.conf.tmp | \ - sed 's/\(.*:$host{.*\)$service;\(.*\)/\1\2/g' \ - > /home/bdsm/.bdsm.conf.tmp2 - mv /home/bdsm/.bdsm.conf.tmp2 /home/bdsm/.bdsm.conf.tmp - else - header "Reconfiguring $service" - svc=`echo $service | sed 's/(.*)$//g'` - if [ -f /home/bdsm/bdsm.d/$svc.$type ]; then - serviceFile="$svc.$type" - elif [ -f /home/bdsm/bdsm.d/$svc.generic ]; then - serviceFile="$svc.generic" - else - error "$service plugin not found!" - fi - - max=`/home/bdsm/bdsm.d/$serviceFile help | grep "^::" | wc -l` - newservice=`echo $service | cut -d'(' -f1`"(" - i=1 - while [ $i -le $max ]; do - param="`/home/bdsm/bdsm.d/$serviceFile help | grep "^::" | sed -n ${i}p`" - paramName="`echo ${param} | sed 's/^::\(.*\) *--.*/\1/g'`" - paramDesc="`echo ${param} | sed 's/^.*--\(.*\)/\1/g'`" - prompt "$paramName: " - read param - newservice=${newservice}${param}"," - i=`expr $i + 1` - done - newservice=`echo $newservice | sed 's/,$/)/g'` - fi - - # TODO - # Validate removal/replacement of old service - # TODO - curHost="`cat /home/bdsm/.bdsm.conf.tmp | grep ":$host{"`" - curHost=`echo $curHost | sed "s/$service;//g" | sed "s|}\$|$newservice;}|g"` - cat /home/bdsm/.bdsm.conf.tmp | \ - grep -v ":$host{" \ - > /home/bdsm/.bdsm.conf.tmp2 - echo $curHost >> /home/bdsm/.bdsm.conf.tmp2 - mv /home/bdsm/.bdsm.conf.tmp2 /home/bdsm/.bdsm.conf.tmp - # Reload the hosts list - hosts=$(cat /home/bdsm/.bdsm.conf.tmp | cut -d'{' -f1) - done - - # Convert config back to a more readable format - cat /home/bdsm/.bdsm.conf.tmp | sed 's/{/\ - {\ - /g' | sed 's/;/;\ - /g' | sed 's/}/}\ - /g' > /home/bdsm/bdsm.conf - rm /home/bdsm/.bdsm.conf.tmp - - info "Configuration complete!" - info "Don't forget to DEPLOY these changes!" - exit 0 -} - -deploy() -{ - i=0 - while [ $i -lt ${#hosts[@]} ]; do - type=`echo ${hosts[$i]} | cut -d':' -f1` - host=`echo ${hosts[$i]} | cut -d':' -f2-` - info "Deploying on $host..." - - ssh -q -o PreferredAuthentications=publickey bdsm@$host "echo > /dev/null" - ret=$? - if [ $ret -ne 0 ]; then - info "Passwordless SSH not configured. Login as root to configure:" - cat /home/bdsm/.ssh/id_rsa.pub | ssh -q root@$host "cat >> /tmp/bdsmid; useradd --home-dir /home/bdsm bdsm; mkdir -p /home/bdsm/.ssh/; cat /tmp/bdsmid >> /home/bdsm/.ssh/authorized_keys" - - #info "Logging in as bdsm user to transfer SSH key..." - #cat /home/bdsm/.ssh/id_rsa.pub | ssh bdsm@$host 'cat >> ~/.ssh/authorized_keys' - fi - - ssh -q bdsm@$host "mkfifo /home/bdsm/out.fifo" - i=`expr $i + 1` - done - - mkfifo /home/bdsm/in.fifo - mkdir /home/bdsm/pids -} - -# SCRIPT START -############################################################################### - -# 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/[[:space:]]//g" | sed 's/}/}\ -/g' | sed 's/^;*//g' | sed 's/\([{}]\);/\1/g' \ -> /home/bdsm/.bdsm.conf.tmp - -# 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 == "configure" ]; then - configure -elif [ $1 == "deploy" ]; then - deploy -else - help -fi diff --git a/bin/bdsm.sh b/bin/bdsm.sh index fb26406..0e8a2fc 100755 --- a/bin/bdsm.sh +++ b/bin/bdsm.sh @@ -289,19 +289,19 @@ status() pid=`cat /home/bdsm/pids/$host.in 2>/dev/null` if [ ! -f $config_path/pids/$host.in ]; then - echo "$type:$host:MONITOR|???" + echo "$type:$host:MONITOR|DOWN" elif [ `ps -p $pid | wc -l` -ge 2 ]; then echo "$type:$host:MONITOR|UP" else - echo "$type:$host:MONITOR|DOWN" + echo "$type:$host:MONITOR|FAILED" fi i=`expr $i + 1` done processor="`basename /home/bdsm/pids/*.processor`" - processor="`$config_path/processors/$processor status`" - echo "PROCESSOR|$processor" + status="`$config_path/processors/$processor status`" + echo "PROCESSOR|$status" } configure() @@ -475,7 +475,7 @@ deploy() # Reformat config file to be more machine-readable # Removes whitespace and adds semicolons after property values -cat /$config_pathbdsm.conf | tr '\n' ' ' | sed "s/[[:space:]]//g" | sed 's/}/}\ +cat $config_path/bdsm.conf | tr '\n' ' ' | sed "s/[[:space:]]//g" | sed 's/}/}\ /g' | sed 's/^;*//g' | sed 's/\([{}]\);/\1/g' \ > $config_path/.bdsm.conf.tmp diff --git a/etc/bdsm/bdsm.db b/etc/bdsm/bdsm.db deleted file mode 100644 index e69de29..0000000 diff --git a/etc/bdsm/processors/file.processor b/etc/bdsm/processors/file.processor old mode 100644 new mode 100755 diff --git a/etc/bdsm/processors/sqlite3.processor b/etc/bdsm/processors/sqlite3.processor old mode 100644 new mode 100755