summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xssn.at>2010-09-29 20:37:46 +0200
committerFlorian Pritz <bluewind@xssn.at>2010-09-29 20:37:46 +0200
commit28bd88fd90570f3e400de5e54ca7ab2432e3826b (patch)
treefc656c32f4be7c59fff4ed199e504d5294b8b3a8
parent6af803f4d9df06b06c31ebc907deb106df9c1e9b (diff)
downloadbin-28bd88fd90570f3e400de5e54ca7ab2432e3826b.tar.gz
bin-28bd88fd90570f3e400de5e54ca7ab2432e3826b.tar.xz
watchdog.sh: misc changes
Signed-off-by: Florian Pritz <bluewind@xssn.at>
-rwxr-xr-xwatchdog.sh43
1 files changed, 29 insertions, 14 deletions
diff --git a/watchdog.sh b/watchdog.sh
index 0101b26..ae5e482 100755
--- a/watchdog.sh
+++ b/watchdog.sh
@@ -17,7 +17,7 @@
#-------------------------- CONFIGURATION -----------------------------#
# Time to wait between 2 checks; Shouldn't be lower than 10 as that
# could cause overlap with the timeouts
-WAIT=60
+WAIT=30
# Take action after x faild checks
MAX_FAIL=2
@@ -38,6 +38,8 @@ EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_BUG=10
+DEBUG=0
+
# Colors for output
red='\e[0;31m'
RED='\e[1;31m'
@@ -50,16 +52,23 @@ CYAN='\e[1;36m'
NC='\e[0m'
function usage {
- echo -e "${blue}Usage:${NC} ${SCRIPTNAME} [OPTIONS] [<hostname or IP> <service type>]" >&2
+ echo -e "${blue}Usage:${NC} ${SCRIPTNAME} [OPTIONS]" >&2
echo -e "Possible service types: http, ping (default)"
echo -e "Options:" >&2
echo -e "-h this help" >&2
+ echo -e "-d debug output" >&2
echo -e "-f <file> loads IP list from a file" >&2
echo -e "-t <time> Time between 2 checks"
echo -e "-m <tries> Max. fails before taking actions"
[[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
}
+dbg() {
+ if [[ $DEBUG -gt 0 ]]; then
+ echo "$@" >&2
+ fi
+}
+
if [ ! -f "$CURL" ]; then
if [ ! -f "$WGET" ]; then
echo -e "${red}Wget doesn't exist!$NC"
@@ -81,7 +90,7 @@ if [ ! "$1" ] && [ ! "$2" ]; then
usage $EXIT_SUCCESS
fi
-while getopts ':f:ht:m:' OPTION ; do
+while getopts ':f:hdt:m:' OPTION ; do
case $OPTION in
f)
IPFILE="$OPTARG"
@@ -95,6 +104,9 @@ while getopts ':f:ht:m:' OPTION ; do
h)
usage $EXIT_SUCCESS
;;
+ d)
+ DEBUG=1
+ ;;
\?)
echo "Unknown option \"-$OPTARG\"." >&2
usage $EXIT_ERROR
@@ -113,15 +125,12 @@ done
shift $(( OPTIND - 1 ))
-if (( $# > 1 )) ; then
- INPUT="$1 $2"
-else
- INPUT=$(cat $IPFILE)
-fi
-
watcher () {
IP="$1"
TYPE="$2"
+ WAIT="$3"
+
+ dbg "watcher: $IP - $TYPE - $WAIT"
case "$TYPE" in
http) COMMAND="$HTTP";;
@@ -141,12 +150,15 @@ watcher () {
fi
let counter=$counter+1
if [ "$counter" -eq $MAX_FAIL ]; then
- mail root -s "$IP - $TYPE DOWN! " <<< "$IP - $TYPE is down since $downtime"
+ mail root -s "$IP - $TYPE DOWN! " <<< "$IP - $TYPE is down since $downtime
+$(tracepath $IP)"
+ dbg "$IP - $TYPE down since $downtime"
fi
else
if [ "$counter" -gt $MAX_FAIL ] || [ "$counter" -eq $MAX_FAIL ]; then
mail root -s "$IP - $TYPE UP! " <<< "$IP - $TYPE is OK again.
Downtime: ${downtime} - $(date)"
+ dbg "$IP - $TYPE up again $(date)"
echo "$IP - $TYPE Downtime: ${downtime} - $(date)" >> $LOGFILE
fi
counter=0
@@ -163,21 +175,24 @@ watcher () {
# stops watchers and exits
cleanup() {
+ dbg "cleaning up..."
kill $jobs &> /dev/null
exit
}
-trap cleanup 0 2 15
+trap cleanup 2 15
# start watchers
while read line
do
watcher $line &
-done <<< $INPUT
+done < $IPFILE
# Prepare killing all watchers
jobs=$(jobs -p)
disown -ar
-echo "Press any key to kill"
-read -n 1
+echo "Press ^C to kill"
+while :; do
+ sleep 999
+done