5300ecab9fad6a754e2eed323f8567d8c92a9307
1 #!/usr/bin/python
2 #
3 # Nag(ix)SC -- nagixsc_xml2cfg.py
4 #
5 # Copyright (C) 2009-2010 Sven Velt <sv@teamix.net>
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #import base64
22 import libxml2
23 import optparse
24 import socket
25 import sys
27 parser = optparse.OptionParser()
29 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
30 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
31 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
32 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
33 parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
34 parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file')
35 parser.add_option('-D', '', dest='service', help='Service description to search for in XML file')
36 parser.add_option('', '--host-template', dest='tmpl_host', help='Filename of host template')
37 parser.add_option('', '--service-template', dest='tmpl_service', help='Filename of service template')
38 parser.add_option('-O', '', dest='output', help='Output "hosts", "services" or "both" (default)')
39 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
41 parser.set_defaults(url=None)
42 parser.set_defaults(httpuser=None)
43 parser.set_defaults(httppasswd=None)
44 parser.set_defaults(file='nagixsc.xml')
45 parser.set_defaults(schemacheck='')
46 parser.set_defaults(host=None)
47 parser.set_defaults(service=None)
48 parser.set_defaults(output=None)
49 parser.set_defaults(tmpl_host=None)
50 parser.set_defaults(tmpl_service=None)
51 parser.set_defaults(verb=0)
53 (options, args) = parser.parse_args()
55 # Hard coded default for host template
56 HOSTTEMPL='''define host {
57 use templ_host_default
59 host_name %(host_name)s
60 address %(address)s
61 }
62 '''
64 # Hard coded default for service template
65 SERVICETEMPL='''define service {
66 use templ_service_passive
68 host_name %(host_name)s
69 service_description %(service_description)s
70 check_command check_passive
71 }
72 '''
74 ##############################################################################
76 from nagixsc import *
78 ##############################################################################
80 # Output
81 if not options.output in [None, 'both', 'hosts', 'services']:
82 print 'Unknown output mode "%s"!' % options.output
83 sys.exit(1)
85 if options.output in [None, 'both']:
86 options.output = ['hosts', 'services']
87 else:
88 options.output = [options.output,]
90 # Read host and/or service template
91 if options.tmpl_host and 'hosts' in options.output:
92 HOSTTEMPL = open(options.tmpl_host).read()
93 if options.tmpl_service and 'services' in options.output:
94 SERVICETEMPL = open(options.tmpl_service).read()
96 # Get URL or file
97 doc = read_xml(options)
99 # Check XML against DTD
100 if options.schemacheck:
101 dtd = libxml2.parseDTD(None, options.schemacheck)
102 ctxt = libxml2.newValidCtxt()
103 ret = doc.validateDtd(ctxt, dtd)
104 if ret != 1:
105 print "error doing DTD validation"
106 sys.exit(1)
107 dtd.freeDtd()
108 del dtd
109 del ctxt
112 # Check XML file basics
113 (status, statusstring) = xml_check_version(doc)
114 debug(1, options.verb, statusstring)
115 if not status:
116 print statusstring
117 sys.exit(127)
120 # Put XML to Python dict
121 checks = xml_to_dict(doc, options.verb, options.host, options.service)
124 # Set default socket options
125 if hasattr(socket, 'setdefaulttimeout'):
126 socket.setdefaulttimeout(2)
128 # Loop over check results and search for new hosts and new services
129 foundhosts = []
131 for check in checks:
132 if not check['host_name'] in foundhosts:
133 foundhosts.append(check['host_name'])
135 if 'hosts' in options.output:
136 try:
137 check['address'] = socket.gethostbyname(check['host_name'])
138 except socket.gaierror:
139 check['address'] = '127.0.0.1'
140 print HOSTTEMPL % check
142 if check['service_description'] and 'services' in options.output:
143 print SERVICETEMPL % check