Code

Reorder daemonizing (creation of PID file)
[nagixsc.git] / nagixsc_http2nagios.py
index 4c23fe50d908cf549b6df4e25cfaab5a5bf82956..0a709a585b9ea745d5e376c480808ddcbf22d85e 100755 (executable)
@@ -22,6 +22,8 @@ from nagixsc import *
 parser = optparse.OptionParser()
 
 parser.add_option('-c', '', dest='cfgfile', help='Config file')
+parser.add_option('-d', '--daemon', action='store_true', dest='daemon', help='Daemonize, go to background')
+parser.add_option('', '--nossl', action='store_true', dest='nossl', help='Disable SSL (overwrites config file)')
 
 parser.set_defaults(cfgfile='http2nagios.cfg')
 
@@ -49,6 +51,10 @@ except ConfigParser.NoOptionError, e:
        print 'Config file error: %s ' % e
        sys.exit(1)
 
+if os.access(config['checkresultdir'],os.W_OK) == False:
+       print 'Checkresult directory "%s" is not writable.' % config['checkresultdir']
+       sys.exit(1)
+
 users = {}
 for u in cfgread.options('users'):
        users[u] = cfgread.get('users', u)
@@ -111,20 +117,26 @@ class HTTP2NagiosHandler(MyHTTPRequestHandler):
                                self.wfile.write('Wrote %s check results, %s failed' % (count_services, count_failed))
                                return
                        else:
-                               http_error(500, 'Could not write all %s check results' % count_services)
+                               self.http_error(501, 'Could not write all %s check results' % count_services)
                                return
 
                else:
-                       http_error(500, 'Nag(IX)SC - No data received')
+                       self.http_error(502, 'Nag(IX)SC - No data received')
                        return
 
 
 
 def main():
+       if options.nossl:
+               config['ssl'] = False
+
        if config['ssl'] and not os.path.isfile(config['cert']):
                print 'SSL certificate "%s" not found!' % config['cert']
                sys.exit(127)
 
+       if options.daemon:
+               daemonize(pidfile='/var/run/nagixsc_http2nagios.pid')
+
        server = MyHTTPServer((config['ip'], config['port']), HTTP2NagiosHandler, ssl=config['ssl'], sslpemfile=config['cert'])
        try:
                server.serve_forever()