示例#1
0
    def test_insert_lls_event(self):
        """Insertion d'un évènement brut concernant un SBN"""

        self.make_dependencies()

        # Création d'un message d'événement portant sur un SBN.
        info_dictionary = {
                "type": "event",
                "timestamp": datetime.fromtimestamp(1239104006),
                "host": "server.example.com",
                "service": "Load",
                "state": u"WARNING",
                "message": u"WARNING: Load average is above 4 (4.5)",
                }
        info_dictionary['idsupitem'] = SupItem.get_supitem(
            info_dictionary['host'],
            info_dictionary['service']
        )

        # Insertion de l'événement dans la BDD
        idevent = insert_event(info_dictionary)

        assert idevent is not None
        event = DBSession.query(Event).one()

        # Vérification des informations de l'événement dans la BDD.
        self.assertEquals(LowLevelService, type(event.supitem))
        self.assertEquals(1239104006, time.mktime(event.timestamp.timetuple()))
        self.assertEquals(u'server.example.com', event.supitem.host.name)
        self.assertEquals(u'Load', event.supitem.servicename)
        self.assertEquals(u'WARNING',
            StateName.value_to_statename(event.current_state))
        self.assertEquals(u'WARNING',
            StateName.value_to_statename(event.initial_state))
        self.assertEquals(u'WARNING',
            StateName.value_to_statename(event.peak_state))
        self.assertEquals(u'WARNING: Load average is above 4 (4.5)',
                            event.message)

        # Insertion de l'état dans la BDD
        state = DBSession.query(State).get(info_dictionary['idsupitem'])
        # le timestamp par défaut est plus récent et insert_state refusera la
        # mise à jour
        state.timestamp = info_dictionary['timestamp']
        insert_state(info_dictionary)

        # Vérification des informations de l'état dans la BDD.
        self.assertEquals(LowLevelService, type(state.supitem))
        self.assertEquals(1239104006, time.mktime(state.timestamp.timetuple()))
        self.assertEquals('server.example.com', state.supitem.host.name)
        self.assertEquals('Load', state.supitem.servicename)
        self.assertEquals('WARNING',
            StateName.value_to_statename(state.state))
        self.assertEquals('WARNING: Load average is above 4 (4.5)',
                            state.message)
示例#2
0
    def test_insert_host_event(self):
        """Insertion d'un évènement brut concernant un hôte"""

        self.make_dependencies()

        # Création d'un message d'événement portant sur un hôte.
        info_dictionary = {
                "type": "event",
                "timestamp": datetime.fromtimestamp(1239104006),
                "host": "server.example.com",
                "state": u"DOWN",
                "message": u"DOWN: No ping response",
                }
        info_dictionary['idsupitem'] = SupItem.get_supitem(
            info_dictionary['host'], None,
        )

        # Insertion de l'événement dans la BDD
        idevent = insert_event(info_dictionary)

        assert idevent is not None
        event = DBSession.query(Event).one()

        # Vérification des informations de l'événement dans la BDD.
        self.assertEquals(Host, type(event.supitem))
        self.assertEquals(1239104006, time.mktime(event.timestamp.timetuple()))
        self.assertEquals(u'server.example.com', event.supitem.name)
        self.assertEquals(u'DOWN',
            StateName.value_to_statename(event.current_state))
        self.assertEquals(u'DOWN',
            StateName.value_to_statename(event.initial_state))
        self.assertEquals(u'DOWN',
            StateName.value_to_statename(event.peak_state))
        self.assertEquals(u'DOWN: No ping response',
                            event.message)

        # Insertion de l'état dans la BDD
        state = DBSession.query(State).get(info_dictionary['idsupitem'])
        # le timestamp par défaut est plus récent et insert_state refusera la
        # mise à jour
        state.timestamp = info_dictionary['timestamp']
        insert_state(info_dictionary)

        # Vérification des informations de l'état dans la BDD.
        self.assertEquals(Host, type(state.supitem))
        self.assertEquals(1239104006, time.mktime(state.timestamp.timetuple()))
        self.assertEquals('server.example.com', state.supitem.name)
        self.assertEquals('DOWN',
            StateName.value_to_statename(state.state))
        self.assertEquals('DOWN: No ping response', state.message)
示例#3
0
 def test_insert_old_state(self):
     """Abandon de l'insertion d'un état ancien"""
     self.make_dependencies()
     ts_old = 1239104006
     ts_recent = 1239104042
     ts_recent_dt = datetime.fromtimestamp(ts_recent)
     idsupitem = SupItem.get_supitem("server.example.com", "Load")
     # Insertion de l'état récent
     state = DBSession.query(State).get(idsupitem)
     state.timestamp = ts_recent_dt
     # Création d'un message d'événement portant sur un SBN.
     info_dictionary = {
             "type": "event",
             "timestamp": datetime.fromtimestamp(ts_old),
             "host": "server.example.com",
             "service": "Load",
             "state": "WARNING",
             "message": "WARNING: Load average is above 4 (4.5)",
             }
     info_dictionary['idsupitem'] = SupItem.get_supitem(
         info_dictionary['host'],
         info_dictionary['service']
     )
     # Insertion de l'ancien événement dans la BDD
     result = insert_state(info_dictionary)
     self.assertTrue(isinstance(result, OldStateReceived))
     supitem = DBSession.query(SupItem).get(idsupitem)
     self.assertEqual(supitem.state.timestamp, ts_recent_dt)
示例#4
0
    def simulate_message_reception(self,
        new_state, host_name, service_name=None):
        """
        Génère un message de changement d'état concernant l'item passé en
        paramètre, réalise les mêmes traitements que ceux du rule_dispatcher
        et des règles, et déclenche l'exécution de la fonction make_correvent.
        """

        # On incrémente l'identifiant du message
        self.msgid += 1

        # On génère un timestamp à partir de la date courante
        timestamp = datetime.now()

        infos = {
            'type': "event",
            'id': self.msgid,
            'timestamp': timestamp,
            'service': service_name,
            'state': new_state,
            'message': new_state,
        }

        if host_name:
            infos['host'] = host_name
        else:
            infos['host'] = helpers.settings['correlator']['nagios_hls_host']

        idsupitem = SupItem.get_supitem(host_name, service_name)

        # On ajoute les données nécessaires dans le contexte.
        ctx = self.context_factory(self.msgid)
        yield ctx.set('hostname', host_name)
        yield ctx.set('servicename', service_name)
        yield ctx.set('statename', new_state)
        yield ctx.set('idsupitem', idsupitem)

        # On insère les données nécessaires dans la BDD:
        info_dictionary = {
            "id": self.msgid,
            "host": host_name,
            "service": service_name,
            "state": new_state,
            "timestamp": timestamp,
            "message": new_state,
            "idsupitem": idsupitem,
        }

        # - D'abord l'évènement ;
        LOGGER.info("Inserting event")
        raw_id = insert_event(info_dictionary)
        yield ctx.set('raw_event_id', raw_id)
        # - Et ensuite l'état.
        LOGGER.info("Inserting state")
        # Si le timestamp est trop récent insert_state ne fera rien
        DBSession.query(State).get(idsupitem).timestamp = timestamp
        insert_state(info_dictionary)
        DBSession.flush()

        # On force le traitement du message, par la fonction make_correvent,
        # comme s'il avait été traité au préalable par le rule_dispatcher.
        corrbuilder = CorrEventBuilder(Mock(), DummyDatabaseWrapper(True))
        corrbuilder.context_factory = self.context_factory

        LOGGER.info('Creating a new correlated event')
        yield corrbuilder.make_correvent(info_dictionary)