Exemplo n.º 1
0
    def testFileSinkBasic(self):
        sink = FileSink(self.config)
        alerts = []
        nAlerts = 10
        for i in range(nAlerts):
            a = Alert(Source=__file__,
                      Level=i,
                      Timestamp=time.time(),
                      Type="Test")
            alerts.append(a)
        sink.send(alerts)
        # test by reading back
        loadAlerts = sink.load()
        self.assertEqual(len(loadAlerts), nAlerts)

        # Since FileSink implementation depends on line-separated JSONs of
        # Alert instance, test handling new lines in the payload
        alerts = []
        testMsg = "addtional \n message"
        for i in range(10, 20):
            a = Alert(Source=__file__,
                      Level=i,
                      Timestamp=time.time(),
                      Type="Test",
                      Details={"message": testMsg})
            alerts.append(a)
        self.failUnless(os.path.exists(self.config.outputfile))
        sink.send(alerts)
        # test by reading back
        loadAlerts = sink.load()
        self.assertEqual(len(loadAlerts), 20)
        for a in loadAlerts[10:]:
            self.assertEqual(a["Details"]["message"], testMsg)
Exemplo n.º 2
0
 def testFileSinkBasic(self):
     sink = FileSink(self.config)
     alerts = []
     nAlerts = 10
     for i in range(nAlerts):
         a = Alert(Source = __file__, Level = i, Timestamp = time.time(),
                   Type = "Test")
         alerts.append(a)
     sink.send(alerts)
     # test by reading back
     loadAlerts = sink.load()
     self.assertEqual(len(loadAlerts), nAlerts)
     
     # Since FileSink implementation depends on line-separated JSONs of
     # Alert instance, test handling new lines in the payload
     alerts = []
     testMsg = "addtional \n message"
     for i in range(10, 20):
         a = Alert(Source = __file__, Level = i, Timestamp = time.time(),
                   Type = "Test", Details = {"message": testMsg})
         alerts.append(a)
     self.failUnless(os.path.exists(self.config.outputfile))            
     sink.send(alerts)
     # test by reading back
     loadAlerts = sink.load()
     self.assertEqual(len(loadAlerts), 20)
     for a in loadAlerts[10:]:
         self.assertEqual(a["Details"]["message"], testMsg)
Exemplo n.º 3
0
 def testProcessorWithReceiverAndFileSink(self):
     # add corresponding part of the configuration for FileSink(s)
     config = self.config.AlertProcessor
     config.critical.sinks.section_("file")
     config.critical.sinks.file.outputfile = self.criticalOutputFile 
     
     config.soft.sinks.section_("file")
     config.soft.sinks.file.outputfile = self.softOutputFile
     
     processor = Processor(config)
     # Receiver is waited for shutdown / shutdown explicitly in tearDown()
     self.receiver = Receiver(self.addr, processor, self.ctrl)
     self.receiver.startReceiver() # non blocking call
     
     # run worker(), this time directly without Process as above,
     # worker will send 10 Alerts to Receiver
     worker(self.addr, self.ctrl, 10)
     
     # wait until Receiver is shut down (by a message from worker(), then all
     # alerts shall be delivered and could proceed to check if successfully delivered
     while self.receiver.isReady():
         time.sleep(ReceiverLogic.TIMEOUT_AFTER_SHUTDOWN * 1.5)
         logging.info("%s: Waiting for Receiver shutdown ..." % inspect.stack()[0][3])
     
     # check the FileSink output files for content:
     # the soft Alerts has threshold level set to 0 so Alerts
     # with level 1 and higher, resp. for critical the level
     # was above set to 5 so 6 and higher out of worker's 0 .. 9
     # (10 Alerts altogether) shall be present
     softSink = FileSink(config.soft.sinks.file)
     criticalSink = FileSink(config.critical.sinks.file)
     softList = softSink.load()
     criticalList = criticalSink.load()
     # check soft level alerts
     # levels 1 .. 4 went in (level 0 is, according to the config not considered)
     self.assertEqual(len(softList), 3) 
     for a, level in zip(softList, range(1, 4)):
         self.assertEqual(a["Level"], level)
     # check 'critical' levels
     # only levels 5 .. 9 went in
     self.assertEqual(len(criticalList), 5)
     for a, level in zip(criticalList, range(5, 10)):
         self.assertEqual(a["Level"], level)
Exemplo n.º 4
0
 def testProcessorWithReceiverAndFileSink(self):
     # add corresponding part of the configuration for FileSink(s)
     config = self.config.AlertProcessor
     config.critical.sinks.section_("file")
     config.critical.sinks.file.outputfile = self.criticalOutputFile 
     
     config.soft.sinks.section_("file")
     config.soft.sinks.file.outputfile = self.softOutputFile
     
     processor = Processor(config)
     rec = Receiver(self.addr, processor, self.ctrl)
     rec.startReceiver() # non blocking call
     
     # run worker(), this time directly without Process as above,
     # worker will send 10 Alerts to Receiver
     worker(self.addr, self.ctrl, 10)
     
     # wait until the Receiver is shut by worker
     while rec.isReady():
         time.sleep(0.3)
         print "%s waiting for Receiver to shut ..." % inspect.stack()[0][3]
         
     # now check the FileSink output files for content:
     # the soft Alerts has threshold level set to 0 so Alerts
     # with level 1 and higher, resp. for critical the level
     # was above set to 5 so 6 and higher out of worker's 0 .. 9
     # (10 Alerts altogether) shall be present
     softSink = FileSink(config.soft.sinks.file)
     criticalSink = FileSink(config.critical.sinks.file)
     softList = softSink.load()
     criticalList = criticalSink.load()
     # check soft level alerts
     # levels 1 .. 4 went in (level 0 is, according to the config not considered)
     self.assertEqual(len(softList), 3) 
     for a, level in zip(softList, range(1, 4)):
         self.assertEqual(a["Level"], level)
     # check 'critical' levels
     # only levels 5 .. 9 went in
     self.assertEqual(len(criticalList), 5)
     for a, level in zip(criticalList, range(5, 10)):
         self.assertEqual(a["Level"], level)
Exemplo n.º 5
0
    def testProcessorWithReceiverAndFileSink(self):
        # add corresponding part of the configuration for FileSink(s)
        config = self.config.AlertProcessor
        config.critical.sinks.section_("file")
        config.critical.sinks.file.outputfile = self.criticalOutputFile

        config.soft.sinks.section_("file")
        config.soft.sinks.file.outputfile = self.softOutputFile

        processor = Processor(config)
        # Receiver is waited for shutdown / shutdown explicitly in tearDown()
        self.receiver = Receiver(self.addr, processor, self.ctrl)
        self.receiver.startReceiver() # non blocking call

        # run worker(), this time directly without Process as above,
        # worker will send 10 Alerts to Receiver
        worker(self.addr, self.ctrl, 10)

        # wait until Receiver is shut down (by a message from worker(), then all
        # alerts shall be delivered and could proceed to check if successfully delivered
        while self.receiver.isReady():
            time.sleep(ReceiverLogic.TIMEOUT_AFTER_SHUTDOWN * 1.5)
            logging.info("%s: Waiting for Receiver shutdown ..." % inspect.stack()[0][3])

        # check the FileSink output files for content:
        # the soft Alerts has threshold level set to 0 so Alerts
        # with level 1 and higher, resp. for critical the level
        # was above set to 5 so 6 and higher out of worker's 0 .. 9
        # (10 Alerts altogether) shall be present
        softSink = FileSink(config.soft.sinks.file)
        criticalSink = FileSink(config.critical.sinks.file)
        softList = softSink.load()
        criticalList = criticalSink.load()
        # check soft level alerts
        # levels 1 .. 4 went in (level 0 is, according to the config not considered)
        self.assertEqual(len(softList), 3)
        for a, level in zip(softList, range(1, 4)):
            self.assertEqual(a["Level"], level)
        # check 'critical' levels
        # only levels 5 .. 9 went in
        self.assertEqual(len(criticalList), 5)
        for a, level in zip(criticalList, range(5, 10)):
            self.assertEqual(a["Level"], level)
Exemplo n.º 6
0
    def testForwardSinkEntireChain(self):
        """
        The test chain looks as follows:
        worker -> Receiver1(+its Processor configured to do ForwardSink) -> Receiver2 whose
            address as the destination the ForwardSink is configured with -> Receiver2 will
            do FileSink so that it's possible to verify the chain.

        """
        # configuration for the Receiver+Processor+ForwardSink 1 (group)
        config1 = Configuration()
        config1.component_("AlertProcessor")
        config1.AlertProcessor.section_("critical")
        config1.AlertProcessor.section_("soft")

        config1.AlertProcessor.critical.level = 5
        config1.AlertProcessor.soft.level = 0
        config1.AlertProcessor.soft.bufferSize = 0

        config1.AlertProcessor.critical.section_("sinks")
        config1.AlertProcessor.soft.section_("sinks")

        config1.AlertProcessor.critical.sinks.section_("forward")
        config1.AlertProcessor.soft.sinks.section_("forward")
        # address of the Receiver2
        config1.AlertProcessor.critical.sinks.forward.address = self.address2
        config1.AlertProcessor.critical.sinks.forward.controlAddr = self.controlAddr2
        config1.AlertProcessor.critical.sinks.forward.label = "ForwardSinkTest"
        config1.AlertProcessor.soft.sinks.forward.address = self.address2
        config1.AlertProcessor.soft.sinks.forward.controlAddr = self.controlAddr2
        config1.AlertProcessor.soft.sinks.forward.label = "ForwardSinkTest"

        # 1) first item of the chain is source of Alerts: worker()

        # 2) second item is Receiver1 + its Processor + its ForwardSink
        processor1 = Processor(config1.AlertProcessor)
        # ForwardSink will be created automatically by the Processor
        receiver1 = Receiver(self.address1, processor1, self.controlAddr1)
        receiver1.startReceiver() # non blocking call

        # 3) third group is Receiver2 with its Processor and final FileSink
        config2 = Configuration()
        config2.component_("AlertProcessor")
        config2.AlertProcessor.section_("critical")
        config2.AlertProcessor.section_("soft")

        config2.AlertProcessor.critical.level = 5
        config2.AlertProcessor.soft.level = 0
        config2.AlertProcessor.soft.bufferSize = 0

        config2.AlertProcessor.critical.section_("sinks")
        config2.AlertProcessor.soft.section_("sinks")

        config2.AlertProcessor.critical.sinks.section_("file")
        config2.AlertProcessor.soft.sinks.section_("file")
        # configuration of the final sink
        config2.AlertProcessor.critical.sinks.file.outputfile = self.outputfileCritical
        config2.AlertProcessor.soft.sinks.file.outputfile = self.outputfileSoft

        processor2 = Processor(config2.AlertProcessor)
        # final FileSink will be automatically created by the Processor
        receiver2 = Receiver(self.address2, processor2, self.controlAddr2)
        receiver2.startReceiver() # non blocking call

        # now send the Alert messages via worker() and eventually shut the receiver1
        worker(self.address1, self.controlAddr1, 10)
        # wait until receiver1 shuts
        while receiver1.isReady():
            time.sleep(0.4)
            print "%s waiting for Receiver1 to shut ..." % inspect.stack()[0][3]

        # shut down receiver2 - need to sendShutdown() to it
        s = Sender(self.address2, self.controlAddr2, "some_id")
        s.sendShutdown()
        # wait until receiver2 shuts
        while receiver2.isReady():
            time.sleep(0.4)
            print "%s waiting for Receiver2 to shut ..." % inspect.stack()[0][3]

        # check the result in the files
        # the bufferSize for soft-level Alerts was set to 0 so all
        # Alerts should be present also in the soft-level type file
        # initial 10 Alerts (Level 0 .. 9) gets distributed though a cascade
        # of two Receivers. soft alerts with level 0 .. 4 are considered
        # so Receiver1 forwards through its ForwardSink 0 .. 4 Alerts as soft and
        # 5 .. 9 level Alerts through 'critical'. order is not guaranteed
        # critical Alerts
        fileConfig = ConfigSection("file")
        fileConfig.outputfile = self.outputfileCritical
        sink = FileSink(fileConfig)
        expectedLevels = range(5, 10) # that is 5 .. 9
        loadAlerts = sink.load()
        self.assertEqual(len(loadAlerts), len(expectedLevels))
        d = dict(very = "interesting")
        for a in loadAlerts:
            self.assertEqual(a["Details"], d)

        # soft Alerts
        fileConfig = ConfigSection("file")
        fileConfig.outputfile = self.outputfileSoft
        sink = FileSink(fileConfig)
        expectedLevels = range(0, 5) # that is 0 .. 4
        loadAlerts = sink.load()
        self.assertEqual(len(loadAlerts), len(expectedLevels))
        for a in loadAlerts:
            self.assertEqual(a["Details"], d)
Exemplo n.º 7
0
    def testForwardSinkEntireChain(self):
        """
        The test chain looks as follows:
        worker -> Receiver1(+its Processor configured to do ForwardSink) -> Receiver2 whose
            address as the destination the ForwardSink is configured with -> Receiver2 will
            do FileSink so that it's possible to verify the chain.

        """
        # configuration for the Receiver+Processor+ForwardSink 1 (group)
        config1 = Configuration()
        config1.component_("AlertProcessor")
        config1.AlertProcessor.section_("critical")
        config1.AlertProcessor.section_("soft")

        config1.AlertProcessor.critical.level = 5
        config1.AlertProcessor.soft.level = 0
        config1.AlertProcessor.soft.bufferSize = 0

        config1.AlertProcessor.critical.section_("sinks")
        config1.AlertProcessor.soft.section_("sinks")

        config1.AlertProcessor.critical.sinks.section_("forward")
        config1.AlertProcessor.soft.sinks.section_("forward")
        # address of the Receiver2
        config1.AlertProcessor.critical.sinks.forward.address = self.address2
        config1.AlertProcessor.critical.sinks.forward.controlAddr = self.controlAddr2
        config1.AlertProcessor.critical.sinks.forward.label = "ForwardSinkTest"
        config1.AlertProcessor.soft.sinks.forward.address = self.address2
        config1.AlertProcessor.soft.sinks.forward.controlAddr = self.controlAddr2
        config1.AlertProcessor.soft.sinks.forward.label = "ForwardSinkTest"

        # 1) first item of the chain is source of Alerts: worker()

        # 2) second item is Receiver1 + its Processor + its ForwardSink
        processor1 = Processor(config1.AlertProcessor)
        # ForwardSink will be created automatically by the Processor
        receiver1 = Receiver(self.address1, processor1, self.controlAddr1)
        receiver1.startReceiver()  # non blocking call

        # 3) third group is Receiver2 with its Processor and final FileSink
        config2 = Configuration()
        config2.component_("AlertProcessor")
        config2.AlertProcessor.section_("critical")
        config2.AlertProcessor.section_("soft")

        config2.AlertProcessor.critical.level = 5
        config2.AlertProcessor.soft.level = 0
        config2.AlertProcessor.soft.bufferSize = 0

        config2.AlertProcessor.critical.section_("sinks")
        config2.AlertProcessor.soft.section_("sinks")

        config2.AlertProcessor.critical.sinks.section_("file")
        config2.AlertProcessor.soft.sinks.section_("file")
        # configuration of the final sink
        config2.AlertProcessor.critical.sinks.file.outputfile = self.outputfileCritical
        config2.AlertProcessor.soft.sinks.file.outputfile = self.outputfileSoft

        processor2 = Processor(config2.AlertProcessor)
        # final FileSink will be automatically created by the Processor
        receiver2 = Receiver(self.address2, processor2, self.controlAddr2)
        receiver2.startReceiver()  # non blocking call

        # now send the Alert messages via worker() and eventually shut the receiver1
        worker(self.address1, self.controlAddr1, 10)
        # wait until receiver1 shuts
        while receiver1.isReady():
            time.sleep(0.4)
            print "%s waiting for Receiver1 to shut ..." % inspect.stack(
            )[0][3]

        # shut down receiver2 - need to sendShutdown() to it
        s = Sender(self.address2, self.controlAddr2, "some_id")
        s.sendShutdown()
        # wait until receiver2 shuts
        while receiver2.isReady():
            time.sleep(0.4)
            print "%s waiting for Receiver2 to shut ..." % inspect.stack(
            )[0][3]

        # check the result in the files
        # the bufferSize for soft-level Alerts was set to 0 so all
        # Alerts should be present also in the soft-level type file
        # initial 10 Alerts (Level 0 .. 9) gets distributed though a cascade
        # of two Receivers. soft alerts with level 0 .. 4 are considered
        # so Receiver1 forwards through its ForwardSink 0 .. 4 Alerts as soft and
        # 5 .. 9 level Alerts through 'critical'. order is not guaranteed
        # critical Alerts
        fileConfig = ConfigSection("file")
        fileConfig.outputfile = self.outputfileCritical
        sink = FileSink(fileConfig)
        expectedLevels = range(5, 10)  # that is 5 .. 9
        loadAlerts = sink.load()
        self.assertEqual(len(loadAlerts), len(expectedLevels))
        d = dict(very="interesting")
        for a in loadAlerts:
            self.assertEqual(a["Details"], d)

        # soft Alerts
        fileConfig = ConfigSection("file")
        fileConfig.outputfile = self.outputfileSoft
        sink = FileSink(fileConfig)
        expectedLevels = range(0, 5)  # that is 0 .. 4
        loadAlerts = sink.load()
        self.assertEqual(len(loadAlerts), len(expectedLevels))
        for a in loadAlerts:
            self.assertEqual(a["Details"], d)