Code

Added English translations for QUICKSTART.txt and obsess.README.
[nagixsc.git] / nagixsc_xml2cfg.py
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; only version 2 of the License is applicable.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 #import base64
21 import libxml2
22 import optparse
23 import socket
24 import sys
26 parser = optparse.OptionParser()
28 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
29 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
30 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
31 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
32 parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
33 parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file')
34 parser.add_option('-D', '', dest='service', help='Service description to search for in XML file')
35 parser.add_option('', '--host-template', dest='tmpl_host', help='Filename of host template')
36 parser.add_option('', '--service-template', dest='tmpl_service', help='Filename of service template')
37 parser.add_option('-O', '', dest='output', help='Output "hosts", "services" or "both" (default)')
38 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
40 parser.set_defaults(url=None)
41 parser.set_defaults(httpuser=None)
42 parser.set_defaults(httppasswd=None)
43 parser.set_defaults(file='nagixsc.xml')
44 parser.set_defaults(schemacheck='')
45 parser.set_defaults(host=None)
46 parser.set_defaults(service=None)
47 parser.set_defaults(output=None)
48 parser.set_defaults(tmpl_host=None)
49 parser.set_defaults(tmpl_service=None)
50 parser.set_defaults(verb=0)
52 (options, args) = parser.parse_args()
54 # Hard coded default for host template
55 HOSTTEMPL='''define host {
56         use             templ_host_default
58         host_name       %(host_name)s
59         address         %(address)s
60 }
61 '''
63 # Hard coded default for service template
64 SERVICETEMPL='''define service {
65         use                     templ_service_passive
67         host_name               %(host_name)s
68         service_description     %(service_description)s
69         check_command           check_passive
70 }
71 '''
73 ##############################################################################
75 from nagixsc import *
77 ##############################################################################
79 # Output
80 if not options.output in [None, 'both', 'hosts', 'services']:
81         print 'Unknown output mode "%s"!' % options.output
82         sys.exit(1)
84 if options.output in [None, 'both']:
85         options.output = ['hosts', 'services']
86 else:
87         options.output = [options.output,]
89 # Read host and/or service template
90 if options.tmpl_host and 'hosts' in options.output:
91         HOSTTEMPL = open(options.tmpl_host).read()
92 if options.tmpl_service and 'services' in options.output:
93         SERVICETEMPL = open(options.tmpl_service).read()
95 # Get URL or file
96 doc = read_xml(options)
98 # Check XML against DTD
99 if options.schemacheck:
100         dtd = libxml2.parseDTD(None, options.schemacheck)
101         ctxt = libxml2.newValidCtxt()
102         ret = doc.validateDtd(ctxt, dtd)
103         if ret != 1:
104                 print "error doing DTD validation"
105                 sys.exit(1)
106         dtd.freeDtd()
107         del dtd
108         del ctxt
111 # Check XML file basics
112 (status, statusstring) = xml_check_version(doc)
113 debug(1, options.verb, statusstring)
114 if not status:
115         print statusstring
116         sys.exit(127)
119 # Put XML to Python dict
120 checks = xml_to_dict(doc, options.verb, options.host, options.service)
123 # Set default socket options
124 if hasattr(socket, 'setdefaulttimeout'):
125         socket.setdefaulttimeout(2)
127 # Loop over check results and search for new hosts and new services
128 foundhosts = []
130 for check in checks:
131         if not check['host_name'] in foundhosts:
132                 foundhosts.append(check['host_name'])
134                 if 'hosts' in options.output:
135                         try:
136                                 check['address'] = socket.gethostbyname(check['host_name'])
137                         except socket.gaierror:
138                                 check['address'] = '127.0.0.1'
139                         print HOSTTEMPL % check
141         if check['service_description'] and 'services' in options.output:
142                 print SERVICETEMPL % check