Code

WIP: PoC of "http2nagios" / rename "cgi"->"http"
[nagixsc.git] / nagixsc_conf2http.py
1 #!/usr/bin/python
3 import cherrypy
4 import os
5 import re
6 import subprocess
8 config = {      'server.socket_host':           '0.0.0.0',
9                         'server.socket_port':           15666,
10                         'log.screen':                           False,
11                         'log.access_file':                      None,
12                         'log.error_file':                       None,
13                 }
15 users = {       'nagixsc':              '019b0966d98fb71d1a4bc4ca0c81d5cc',             # PW: nagixsc
16                 }
18 CONFDIR='./examples'
19 C2X='./nagixsc_conf2xml.py'
20 class Conf2CGI:
21         def default(*args, **kwargs):
22                 cmdline = C2X
24                 if len(args) >= 5:
25                         print 'Ignoring arguments: ', args[4:]
27                 if len(args) >= 4:
28                         c_service = args[3]
29                 else:
30                         c_service = ''
32                 if len(args) >= 3:
33                         c_host = args[2]
34                 else:
35                         c_host = ''
37                 if len(args) >= 2:
38                         c_configfile = args[1]
39                 else:
40                         c_configfile = ''
41                         print 'No config file specified!'
43                 if c_configfile:
44                         cherrypy.lib.auth.basic_auth('Nag(ix)SC HTTP', users)
46                         if re.search('\.\.', c_configfile):
47                                 return 'Found ".." in config file name'
48                         if not re.search('^[a-zA-Z0-9-_\.]+$', c_configfile):
49                                 return 'Config file name contains invalid characters'
50                         cmdline += ' -c ' + os.path.join(CONFDIR, c_configfile)
52                         if c_host:
53                                 cmdline += ' -H %s' % c_host
54                                 if c_service:
55                                         cmdline += ' -D %s' % c_service
56                         try:
57                                 cmd     = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
58                                 output  = cmd.communicate()[0].rstrip()
59                         except OSError:
60                                 return 'Could not execute "%s"' % cmdline
62                         cherrypy.response.headers['Content-Type'] = 'text/xml'
63                         return output
64                 else:
65                         return '42'
67         default.exposed = True
69 cherrypy.config.update(config)
70 cherrypy.tree.mount(Conf2CGI(), '')
71 cherrypy.quickstart(config=config)