Code

check for writable checkresult directory
[nagixsc.git] / nagixsc / __init__.py
index 56c4cf3ead117ca047a818085912232a5ad514d4..8f11a42dbf37d65b153759e4daf51be852514a50 100644 (file)
@@ -61,15 +61,27 @@ def read_inifile(inifile):
 ##############################################################################
 
 def exec_check(host_name, service_descr, cmdline):
+       cmdarray = shlex.split(cmdline)
+
+       check = {}
+       check['host_name'] = host_name
+       check['service_description'] = service_descr
+
+       if len(cmdarray) == 0:
+               check['output'] = 'No command line specified!'
+               check['returncode'] = 127
+               return check
+
        try:
-               cmd     = subprocess.Popen(shlex.split(cmdline), stdout=subprocess.PIPE)
-               output  = cmd.communicate()[0].rstrip()
-               retcode = cmd.returncode
+               cmd     = subprocess.Popen(cmdarray, stdout=subprocess.PIPE)
+               check['output'] = cmd.communicate()[0].rstrip()
+               check['returncode'] = cmd.returncode
        except OSError:
-               output  = 'Could not execute "%s"' % cmdline
-               retcode = 127
+               check['output'] = 'Could not execute "%s"' % cmdline
+               check['returncode'] = 127
 
-       return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')}
+       check['timestamp'] = datetime.datetime.now().strftime('%s')
+       return check
 
 
 ##############################################################################
@@ -294,7 +306,8 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
                else:
                        timestamp = filetimestamp
 
-               if retcode and output:
+               # Append only if no service filter
+               if not servicefilter and retcode and output:
                        checks.append({'host_name':hostname, 'service_description':None, 'returncode':retcode, 'output':output, 'timestamp':timestamp})
 
 
@@ -370,6 +383,14 @@ def xml_from_dict(checks, encoding='base64'):
        return xmldoc
 
 
+def xml_merge(xmldocs):
+       checks = []
+       for xmldoc in xmldocs:
+               checks.extend(xml_to_dict(xmldoc))
+       newxmldoc = xml_from_dict(checks)
+       return newxmldoc
+
+
 def check_mark_outdated(check, now, maxtimediff, markold):
        timedelta = now - check['timestamp']
        if timedelta > maxtimediff:
@@ -409,6 +430,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: