From: Sven Velt Date: Thu, 4 Mar 2010 13:58:01 +0000 (+0100) Subject: HTTP-Server can now daemonize X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e97877cd20572c15b42028329f76c0ab7914b652;p=nagixsc.git HTTP-Server can now daemonize Added daemonize() to nagixsc and "-d" or "--daemon" to conf2http and http2nagios. Signed-off-by: Sven Velt --- diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index 56c4cf3..3272084 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -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: diff --git a/nagixsc_conf2http.py b/nagixsc_conf2http.py index abeed4e..3dece81 100755 --- a/nagixsc_conf2http.py +++ b/nagixsc_conf2http.py @@ -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() diff --git a/nagixsc_http2nagios.py b/nagixsc_http2nagios.py index ad86b65..e327bc1 100755 --- a/nagixsc_http2nagios.py +++ b/nagixsc_http2nagios.py @@ -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()