Code

WIP: PoC of "http2nagios" / rename "cgi"->"http"
authorSven Velt <sven@velt.de>
Fri, 11 Dec 2009 11:58:29 +0000 (12:58 +0100)
committerSven Velt <sven@velt.de>
Fri, 11 Dec 2009 11:58:29 +0000 (12:58 +0100)
nagixsc_conf2cgi.py [deleted file]
nagixsc_conf2http.py [new file with mode: 0755]
nagixsc_http2nagios.py [new file with mode: 0755]

diff --git a/nagixsc_conf2cgi.py b/nagixsc_conf2cgi.py
deleted file mode 100755 (executable)
index 96d86ac..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-#!/usr/bin/python
-
-import cherrypy
-import os
-import re
-import subprocess
-
-config = {     'server.socket_host':           '0.0.0.0',
-                       'server.socket_port':           15666,
-                       'log.screen':                           False,
-                       'log.access_file':                      None,
-                       'log.error_file':                       None,
-               }
-
-users = {      'nagixsc':              '019b0966d98fb71d1a4bc4ca0c81d5cc',             # PW: nagixsc
-               }
-
-CONFDIR='./examples'
-C2X='./nagixsc_conf2xml.py'
-class Conf2CGI:
-       def default(*args, **kwargs):
-               cmdline = C2X
-
-               if len(args) >= 5:
-                       print 'Ignoring arguments: ', args[4:]
-
-               if len(args) >= 4:
-                       c_service = args[3]
-               else:
-                       c_service = ''
-
-               if len(args) >= 3:
-                       c_host = args[2]
-               else:
-                       c_host = ''
-
-               if len(args) >= 2:
-                       c_configfile = args[1]
-               else:
-                       c_configfile = ''
-                       print 'No config file specified!'
-
-               if c_configfile:
-                       cherrypy.lib.auth.basic_auth('Nag(ix)SC HTTP', users)
-
-                       if re.search('\.\.', c_configfile):
-                               return 'Found ".." in config file name'
-                       if not re.search('^[a-zA-Z0-9-_\.]+$', c_configfile):
-                               return 'Config file name contains invalid characters'
-                       cmdline += ' -c ' + os.path.join(CONFDIR, c_configfile)
-
-                       if c_host:
-                               cmdline += ' -H %s' % c_host
-                               if c_service:
-                                       cmdline += ' -D %s' % c_service
-                       try:
-                               cmd     = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
-                               output  = cmd.communicate()[0].rstrip()
-                       except OSError:
-                               return 'Could not execute "%s"' % cmdline
-
-                       cherrypy.response.headers['Content-Type'] = 'text/xml'
-                       return output
-               else:
-                       return '42'
-
-       default.exposed = True
-
-cherrypy.config.update(config)
-cherrypy.tree.mount(Conf2CGI(), '')
-cherrypy.quickstart(config=config)
-
diff --git a/nagixsc_conf2http.py b/nagixsc_conf2http.py
new file mode 100755 (executable)
index 0000000..96d86ac
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+import cherrypy
+import os
+import re
+import subprocess
+
+config = {     'server.socket_host':           '0.0.0.0',
+                       'server.socket_port':           15666,
+                       'log.screen':                           False,
+                       'log.access_file':                      None,
+                       'log.error_file':                       None,
+               }
+
+users = {      'nagixsc':              '019b0966d98fb71d1a4bc4ca0c81d5cc',             # PW: nagixsc
+               }
+
+CONFDIR='./examples'
+C2X='./nagixsc_conf2xml.py'
+class Conf2CGI:
+       def default(*args, **kwargs):
+               cmdline = C2X
+
+               if len(args) >= 5:
+                       print 'Ignoring arguments: ', args[4:]
+
+               if len(args) >= 4:
+                       c_service = args[3]
+               else:
+                       c_service = ''
+
+               if len(args) >= 3:
+                       c_host = args[2]
+               else:
+                       c_host = ''
+
+               if len(args) >= 2:
+                       c_configfile = args[1]
+               else:
+                       c_configfile = ''
+                       print 'No config file specified!'
+
+               if c_configfile:
+                       cherrypy.lib.auth.basic_auth('Nag(ix)SC HTTP', users)
+
+                       if re.search('\.\.', c_configfile):
+                               return 'Found ".." in config file name'
+                       if not re.search('^[a-zA-Z0-9-_\.]+$', c_configfile):
+                               return 'Config file name contains invalid characters'
+                       cmdline += ' -c ' + os.path.join(CONFDIR, c_configfile)
+
+                       if c_host:
+                               cmdline += ' -H %s' % c_host
+                               if c_service:
+                                       cmdline += ' -D %s' % c_service
+                       try:
+                               cmd     = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
+                               output  = cmd.communicate()[0].rstrip()
+                       except OSError:
+                               return 'Could not execute "%s"' % cmdline
+
+                       cherrypy.response.headers['Content-Type'] = 'text/xml'
+                       return output
+               else:
+                       return '42'
+
+       default.exposed = True
+
+cherrypy.config.update(config)
+cherrypy.tree.mount(Conf2CGI(), '')
+cherrypy.quickstart(config=config)
+
diff --git a/nagixsc_http2nagios.py b/nagixsc_http2nagios.py
new file mode 100755 (executable)
index 0000000..f104957
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/python
+
+import cherrypy
+import os
+import re
+import subprocess
+
+config = {     'server.socket_host':           '0.0.0.0',
+                       'server.socket_port':           15667,
+                       'log.screen':                           False,
+                       'log.access_file':                      None,
+                       'log.error_file':                       None,
+               }
+
+users = {      'nagixsc':              '019b0966d98fb71d1a4bc4ca0c81d5cc',             # PW: nagixsc
+               }
+
+XMLFILESIZE=102400
+X2N='./nagixsc_xml2nagios.py -O passive -vvv -f -'
+
+class CGI2Nagios:
+       def default(*args, **kwargs):
+               cmdline = X2N
+
+               if len(args) >= 3:
+                       print 'Ignoring arguments: ', args[2:]
+
+               if len(args) >= 2:
+                       c_configfile = args[1]
+               else:
+                       c_configfile = ''
+
+               cherrypy.lib.auth.basic_auth('Nag(ix)SC HTTP', users)
+
+               print kwargs
+               if kwargs.has_key('xmlfile'):
+                       if type(kwargs['xmlfile']) == str:
+                               xmltext = kwargs['xmlfile']
+                       else:
+                               xmltext = kwargs['xmlfile'].file.read(XMLFILESIZE+1)
+
+                       if len(xmltext) > 0:
+                               try:
+                                       cmd     = subprocess.Popen(cmdline.split(' '), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+                                       output  = cmd.communicate(xmltext)[0].rstrip()
+                                       cherrypy.response.headers['Content-Type'] = 'text/plain'
+                                       return output
+                               except OSError:
+                                       return 'Nag(IX)SC - Could not execute "%s"' % cmdline
+
+                               return 'Nag(IX)SC - OK'
+                       else:
+                               return 'Nag(IX)SC - No data received'
+               else:
+                       return """
+                       <html><body>
+                               <form action="." method="post" enctype="multipart/form-data">
+                               filename: <input type="file" name="xmlfile" /><br />
+                               <input type="submit" />
+                               </form>
+                       </body></html>
+                       """
+
+       default.exposed = True
+
+print 'curl -v -u nagixsc:nagixsc -F \'xmlfile=@xml/nagixsc.xml\' http://127.0.0.1:15667/foo/\n\n\n\n'
+
+cherrypy.config.update(config)
+cherrypy.tree.mount(CGI2Nagios(), '')
+cherrypy.quickstart(config=config)
+