def test_manage_unknown_host_check_result_brok(self):
        # Prepare the Brok
        data = {
            "time_stamp": 1234567890,
            "return_code": "2",
            "host_name": "test_host_0",
            "output": "Bob is not happy",
            "perf_data": "rtt=9999",
        }
        brok = Brok("unknown_host_check_result", data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_host_check_result_brok(brok)

        self.assertEqual(
            broker.buffer[0],
            {
                "timestamp": 1234567890,
                "tags": {"host_name": "test_host_0"},
                "name": "metric_rtt",
                "fields": {"value": 9999},
            },
        )
Exemplo n.º 2
0
    def test_reversed_list(self):
        """ Test to ensure new conf is properly merge with different servicegroup definition
        The first conf has all its servicegroup defined servicegroups.cfg and services.cfg
        The second conf has both, so that servicegroups defined ins services.cfg are genretaed by Shinken
        This lead to another generated id witch should be handled properly when regenerating reversed list / merging
        servicegroups definition
        """

        sg = self.sched.servicegroups.find_by_name('servicegroup_01')
        prev_id = sg.id

        reg = Regenerator()
        data = {"instance_id": 0}
        b = Brok('program_status', data)
        b.prepare()
        reg.manage_program_status_brok(b)
        reg.all_done_linking(0)


        self.setup_with_file("etc/shinken_reversed_list.cfg")

        reg.all_done_linking(0)

        #for service in self.sched.servicegroups:
        #    assert(service.servicegroup_name in self.sched.servicegroups.reversed_list.keys())
        #    assert(service.id == self.sched.servicegroups.reversed_list[service.servicegroup_name])

        sg = self.sched.servicegroups.find_by_name('servicegroup_01')
        assert(prev_id != sg.id)

        for sname in [u'servicegroup_01', u'ok', u'flap', u'unknown', u'random',
                      u'servicegroup_02', u'servicegroup_03', u'warning', u'critical',
                      u'servicegroup_04', u'servicegroup_05', u'pending', u'mynewgroup']:
            sg = self.sched.servicegroups.find_by_name(sname)
            assert(sname is not None)
    def test_manage_unknown_service_check_result_brok(self):
        # Prepare the Brok
        data = {
            "host_name": "test_host_0",
            "time_stamp": 1234567890,
            "service_description": "test_ok_0",
            "return_code": "1",
            "output": "Bobby is not happy",
            "perf_data": "rtt=9999;5;10;0;10000",
        }
        brok = Brok("unknown_service_check_result", data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_service_check_result_brok(brok)
        self.assertEqual(
            broker.buffer[0],
            {
                "timestamp": 1234567890,
                "tags": {"service_description": "test_ok_0", "host_name": "test_host_0"},
                "name": "metric_rtt",
                "fields": {"max": 10000, "warning": 5, "critical": 10, "value": 9999, "min": 0},
            },
        )
Exemplo n.º 4
0
    def test_simplelog(self):
        print self.conf.modules
        #get our modules
        mod = None
        for m in self.conf.modules:
            if m.module_type == 'simple_log':
                mod = m
        self.assert_(mod is not None)
        self.assert_(mod.path == 'tmp/nagios.log')
        self.assert_(mod.module_name == 'Simple-log')

        try:
            os.unlink(mod.path)
        except:
            pass

        sl = get_instance(mod)
        print sl
        #Hack here :(
        sl.properties = {}
        sl.properties['to_queue'] = None
        sl.init()
        b = Brok('log', {'log': "look at my ass.\n"})
        sl.manage_brok(b)
        b = Brok('log', {'log': "look at my ass again.\n"})
        sl.manage_brok(b)
        sl.file.close()
        fd = open(mod.path)
        buf = fd.readline()
        self.assert_(buf == "look at my ass.\n")
        buf = fd.readline()
        self.assert_(buf == "look at my ass again.\n")
        fd.close()
        os.unlink(mod.path)
    def test_manage_unknown_host_check_result_brok(self):
        # Prepare the Brok
        data = {
            'time_stamp': 1234567890,
            'return_code': '2',
            'host_name': 'test_host_0',
            'output': 'Bob is not happy',
            'perf_data': 'rtt=9999'
        }
        brok = Brok('unknown_host_check_result', data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_host_check_result_brok(brok)

        self.assertEqual(
            broker.buffer[0], {
                'timestamp': 1234567890,
                'tags': {
                    'host_name': 'test_host_0'
                },
                'name': 'metric_rtt',
                'fields': {
                    'value': 9999
                }
            })
Exemplo n.º 6
0
    def test_manage_unknown_service_check_result_brok(self):
        # Prepare the Brok
        data = {
            'host_name': 'test_host_0', 'time_stamp': 1234567890,
            'service_description': 'test_ok_0', 'return_code': '1',
            'output': 'Bobby is not happy',
            'perf_data': 'rtt=9999;5;10;0;10000'
        }
        brok = Brok('unknown_service_check_result', data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_service_check_result_brok(brok)
        self.assertEqual(
            broker.buffer[0],
            {'fields':
                {'min': 0.0,
                 'max': 10000.0,
                 'value': 9999.0,
                 'warning': 5.0,
                 'critical': 10.0,
                 'unit': ''},
                'time': 1234567890,
                'tags': {'service_description': 'test_ok_0',
                         'host_name': 'test_host_0'},
                'measurement': 'metric_rtt'}
        )
Exemplo n.º 7
0
 def test_log_brok_illegal_char(self):
     data = {
         'log': '[1329144231] SERVICE ALERT: www.cibc.com;www.cibc.com;WARNING;HARD;4;WARNING - load average: 5.04, 4.67, 5.04'
     }
     brok = Brok('log', data)
     brok.prepare()
     broker = self.influx_broker
     broker.manage_log_brok(brok)
     point = broker.buffer[0]
     self.assertEqual(point['name'], 'www.cibc.com>www.cibc.com>_events_>ALERT')
Exemplo n.º 8
0
 def test_log_brok_illegal_char(self):
     data = {
         'log': '[1329144231] SERVICE ALERT: www.cibc.com;www.cibc.com;WARNING;HARD;4;WARNING - load average: 5.04, 4.67, 5.04'  # nopep8
     }
     brok = Brok('log', data)
     brok.prepare()
     broker = self.influx_broker
     broker.manage_log_brok(brok)
     point = broker.buffer[0]
     self.assertEqual(point['measurement'], 'EVENT')
     self.assertEqual(point['tags']['host_name'], 'www.cibc.com')
     self.assertEqual(point['tags']['service_description'], 'www.cibc.com')
 def test_log_brok_illegal_char(self):
     data = {
         "log": "[1329144231] SERVICE ALERT: www.cibc.com;www.cibc.com;WARNING;HARD;4;WARNING - load average: 5.04, 4.67, 5.04"
     }
     brok = Brok("log", data)
     brok.prepare()
     broker = self.influx_broker
     broker.manage_log_brok(brok)
     point = broker.buffer[0]
     self.assertEqual(point["name"], "ALERT")
     self.assertEqual(point["tags"]["host_name"], "www.cibc.com")
     self.assertEqual(point["tags"]["service_desc"], "www.cibc.com")
 def test_log_brok_illegal_char(self):
     data = {
         'log':
         '[1329144231] SERVICE ALERT: www.cibc.com;www.cibc.com;WARNING;HARD;4;WARNING - load average: 5.04, 4.67, 5.04'
     }
     brok = Brok('log', data)
     brok.prepare()
     broker = InfluxdbBroker(self.basic_modconf)
     broker.manage_log_brok(brok)
     point = broker.buffer[0]
     self.assertEqual(point['name'],
                      'www_cibc_com.www_cibc_com._events_.ALERT')
Exemplo n.º 11
0
def json_cpe(cpe_name):

    user = app.bottle.request.environ['USER']
    cpe = app.datamgr.get_host(cpe_name, user)

    data = dict(
        host_name=cpe_name,
        source='krillui',
        type='null',
        ts=int(time.time()),
        data={
            # 'cpe_registration_host': cpe.cpe_registration_host or None,
            # 'cpe_registration_id': cpe.cpe_registration_id or None
        },
    )

    result = app.krillui_module.get_data(cpe_name)

    if not bool(result) or not result.get('last_pollresquest_time') or (result.get('last_pollresquest_time') and (time.time() - result.get('last_pollresquest_time')) > 5 ):
        # logger.info("[WebUI:cpe_poll] pollrequest! cpe=%s" % cpe_name)
        result.update({'last_pollresquest_time': time.time()})
        b = Brok(type='pollrequest', data=data)
        app.from_q.put(b)

    return result
Exemplo n.º 12
0
def cpe_tr069(name):
    # logger.info("[WebUI:tr069] cpe_tr069! cpe=%s" % name)
    try:
        b = Brok('restore_tr069_host', {'host_name': name})
        app.from_q.put(b)
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'fail', 'msg': str(e)}
Exemplo n.º 13
0
    def get_next_schedule_brok(self):
        cls = self.__class__
        my_type = cls.my_type

        data = {}
        self.fill_data_brok_from(data, 'next_schedule')
        b = Brok(my_type + '_next_schedule', data)
        return b
Exemplo n.º 14
0
    def get_initial_status_brok(self):
        cls = self.__class__
        my_type = cls.my_type
        data = {'id': self.id}

        self.fill_data_brok_from(data, 'full_status')
        b = Brok('initial_' + my_type + '_status', data)
        return b
Exemplo n.º 15
0
    def get_check_result_brok(self):
        cls = self.__class__
        my_type = cls.my_type

        data = {}
        self.fill_data_brok_from(data, 'check_result')
        b = Brok(my_type + '_check_result', data)
        return b
Exemplo n.º 16
0
def cpe_unprovision(name):
    # logger.info("[WebUI:cpe_poll] cpe_unprovision! cpe=%s" % name)
    try:
        b = Brok('unprovision_host', {'host_name': name})
        app.from_q.put(b)
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'fail', 'msg': str(e)}
Exemplo n.º 17
0
    def test_simplelog(self):
        print self.conf.modules
        # get our modules
        mod = None
        for m in self.conf.modules:
            if m.module_name == 'ToMerlindb_Sqlite':
                mod = m
        self.assert_(mod is not None)
        self.assert_(mod.database_path == '/usr/local/shinken/var/merlindb.sqlite')
        self.assert_(mod.module_type == 'merlindb')
        self.assert_(mod.backend == 'sqlite')

        md = get_instance(mod)
        print "TOTO", md.db_backend.__dict__

        md.init()
        b = Brok('clean_all_my_instance_id', {'instance_id': 0})
        b.prepare()
        md.manage_brok(b)
        r = md.db_backend.db_cursor.execute("SELECT count(*) from timeperiod WHERE instance_id = '0'")
        self.assert_(r.fetchall() == [(0,)])
Exemplo n.º 18
0
    def test_manage_unknown_host_check_result_brok(self):
        # Prepare the Brok
        data = {
            'time_stamp': 1234567890, 'return_code': '2',
            'host_name': 'test_host_0', 'output': 'Bob is not happy',
            'perf_data': 'rtt=9999'
        }
        brok = Brok('unknown_host_check_result', data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_host_check_result_brok(brok)

        self.assertEqual(
            broker.buffer[0],
            {'points': [[1234567890, 9999, '', None, None, None, None]],
             'name': 'test_host_0>rtt', 'columns':
                ['time', 'value', 'unit', 'warning', 'critical', 'min', 'max']
             }
        )
Exemplo n.º 19
0
    def test_manage_unknown_service_check_result_brok(self):
        # Prepare the Brok
        data = {
            'host_name': 'test_host_0', 'time_stamp': 1234567890,
            'service_description': 'test_ok_0', 'return_code': '1',
            'output': 'Bobby is not happy',
            'perf_data': 'rtt=9999;5;10;0;10000'
        }
        brok = Brok('unknown_service_check_result', data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_service_check_result_brok(brok)

        self.assertEqual(
            broker.buffer[0],
            {'points': [[1234567890, 9999, '', 5, 10, 0, 10000]], 'name':
                'test_host_0>test_ok_0>rtt', 'columns':
                ['time', 'value', 'unit', 'warning', 'critical', 'min', 'max']
             }
        )
Exemplo n.º 20
0
    def test_manage_unknown_host_check_result_brok(self):
        # Prepare the Brok
        data = {
            'time_stamp': 1234567890, 'return_code': '2',
            'host_name': 'test_host_0', 'output': 'Bob is not happy',
            'perf_data': 'rtt=9999'
        }
        brok = Brok('unknown_host_check_result', data)
        brok.prepare()

        # Send the brok
        broker = self.influx_broker
        broker.manage_unknown_host_check_result_brok(brok)

        self.assertEqual(
            broker.buffer[0],
            {'fields':
                {'unit': '', 'value': 9999.0},
                'time': 1234567890,
                'tags': {'host_name': 'test_host_0'},
                'measurement': 'metric_rtt'}
        )
Exemplo n.º 21
0
    def test_simplelog(self):
        print self.conf.modules
        # get our modules
        mod = None
        for m in self.conf.modules:
            if m.module_name == 'ToMerlindb_Sqlite':
                mod = m
        self.assert_(mod is not None)
        self.assert_(
            mod.database_path == '/usr/local/shinken/var/merlindb.sqlite')
        self.assert_(mod.module_type == 'merlindb')
        self.assert_(mod.backend == 'sqlite')

        md = get_instance(mod)
        print "TOTO", md.db_backend.__dict__

        md.init()
        b = Brok('clean_all_my_instance_id', {'instance_id': 0})
        b.prepare()
        md.manage_brok(b)
        r = md.db_backend.db_cursor.execute(
            "SELECT count(*) from timeperiod WHERE instance_id = '0'")
        self.assert_(r.fetchall() == [(0, )])
Exemplo n.º 22
0
    def test_reversed_list(self):
        """ Test to ensure new conf is properly merge with different servicegroup definition
        The first conf has all its servicegroup defined servicegroups.cfg and services.cfg
        The second conf has both, so that servicegroups defined ins services.cfg are genretaed by Shinken
        This lead to another generated id witch should be handled properly when regenerating reversed list / merging
        servicegroups definition
        """

        sg = self.sched.servicegroups.find_by_name('servicegroup_01')
        prev_id = sg.id

        reg = Regenerator()
        data = {"instance_id": 0}
        b = Brok('program_status', data)
        b.prepare()
        reg.manage_program_status_brok(b)
        reg.all_done_linking(0)

        self.setup_with_file("etc/shinken_reversed_list.cfg")

        reg.all_done_linking(0)

        #for service in self.sched.servicegroups:
        #    assert(service.servicegroup_name in self.sched.servicegroups.reversed_list.keys())
        #    assert(service.id == self.sched.servicegroups.reversed_list[service.servicegroup_name])

        sg = self.sched.servicegroups.find_by_name('servicegroup_01')
        assert (prev_id != sg.id)

        for sname in [
                u'servicegroup_01', u'ok', u'flap', u'unknown', u'random',
                u'servicegroup_02', u'servicegroup_03', u'warning',
                u'critical', u'servicegroup_04', u'servicegroup_05',
                u'pending', u'mynewgroup'
        ]:
            sg = self.sched.servicegroups.find_by_name(sname)
            assert (sname is not None)
Exemplo n.º 23
0
 def get_initial_status_brok(self):
     cls = self.__class__
     data = {}
     # Now config properties
     for prop, entry in cls.properties.items():
         if entry.fill_brok != []:
             if self.has(prop):
                 data[prop] = getattr(self, prop)
     # Here members is just a bunch of host, I need name in place
     data['members'] = []
     for i in self.members:
         # it look like lisp! ((( ..))), sorry....
         data['members'].append((i.id, i.get_name()))
     b = Brok('initial_' + cls.my_type + '_status', data)
     return b
    def test_manage_log_brok(self):
        data = {
            'log':
            '[1402515279] HOST NOTIFICATION: admin;localhost;CRITICAL;notify-service-by-email;Connection refused'
        }
        brok = Brok('log', data)
        brok.prepare()

        broker = self.influx_broker
        broker.manage_log_brok(brok)

        # make sure that this has generated only 1 point
        self.assertEqual(len(broker.buffer), 1)
        point = broker.buffer[0]

        # validate the point
        expected = {
            'timestamp': 1402515279,
            'tags': {
                'service_desc': '_self_',
                'host_name': 'localhost',
                'event_type': 'NOTIFICATION'
            },
            'name': 'ALERT',
            'fields': {
                'time': 1402515279,
                'state': 'CRITICAL',
                'contact': 'admin',
                'notification_type': 'HOST',
                'notification_method': 'notify-service-by-email',
                'output': 'Connection refused'
            }
        }
        self.assertEqual(expected, point)

        # A service notification's tags should include service_desc
        data[
            'log'] = '[1402515279] SERVICE NOTIFICATION: admin;localhost;check-ssh;CRITICAL;notify-service-by-email;Connection refused'
        brok = Brok('log', data)
        brok.prepare()
        broker.buffer = []
        broker.manage_log_brok(brok)
        point = broker.buffer[0]
        self.assertEqual(point['name'], 'ALERT')
        self.assertEqual(point['tags']['service_desc'], 'check-ssh')
    def test_manage_log_brok(self):
        data = {
            'log':
            '[1402515279] HOST NOTIFICATION: admin;localhost;CRITICAL;notify-service-by-email;Connection refused'
        }
        brok = Brok('log', data)
        brok.prepare()

        broker = InfluxdbBroker(self.basic_modconf)
        broker.manage_log_brok(brok)

        # make sure that this has generated only 1 point
        self.assertEqual(len(broker.buffer), 1)
        point = broker.buffer[0]

        # validate the point
        expected = {
            'points': [[
                None, 'CRITICAL', 'admin', 1402515279,
                'notify-service-by-email', 'HOST'
            ]],
            'name':
            'localhost._events_.NOTIFICATION',
            'columns': [
                'acknownledgement', 'state', 'contact', 'time',
                'notification_method', 'notification_type'
            ]
        }

        self.assertEqual(expected, point)

        # And that there is as much columns as there is points
        # (manage_log_brok has a special way of creating points)
        self.assertEqual(len(point['points'][0]), len(point['columns']))

        # A service notification's name should be different (host.service._events_.[event_type])
        data[
            'log'] = '[1402515279] SERVICE NOTIFICATION: admin;localhost;check-ssh;CRITICAL;notify-service-by-email;Connection refused'
        brok = Brok('log', data)
        brok.prepare()
        broker.buffer = []
        broker.manage_log_brok(brok)
        point = broker.buffer[0]
        self.assertEqual(point['name'],
                         'localhost.check-ssh._events_.NOTIFICATION')
Exemplo n.º 26
0
    def test_manage_log_brok(self):
        data = {
            "log": "[1402515279] HOST NOTIFICATION: admin;localhost;CRITICAL;notify-service-by-email;Connection refused"
        }
        brok = Brok("log", data)
        brok.prepare()

        broker = self.influx_broker
        broker.manage_log_brok(brok)

        # make sure that this has generated only 1 point
        self.assertEqual(len(broker.buffer), 1)
        point = broker.buffer[0]

        # validate the point
        expected = {
            "timestamp": 1402515279,
            "tags": {"service_desc": "_self_", "host_name": "localhost", "event_type": "NOTIFICATION"},
            "name": "ALERT",
            "fields": {
                "time": 1402515279,
                "state": "CRITICAL",
                "contact": "admin",
                "notification_type": "HOST",
                "notification_method": "notify-service-by-email",
                "output": "Connection refused",
            },
        }
        self.assertEqual(expected, point)

        # A service notification's tags should include service_desc
        data[
            "log"
        ] = "[1402515279] SERVICE NOTIFICATION: admin;localhost;check-ssh;CRITICAL;notify-service-by-email;Connection refused"
        brok = Brok("log", data)
        brok.prepare()
        broker.buffer = []
        broker.manage_log_brok(brok)
        point = broker.buffer[0]
        self.assertEqual(point["name"], "ALERT")
        self.assertEqual(point["tags"]["service_desc"], "check-ssh")
Exemplo n.º 27
0
    def test_manage_log_brok(self):
        data = {
            'log': '[1402515279] HOST NOTIFICATION: admin;localhost;CRITICAL;notify-service-by-email;Connection refused'
        }
        brok = Brok('log', data)
        brok.prepare()

        broker = self.influx_broker
        broker.manage_log_brok(brok)

        # make sure that this has generated only 1 point
        self.assertEqual(len(broker.buffer), 1)
        point = broker.buffer[0]

        # validate the point
        expected = {
            'points': [[None, 'CRITICAL', 'admin', 1402515279, 'notify-service-by-email', 'HOST']],
            'name': 'localhost>_self_>_events_>NOTIFICATION',
            'columns': ['acknownledgement', 'state', 'contact', 'time', 'notification_method', 'notification_type']
        }

        self.assertEqual(expected, point)

        # And that there is as much columns as there is points
        # (manage_log_brok has a special way of creating points)
        self.assertEqual(
            len(point['points'][0]),
            len(point['columns'])
        )

        # A service notification's name should be different (host.service._events_.[event_type])
        data['log'] = '[1402515279] SERVICE NOTIFICATION: admin;localhost;check-ssh;CRITICAL;notify-service-by-email;Connection refused'
        brok = Brok('log', data)
        brok.prepare()
        broker.buffer = []
        broker.manage_log_brok(brok)
        point = broker.buffer[0]
        self.assertEqual(point['name'], 'localhost>check-ssh>_events_>NOTIFICATION')
Exemplo n.º 28
0
    def test_manage_log_brok(self):
        data = {
            'log': '[1402515279] HOST NOTIFICATION: admin;localhost;CRITICAL;notify-service-by-email;Connection refused'  # nopep8
        }
        brok = Brok('log', data)
        brok.prepare()

        broker = self.influx_broker
        broker.manage_log_brok(brok)

        # make sure that this has generated only 1 point
        self.assertEqual(len(broker.buffer), 1)
        point = broker.buffer[0]

        # validate the point
        expected = {'fields':
                    {'time': 1402515279,
                     'state': 'CRITICAL',
                     'contact': 'admin',
                     'notification_type': 'HOST',
                     'notification_method': 'notify-service-by-email',
                     'output': 'Connection refused'},
                    'time': 1402515279,
                    'tags': {'service_description': '_self_',
                             'host_name': 'localhost',
                             'event_type': 'NOTIFICATION'},
                    'measurement': 'EVENT'}
        self.assertEqual(expected, point)

        # A service notification's tags should include service_desc
        data['log'] = '[1402515279] SERVICE NOTIFICATION: admin;localhost;check-ssh;CRITICAL;notify-service-by-email;Connection refused'  # nopep8
        brok = Brok('log', data)
        brok.prepare()
        broker.buffer = []
        broker.manage_log_brok(brok)
        point = broker.buffer[0]
        self.assertEqual(point['measurement'], 'EVENT')
        self.assertEqual(point['tags']['service_description'], 'check-ssh')
Exemplo n.º 29
0
    def test_simplelog(self):
        print self.conf.modules
        # get our modules
        mod = None
        for m in self.conf.modules:
            if m.module_type == 'simple_log':
                mod = m
        self.assert_(mod is not None)
        self.assert_(mod.path == 'tmp/nagios.log')
        self.assert_(mod.module_name == 'Simple-log')

        try:
            os.unlink(mod.path)
        except:
            pass

        sl = get_instance(mod)
        print sl
        # Hack here :(
        sl.properties = {}
        sl.properties['to_queue'] = None
        sl.init()
        b = Brok('log', {'log': "look at my ass.\n"})
        b.prepare()
        sl.manage_brok(b)
        b = Brok('log', {'log': "look at my ass again.\n"})
        b.prepare()
        sl.manage_brok(b)
        sl.file.close()
        fd = open(mod.path)
        buf = fd.readline()
        self.assert_(buf == "look at my ass.\n")
        buf = fd.readline()
        self.assert_(buf == "look at my ass again.\n")
        fd.close()
        os.unlink(mod.path)
Exemplo n.º 30
0
    def get_initial_status_brok(self):
        data = {'id': self.id}

        self.fill_data_brok_from(data, 'full_status')
        b = Brok('downtime_raise', data)
        return b