forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_event_handler.sh
executable file
·198 lines (180 loc) · 6.96 KB
/
lib_event_handler.sh
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
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
#
# Author: Hari Sekhon
# Date: 2008-01-08 13:00:54 +0000 (Tue, 08 Jan 2008)
#
# http://github.com/harisekhon/nagios-plugins
#
# License: see accompanying LICENSE file
#
# Consts and Functions to use when writing Nagios Event Handlers in Bash
# First things first, let's get all generic Nagios stuff
lib="lib_nagios.sh"
srcdir=`dirname $0`
. "$srcdir"/$lib ||
{ echo "Error Sourcing Nagios Library"
mail -s "$HOSTNAME: Event Handler Error Sourcing Nagios Library" ${MAILTO:[email protected]} < /dev/null
exit 3
}
#
# Now replacement or addition to standard stuff follows
#
# ============================================================================ #
# V A R I A B L E S & C O N S T A N T S #
# ============================================================================ #
#srcdir=`dirname $0`
#LOGFILE=${LOGFILE:-/dev/null}
# ============================================================================ #
# C H A I N E D S O U R C I N G F U N C T I O N S #
# ============================================================================ #
source_nrpe(){
local lib="lib_check_nrpe.sh"
. "$srcdir/$lib" ||
{ echo "Event Handler: FAILED to source $lib properly"
mail -s "$HOSTNAME: Event Handler Error Sourcing Nrpe" $MAILTO < $LOGFILE
die
}
}
source_nt(){
local lib="lib_check_nt.sh"
. "$srcdir/$lib" ||
{ echo "Event Handler: FAILED to source $lib properly"
mail -s "$HOSTNAME: Event Handler Error Sourcing Check_nt" $MAILTO < $LOGFILE
die
}
}
# ============================================================================ #
# U S A G E & A R G H A N D L I N G F U N C T I O N S #
# ============================================================================ #
usage(){
# Prints usage for event handler and then calls die to exit. No args.
echo
echo "Nagios Event Handler"
echo
echo "usage: ${0##*/} -H HOSTADDRESS -s SERVICESTATE -t SERVICESTATETYPE -o SERVICEOUTPUT"
echo
echo "All arguments are required. They correspond to Nagios Macros"
echo
echo " HOSTADDRESS The IP Adress or Hostname against which this event handler"
echo " should fire. If you are checking a service through a "
echo " public interface then you should probably be using a"
echo " static internal hostname or ip address, not a Nagios macro"
echo
echo " SERVICESTATE The State of the service that was checked."
echo " Valid Values: OK, WARNING, CRITICAL, UNKNOWN"
echo " Values are case sensitive"
echo
echo " SERVICESTATETYPE The State type. Valid values: SOFT, HARD"
echo " Values are case sensitive"
echo
echo " SERVICEOUTPUT The Output from the service check which should include"
echo " the error message which can be used for logical handling"
echo
die
} >&2
parse_args(){
# Does all the option processing for the 4 main options that you need for an event handler
#
# Usage:
#
# parse_args $@
#
# Useful if you are doing the basic args, if you need something more specific you'll have to roll your own
# Missing OO here really but it's still faster to develop in this.
#
while [ "$#" -ge 1 ]; do
case "$1" in
-h|--help) usage
;;
-H) if ! [ $# -ge 2 ]; then
arg_err "You must supply a HOSTNAME/IP ADDRESS after the -H switch"
else
HOST="$2"
fi
;;
-s) if ! [ $# -ge 2 ]; then
arg_err "You must supply a SERVICESTATE after the -s switch"
else
STATE="$2"
fi
;;
-t) if ! [ $# -ge 2 ]; then
arg_err "You must supply a SERVICESTATETYPE after the -t switch"
else
STATETYPE="$2"
fi
;;
-o) if ! [ $# -ge 2 ]; then
arg_err "You must supply SERVICEOUTPUT after the -o switch"
else
SERVICEOUTPUT="$2"
fi
;;
*) arg_err "'$1' is not valid option"
;;
esac
shift
shift
done
}
validate_input(){
# Performs checks on the 4 basic options that you need for an event handler
#
# Usage:
#
# validate_args
#
# Should be called after parse_args in event handler
#
if [ -z "${HOST:-}" ]; then
arg_err "You must supply a HOSTNAME/IP ADDRESS"
elif ! grep "^[A-Za-z0-9\.-]\+$" <<< "$HOST" &>/dev/null; then
arg_err "'$HOST' is not a valid host name"
fi
if [ -z "${STATE:-}" ]; then
arg_err "You must supply a SERVICESTATE"
elif [ "$STATE" != "OK" \
-a "$STATE" != "WARNING" \
-a "$STATE" != "CRITICAL" \
-a "$STATE" != "UNKNOWN" ]; then
arg_err "STATE supplied is INVALID - must be one of the following: OK, WARNING, CRITICAL, UNKNOWN"
fi
if [ -z "${STATETYPE:-}" ]; then
arg_err "You must supply a SERVICESTATETYPE (ie HARD or SOFT)"
elif [ "$STATETYPE" != "HARD" -a "$STATETYPE" != "SOFT" ]; then
arg_err "STATETYPE supplied is INVALID - must be either HARD or SOFT"
fi
if [ -z "${SERVICEOUTPUT:-}" ]; then
arg_err "You must supply the SERVICEOUTPUT"
fi
}
# ============================================================================ #
# E R R O R H A N D L I N G F U N C T I O N S #
# ============================================================================ #
die(){
# Quits with message and notifications
#
# Usage:
#
# die "the problem was X"
#
# Takes the initial name of the event handler in order to keep it generic enough
# to be used everywhere and still retain a custom title for notification
#
# Name of caller should be event_XYZ
# X is then stripped out and used such
# EMAIL: "$HOSTNAME: XYZ Event Handler Error"
#
# Expects MAILTO and LOGFILE to be set, otherwise defaults to [email protected]
# and dev null for safety
echo "$@"
local MAILTO=${MAILTO:[email protected]}
local LOGFILE=${LOGFILE:-/dev/null}
if grep "^event_handler_[A-Za-z0-9]\+$" <<< "$0" &>/dev/null; then
local event_handler_name=`sed 's/event_//'`
else
local event_handler_name=""
fi
mail -s "${HOSTNAME:-`hostname -s`}: $event_handler_name Event Handler Error" $MAILTO < $LOGFILE
exit $CRITICAL
}