]> granicus.if.org Git - icinga2/commitdiff
Bugfixes and config conversion script.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 28 Jun 2012 12:24:41 +0000 (14:24 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 28 Jun 2012 12:24:41 +0000 (14:24 +0200)
components/compat/compatcomponent.cpp
convert-config.py [new file with mode: 0755]
icinga/nagioschecktask.cpp
icinga/nagioschecktask.h
icinga/service.cpp
icinga/service.h

index 3e2e9fe0c0f9adbb868f3701f4a7a6e7938d6f2a..191d89768953956dd05d0b76472acf977e0e90d5 100644 (file)
@@ -107,7 +107,7 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, Service service)
 
        fp << "servicestatus {" << endl
            << "\t" << "host_name=" << service.GetHost().GetName() << endl
-          << "\t" << "service_description=" << service.GetDisplayName() << endl
+          << "\t" << "service_description=" << service.GetAlias() << endl
           << "\t" << "check_interval=" << service.GetCheckInterval() / 60.0 << endl
           << "\t" << "retry_interval=" << service.GetRetryInterval() / 60.0 << endl
           << "\t" << "has_been_checked=" << (cr ? 1 : 0) << endl
@@ -134,7 +134,7 @@ void CompatComponent::DumpServiceObject(ofstream& fp, Service service)
 {
        fp << "define service {" << endl
           << "\t" << "host_name" << "\t" << service.GetHost().GetName() << endl
-          << "\t" << "service_description" << "\t" << service.GetDisplayName() << endl
+          << "\t" << "service_description" << "\t" << service.GetAlias() << endl
           << "\t" << "check_command" << "\t" << "check_i2" << endl
           << "\t" << "check_interval" << "\t" << service.GetCheckInterval() / 60.0 << endl
           << "\t" << "retry_interval" << "\t" << service.GetRetryInterval() / 60.0 << endl
diff --git a/convert-config.py b/convert-config.py
new file mode 100755 (executable)
index 0000000..8ff11e9
--- /dev/null
@@ -0,0 +1,229 @@
+#!/usr/bin/env python
+import sys
+import re
+
+def readObject():
+    inObject = False
+
+    obj = {
+        'type': None,
+        'properties': {}
+    }
+
+    for line in sys.stdin:
+        # remove new-line as well as other whitespace characters
+        line = line.strip()
+
+        # replace tabs with space
+        line = line.replace("\t", ' ')
+
+        # ignore comments and empty lines
+        if line == '' or line[0] == '#':
+            continue
+
+        if not inObject:
+            match = re.match('^define +([^ ]+) *{$', line)
+
+            if not match:
+                raise ValueError('Invalid line in config file: ' + line)
+
+            obj['type'] = match.group(1)
+            inObject = True
+        else:
+            match = re.match('^}$', line)
+
+            if match:
+                return obj
+
+            match = re.match('^ *([^ ]+) *(.*)$', line)
+
+            if match:
+                obj['properties'][match.group(1)] = match.group(2)
+            else:
+                raise ValueError('Invalid line in config file: ' + line)
+
+    return None
+
+def dumpValue(obj, indent = 0):
+    result = '';
+
+    indent += 1
+
+    if isinstance(obj, dict):
+        result = "{\n"
+
+        for k, v in obj.iteritems():
+            op = '+=' if isinstance(v, (dict, list)) else '='
+            result += "\t" * indent + k + ' ' + op + ' ' + dumpValue(v, indent) + ",\n"
+
+        result += "\t" * (indent - 1) + "}"
+    elif isinstance(obj, list):
+        result = "{\n"
+
+        for v in obj:
+            result += "\t" * indent + dumpValue(v) + ",\n"
+
+        result += "\t" * (indent - 1) + "}"
+    elif isinstance(obj, (int, long)):
+        result = str(obj)
+    else:
+        result = ''.join(['"', str(obj), '"'])
+
+    return result
+
+def printObject(obj):
+    if 'abstract' in obj and obj['abstract']:
+        print 'abstract',
+
+    if 'local' in obj and obj['local']:
+        print 'local',
+
+    if 'temporary' in obj and obj['temporary']:
+        print 'temporary',
+
+    print 'object', obj['type'], ''.join(['"', obj['name'], '"']),
+
+    if 'parents' in obj and len(obj['parents']) > 0:
+        print 'inherits',
+        print ', '.join([''.join(['"', parent, '"']) for parent in obj['parents']]),
+
+    print dumpValue(obj['properties'])
+    print
+
+nagios_svc_template = {
+    'name': 'nagios-service',
+    'type': 'service',
+    'abstract': True,
+    'properties': {
+        'check_type': 'nagios',
+        'macros': {
+            'USER1': '/tmp/nagios/plugins',
+            'SERVICESTATE': 0,
+            'SERVICEDURATIONSEC': 0,
+            'TOTALHOSTSERVICESCRITICAL': 0,
+            'TOTALHOSTSERVICESWARNING': 0
+        }
+    }
+}
+
+printObject(nagios_svc_template)
+
+allObjects = []
+objects = {}
+
+while True:
+    obj = readObject()
+
+    if obj == None:
+        break
+
+    props = obj['properties']
+
+    # transform the name property
+    name = None
+    for prop in [obj['type'] + '_name', 'name', 'service_description']:
+        if prop in props:
+            if prop == 'service_description':
+                name = props[prop] + '-' + props['host_name']
+            else:
+                name = props[prop]
+            del props[prop]
+            break
+
+    if name == None:
+        raise ValueError('Object has no name: ' + str(obj))
+
+    obj['name'] = name
+
+    if not obj['type'] in objects:
+        objects[obj['type']] = {}
+
+    allObjects.append(obj)
+    objects[obj['type']][obj['name']] = obj
+
+for obj in allObjects:
+    props = obj['properties']
+    newprops = {}
+
+    obj['parents'] = []
+
+    # transform 'register' property
+    if 'register' in props:
+        if int(props['register']) == 0:
+            obj['abstract'] = True
+
+        del props['register']
+
+    # transform 'use' property        
+    if 'use' in props:
+        obj['parents'] = props['use'].split(',')
+        del props['use']
+
+    # transform commands into service templates
+    if obj['type'] == 'command':
+        obj['abstract'] = True
+        obj['type'] = 'service'
+        obj['parents'].append('nagios-service')
+
+        if 'command_line' in props:
+            newprops['check_command'] = props['command_line']
+            del props['command_line']
+
+    # transform contactgroups/hostgroups/servicegroups
+    #elif obj['type'] in ['contactgroup', 'hostgroup', 'servicegroup']:
+    #    if 'alias' in props:
+    #        newprops['alias'] = props['alias']
+    #        del props['alias']
+    #
+    #    if 'members' in props:
+    #        newprops['members'] = props['members'].split(',')
+    #        del props['members']
+
+    # transform services
+    elif obj['type'] == 'service':
+        newprops['macros'] = {}
+
+        if 'check_command' in props:
+            tokens = props['check_command'].split('!')
+            obj['parents'].append(tokens[0])
+
+            num = 0
+            for token in tokens[1:]:
+                num += 1
+                newprops['macros']['ARG' + str(num)] = token
+
+            del props['check_command']
+
+        if 'check_interval' in props:
+            newprops['check_interval'] = int(float(props['check_interval']) * 60)
+            del props['check_interval']
+
+        if 'retry_interval' in props:
+            newprops['retry_interval'] = int(float(props['retry_interval']) * 60)
+            del props['retry_interval']
+
+        if 'max_check_attempts' in props:
+            newprops['max_check_attempts'] = int(props['max_check_attempts'])
+            del props['max_check_attempts']
+
+        newprops['macros']['SERVICEDESC'] = obj['name']
+
+        if 'host_name' in props:
+            newprops['host_name'] = props['host_name']
+            newprops['macros']['HOSTNAME'] = props['host_name']
+            del props['host_name']
+
+            newprops['alias'] = obj['name']
+            obj['name'] = newprops['host_name'] + '-' + obj['name']
+
+        for k, v in props.iteritems():
+            if k[0] == '_':
+                newprops['macros'][k] = v
+
+    obj['properties'] = newprops
+
+    #if len(props) > 0:
+    #    obj['properties']['old'] = props
+
+    printObject(obj)
+
index ee1e2b43c625c56638eee64917b904ad3728f304..92857dfdd4f5f39cd3b2257dfe6e50904bdc070b 100644 (file)
@@ -15,7 +15,7 @@ NagiosCheckTask::NagiosCheckTask(const Service& service)
        : CheckTask(service), m_FP(NULL), m_UsePopen(false)
 {
        string checkCommand = service.GetCheckCommand();
-       m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros()); // + " 2>&1";
+       m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros());
 }
 
 void NagiosCheckTask::Enqueue(void)
index 422e901817190e5a0698da41d9d67b229a4f6eff..808009860632d83ac7faf4ae4f018a7c6992ad31 100644 (file)
@@ -10,7 +10,7 @@ public:
        typedef shared_ptr<NagiosCheckTask> Ptr;
        typedef weak_ptr<NagiosCheckTask> WeakPtr;
 
-       static const int MaxChecksPerThread = 128;
+       static const int MaxChecksPerThread = 64;
 
        NagiosCheckTask(const Service& service);
 
index c630b0536706d88e00676f1c1ded7ce259067a5a..c8b8dd20a661ea73b97a647cf977cf78b4735d07 100644 (file)
@@ -2,11 +2,11 @@
 
 using namespace icinga;
 
-string Service::GetDisplayName(void) const
+string Service::GetAlias(void) const
 {
        string value;
 
-       if (GetConfigObject()->GetProperty("displayname", &value))
+       if (GetConfigObject()->GetProperty("alias", &value))
                return value;
 
        return GetName();
index 511aaa8b646aa3ada1bbf5f7b85ef2afd5168ba5..d7e116112c986cf3de4bcfc943fb395b755ae5af 100644 (file)
@@ -31,7 +31,7 @@ public:
 
        static Service GetByName(string name);
 
-       string GetDisplayName(void) const;
+       string GetAlias(void) const;
        Host GetHost(void) const;
        Dictionary::Ptr GetMacros(void) const;
        string GetCheckType(void) const;