Annotation of badi/public_scripts/canardien/canardien, revision 1.1.1.1
1.1 adi 1: #!/bin/bash
2:
3: # canardien 0.0.2
4: # (c) 2005 under GPL by Adrian Zaugg
5:
6: # canardiens pings ente.limmat.ch to determine wether she is gone diving.
7:
8: # ToDo: multi host, multi eMail
9:
10: ## Settings
11: #
12:
13: # Host to ping. It's ente.limmat.ch
14: HOST="ente.limmat.ch"
15:
16: # eMail Alerts
17: # Send alert email messages to the following address(es). Leave empty
18: # for no alert. For multiple destinations use a comma separated list.
19: ALERT_TO=""
20:
21: # Subject of alert email
22: ALERT_SUBJECT="Achtung: $HOST antwortet nicht mehr!"
23:
24: # Text of Message (But in 'single quotes' to protect variables. They should get
25: # expanded at the time the message is sent!)
26: ALERT_TEXT='"\n[$TIME_STAMP]\n\nALERT!!\n\n\t$HOST is down!\n\nYou should probably do something, please.\nKind regards, $PINGHOST."'
27:
28: # path to fping
29: PING=`which fping`
30:
31: # answer of fping to reachable hosts
32: ALIVE_ANSWER="is alive"
33:
34: # Temporary file path
35: TMPDIR="/tmp"
36:
37: # This hosts name
38: PINGHOST=`uname -n`
39:
40: # set to an empty string to avoid debug output,
41: # to "low" for a few, output and to anything else
42: # for verbose output
43: DEBUG=loud
44:
45: # for silent (non-error) operation set to anything,
46: # comment out to enable text output
47: # (migration to use shout is in progress)
48: SILENT=shshsh
49:
50:
51: # -----------functions-----------
52:
53: # If ente.limmat.ch responds to pings, she is considered up.
54: function checkconnection {
55: unset UP
56: if [ ${#PING} -eq 0 ]; then
57: echo "Error: fping external program not in path. Exitting."
58: exit 1
59: fi
60:
61: PING_ANSWER="$($PING "$HOST")"
62: PING_ERRNUM=$?
63: if [ $PING_ERRNUM -gt 2 ]; then
64: echo "Error $PING_ERRNUM in fping $(head -1 "$PING_ANSWER") . Exitting."
65: exit 1
66: fi
67:
68: if [ `echo "$PING_ANSWER" | grep -c "$ALIVE_ANSWER"` -gt 0 ]; then
69: UP=true
70: fi
71: TIME_STAMP=`date +"%a %e.%m.%y %H:%M:%S"`
72: if [ -n "$UP" ]; then
73: if [ ! "$DEBUG" = "low" ] && [ -n "$DEBUG" ]; then
74: shout "The connection is up."
75: fi
76: else
77: if [ -n "$DEBUG" ]; then
78: shout "The connection is down."
79: fi
80: fi
81: }
82:
83: # Rember recent state of ente.limmat.ch, trigger alert
84: function rememberstate {
85: # Put a simple time stamp in a tmp file, when host gets down
86: if [ -z "$UP" ]; then
87: if [ ! -e "$TMPDIR/.canardien-$PINGHOST-$HOST" ]; then
88: # set time stamp
89: echo -ne "$TIME_STAMP" > "$TMPDIR/.canardien-$PINGHOST-$HOST"
90: # send alert
91: sendalert
92: elif [ -n "$DEBUG" ]; then
93: shout "Down since `cat $TMPDIR/.canardien-$PINGHOST-$HOST`."
94: fi
95: else
96: # Host is up
97: if [ -e "$TMPDIR/.canardien-$PINGHOST-$HOST" ]; then
98: if [ -n "$DEBUG" ]; then
99: shout "$HOST is up again."
100: fi
101: # send alert
102: ALERT_SUBJECT="$HOST antwortet wieder!"
103: ALERT_TEXT='"[$(date +"%a %e.%m.%y %H:%M:%S")]\n\n\n$HOST is up again.\n--------------------------------------\n(downtime began $(cat "$TMPDIR/.canardien-$PINGHOST-$HOST"))\n\n\n Kind regards, $PINGHOST."'
104: sendalert
105: # delete tmp file
106: rm "$TMPDIR/.canardien-$PINGHOST-$HOST"
107: else
108: # still running
109: if [ ! "$DEBUG" = "low" ] && [ -n "$DEBUG" ]; then
110: shout "$HOST is still up."
111: fi
112: fi
113: fi
114: }
115:
116: # Send an email alert
117: function sendalert {
118: # send mail
119: if [ ! -z "$ALERT_TO" ]; then
120: eval MSG=\$$ALERT_TEXT
121: echo -e -n "$MSG" | mail -s "$ALERT_SUBJECT" "$ALERT_TO"
122: if [ -n "$DEBUG" ]; then
123: shout "Alert sent to $ALERT_TO."
124: fi
125: fi
126: }
127:
128: # To avoid any output in case of a silent operation,
129: # shout instead of echo.
130: function shout()
131: {
132: if [ -z "$SILENT" ]; then
133: echo -e "$1"
134: fi
135: }
136:
137:
138: # -----------main-----------
139:
140: checkconnection
141: rememberstate
142: exit 0
143:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>