forked from wAmpIre/nagixsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagixsc_xml2cfg.py
executable file
·144 lines (116 loc) · 4.35 KB
/
nagixsc_xml2cfg.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
#!/usr/bin/python
#
# Nag(ix)SC -- nagixsc_xml2cfg.py
#
# Copyright (C) 2009-2010 Sven Velt <sv@teamix.net>
#
# 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 socket
import sys
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('-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('-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('', '--host-template', dest='tmpl_host', help='Filename of host template')
parser.add_option('', '--service-template', dest='tmpl_service', help='Filename of service template')
parser.add_option('-O', '', dest='output', help='Output "hosts", "services" or "both" (default)')
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(file='nagixsc.xml')
parser.set_defaults(schemacheck='')
parser.set_defaults(host=None)
parser.set_defaults(service=None)
parser.set_defaults(output=None)
parser.set_defaults(tmpl_host=None)
parser.set_defaults(tmpl_service=None)
parser.set_defaults(verb=0)
(options, args) = parser.parse_args()
# Hard coded default for host template
HOSTTEMPL='''define host {
use templ_host_default
host_name %(host_name)s
address %(address)s
}
'''
# Hard coded default for service template
SERVICETEMPL='''define service {
use templ_service_passive
host_name %(host_name)s
service_description %(service_description)s
check_command check_passive
}
'''
##############################################################################
from nagixsc import *
##############################################################################
# Output
if not options.output in [None, 'both', 'hosts', 'services']:
print 'Unknown output mode "%s"!' % options.output
sys.exit(1)
if options.output in [None, 'both']:
options.output = ['hosts', 'services']
else:
options.output = [options.output,]
# Read host and/or service template
if options.tmpl_host and 'hosts' in options.output:
HOSTTEMPL = open(options.tmpl_host).read()
if options.tmpl_service and 'services' in options.output:
SERVICETEMPL = open(options.tmpl_service).read()
# Get URL or file
doc = read_xml(options)
# 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)
# Put XML to Python dict
checks = xml_to_dict(doc, options.verb, options.host, options.service)
# Set default socket options
if hasattr(socket, 'setdefaulttimeout'):
socket.setdefaulttimeout(2)
# Loop over check results and search for new hosts and new services
foundhosts = []
for check in checks:
if not check['host_name'] in foundhosts:
foundhosts.append(check['host_name'])
if 'hosts' in options.output:
try:
check['address'] = socket.gethostbyname(check['host_name'])
except socket.gaierror:
check['address'] = '127.0.0.1'
print HOSTTEMPL % check
if check['service_description'] and 'services' in options.output:
print SERVICETEMPL % check