forked from wAmpIre/nagixsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagixsc_conf2xml.py
executable file
·77 lines (60 loc) · 2.87 KB
/
nagixsc_conf2xml.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
#!/usr/bin/python
#
# Nag(ix)SC -- nagixsc_conf2xml.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 optparse
import sys
import urllib2
##############################################################################
from nagixsc import *
##############################################################################
parser = optparse.OptionParser()
parser.add_option('-c', '', dest='conffile', help='Config file')
parser.add_option('-o', '', dest='outfile', help='Output file name, "-" for STDOUT or HTTP POST URL')
parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
parser.add_option('-q', '', action='store_true', dest='quiet', help='Be quiet')
parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
parser.set_defaults(conffile='nagixsc.conf')
parser.set_defaults(outfile='-')
parser.set_defaults(encoding='base64')
parser.set_defaults(host=None)
parser.set_defaults(service=None)
parser.set_defaults(verb=0)
(options, args) = parser.parse_args()
##############################################################################
if not check_encoding(options.encoding):
print 'Wrong encoding method "%s"!' % options.encoding
print 'Could be one of: "%s"' % '", "'.join(available_encodings())
sys.exit(127)
##############################################################################
config = read_inifile(options.conffile)
if not config:
print 'Config file "%s" could not be read!' % options.conffile
sys.exit(5)
# Execute checks, build dict
checks = conf2dict(config, options.host, options.service)
# Convert to XML
xmldoc = xml_from_dict(checks, options.encoding)
# Output
response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd)
if response and not options.quiet:
print response