640539fc98013311286c65ead7f022575501e282
1 #!/usr/bin/python
3 import BaseHTTPServer
4 import ConfigParser
5 import base64
6 import optparse
7 import os
8 import re
9 import subprocess
10 import sys
12 try:
13 from hashlib import md5
14 except ImportError:
15 from md5 import md5
17 ##############################################################################
19 from nagixsc import *
21 ##############################################################################
23 parser = optparse.OptionParser()
25 parser.add_option('-c', '', dest='cfgfile', help='Config file')
27 parser.set_defaults(cfgfile='conf2http.cfg')
29 (options, args) = parser.parse_args()
31 cfgread = ConfigParser.SafeConfigParser()
32 cfgread.optionxform = str # We need case-sensitive options
33 cfg_list = cfgread.read(options.cfgfile)
35 if cfg_list == []:
36 print 'Config file "%s" could not be read!' % options.cfgfile
37 sys.exit(1)
39 config = {}
40 try:
41 config['ip'] = cfgread.get('server', 'ip')
42 config['port'] = cfgread.getint('server', 'port')
44 config['conf_dir'] = cfgread.get('server', 'conf_dir')
45 config['conf2xml_cmdline'] = cfgread.get('server', 'conf2xml_cmdline')
47 except ConfigParser.NoOptionError, e:
48 print 'Config file error: %s ' % e
49 sys.exit(1)
51 users = {}
52 for u in cfgread.options('users'):
53 users[u] = cfgread.get('users', u)
55 ##############################################################################
57 class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
59 def http_error(self, code, output):
60 self.send_response(code)
61 self.send_header('Content-Type', 'text/plain')
62 self.end_headers()
63 self.wfile.write(output)
64 return
67 def do_GET(self):
68 cmdline = config['conf2xml_cmdline']
70 path = self.path.split('/')
72 # Check Basic Auth
73 try:
74 authdata = base64.b64decode(self.headers['Authorization'].split(' ')[1]).split(':')
75 if not users[authdata[0]] == md5(authdata[1]).hexdigest():
76 raise Exception
77 except:
78 self.send_response(401)
79 self.send_header('WWW-Authenticate', 'Basic realm="Nag(ix)SC Pull"')
80 self.send_header('Content-Type', 'text/plain')
81 self.end_headers()
82 self.wfile.write('Sorry! No action without login!')
83 return
86 if len(path) >= 4:
87 service = path[3]
88 else:
89 service = None
91 if len(path) >= 3:
92 host = path[2]
93 else:
94 host = None
96 if len(path) >= 2:
97 configfile = path[1] + '.conf'
98 else:
99 self.http_error(500, 'No config file specified')
100 return
102 if re.search('\.\.', configfile):
103 self.http_error(500, 'Found ".." in config file name')
104 return
105 if not re.search('^[a-zA-Z0-9-_\.]+$', configfile):
106 self.http_error(500, 'Config file name contains invalid characters')
107 return
109 check_config = read_inifile(os.path.join(config['conf_dir'], configfile))
110 if not check_config:
111 self.http_error(500, 'Could not read config file "%s"' % configfile)
112 return
114 checks = conf2dict(check_config, host, service)
115 if not checks:
116 self.http_error(500, 'No checks executed')
117 return
119 self.send_response(200)
120 self.send_header('Content-Type', 'text/xml')
121 self.end_headers()
122 self.wfile.write(xml_from_dict(checks))
124 return
128 def main():
129 try:
130 server = BaseHTTPServer.HTTPServer((config['ip'], config['port']), Conf2HTTPHandler)
131 server.serve_forever()
132 except:
133 server.socket.close()
135 if __name__ == '__main__':
136 main()