From a5788647115536b1318d62e24b11597603837f00 Mon Sep 17 00:00:00 2001 From: Brian Flowers Date: Tue, 20 Sep 2016 02:39:08 -0400 Subject: [PATCH] Upgraded logger to processor framework --- bdsm.d/file.logger | 116 -------------------------------------------------- bdsm.d/file.processor | 106 +++++++++++++++++++++++++++++++++++++++++++++ bdsm.sh | 17 +++++--- 3 files changed, 117 insertions(+), 122 deletions(-) delete mode 100644 bdsm.d/file.logger create mode 100644 bdsm.d/file.processor diff --git a/bdsm.d/file.logger b/bdsm.d/file.logger deleted file mode 100644 index f76f4e6..0000000 --- a/bdsm.d/file.logger +++ /dev/null @@ -1,116 +0,0 @@ -#!/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/file.processor b/bdsm.d/file.processor new file mode 100644 index 0000000..dbbccd8 --- /dev/null +++ b/bdsm.d/file.processor @@ -0,0 +1,106 @@ +#!/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 +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() +{ + cat /home/bdsm/in.fifo >> $FILE + 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 -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.sh b/bdsm.sh index 1b01296..b029635 100755 --- a/bdsm.sh +++ b/bdsm.sh @@ -163,9 +163,16 @@ help() start() { # Start the processor - #TODO: Plugin system? - cat /home/bdsm/in.fifo > /home/bdsm/bdsm.log & - echo $! > /home/bdsm/.processor.pid + 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" + else if [ ${#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 @@ -223,8 +230,7 @@ stop() fi done - 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 @@ -322,7 +328,6 @@ configure() done header "Configuring $host" - #availServices=$(ls /home/bdsm/bdsm.d | cut -d'.' -f1 | sort -u) availServices=() while read service; do if [ -f /home/bdsm/bdsm.d/$service.$type ]; then -- 1.8.3.1