Пример #1
0
 def testPeriodPollerOnRealProcess(self):
     config = getConfig("/tmp")
     config.component_("AlertProcessor")
     config.AlertProcessor.section_("critical")
     config.AlertProcessor.section_("soft")
     config.AlertProcessor.critical.level = 5
     config.AlertProcessor.soft.level = 0        
     config.component_("AlertGenerator")
     config.AlertGenerator.section_("bogusPoller")
     config.AlertGenerator.bogusPoller.soft = 5 # [percent]
     # the way the worker is implemented should take 100% CPU, but it sometimes
     # take a while, test safer threshold here, testing thresholds
     # more rigorously happens in Pollers_t
     config.AlertGenerator.bogusPoller.critical = 50 # [percent] 
     config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
     # period during which measurements are collected before evaluating for possible alert triggering
     config.AlertGenerator.bogusPoller.period = 1
     
     generator = utils.AlertGeneratorMock(config)
     poller = PeriodPoller(config.AlertGenerator.bogusPoller, generator)
     poller.sender = utils.SenderMock()
     # get CPU usage percentage, it's like measuring CPU usage of a real
     # component, so use the appropriate poller's method for that
     # (PeriodPoller itself is higher-level class so it doesn't define
     # a method to provide sampling data)
     poller.sample = lambda processDetail: ComponentsCPUPoller.sample(processDetail)
     
     p = utils.getProcess()
     self.testProcesses.append(p)
     while not p.is_alive():
         time.sleep(0.2)        
     name = "mytestprocess-testPeriodPollerBasic"
     pd = ProcessDetail(p.pid, name)
     # need to repeat sampling required number of measurements
     numOfMeasurements = int(config.AlertGenerator.bogusPoller.period / 
                             config.AlertGenerator.bogusPoller.pollInterval)
     mes = Measurements(numOfMeasurements)
     self.assertEqual(len(mes), 0)
     for i in range(mes._numOfMeasurements):
         poller.check(pd, mes)
         
     # 1 alert should have arrived, test it
     #    though there may be a second alert as well if the test managed to
     #    run second round - don't test number of received alerts
     #    also the Level and threshold is not deterministic: given it's
     #    measured on a live process it can't be determined up-front how
     #    much CPU this simple process will be given: don't test Level
     #    and threshold
     a = poller.sender.queue[0]
     self.assertEqual(a["Component"], generator.__class__.__name__)
     self.assertEqual(a["Source"], poller.__class__.__name__)
     d = a["Details"]
     self.assertEqual(d["numMeasurements"], mes._numOfMeasurements)
     self.assertEqual(d["component"], name)
     self.assertEqual(d["period"], config.AlertGenerator.bogusPoller.period)
     
     # since the whole measurement cycle was done, values should have been nulled
     self.assertEqual(len(mes), 0)
Пример #2
0
    def testPeriodPollerCalculationPredefinedInput(self):
        config = getConfig("/tmp")
        config.component_("AlertProcessor")
        config.AlertProcessor.section_("critical")
        config.AlertProcessor.section_("soft")
        config.AlertProcessor.critical.level = 5
        config.AlertProcessor.soft.level = 0
        config.component_("AlertGenerator")
        config.AlertGenerator.section_("bogusPoller")
        # put some threshold numbers, just need to check output calculation
        # from check() method
        config.AlertGenerator.bogusPoller.soft = 5  # [percent]
        config.AlertGenerator.bogusPoller.critical = 50  # [percent]
        config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
        config.AlertGenerator.bogusPoller.period = 1

        generator = utils.AlertGeneratorMock(config)
        poller = PeriodPoller(config.AlertGenerator.bogusPoller, generator)
        # since poller may trigger an alert, give it mock sender
        poller.sender = utils.SenderMock()
        # provide sample method with predefined input, float
        predefInput = 10.12
        poller.sample = lambda processDetail: predefInput

        processDetail = None
        numOfMeasurements = int(config.AlertGenerator.bogusPoller.period /
                                config.AlertGenerator.bogusPoller.pollInterval)
        mes = Measurements(numOfMeasurements)
        for i in range(mes._numOfMeasurements):
            poller.check(processDetail, mes)

        # the above loop should went 5 times, should reach evaluation of 5 x predefInput
        # values, the average should end up 10, which should trigger soft threshold
        self.assertEqual(len(poller.sender.queue), 1)
        a = poller.sender.queue[0]

        self.assertEqual(a["Component"], generator.__class__.__name__)
        self.assertEqual(a["Source"], poller.__class__.__name__)
        d = a["Details"]
        self.assertEqual(d["threshold"],
                         "%s%%" % config.AlertGenerator.bogusPoller.soft)
        self.assertEqual(d["numMeasurements"], mes._numOfMeasurements)
        self.assertEqual(d["period"], config.AlertGenerator.bogusPoller.period)
        self.assertEqual(d["average"], "%s%%" % predefInput)
        # since the whole measurement cycle was done, values should have been nulled
        self.assertEqual(len(mes), 0)
Пример #3
0
 def testPeriodPollerCalculationPredefinedInput(self):
     config = getConfig("/tmp")
     config.component_("AlertProcessor")
     config.AlertProcessor.section_("critical")
     config.AlertProcessor.section_("soft")
     config.AlertProcessor.critical.level = 5
     config.AlertProcessor.soft.level = 0        
     config.component_("AlertGenerator")
     config.AlertGenerator.section_("bogusPoller")
     # put some threshold numbers, just need to check output calculation
     # from check() method
     config.AlertGenerator.bogusPoller.soft = 5 # [percent]
     config.AlertGenerator.bogusPoller.critical = 50 # [percent] 
     config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
     config.AlertGenerator.bogusPoller.period = 1
     
     generator = utils.AlertGeneratorMock(config)
     poller = PeriodPoller(config.AlertGenerator.bogusPoller, generator)
     # since poller may trigger an alert, give it mock sender
     poller.sender = utils.SenderMock()
     # provide sample method with predefined input, float
     predefInput = 10.12
     poller.sample = lambda processDetail: predefInput
     
     processDetail = None
     numOfMeasurements = int(config.AlertGenerator.bogusPoller.period / 
                             config.AlertGenerator.bogusPoller.pollInterval)
     mes = Measurements(numOfMeasurements)
     for i in range(mes._numOfMeasurements):
         poller.check(processDetail, mes)
         
     # the above loop should went 5 times, should reach evaluation of 5 x predefInput
     # values, the average should end up 10, which should trigger soft threshold
     self.assertEqual(len(poller.sender.queue), 1)
     a = poller.sender.queue[0]
     
     self.assertEqual(a["Component"], generator.__class__.__name__)
     self.assertEqual(a["Source"], poller.__class__.__name__)
     d = a["Details"]
     self.assertEqual(d["threshold"], "%s%%" % config.AlertGenerator.bogusPoller.soft)
     self.assertEqual(d["numMeasurements"], mes._numOfMeasurements)
     self.assertEqual(d["period"], config.AlertGenerator.bogusPoller.period)
     self.assertEqual(d["average"], "%s%%" % predefInput)
     # since the whole measurement cycle was done, values should have been nulled
     self.assertEqual(len(mes), 0)
Пример #4
0
    def testPeriodPollerOnRealProcess(self):
        config = getConfig("/tmp")
        config.component_("AlertProcessor")
        config.AlertProcessor.section_("critical")
        config.AlertProcessor.section_("soft")
        config.AlertProcessor.critical.level = 5
        config.AlertProcessor.soft.level = 0
        config.component_("AlertGenerator")
        config.AlertGenerator.section_("bogusPoller")
        config.AlertGenerator.bogusPoller.soft = 5  # [percent]
        config.AlertGenerator.bogusPoller.critical = 50  # [percent]
        config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
        # period during which measurements are collected before evaluating for
        # possible alert triggering
        config.AlertGenerator.bogusPoller.period = 1

        generator = utils.AlertGeneratorMock(config)
        poller = PeriodPoller(config.AlertGenerator.bogusPoller, generator)
        poller.sender = utils.SenderMock()
        # get CPU usage percentage, it's like measuring CPU usage of a real
        # component, so use the appropriate poller's method for that
        # (PeriodPoller itself is higher-level class so it doesn't define
        # a method to provide sampling data)
        poller.sample = lambda processDetail: ComponentsCPUPoller.sample(
            processDetail)

        # get own pid
        pid = os.getpid()
        name = inspect.stack()[0][3]  # test name
        pd = ProcessDetail(pid, name)
        # need to repeat sampling required number of measurements
        numOfMeasurements = int(config.AlertGenerator.bogusPoller.period /
                                config.AlertGenerator.bogusPoller.pollInterval)
        mes = Measurements(numOfMeasurements)
        self.assertEqual(len(mes), 0)
        for i in range(mes._numOfMeasurements):
            poller.check(pd, mes)

        # since the whole measurement cycle was done, values should have been nulled
        self.assertEqual(len(mes), 0)
Пример #5
0
 def testPeriodPollerOnRealProcess(self):
     config = getConfig("/tmp")
     config.component_("AlertProcessor")
     config.AlertProcessor.section_("critical")
     config.AlertProcessor.section_("soft")
     config.AlertProcessor.critical.level = 5
     config.AlertProcessor.soft.level = 0        
     config.component_("AlertGenerator")
     config.AlertGenerator.section_("bogusPoller")
     config.AlertGenerator.bogusPoller.soft = 5 # [percent]
     config.AlertGenerator.bogusPoller.critical = 50 # [percent] 
     config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
     # period during which measurements are collected before evaluating for
     # possible alert triggering
     config.AlertGenerator.bogusPoller.period = 1
     
     generator = utils.AlertGeneratorMock(config)
     poller = PeriodPoller(config.AlertGenerator.bogusPoller, generator)
     poller.sender = utils.SenderMock()
     # get CPU usage percentage, it's like measuring CPU usage of a real
     # component, so use the appropriate poller's method for that
     # (PeriodPoller itself is higher-level class so it doesn't define
     # a method to provide sampling data)
     poller.sample = lambda processDetail: ComponentsCPUPoller.sample(processDetail)
     
     # get own pid
     pid = os.getpid()
     name = inspect.stack()[0][3] # test name
     pd = ProcessDetail(pid, name)
     # need to repeat sampling required number of measurements
     numOfMeasurements = int(config.AlertGenerator.bogusPoller.period / 
                             config.AlertGenerator.bogusPoller.pollInterval)
     mes = Measurements(numOfMeasurements)
     self.assertEqual(len(mes), 0)
     for i in range(mes._numOfMeasurements):
         poller.check(pd, mes)
                 
     # since the whole measurement cycle was done, values should have been nulled
     self.assertEqual(len(mes), 0)