Browse Source

gestion tcpwrapper + génération de nrpe.conf sur le modele de nagios.conf + ajout d'un plugin

develop
Emmanuel Garette 2 months ago
parent
commit
66c979a9c6
3 changed files with 339 additions and 0 deletions
  1. 2
    0
      dicos/20_nrpe.xml
  2. 1
    0
      eole-nrpe.mk
  3. 336
    0
      plugins/check_service.sh

+ 2
- 0
dicos/20_nrpe.xml View File

@@ -3,10 +3,12 @@
3 3
 <creole>
4 4
     <files>
5 5
         <file name="/etc/nagios/nagios.cfg"/>
6
+        <file name="/etc/nagios/nrpe.cfg" source='nagios.cfg'/>
6 7
         <service>nagios-nrpe-server</service>
7 8
         <package>nagios-nrpe-server</package>
8 9
         <service_access service='nagios-nrpe-server'>
9 10
             <port>5666</port>
11
+            <tcpwrapper>nrpe</tcpwrapper>
10 12
         </service_access>
11 13
         <service_restriction service='nagios-nrpe-server'>
12 14
             <ip interface='auto' netmask='255.255.255.255' ip_type='SymLinkOption'>nrpe_allowed_hosts</ip>

+ 1
- 0
eole-nrpe.mk View File

@@ -0,0 +1 @@
1
+plugins_PROG_DIR        := $(DESTDIR)/usr/lib/nagios/plugins/

+ 336
- 0
plugins/check_service.sh View File

@@ -0,0 +1,336 @@
1
+#!/usr/bin/env bash
2
+
3
+# Author: Jon Schipp
4
+# 2015-03-09 [Pascal Hegy] - Add sudo for linux
5
+# 2015-03-09 [Pascal Hegy] - Change USER variable to USERNAME to avoid the use and confusion with the USER env variable
6
+# 2017-08-30 [Roberto Leibman] - Reordered checks to make sure dead and inactive get checked first
7
+# 2018-04-25 [Robin Gierse] - Update check via systemctl for Linux with grep to produce better output for systemctl
8
+
9
+########
10
+# Examples:
11
+
12
+# 1.) List services for osx
13
+# $ ./check_service.sh -l -o osx
14
+#
15
+# 2.) Check status of SSH service on a linux machine
16
+# $ ./check_service.sh -o linux -s sshd
17
+
18
+# 3.) Manually select service management tool and service
19
+# $ ./check_service.sh -o linux -t "service rsyslog status"
20
+
21
+# Nagios Exit Codes
22
+OK=0
23
+WARNING=1
24
+CRITICAL=2
25
+UNKNOWN=3
26
+
27
+# Weather or not we can trust the exit code from the service management tool.
28
+# Defaults to 0, put to 1 for systemd.  Otherwise we must rely on parsing the
29
+# output from the service management tool.
30
+TRUST_EXIT_CODE=0
31
+
32
+usage()
33
+{
34
+cat <<EOF
35
+Check status of system services for Linux, FreeBSD, OSX, and AIX.
36
+     Options:
37
+        -s <service>    Specify service name
38
+        -l              List services
39
+        -o <os>         OS type, "linux/osx/freebsd/aix"
40
+        -u <user>       User if you need to ``sudo -u'' for launchctl (def: nagios, linux and osx only)
41
+        -t <tool>       Manually specify service management tool (def: autodetect) with status and service
42
+                        e.g. ``-t "service nagios status"''
43
+EOF
44
+}
45
+
46
+argcheck() {
47
+# if less than n argument
48
+if [ $ARGC -lt $1 ]; then
49
+        echo "Missing arguments! Use \`\`-h'' for help."
50
+        exit 1
51
+fi
52
+}
53
+
54
+os_check() {
55
+if [ "$OS" == null ]; then
56
+	unamestr=$(uname)
57
+        if [[ $unamestr == 'Linux' ]]; then
58
+                OS='linux'
59
+        elif [[ $unamestr == 'FreeBSD' ]]; then
60
+               OS='freebsd'
61
+        elif [[ $unamestr == 'Darwin' ]]; then
62
+               OS='osx'	       
63
+        else
64
+                echo "OS not recognized, Use \`-o\` and specify the OS as an argument"
65
+                exit 3
66
+        fi
67
+fi
68
+}
69
+
70
+
71
+
72
+determine_service_tool() {
73
+if [[ $OS == linux ]]; then
74
+        if command -v systemctl >/dev/null 2>&1; then
75
+                SERVICETOOL="systemctl status $SERVICE | grep -i Active"
76
+                LISTTOOL="systemctl"
77
+                if [ $USERNAME ]; then
78
+                    SERVICETOOL="sudo -u $USERNAME systemctl status $SERVICE"
79
+                    LISTTOOL="sudo -u $USERNAME systemctl"
80
+                fi
81
+		TRUST_EXIT_CODE=1
82
+        elif command -v service >/dev/null 2>&1; then
83
+                SERVICETOOL="service $SERVICE status"
84
+                LISTTOOL="service --status-all"
85
+                if [ $USERNAME ]; then
86
+                    SERVICETOOL="sudo -u $USERNAME service $SERVICE status"
87
+                    LISTTOOL="sudo -u $USERNAME service --status-all"
88
+                fi
89
+        elif command -v initctl >/dev/null 2>&1; then
90
+                SERVICETOOL="status $SERVICE"
91
+                LISTTOOL="initctl list"
92
+                if [ $USERNAME ]; then
93
+                    SERVICETOOL="sudo -u $USERNAME status $SERVICE"
94
+                    LISTTOOL="sudo -u $USERNAME initctl list"
95
+                fi
96
+        elif command -v chkconfig >/dev/null 2>&1; then
97
+                SERVICETOOL=chkconfig
98
+                LISTTOOL="chkconfig --list"
99
+                if [ $USERNAME ]; then
100
+                    SERVICETOOL="sudo -u $USERNAME chkconfig"
101
+                    LISTTOOL="sudo -u $USERNAME chkconfig --list"
102
+                fi
103
+        elif [ -f /etc/init.d/$SERVICE ] || [ -d /etc/init.d ]; then
104
+                SERVICETOOL="/etc/init.d/$SERVICE status | tail -1"
105
+                LISTTOOL="ls -1 /etc/init.d/"
106
+                if [ $USERNAME ]; then
107
+                    SERVICETOOL="sudo -u $USERNAME /etc/init.d/$SERVICE status | tail -1"
108
+                    LISTTOOL="sudo -u $USERNAME ls -1 /etc/init.d/"
109
+                fi
110
+        else
111
+                echo "Unable to determine the system's service tool!"
112
+                exit 1
113
+        fi
114
+fi
115
+
116
+if [[ $OS == freebsd ]]; then
117
+        if command -v service >/dev/null 2>&1; then
118
+                SERVICETOOL="service $SERVICE status"
119
+                LISTTOOL="service -l"
120
+        elif [ -f /etc/rc.d/$SERVICE ] || [ -d /etc/rc.d ]; then
121
+                SERVICETOOL="/etc/rc.d/$SERVICE status"
122
+                LISTTOOL="ls -1 /etc/rc.d/"
123
+        else
124
+                echo "Unable to determine the system's service tool!"
125
+                exit 1
126
+        fi
127
+fi
128
+
129
+if [[ $OS == osx ]]; then
130
+        if [ -f /usr/sbin/serveradmin >/dev/null 2>&1 ] && serveradmin list | grep "$SERVICE" 2>&1 >/dev/null; then
131
+                SERVICETOOL="serveradmin status $SERVICE"
132
+                LISTTOOL="serveradmin list"
133
+        elif [ -f /Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin >/dev/null 2>&1 ] && \
134
+               /Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin list | \
135
+                grep "$SERVICE" 2>&1 >/dev/null; then
136
+                SERVICETOOL="/Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin status $SERVICE"
137
+                LISTTOOL="/Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin list"
138
+        elif command -v launchctl >/dev/null 2>&1; then
139
+                SERVICETOOL="launchctl list | grep -v ^- | grep $SERVICE || echo $SERVICE not running! "
140
+                LISTTOOL="launchctl list"
141
+                if [ $USERNAME ]; then
142
+                        SERVICETOOL="sudo -u $USERNAME launchctl list | grep -v ^- | grep $SERVICE || echo $SERVICE not running! "
143
+                        LISTTOOL="sudo -u $USERNAME launchctl list"
144
+                fi
145
+        elif command -v service >/dev/null 2>&1; then
146
+                SERVICETOOL="service --test-if-configured-on $SERVICE"
147
+                LISTTOOL="service list"
148
+        else
149
+                echo "Unable to determine the system's service tool!"
150
+                exit 1
151
+        fi
152
+fi
153
+
154
+if [[ $OS == aix ]]; then
155
+        if command -v lssrc >/dev/null 2>&1; then
156
+                SERVICETOOL="lssrc -s $SERVICE | grep -v Subsystem"
157
+                LISTTOOL="lssrc -a"
158
+        else
159
+                echo "Unable to determine the system's service tool!"
160
+                exit 1
161
+        fi
162
+fi
163
+}
164
+
165
+ARGC=$#
166
+LIST=0
167
+MANUAL=0
168
+OS=null
169
+SERVICETOOL=null
170
+LISTTOOL=null
171
+SERVICE=".*"
172
+#USERNAME=nagios
173
+
174
+argcheck 1
175
+
176
+while getopts "hls:o:t:u:" OPTION
177
+do
178
+     case $OPTION in
179
+         h)
180
+             usage
181
+             exit 0
182
+             ;;
183
+         l)
184
+             LIST=1
185
+             ;;
186
+         s)
187
+             SERVICE="$OPTARG"
188
+             ;;
189
+         o)
190
+             if [[ "$OPTARG" == linux ]]; then
191
+                     OS="$OPTARG"
192
+             elif [[ "$OPTARG" == osx ]]; then
193
+                     OS="$OPTARG"
194
+             elif [[ "$OPTARG" == freebsd ]]; then
195
+                     OS="$OPTARG"
196
+             elif [[ "$OPTARG" == aix ]]; then
197
+                     OS="$OPTARG"
198
+             else
199
+                     echo "Unknown type!"
200
+                     exit 1
201
+             fi
202
+             ;;
203
+         t)
204
+             MANUAL=1
205
+             MANUALSERVICETOOL="$OPTARG"
206
+             ;;
207
+         u)
208
+             USERNAME="$OPTARG"
209
+             ;;
210
+         \?)
211
+             exit 1
212
+             ;;
213
+     esac
214
+done
215
+
216
+os_check
217
+
218
+if [ $MANUAL -eq 1 ]; then
219
+SERVICETOOL=$MANUALSERVICETOOL
220
+else
221
+determine_service_tool
222
+fi
223
+
224
+# -l conflicts with -t                                                                                                                                                   
225
+if [ $MANUAL -eq 1 ] && [ $LIST -eq 1 ]; then
226
+    echo "Options conflict: \`\`-t'' and \`\`-l''"
227
+    exit 2
228
+fi
229
+
230
+if [ $LIST -eq 1 ]; then
231
+        if [[ $LISTTOOL != null ]]; then
232
+                $LISTTOOL
233
+                exit 0
234
+        else
235
+                echo "OS not specified! Use \`\`-o''"
236
+                exit 2
237
+        fi
238
+fi
239
+
240
+# Check the status of a service
241
+STATUS_MSG=$(eval "$SERVICETOOL" 2>&1)
242
+EXIT_CODE=$?
243
+
244
+## Exit code from the service tool - if it's non-zero, we should
245
+## probably return CRITICAL.  (though, in some cases UNKNOWN would
246
+## probably be more appropriate)
247
+[ $EXIT_CODE -ne 0 ] && echo "$STATUS_MSG" && exit $CRITICAL
248
+
249
+## For systemd and most systems, $EXIT_CODE can be trusted - if it's 0, the service is running.
250
+## Ref https://github.com/jonschipp/nagios-plugins/issues/15
251
+[ $TRUST_EXIT_CODE -eq 1 ] && [ $EXIT_CODE -eq 0 ] && echo "$STATUS_MSG" && exit $OK 
252
+
253
+case $STATUS_MSG in
254
+
255
+*stop*)
256
+        echo "$STATUS_MSG"
257
+        exit $CRITICAL
258
+        ;;
259
+*STOPPED*)
260
+        echo "$STATUS_MSG"
261
+        exit $CRITICAL
262
+        ;;
263
+*not*running*)
264
+        echo "$STATUS_MSG"
265
+        exit $CRITICAL
266
+        ;;
267
+*NOT*running*)
268
+        echo "$STATUS_MSG"
269
+        exit $CRITICAL
270
+        ;;
271
+*NOT*RUNNING*)
272
+        echo "$STATUS_MSG"
273
+        exit $CRITICAL
274
+        ;;
275
+*inactive*)
276
+        echo "$STATUS_MSG"
277
+        exit $CRITICAL
278
+        ;;
279
+*dead*)
280
+        echo "$STATUS_MSG"
281
+        exit $CRITICAL
282
+        ;;
283
+*running*)
284
+        echo "$STATUS_MSG"
285
+        exit $OK
286
+        ;;
287
+*RUNNING*)
288
+        echo "$STATUS_MSG"
289
+        exit $OK
290
+        ;;
291
+*SUCCESS*)
292
+        echo "$STATUS_MSG"
293
+        exit $OK
294
+        ;;
295
+*[eE]rr*)
296
+        echo "Error in command: $STATUS_MSG"
297
+        exit $CRITICAL
298
+        ;;
299
+*[fF]ailed*)
300
+        echo "$STATUS_MSG"
301
+        exit $CRITICAL
302
+        ;;
303
+*[eE]nable*)
304
+        echo "$STATUS_MSG"
305
+        exit $OK
306
+        ;;
307
+*[dD]isable*)
308
+        echo "$STATUS_MSG"
309
+        exit $CRITICAL
310
+        ;;
311
+*[cC]annot*)
312
+        echo "$STATUS_MSG"
313
+        exit $CRITICAL
314
+        ;;
315
+*[aA]ctive*)
316
+        echo "$STATUS_MSG"
317
+        exit $OK
318
+        ;;
319
+*Subsystem*not*on*file)
320
+        echo "$STATUS_MSG"
321
+        exit $CRITICAL
322
+        ;;
323
+[1-9][1-9]*)
324
+        echo "$SERVICE running: $STATUS_MSG"
325
+        exit $OK
326
+        ;;
327
+"")
328
+	echo "$SERVICE is not running: no output from service command"
329
+	exit $CRITICAL
330
+	;;
331
+*)
332
+        echo "Unknown status: $STATUS_MSG"
333
+        echo "Is there a typo in the command or service configuration?: $STATUS_MSG"
334
+        exit $UNKNOWN
335
+        ;;
336
+esac

Loading…
Cancel
Save