blob: a807a7e841b8483825a52d96bf611085a55d3e1d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#!/bin/bash
#----------------------------------------------------
# Version: 0.1.7.0
# Author: Florian "Bluewind" Pritz <f-p@gmx.at>
#
# Copyright (C) 2008-2009 Florian Pritz
#
# Licensed under GNU General Public License v3
# (see COPYING for full license text)
#
#----------------------------------------------------
# Script to check if services are working and take
# actions if not
#----------------------------------------------------
# NOTE:
# Format for service file
# <IP or hostname> <service name>
#-------------------------- CONFIGURATION -----------------------------#
# Time to wait between 2 checks; Shouldn't be lower than 10 as that
# could cause overlap with the timeouts
WAIT=60
# Take action after x faild checks
MAX_FAIL=2
# set to /dev/null if you don't want logs
LOGFILE="$HOME/watchdog.log"
# Paths (Shouldn't need to be changed)
CURL="/usr/bin/curl"
WGET="/usr/bin/wget"
PING="/bin/ping"
#----------------------------------------------------------------------#
SCRIPTNAME=$(basename $0)
EXIT_SUCCESS=0
EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_BUG=10
# Colors for output
red='\e[0;31m'
RED='\e[1;31m'
green='\e[0;32m'
GREEN='\e[1;32m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m'
function usage {
echo -e "${blue}Usage:${NC} ${SCRIPTNAME} [OPTIONS] [<hostname or IP> <service type>]" >&2
echo -e "Possible service types: http, ping (default)"
echo -e "Options:" >&2
echo -e "-h this help" >&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
}
if [ ! -f "$CURL" ]; then
if [ ! -f "$WGET" ]; then
echo -e "${red}Wget doesn't exist!$NC"
exit $EXIT_ERROR
fi
HTTP="$WGET -t 2 -T 1 -O /dev/null"
else
HTTP="$CURL --connect-timeout 1 --retry 2 -O /dev/null"
fi
if [ ! -f "$PING" ]; then
echo -e "${red}Ping command doesn't exist! Please fix the path.$NC"
exit $EXIT_ERROR
else
PING="$PING -c 1 -W 1"
fi
if [ ! "$1" ] && [ ! "$2" ]; then
usage $EXIT_SUCCESS
fi
while getopts ':f:ht:m:' OPTION ; do
case $OPTION in
f)
IPFILE="$OPTARG"
;;
t)
WAIT="$OPTARG"
;;
m)
MAX_FAIL="$OPTARG"
;;
h)
usage $EXIT_SUCCESS
;;
\?)
echo "Unknown option \"-$OPTARG\"." >&2
usage $EXIT_ERROR
;;
:)
echo "Option \"-$OPTARG\" needs an argument" >&2
usage $EXIT_ERROR
;;
*)
echo "This shouldn't happen, please file a bugreport.">&2
usage $EXIT_BUG
;;
esac
done
shift $(( OPTIND - 1 ))
if (( $# > 1 )) ; then
INPUT="$1 $2"
else
INPUT=$(cat $IPFILE)
fi
watcher () {
IP="$1"
TYPE="$2"
case "$TYPE" in
http) COMMAND="$HTTP";;
*) COMMAND="$PING";;
esac
counter=0
while :;
do
precmd_time=$(date +%s)
$COMMAND $IP &> /dev/null
exitcode=$?
if [ "$exitcode" -ne "0" ]; then
if [ "$counter" -lt "1" ]; then
downtime=`date`
fi
let counter=$counter+1
if [ "$counter" -eq $MAX_FAIL ]; then
mail root -s "$IP - $TYPE DOWN! " <<< "$IP - $TYPE is 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)"
echo "$IP - $TYPE Downtime: ${downtime} - $(date)" >> $LOGFILE
fi
counter=0
fi
current_time=$(date +%s)
processing_time=`expr ${current_time} - ${precmd_time}`
sleeptime=`expr ${WAIT} - ${processing_time}`
if [ "${sleeptime}" -lt "1" ]; then
sleeptime=0
fi
sleep ${sleeptime}
done
}
# stops watchers and exits
cleanup() {
kill $jobs &> /dev/null
exit
}
trap cleanup 0 2 15
# start watchers
while read line
do
watcher $line &
done <<< $INPUT
# Prepare killing all watchers
jobs=$(jobs -p)
disown -ar
echo "Press any key to kill"
read -n 1
|