def setUp(self): self.testInit = TestInit(__file__) self.testInit.setLogging(logLevel=logging.DEBUG) self.testDir = self.testInit.generateWorkDir() self.config = getConfig(self.testDir) # mock generator instance to communicate some configuration values self.generator = utils.AlertGeneratorMock(self.config)
def setUp(self): self.testInit = TestInit(__file__) self.testInit.setLogging(logLevel = logging.DEBUG) self.testDir = self.testInit.generateWorkDir() self.config = getConfig(self.testDir) self.testProcesses = [] self.testComponentDaemonXml = "/tmp/TestComponent/Daemon.xml"
def testAlertGeneratorCouchDbSizePollerBasic(self): config = getConfig("/tmp") try: poller = CouchDbSizePoller(config.AlertGenerator.couchDbSizePoller, self.generator) except Exception, ex: self.fail("%s: exception: %s" % (self.testName, ex))
def setUp(self): self.testInit = TestInit(__file__) self.testInit.setLogging(logLevel = logging.DEBUG) self.testDir = self.testInit.generateWorkDir() self.config = getConfig(self.testDir) # mock generator instance to communicate some configuration values self.generator = utils.AlertGeneratorMock(self.config)
def testAlertsMessagingBasic(self): config = getConfig("/tmp") self.assertTrue(hasattr(config, "Alert")) # initialization # sender: instance of Alert messages Sender # preAlert: pre-defined values for Alert instances generated from this class self.config = config # needed in setUpAlertsMessaging preAlert, sender = alertAPI.setUpAlertsMessaging(self, compName="testBasic") sendAlert = alertAPI.getSendAlert(sender=sender, preAlert=preAlert) # set up a temporary alert message receiver handler, receiver = utils.setUpReceiver(config.Alert.address, config.Alert.controlAddr) # test sending alert msg = "this is my message Basic" sendAlert(100, msg=msg) # wait for the alert to arrive while len(handler.queue) == 0: time.sleep(0.3) print "%s waiting for alert to arrive ..." % inspect.stack()[0][3] self.assertEqual(len(handler.queue), 1) alert = handler.queue[0] self.assertEqual(alert["Component"], "testBasic") self.assertEqual(alert["Level"], 100) self.assertEqual(alert["Source"], self.__class__.__name__) self.assertEqual(alert["Details"]["msg"], msg) sender.unregister() receiver.shutdown()
def testAlertsMessagingBasic(self): config = getConfig("/tmp") self.assertTrue(hasattr(config, "Alert")) # initialization # sender: instance of Alert messages Sender # preAlert: pre-defined values for Alert instances generated from this class self.config = config # needed in setUpAlertsMessaging preAlert, sender = alertAPI.setUpAlertsMessaging(self, compName = "testBasic") sendAlert = alertAPI.getSendAlert(sender = sender, preAlert = preAlert) # set up a temporary alert message receiver handler, receiver = utils.setUpReceiver(config.Alert.address, config.Alert.controlAddr) # test sending alert msg = "this is my message Basic" sendAlert(100, msg = msg) # wait for the alert to arrive while len(handler.queue) == 0: time.sleep(0.3) print "%s waiting for alert to arrive ..." % inspect.stack()[0][3] self.assertEqual(len(handler.queue), 1) alert = handler.queue[0] self.assertEqual(alert["Component"], "testBasic") self.assertEqual(alert["Level"], 100) self.assertEqual(alert["Source"], self.__class__.__name__) self.assertEqual(alert["Details"]["msg"], msg) sender.unregister() receiver.shutdown()
def testAlertsSetUpAndSendingViaBaseWorkerThread(self): # calls as they are made from child/client classes of BaseWorkerThread config = getConfig("/tmp") self.assertTrue(hasattr(config, "Alert")) # test the same way alerts are set up in the client code (currently # all cases via BaseWorkerThread) # this call creates .sender, but here will be set to None thread = BaseWorkerThread() thread.config = config thread.initAlerts(compName="test2") self.assertTrue(thread.sender) # set up a temporary alert message receiver handler, receiver = utils.setUpReceiver(config.Alert.address, config.Alert.controlAddr) # send an alert message msg = "this is my message 1" thread.sendAlert(10, msg=msg) # wait for the alert to arrive while len(handler.queue) == 0: time.sleep(0.3) print "%s waiting for alert to arrive ..." % inspect.stack()[0][3] self.assertEqual(len(handler.queue), 1) alert = handler.queue[0] self.assertEqual(alert["Component"], "test2") self.assertEqual(alert["Level"], 10) self.assertEqual(alert["Source"], thread.__class__.__name__) self.assertEqual(alert["Details"]["msg"], msg) thread.sender.unregister() receiver.shutdown()
def testComponentsCPUPollerPossiblyOnLiveAgent(self): """ If there is currently running agent upon WMAGENT_CONFIG configuration, then the test will pick up live processes and poll them. """ # check if the live agent configuration was loaded (above this class) if globals().has_key("config"): self.config = config # AlertProcessor values - values for Level soft, resp. critical # are also needed by this AlertGenerator test self.config.component_("AlertProcessor") self.config.AlertProcessor.componentDir = "/tmp" self.config.AlertProcessor.section_("critical") self.config.AlertProcessor.section_("soft") self.config.AlertProcessor.critical.level = 5 self.config.AlertProcessor.soft.level = 0 self.config.component_("AlertGenerator") self.config.AlertGenerator.componentDir = "/tmp" self.config.section_("Alert") self.config.Alert.address = "tcp://127.0.0.1:6557" self.config.Alert.controlAddr = "tcp://127.0.0.1:6559" self.config.AlertGenerator.section_("componentsCPUPoller") else: self.config = getConfig("/tmp") self.config.AlertGenerator.componentsCPUPoller.soft = 70 self.config.AlertGenerator.componentsCPUPoller.critical = 80 self.config.AlertGenerator.componentsCPUPoller.pollInterval = 0.2 self.config.AlertGenerator.componentsCPUPoller.period = 0.3 # generator has already been instantiated, but need another one # with just defined configuration # mock generator instance to communicate some configuration values self.generator = utils.AlertGeneratorMock(self.config) handler, receiver = utils.setUpReceiver(self.generator.config.Alert.address, self.generator.config.Alert.controlAddr) numMeasurements = self.config.AlertGenerator.componentsCPUPoller.period / self.config.AlertGenerator.componentsCPUPoller.pollInterval poller = ComponentsCPUPoller(self.config.AlertGenerator.componentsCPUPoller, self.generator) # inject own input sample data provider thresholdToTest = self.config.AlertGenerator.componentsCPUPoller.soft # there is in fact input argument in this case which needs be ignored poller.sample = lambda proc_: random.randint(thresholdToTest - 10, thresholdToTest) proc = multiprocessing.Process(target = poller.poll, args = ()) proc.start() self.assertTrue(proc.is_alive()) # no alert shall arrive time.sleep(5 * self.config.AlertGenerator.componentsCPUPoller.period) proc.terminate() poller.shutdown() receiver.shutdown() self.assertFalse(proc.is_alive())
def testAlertsSetUpAndSendingViaBaseWorkerThread(self): # calls as they are made from child/client classes of BaseWorkerThread config = getConfig("/tmp") self.assertTrue(hasattr(config, "Alert")) # test the same way alerts are set up in the client code (currently # all cases via BaseWorkerThread) # this call creates .sender, but here will be set to None thread = BaseWorkerThread() thread.config = config thread.initAlerts(compName = "test2") self.assertTrue(thread.sender) # set up a temporary alert message receiver handler, receiver = utils.setUpReceiver(config.Alert.address, config.Alert.controlAddr) # send an alert message msg = "this is my message 1" thread.sendAlert(10, msg = msg) # wait for the alert to arrive while len(handler.queue) == 0: time.sleep(0.3) print "%s waiting for alert to arrive ..." % inspect.stack()[0][3] self.assertEqual(len(handler.queue), 1) alert = handler.queue[0] self.assertEqual(alert["Component"], "test2") self.assertEqual(alert["Level"], 10) self.assertEqual(alert["Source"], thread.__class__.__name__) self.assertEqual(alert["Details"]["msg"], msg) thread.sender.unregister() receiver.shutdown()
def testMySQLCPUPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) try: poller = MySQLCPUPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception, ex: self.fail("%s: exception: %s" % (self.testName, ex))
def testMySQLCPUPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) try: poller = MySQLCPUPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception, ex: self.fail("%s: exception: %s" % (self.testName, ex))
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)
def testComponentsCPUPollerPossiblyOnLiveAgent(self): """ If there is currently running agent upon WMAGENT_CONFIG configuration, then the test will pick up live processes and poll them. """ # check if the live agent configuration was loaded (above this class) if globals().has_key("config"): self.config = config # AlertProcessor values - values for Level soft, resp. critical # are also needed by this AlertGenerator test self.config.component_("AlertProcessor") self.config.AlertProcessor.componentDir = "/tmp" self.config.AlertProcessor.section_("critical") self.config.AlertProcessor.section_("soft") self.config.AlertProcessor.critical.level = 5 self.config.AlertProcessor.soft.level = 0 self.config.component_("AlertGenerator") self.config.AlertGenerator.componentDir = "/tmp" self.config.section_("Alert") self.config.Alert.address = "tcp://127.0.0.1:6557" self.config.Alert.controlAddr = "tcp://127.0.0.1:6559" self.config.AlertGenerator.section_("componentsCPUPoller") else: self.config = getConfig("/tmp") self.config.AlertGenerator.componentsCPUPoller.soft = 70 self.config.AlertGenerator.componentsCPUPoller.critical = 80 self.config.AlertGenerator.componentsCPUPoller.pollInterval = 0.2 self.config.AlertGenerator.componentsCPUPoller.period = 0.3 # generator has already been instantiated, but need another one # with just defined configuration # mock generator instance to communicate some configuration values self.generator = utils.AlertGeneratorMock(self.config) handler, receiver = utils.setUpReceiver(self.generator.config.Alert.address, self.generator.config.Alert.controlAddr) numMeasurements = self.config.AlertGenerator.componentsCPUPoller.period / self.config.AlertGenerator.componentsCPUPoller.pollInterval poller = ComponentsCPUPoller(self.config.AlertGenerator.componentsCPUPoller, self.generator) # inject own input sample data provider thresholdToTest = self.config.AlertGenerator.componentsCPUPoller.soft # there is in fact input argument in this case which needs be ignored poller.sample = lambda proc_: random.randint(thresholdToTest - 10, thresholdToTest) poller.start() self.assertTrue(poller.is_alive()) # no alert shall arrive time.sleep(5 * self.config.AlertGenerator.componentsCPUPoller.period) poller.terminate() receiver.shutdown() self.assertFalse(poller.is_alive())
def setUp(self): self.testInit = TestInit(__file__) self.testInit.setLogging(logLevel = logging.DEBUG) self.testDir = self.testInit.generateWorkDir() self.config = getConfig(self.testDir) # mock generator instance to communicate some configuration values self.generator = utils.AlertGeneratorMock(self.config) self.testProcesses = [] self.testComponentDaemonXml = "/tmp/TestComponent/Daemon.xml"
def testMySQLPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) # take for instance mysqlCPUPoller configuration here, just need # appropriate attributes set try: poller = MySQLPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception, ex: self.fail("%s: exception: %s" % (self.testName, ex))
def testMySQLPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) # take for instance mysqlCPUPoller configuration here, just need # appropriate attributes set try: poller = MySQLPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception, ex: self.fail("%s: exception: %s" % (self.testName, ex))
def setUp(self): self.testInit = TestInit(__file__) self.testInit.setLogging(logLevel = logging.DEBUG) self.testInit.setDatabaseConnection() self.testDir = self.testInit.generateWorkDir() self.config = getConfig(self.testDir) # mock generator instance to communicate some configuration values self.generator = utils.AlertGeneratorMock(self.config) self.testProcesses = [] self.testName = self.id().split('.')[-1]
def testDirectorySizePollerUnitTest(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) poller = DirectorySizePoller(config.AlertGenerator.mysqlDbSizePoller, generator, unitSelection = 1) # kilobytes poller.sender = lambda alert: 1 + 1 self.assertEqual(poller._currSizeUnit, "kB") self.assertEqual(poller._prefixBytesFactor, 1024) # this actually tests the real sample method poller._dbDirectory = "/dev" poller.check() # calls sample() automatically
def testComponentsPollerBasic(self): """ Test ComponentsPoller class. Beware of different process context in real running. """ 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 = 90 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] # period during which measurements are collected before evaluating for possible alert triggering config.AlertGenerator.bogusPoller.period = 10 # need to create some temp directory, real process and it's # Daemon.xml so that is looks like agents component process # and check back the information p = utils.getProcess() self.testProcesses.append(p) while not p.is_alive(): time.sleep(0.2) config.component_("TestComponent") d = os.path.dirname(self.testComponentDaemonXml) config.TestComponent.componentDir = d if not os.path.exists(d): os.mkdir(d) f = open(self.testComponentDaemonXml, 'w') f.write(utils.daemonXmlContent % dict(PID_TO_PUT = p.pid)) f.close() generator = utils.AlertGeneratorMock(config) poller = ComponentsPoller(config.AlertGenerator.bogusPoller, generator) # only 1 component should have valid workDir with proper Daemon.xml content # other components present in the configuration (AlertProcessor, AlertGenerator) # should have been ignored self.assertEqual(len(poller._components), 1) pd = poller._components[0] self.assertEqual(pd.pid, p.pid) self.assertEqual(pd.name, "TestComponent") self.assertEqual(len(pd.children), 0) self.assertEqual(len(poller._compMeasurements), 1) mes = poller._compMeasurements[0] numMeasurements = round(config.AlertGenerator.bogusPoller.period / config.AlertGenerator.bogusPoller.pollInterval, 0) self.assertEqual(mes._numOfMeasurements, numMeasurements) shutil.rmtree(d)
def testDirectorySizePollerUnitTest(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) poller = DirectorySizePoller(config.AlertGenerator.mysqlDbSizePoller, generator, unitSelection=1) # kilobytes poller.sender = lambda alert: 1 + 1 self.assertEqual(poller._currSizeUnit, "kB") self.assertEqual(poller._prefixBytesFactor, 1024) # this actually tests the real sample method poller._dbDirectory = "/dev" poller.check() # calls sample() automatically
def testMySQLCPUPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) try: poller = MySQLCPUPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) self.assertEqual(len(poller._measurements), 0) poller.check() # assuming MySQL server is running, check that 1 sensible measurement value was collected self.assertEqual(len(poller._measurements), 1) self.assertTrue(isinstance(poller._measurements[0], types.FloatType))
def testComponentsPollerBasic(self): """ Test ComponentsPoller class. Beware of different process context in real running. """ 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 = 90 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] # period during which measurements are collected before evaluating for possible alert triggering config.AlertGenerator.bogusPoller.period = 10 # need to create some temp directory, real process and it's # Daemon.xml so that is looks like agents component process # and check back the information pid = os.getpid() config.component_("TestComponent") d = os.path.dirname(self.testComponentDaemonXml) config.TestComponent.componentDir = d if not os.path.exists(d): os.mkdir(d) f = open(self.testComponentDaemonXml, 'w') f.write(utils.daemonXmlContent % dict(PID_TO_PUT=pid)) f.close() generator = utils.AlertGeneratorMock(config) poller = ComponentsPoller(config.AlertGenerator.bogusPoller, generator) # only 1 component should have valid workDir with proper Daemon.xml content # other components present in the configuration (AlertProcessor, AlertGenerator) # should have been ignored self.assertEqual(len(poller._components), 1) pd = poller._components[0] self.assertEqual(pd.pid, pid) self.assertEqual(pd.name, "TestComponent") self.assertEqual(len(pd.children), 0) self.assertEqual(len(poller._compMeasurements), 1) mes = poller._compMeasurements[0] numMeasurements = round( config.AlertGenerator.bogusPoller.period / config.AlertGenerator.bogusPoller.pollInterval, 0) self.assertEqual(mes._numOfMeasurements, numMeasurements) shutil.rmtree(d)
def testMySQLCPUPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) try: poller = MySQLCPUPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) self.assertEqual(len(poller._measurements), 0) poller.check() # assuming MySQL server is running, check that 1 sensible measurement value was collected self.assertEqual(len(poller._measurements), 1) self.assertTrue(isinstance(poller._measurements[0], types.FloatType))
def testMySQLDbSizePollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) try: poller = MySQLDbSizePoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) poller.check() # test failing during set up poller = MySQLDbSizePoller(config.AlertGenerator.mysqlCPUPoller, generator) poller._query = "nonsense query" # this will fail on the above query self.assertRaises(Exception, poller._getDbDir) poller.check()
def testAlertGeneratorCouchDbSizePollerBasic(self): config = getConfig("/tmp") try: poller = CouchDbSizePoller(config.AlertGenerator.couchDbSizePoller, self.generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) poller.check() # -> on real system dir may result in permission denied poller._dbDirectory = "/dev" poller.check() # -> OK # test failing during set up poller = CouchDbSizePoller(config.AlertGenerator.couchDbSizePoller, self.generator) poller._query = "nonsense query" # this will fail on the above query self.assertRaises(Exception, poller._getDbDir) poller.check()
def testMySQLDbSizePollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) try: poller = MySQLDbSizePoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) poller.check() # test failing during set up poller = MySQLDbSizePoller(config.AlertGenerator.mysqlCPUPoller, generator) poller._query = "nonsense query" # this will fail on the above query self.assertRaises(Exception, poller._getDbDir) poller.check()
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)
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)
def testBasePollerHandleFailedPolling(self): config = getConfig("/tmp") # create some non-sence config section. just need a bunch of values defined config.AlertGenerator.section_("bogusPoller") config.AlertGenerator.bogusPoller.soft = 5 # [percent] config.AlertGenerator.bogusPoller.critical = 50 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] config.AlertGenerator.bogusPoller.period = 10 generator = utils.AlertGeneratorMock(config) poller = BasePoller(config.AlertGenerator.bogusPoller, generator) ex = Exception("test exception") class Sender(object): def __call__(self, alert): self.alert = alert poller.sender = Sender() poller._handleFailedPolling(ex) self.assertEqual(poller.sender.alert["Source"], "BasePoller")
def testBasePollerBasic(self): config = getConfig("/tmp") # create some non-sence config section. just need a bunch of values defined config.AlertGenerator.section_("bogusPoller") config.AlertGenerator.bogusPoller.soft = 5 # [percent] config.AlertGenerator.bogusPoller.critical = 50 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] config.AlertGenerator.bogusPoller.period = 10 generator = utils.AlertGeneratorMock(config) poller = BasePoller(config.AlertGenerator.bogusPoller, generator) # define dummy check method poller.check = lambda: 1+1 p = Process(target = poller.poll, args = ()) p.start() # poller now runs time.sleep(0.1) p.terminate() poller.shutdown()
def testBasePollerBasic(self): config = getConfig("/tmp") # create some non-sence config section. just need a bunch of values defined config.AlertGenerator.section_("bogusPoller") config.AlertGenerator.bogusPoller.soft = 5 # [percent] config.AlertGenerator.bogusPoller.critical = 50 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] config.AlertGenerator.bogusPoller.period = 10 generator = utils.AlertGeneratorMock(config) poller = BasePoller(config.AlertGenerator.bogusPoller, generator) # define dummy check method poller.check = lambda: 1+1 poller.start() # poller now runs time.sleep(config.AlertGenerator.bogusPoller.pollInterval * 2) poller.terminate() while poller.is_alive(): time.sleep(0.2) print "%s waiting for test poller to terminate" % inspect.stack()[0][3]
def testMySQLPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) # take for instance mysqlCPUPoller configuration here, just need # appropriate attributes set try: poller = MySQLPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) # this class would not have defined polling sample function, give it one poller.sample = lambda proc: float(12) self.assertEqual(len(poller._measurements), 0) poller.check() self.assertEqual(len(poller._measurements), 1) self.assertEqual(poller._measurements[0], 12) # test handling of a non-existing process MySQLPoller._getProcessPID = lambda inst: 1212121212 self.assertRaises(Exception, MySQLPoller, config.AlertGenerator.mysqlCPUPoller, generator)
def testBasePollerHandleFailedPolling(self): config = getConfig("/tmp") # create some non-sence config section. just need a bunch of values defined config.AlertGenerator.section_("bogusPoller") config.AlertGenerator.bogusPoller.soft = 5 # [percent] config.AlertGenerator.bogusPoller.critical = 50 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] config.AlertGenerator.bogusPoller.period = 10 generator = utils.AlertGeneratorMock(config) poller = BasePoller(config.AlertGenerator.bogusPoller, generator) ex = Exception("test exception") class Sender(object): def __call__(self, alert): self.alert = alert poller.sender = Sender() poller._handleFailedPolling(ex) self.assertEqual(poller.sender.alert["Source"], "BasePoller")
def testBasePollerBasic(self): config = getConfig("/tmp") # create some non-sence config section. just need a bunch of values defined config.AlertGenerator.section_("bogusPoller") config.AlertGenerator.bogusPoller.soft = 5 # [percent] config.AlertGenerator.bogusPoller.critical = 50 # [percent] config.AlertGenerator.bogusPoller.pollInterval = 2 # [second] config.AlertGenerator.bogusPoller.period = 10 generator = utils.AlertGeneratorMock(config) poller = BasePoller(config.AlertGenerator.bogusPoller, generator) # define dummy check method poller.check = lambda: 1 + 1 poller.start() # poller now runs time.sleep(config.AlertGenerator.bogusPoller.pollInterval * 2) poller.terminate() while poller.is_alive(): time.sleep(0.2) print "%s waiting for test poller to terminate" % inspect.stack( )[0][3]
def testMySQLPollerBasic(self): config = getConfig("/tmp") generator = utils.AlertGeneratorMock(config) # take for instance mysqlCPUPoller configuration here, just need # appropriate attributes set try: poller = MySQLPoller(config.AlertGenerator.mysqlCPUPoller, generator) except Exception as ex: self.fail("%s: exception: %s" % (self.testName, ex)) # this class would not have defined polling sample function, give it one poller.sample = lambda proc: float(12) self.assertEqual(len(poller._measurements), 0) poller.check() self.assertEqual(len(poller._measurements), 1) self.assertEqual(poller._measurements[0], 12) # test handling of a non-existing process MySQLPoller._getProcessPID = lambda inst: 1212121212 self.assertRaises(Exception, MySQLPoller, config.AlertGenerator.mysqlCPUPoller, generator)
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)
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)
def testAlertGeneratorCouchDbSizePollerBasic(self): config = getConfig("/tmp") try: poller = CouchDbSizePoller(config.AlertGenerator.couchDbSizePoller, self.generator) except Exception, ex: self.fail("%s: exception: %s" % (self.testName, ex))
def setUp(self): self.testInit = TestInit(__file__) self.testInit.setLogging(logLevel=logging.DEBUG) self.testDir = self.testInit.generateWorkDir() self.config = getConfig(self.testDir) self.testComponentDaemonXml = "/tmp/TestComponent/Daemon.xml"