summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a568476)
raw | patch | inline | side by side (parent: a568476)
author | Sven Velt <sven@velt.de> | |
Thu, 4 Mar 2010 13:58:01 +0000 (14:58 +0100) | ||
committer | Sven 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>
http2nagios.
Signed-off-by: Sven Velt <sven@velt.de>
nagixsc/__init__.py | patch | blob | history | |
nagixsc_conf2http.py | patch | blob | history | |
nagixsc_http2nagios.py | patch | blob | history |
diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py
index 56c4cf3ead117ca047a818085912232a5ad514d4..327208453ff60fb001768708c698a4f67a8301f6 100644 (file)
--- a/nagixsc/__init__.py
+++ b/nagixsc/__init__.py
##############################################################################
+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 abeed4e8252fc59dd494702cee06ff6c6a632ded..3dece813d29ed6c3ffdf4060f333edbaeb27e365 100755 (executable)
--- a/nagixsc_conf2http.py
+++ b/nagixsc_conf2http.py
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')
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 ad86b65eba536feb843e40198c1f06af8a55a11d..e327bc18f46368e9017859848f4d8ac2d1e43e5a 100755 (executable)
--- a/nagixsc_http2nagios.py
+++ b/nagixsc_http2nagios.py
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')
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()