Code

WIP: PoC of "http2nagios" / rename "cgi"->"http"
[nagixsc.git] / nagixsc_http2nagios.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':           15667,
10                         'log.screen':                           False,
11                         'log.access_file':                      None,
12                         'log.error_file':                       None,
13                 }
15 users = {       'nagixsc':              '019b0966d98fb71d1a4bc4ca0c81d5cc',             # PW: nagixsc
16                 }
18 XMLFILESIZE=102400
19 X2N='./nagixsc_xml2nagios.py -O passive -vvv -f -'
21 class CGI2Nagios:
22         def default(*args, **kwargs):
23                 cmdline = X2N
25                 if len(args) >= 3:
26                         print 'Ignoring arguments: ', args[2:]
28                 if len(args) >= 2:
29                         c_configfile = args[1]
30                 else:
31                         c_configfile = ''
33                 cherrypy.lib.auth.basic_auth('Nag(ix)SC HTTP', users)
35                 print kwargs
36                 if kwargs.has_key('xmlfile'):
37                         if type(kwargs['xmlfile']) == str:
38                                 xmltext = kwargs['xmlfile']
39                         else:
40                                 xmltext = kwargs['xmlfile'].file.read(XMLFILESIZE+1)
42                         if len(xmltext) > 0:
43                                 try:
44                                         cmd     = subprocess.Popen(cmdline.split(' '), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
45                                         output  = cmd.communicate(xmltext)[0].rstrip()
46                                         cherrypy.response.headers['Content-Type'] = 'text/plain'
47                                         return output
48                                 except OSError:
49                                         return 'Nag(IX)SC - Could not execute "%s"' % cmdline
51                                 return 'Nag(IX)SC - OK'
52                         else:
53                                 return 'Nag(IX)SC - No data received'
54                 else:
55                         return """
56                         <html><body>
57                                 <form action="." method="post" enctype="multipart/form-data">
58                                 filename: <input type="file" name="xmlfile" /><br />
59                                 <input type="submit" />
60                                 </form>
61                         </body></html>
62                         """
64         default.exposed = True
66 print 'curl -v -u nagixsc:nagixsc -F \'xmlfile=@xml/nagixsc.xml\' http://127.0.0.1:15667/foo/\n\n\n\n'
68 cherrypy.config.update(config)
69 cherrypy.tree.mount(CGI2Nagios(), '')
70 cherrypy.quickstart(config=config)