Code

HTTP-Server can now daemonize
authorSven Velt <sven@velt.de>
Thu, 4 Mar 2010 13:58:01 +0000 (14:58 +0100)
committerSven Velt <sven@velt.de>
Thu, 4 Mar 2010 14:40:08 +0000 (15:40 +0100)
Added daemonize() to nagixsc and "-d" or "--daemon" to conf2http and
http2nagios.

Signed-off-by: Sven Velt <sven@velt.de>
nagixsc/__init__.py
nagixsc_conf2http.py
nagixsc_http2nagios.py

index 56c4cf3ead117ca047a818085912232a5ad514d4..327208453ff60fb001768708c698a4f67a8301f6 100644 (file)
@@ -409,6 +409,45 @@ def encode_multipart(xmldoc, httpuser, httppasswd):
 
 ##############################################################################
 
+def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
+       # 1st fork
+       try:
+               pid = os.fork()
+               if pid > 0:
+                       sys.exit(0)
+       except OSError, e:
+               sys.stderr.write("1st fork failed: (%d) %sn" % (e.errno, e.strerror))
+               sys.exit(1)
+       # Prepare 2nd fork
+       os.chdir("/")
+       os.umask(0)
+       os.setsid( )
+       # 2nd fork
+       try:
+               pid = os.fork()
+               if pid > 0:
+                       sys.exit(0)
+       except OSError, e:
+               sys.stderr.write("2nd fork failed: (%d) %sn" % (e.errno, e.strerror))
+               sys.exit(1)
+       # Redirect stdin, stdout, stderr
+       sys.stdout.flush()
+       sys.stderr.flush()
+       si = file(stdin, 'r')
+       so = file(stdout, 'a+')
+       se = file(stderr, 'a+', 0)
+       os.dup2(si.fileno(), sys.stdin.fileno())
+       os.dup2(so.fileno(), sys.stdout.fileno())
+       os.dup2(se.fileno(), sys.stderr.fileno())
+
+       if pidfile:
+               pid = str(os.getpid())
+               file(pidfile, 'w+').write('%s\n' % pid)
+
+       return
+
+##############################################################################
+
 class MyHTTPServer(BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
                if ssl:
index abeed4e8252fc59dd494702cee06ff6c6a632ded..3dece813d29ed6c3ffdf4060f333edbaeb27e365 100755 (executable)
@@ -22,6 +22,7 @@ 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='conf2http.cfg')
@@ -132,6 +133,9 @@ def main():
                print 'SSL certificate "%s" not found!' % config['cert']
                sys.exit(127)
 
+       if options.daemon:
+               daemonize(pidfile='/var/run/nagixsc_conf2http.pid')
+
        server = MyHTTPServer((config['ip'], config['port']), Conf2HTTPHandler, ssl=config['ssl'], sslpemfile=config['cert'])
        try:
                server.serve_forever()
index ad86b65eba536feb843e40198c1f06af8a55a11d..e327bc18f46368e9017859848f4d8ac2d1e43e5a 100755 (executable)
@@ -22,6 +22,7 @@ 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')
@@ -129,6 +130,9 @@ def main():
                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()