-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnagixsc_xml2nagios.py
executable file
·240 lines (194 loc) · 8.4 KB
/
nagixsc_xml2nagios.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/python
#
# Nag(ix)SC -- nagixsc_xml2nagios.py
#
# Copyright (C) 2009-2010 Sven Velt <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#import base64
import libxml2
import optparse
import os
import sys
import time
NAGIOSCMDs = [ '/usr/local/nagios/var/rw/nagios.cmd', '/var/lib/nagios3/rw/nagios.cmd', ]
CHECKRESULTDIRs = [ '/usr/local/nagios/var/spool/checkresults', '/var/lib/nagios3/spool/checkresults', ]
MODEs = [ 'passive', 'passive_check', 'checkresult', 'checkresult_check', 'active', ]
parser = optparse.OptionParser()
parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
parser.add_option('', '--force-http-auth', action='store_true', dest='httpforceauth', help='Force HTTP authentication (may be unsecure!)')
parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp')
parser.add_option('-m', '', action='store_true', dest='markold', help='Mark (Set state) of too old checks as UNKNOWN')
parser.add_option('-O', '', dest='mode', help='Where/Howto output the results ("%s")' % '", "'.join(MODEs))
parser.add_option('-p', '', dest='pipe', help='Full path to nagios.cmd')
parser.add_option('-r', '', dest='checkresultdir', help='Full path to checkresult directory')
parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file')
parser.add_option('-D', '', dest='service', help='Service description to search for in XML file')
parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
parser.set_defaults(url=None)
parser.set_defaults(httpuser=None)
parser.set_defaults(httppasswd=None)
parser.set_defaults(httpforceauth=None)
parser.set_defaults(file='-')
parser.set_defaults(schemacheck='')
parser.set_defaults(seconds=14400)
parser.set_defaults(markold=False)
parser.set_defaults(mode=False)
parser.set_defaults(pipe=None)
parser.set_defaults(checkresultdir=None)
parser.set_defaults(host=None)
parser.set_defaults(service=None)
parser.set_defaults(verb=0)
(options, args) = parser.parse_args()
##############################################################################
from nagixsc import *
##############################################################################
if options.mode not in MODEs:
print 'Not an allowed mode "%s" - allowed are: "%s"' % (options.mode, '", "'.join(MODEs))
sys.exit(127)
# Check command line options wrt mode
if options.mode == 'passive' or options.mode == 'passive_check':
debug(1, options.verb, 'Running in passive mode')
if options.pipe == None and options.verb <= 2:
for nagioscmd in NAGIOSCMDs:
if os.path.exists(nagioscmd):
options.pipe = nagioscmd
if options.pipe == None and options.verb <= 2:
print 'Need full path to the nagios.cmd pipe!'
sys.exit(127)
debug(2, options.verb, 'nagios.cmd found at %s' % options.pipe)
elif options.mode == 'checkresult' or options.mode == 'checkresult_check':
debug(1, options.verb, 'Running in checkresult mode')
if options.checkresultdir == None and options.verb <= 2:
for crd in CHECKRESULTDIRs:
if os.path.exists(crd):
options.checkresultdir = crd
if options.checkresultdir == None and options.verb <= 2:
print 'Need full path to the checkresultdir!'
sys.exit(127)
debug(2, options.verb, 'Checkresult dir: %s' % options.checkresultdir)
elif options.mode == 'active':
debug(1, options.verb, 'Running in active/plugin mode')
if options.host == None:
debug(1, options.verb, 'No host specified on command line')
if options.service == None:
debug(1, options.verb, 'No service specified on command line, looking at XML file later')
##############################################################################
# Get start time
starttime = time.time()
# Get URL or file
doc = read_xml(options)
# Now timestamp AFTER getting the XML file
now = long(time.time())
# Calculate elapsed time
elapsedtime = time.time() - starttime
# Check XML against DTD
if options.schemacheck:
dtd = libxml2.parseDTD(None, options.schemacheck)
ctxt = libxml2.newValidCtxt()
ret = doc.validateDtd(ctxt, dtd)
if ret != 1:
print "error doing DTD validation"
sys.exit(1)
dtd.freeDtd()
del dtd
del ctxt
# Check XML file basics
(status, statusstring) = xml_check_version(doc)
debug(1, options.verb, statusstring)
if not status:
print statusstring
sys.exit(127)
# Get timestamp and check it
filetimestamp = xml_get_timestamp(doc)
if not filetimestamp:
print 'No timestamp found in XML file, exiting because of invalid XML data...'
sys.exit(127)
fileage = now - filetimestamp
debug(1, options.verb, 'Age of XML file: %s seconds, max allowed: %s seconds' % (fileage, options.seconds))
# Put XML to Python dict
checks = xml_to_dict(doc, options.verb, options.host, options.service)
# Loop over check results and perhaps mark them as outdated
for check in checks:
check = check_mark_outdated(check, now, options.seconds, options.markold)
# Next steps depend on mode, output results
# MODE: passive
if options.mode == 'passive' or options.mode == 'passive_check':
count_services = dict2out_passive(checks, xml_get_timestamp(doc), options.pipe, options.verb)
# Return/Exit as a Nagios Plugin if called with mode 'passive_check'
if options.mode == 'passive_check':
returncode = 0
returnstring = 'OK'
output = '%s check results written which are %s seconds old' % (count_services, fileage)
perfdata = 'runtime=%.3fs;;;; services=%.0f;;;;' % (elapsedtime, count_services)
if options.markold:
if fileage > options.seconds:
returnstring = 'WARNING'
output = '%s check results written, which are %s(>%s) seconds old' % (count_services, fileage, options.seconds)
returncode = 1
print 'Nag(ix)SC %s - %s|%s' % (returnstring, output, perfdata)
sys.exit(returncode)
# MODE: checkresult: "checkresult", "checkresult_check"
elif options.mode.startswith('checkresult'):
(count_services, count_failed, list_failed) = dict2out_checkresult(checks, xml_get_timestamp(doc), options.checkresultdir, options.verb)
if options.mode == 'checkresult':
if list_failed:
for entry in list_failed:
print 'Could not write checkresult files "%s(.ok)" for "%s"/"%s"!' % (entry[0], entry[1], entry[2])
if count_failed == 0:
sys.exit(0)
else:
sys.exit(1)
elif options.mode == 'checkresult_check':
returnstring = ''
output = ''
perfdata = 'runtime=%.3fs;;;; services=%.0f;;;;' % (elapsedtime, count_services)
if count_failed == 0:
returnstring = 'OK'
returncode = 0
output = 'Wrote checkresult files for %s services' % count_services
elif count_failed == count_services:
returnstring = 'CRITICAL'
returncode = 2
output = 'No checkresult files could be writen!'
else:
returnstring = 'WARNING'
returncode = 1
output = 'Could not write %s out of %s checkresult files!' % (count_failed, count_services)
print 'Nag(ix)SC %s - %s|%s' % (returnstring, output, perfdata)
sys.exit(returncode)
# MODE: active
elif options.mode == 'active':
if len(checks) > 1:
print 'Nag(ix)SC UNKNOWN - Found more (%s) than one host/service!' % len(checks)
print 'Found: ' + ', '.join(['%s/%s' % (c['host_name'], c['service_description']) for c in checks])
sys.exit(3)
elif len(checks) == 0:
output = 'Nag(ix)SC UNKNOWN - No check found in XML'
if options.host:
output += ' - Host filter: "%s"' % options.host
if options.service:
output += ' - Service filter: "%s"' % options.service
print output
sys.exit(3)
print checks[0]['output']
sys.exit(int(checks[0]['returncode']))
else:
print 'Unknown mode! This should NEVER happen!'
sys.exit(127)