def test_human_timestamp_format(self): """test output using the human timestamp format""" alignak_logger.setLevel(INFO) self._collector = Collector() sys.stdout = StringIO() alignak_logger.handlers[0].stream = sys.stdout alignak_logger.load_obj(self._collector) alignak_logger.set_human_format(True) if isinstance(alignak_logger.handlers[0], ColorStreamHandler): loglist = self.generic_tst(alignak_logger.info, 'Some log-message', [1, 1], [r'^\[.+?\] INFO: \[Alignak\] Some log-message$', r'^\x1b\[35m\[.+?\] INFO: \[Alignak\] Some log-message\x1b\[0m$']) else: loglist = self.generic_tst(alignak_logger.info, 'Some log-message', [1, 1], [r'^\[.+?\] INFO: \[Alignak\] Some log-message$', r'^\[.+?\] INFO: \[Alignak\] Some log-message$']) times = loglist[1][0].split(' INFO: ', 1)[0] _, time2 = times.rsplit('[', 1) time.strptime(time2.rsplit(']')[0], '%a %b %d %H:%M:%S %Y') logger.set_human_format(False)
def test_human_timestamp_format(self): """test output using the human timestamp format""" alignak_logger.setLevel(INFO) self._collector = Collector() sys.stdout = StringIO() alignak_logger.handlers[0].stream = sys.stdout alignak_logger.load_obj(self._collector) alignak_logger.set_human_format(True) if isinstance(alignak_logger.handlers[0], ColorStreamHandler): loglist = self.generic_tst(alignak_logger.info, 'Some log-message', [ 1, 1 ], [ r'^\[.+?\] INFO: \[Alignak\] Some log-message$', r'^\x1b\[35m\[.+?\] INFO: \[Alignak\] Some log-message\x1b\[0m$' ]) else: loglist = self.generic_tst( alignak_logger.info, 'Some log-message', [1, 1], [ r'^\[.+?\] INFO: \[Alignak\] Some log-message$', r'^\[.+?\] INFO: \[Alignak\] Some log-message$' ]) times = loglist[1][0].split(' INFO: ', 1)[0] _, time2 = times.rsplit('[', 1) time.strptime(time2.rsplit(']')[0], '%a %b %d %H:%M:%S %Y') logger.set_human_format(False)
def test_basic_logging_log(self): sys.stdout = StringIO() self._collector = Collector() sh = StreamHandler(sys.stdout) sh.setFormatter(DEFAULT_FORMATTER) alignak_logger.handlers = [] alignak_logger.addHandler(sh) alignak_logger.load_obj(self._collector) alignak_logger.log_set = True alignak_logger.setLevel(DEBUG) self.generic_tst(lambda x: naglog_result('info', x), 'Some log-message', [1, 1], [r'^\[\d+\] Some log-message\n$', r'^\[\d+\] Some log-message$'])
def test_basic_logging_log(self): sys.stdout = StringIO() self._collector = Collector() sh = StreamHandler(sys.stdout) sh.setFormatter(DEFAULT_FORMATTER) alignak_logger.handlers = [] alignak_logger.addHandler(sh) alignak_logger.load_obj(self._collector) alignak_logger.log_set = True alignak_logger.setLevel(DEBUG) self.generic_tst( lambda x: naglog_result('info', x), 'Some log-message', [1, 1], [r'^\[\d+\] Some log-message\n$', r'^\[\d+\] Some log-message$'])
def test_basic_logging_info_colored(self): alignak_logger.setLevel(INFO) self._collector = Collector() sys.stdout = StringIO() alignak_logger.handlers[0].stream = sys.stdout alignak_logger.load_obj(self._collector) if isinstance(alignak_logger.handlers[0], ColorStreamHandler): self.generic_tst(alignak_logger.info, 'Some log-message', [1, 1], [ r'^\[.+?\] INFO: \[Alignak\] Some log-message$', r'^\x1b\[35m\[.+?\] INFO: \[Alignak\] Some log-message\x1b\[0m$' ]) else: self.generic_tst(alignak_logger.info, 'Some log-message', [1, 1], [ r'^\[.+?\] INFO:\s+Some log-message$', r'^\[.+?\] INFO:\s+Some log-message$' ])
def test_basic_logging_log(self): sys.stdout = StringIO() self._collector = Collector() sh = StreamHandler(sys.stdout) sh.setFormatter(DEFAULT_FORMATTER) alignak_logger.handlers = [] alignak_logger.addHandler(sh) alignak_logger.load_obj(self._collector) alignak_logger.log_set = True logfile = NamedTemporaryFile("w", delete=False) logfile.close() self.logfile_name = logfile.name alignak_logger.register_local_log(logfile.name, purge_buffer=False) alignak_logger.setLevel(DEBUG) self.generic_tst(lambda x: naglog_result('info', x), 'Some log-message', [1, 1, 1], ['', r'^\[\d+\] Some log-message$', r'^\[\d+\] Some log-message$'])
def test_basic_logging_info_colored(self): alignak_logger.setLevel(INFO) self._collector = Collector() sys.stdout = StringIO() alignak_logger.handlers[0].stream = sys.stdout alignak_logger.load_obj(self._collector) if isinstance(alignak_logger.handlers[0], ColorStreamHandler): self.generic_tst(alignak_logger.info, 'Some log-message', [1, 1], [r'^\[.+?\] INFO: \[Alignak\] Some log-message$', r'^\x1b\[35m\[.+?\] INFO: \[Alignak\] Some log-message\x1b\[0m$']) else: self.generic_tst(alignak_logger.info, 'Some log-message', [1, 1], [r'^\[.+?\] INFO:\s+Some log-message$', r'^\[.+?\] INFO:\s+Some log-message$'])
def test_basic_logging_log(self): sys.stdout = StringIO() self._collector = Collector() sh = StreamHandler(sys.stdout) sh.setFormatter(DEFAULT_FORMATTER) alignak_logger.handlers = [] alignak_logger.addHandler(sh) alignak_logger.load_obj(self._collector) alignak_logger.log_set = True logfile = NamedTemporaryFile("w", delete=False) logfile.close() self.logfile_name = logfile.name alignak_logger.register_local_log(logfile.name, purge_buffer=False) alignak_logger.setLevel(DEBUG) self.generic_tst( lambda x: naglog_result('info', x), 'Some log-message', [1, 1, 1], ['', r'^\[\d+\] Some log-message$', r'^\[\d+\] Some log-message$'])
def setup_new_conf(self): """Receiver custom setup_new_conf method Implements specific setup for receiver :return: None """ with self.conf_lock: conf = self.new_conf self.new_conf = None self.cur_conf = conf # Got our name from the globals if 'receiver_name' in conf['global']: name = conf['global']['receiver_name'] else: name = 'Unnamed receiver' self.name = name self.api_key = conf['global']['api_key'] self.secret = conf['global']['secret'] self.http_proxy = conf['global']['http_proxy'] self.statsd_host = conf['global']['statsd_host'] self.statsd_port = conf['global']['statsd_port'] self.statsd_prefix = conf['global']['statsd_prefix'] self.statsd_enabled = conf['global']['statsd_enabled'] statsmgr.register(self, self.name, 'receiver', api_key=self.api_key, secret=self.secret, http_proxy=self.http_proxy, statsd_host=self.statsd_host, statsd_port=self.statsd_port, statsd_prefix=self.statsd_prefix, statsd_enabled=self.statsd_enabled) # pylint: disable=E1101 logger.load_obj(self, name) self.direct_routing = conf['global']['direct_routing'] self.accept_passive_unknown_check_results = \ conf['global']['accept_passive_unknown_check_results'] g_conf = conf['global'] # If we've got something in the schedulers, we do not want it anymore for sched_id in conf['schedulers']: already_got = False # We can already got this conf id, but with another address if sched_id in self.schedulers: new_addr = conf['schedulers'][sched_id]['address'] old_addr = self.schedulers[sched_id]['address'] new_port = conf['schedulers'][sched_id]['port'] old_port = self.schedulers[sched_id]['port'] # Should got all the same to be ok :) if new_addr == old_addr and new_port == old_port: already_got = True if already_got: logger.info("[%s] We already got the conf %d (%s)", self.name, sched_id, conf['schedulers'][sched_id]['name']) wait_homerun = self.schedulers[sched_id]['wait_homerun'] actions = self.schedulers[sched_id]['actions'] external_commands = self.schedulers[sched_id]['external_commands'] con = self.schedulers[sched_id]['con'] sched = conf['schedulers'][sched_id] self.schedulers[sched_id] = sched if sched['name'] in g_conf['satellitemap']: sched.update(g_conf['satellitemap'][sched['name']]) proto = 'http' if sched['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, sched['address'], sched['port']) self.schedulers[sched_id]['uri'] = uri if already_got: self.schedulers[sched_id]['wait_homerun'] = wait_homerun self.schedulers[sched_id]['actions'] = actions self.schedulers[sched_id]['external_commands'] = external_commands self.schedulers[sched_id]['con'] = con else: self.schedulers[sched_id]['wait_homerun'] = {} self.schedulers[sched_id]['actions'] = {} self.schedulers[sched_id]['external_commands'] = [] self.schedulers[sched_id]['con'] = None self.schedulers[sched_id]['running_id'] = 0 self.schedulers[sched_id]['active'] = sched['active'] self.schedulers[sched_id]['timeout'] = sched['timeout'] self.schedulers[sched_id]['data_timeout'] = sched['data_timeout'] # Do not connect if we are a passive satellite if self.direct_routing and not already_got: # And then we connect to it :) self.pynag_con_init(sched_id) logger.debug("[%s] Sending us configuration %s", self.name, conf) if not self.have_modules: self.modules = mods = conf['global']['modules'] self.have_modules = True logger.info("We received modules %s ", mods) # Set our giving timezone from arbiter use_timezone = conf['global']['use_timezone'] if use_timezone != 'NOTSET': logger.info("Setting our timezone to %s", use_timezone) os.environ['TZ'] = use_timezone time.tzset()
from logging import ERROR from alignak_tst_utils import safe_print # Modules are by default on the ../modules myself = os.path.abspath(__file__) global modules_dir modules_dir = os.environ.get('ALIGNAK_MODULES_DIR', "modules") class __DUMMY: def add(self, obj): pass logger.load_obj(__DUMMY()) logger.setLevel(ERROR) ############################################################################# # We overwrite the functions time() and sleep() # This way we can modify sleep() so that it immediately returns although # for a following time() it looks like thee was actually a delay. # This massively speeds up the tests. class TimeHacker(object): def __init__(self): self.my_offset = 0 self.my_starttime = time.time() self.my_oldtime = time.time self.original_time_time = time.time
def setup_new_conf(self): """Receiver custom setup_new_conf method Implements specific setup for receiver :return: None """ with self.conf_lock: conf = self.new_conf self.new_conf = None self.cur_conf = conf # Got our name from the globals if 'receiver_name' in conf['global']: name = conf['global']['receiver_name'] else: name = 'Unnamed receiver' self.name = name self.api_key = conf['global']['api_key'] self.secret = conf['global']['secret'] self.http_proxy = conf['global']['http_proxy'] self.statsd_host = conf['global']['statsd_host'] self.statsd_port = conf['global']['statsd_port'] self.statsd_prefix = conf['global']['statsd_prefix'] self.statsd_enabled = conf['global']['statsd_enabled'] statsmgr.register(self, self.name, 'receiver', api_key=self.api_key, secret=self.secret, http_proxy=self.http_proxy, statsd_host=self.statsd_host, statsd_port=self.statsd_port, statsd_prefix=self.statsd_prefix, statsd_enabled=self.statsd_enabled) # pylint: disable=E1101 logger.load_obj(self, name) self.direct_routing = conf['global']['direct_routing'] self.accept_passive_unknown_check_results = \ conf['global']['accept_passive_unknown_check_results'] g_conf = conf['global'] # If we've got something in the schedulers, we do not want it anymore for sched_id in conf['schedulers']: already_got = False # We can already got this conf id, but with another address if sched_id in self.schedulers: new_addr = conf['schedulers'][sched_id]['address'] old_addr = self.schedulers[sched_id]['address'] new_port = conf['schedulers'][sched_id]['port'] old_port = self.schedulers[sched_id]['port'] # Should got all the same to be ok :) if new_addr == old_addr and new_port == old_port: already_got = True if already_got: logger.info("[%s] We already got the conf %d (%s)", self.name, sched_id, conf['schedulers'][sched_id]['name']) wait_homerun = self.schedulers[sched_id]['wait_homerun'] actions = self.schedulers[sched_id]['actions'] external_commands = self.schedulers[sched_id][ 'external_commands'] con = self.schedulers[sched_id]['con'] sched = conf['schedulers'][sched_id] self.schedulers[sched_id] = sched if sched['name'] in g_conf['satellitemap']: sched.update(g_conf['satellitemap'][sched['name']]) proto = 'http' if sched['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, sched['address'], sched['port']) self.schedulers[sched_id]['uri'] = uri if already_got: self.schedulers[sched_id]['wait_homerun'] = wait_homerun self.schedulers[sched_id]['actions'] = actions self.schedulers[sched_id][ 'external_commands'] = external_commands self.schedulers[sched_id]['con'] = con else: self.schedulers[sched_id]['wait_homerun'] = {} self.schedulers[sched_id]['actions'] = {} self.schedulers[sched_id]['external_commands'] = [] self.schedulers[sched_id]['con'] = None self.schedulers[sched_id]['running_id'] = 0 self.schedulers[sched_id]['active'] = sched['active'] self.schedulers[sched_id]['timeout'] = sched['timeout'] self.schedulers[sched_id]['data_timeout'] = sched[ 'data_timeout'] # Do not connect if we are a passive satellite if self.direct_routing and not already_got: # And then we connect to it :) self.pynag_con_init(sched_id) logger.debug("[%s] Sending us configuration %s", self.name, conf) if not self.have_modules: self.modules = mods = conf['global']['modules'] self.have_modules = True logger.info("We received modules %s ", mods) # Set our giving timezone from arbiter use_timezone = conf['global']['use_timezone'] if use_timezone != 'NOTSET': logger.info("Setting our timezone to %s", use_timezone) os.environ['TZ'] = use_timezone time.tzset()
def setup_new_conf(self): """Parse new configuration and initialize all required :return: None """ with self.conf_lock: conf = self.new_conf self.new_conf = None self.cur_conf = conf # Got our name from the globals g_conf = conf['global'] if 'broker_name' in g_conf: name = g_conf['broker_name'] else: name = 'Unnamed broker' self.name = name self.api_key = g_conf['api_key'] self.secret = g_conf['secret'] self.http_proxy = g_conf['http_proxy'] self.statsd_host = g_conf['statsd_host'] self.statsd_port = g_conf['statsd_port'] self.statsd_prefix = g_conf['statsd_prefix'] self.statsd_enabled = g_conf['statsd_enabled'] # We got a name so we can update the logger and the stats global objects # pylint: disable=E1101 logger.load_obj(self, name) statsmgr.register(self, name, 'broker', api_key=self.api_key, secret=self.secret, http_proxy=self.http_proxy, statsd_host=self.statsd_host, statsd_port=self.statsd_port, statsd_prefix=self.statsd_prefix, statsd_enabled=self.statsd_enabled) logger.debug("[%s] Sending us configuration %s", self.name, conf) # If we've got something in the schedulers, we do not # want it anymore # self.schedulers.clear() for sched_id in conf['schedulers']: # Must look if we already have it to do not overdie our broks already_got = False # We can already got this conf id, but with another address if sched_id in self.schedulers: new_addr = conf['schedulers'][sched_id]['address'] old_addr = self.schedulers[sched_id]['address'] new_port = conf['schedulers'][sched_id]['port'] old_port = self.schedulers[sched_id]['port'] # Should got all the same to be ok :) if new_addr == old_addr and new_port == old_port: already_got = True if already_got: broks = self.schedulers[sched_id]['broks'] running_id = self.schedulers[sched_id]['running_id'] else: broks = {} running_id = 0 sched = conf['schedulers'][sched_id] self.schedulers[sched_id] = sched # replacing scheduler address and port by those defined in satellitemap if sched['name'] in g_conf['satellitemap']: sched = dict(sched) # make a copy sched.update(g_conf['satellitemap'][sched['name']]) proto = 'http' if sched['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, sched['address'], sched['port']) self.schedulers[sched_id]['uri'] = uri self.schedulers[sched_id]['broks'] = broks self.schedulers[sched_id]['instance_id'] = sched['instance_id'] self.schedulers[sched_id]['running_id'] = running_id self.schedulers[sched_id]['active'] = sched['active'] self.schedulers[sched_id]['last_connection'] = 0 self.schedulers[sched_id]['timeout'] = sched['timeout'] self.schedulers[sched_id]['data_timeout'] = sched['data_timeout'] logger.info("We have our schedulers: %s ", self.schedulers) # Now get arbiter for arb_id in conf['arbiters']: # Must look if we already have it already_got = arb_id in self.arbiters if already_got: broks = self.arbiters[arb_id]['broks'] else: broks = {} arb = conf['arbiters'][arb_id] self.arbiters[arb_id] = arb # replacing arbiter address and port by those defined in satellitemap if arb['name'] in g_conf['satellitemap']: arb = dict(arb) # make a copy arb.update(g_conf['satellitemap'][arb['name']]) proto = 'http' if arb['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, arb['address'], arb['port']) self.arbiters[arb_id]['uri'] = uri self.arbiters[arb_id]['broks'] = broks self.arbiters[arb_id]['instance_id'] = 0 # No use so all to 0 self.arbiters[arb_id]['running_id'] = 0 self.arbiters[arb_id]['last_connection'] = 0 # We do not connect to the arbiter. Connection hangs logger.info("We have our arbiters: %s ", self.arbiters) # Now for pollers for pol_id in conf['pollers']: # Must look if we already have it already_got = pol_id in self.pollers if already_got: broks = self.pollers[pol_id]['broks'] running_id = self.schedulers[sched_id]['running_id'] else: broks = {} running_id = 0 poll = conf['pollers'][pol_id] self.pollers[pol_id] = poll # replacing poller address and port by those defined in satellitemap if poll['name'] in g_conf['satellitemap']: poll = dict(poll) # make a copy poll.update(g_conf['satellitemap'][poll['name']]) proto = 'http' if poll['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, poll['address'], poll['port']) self.pollers[pol_id]['uri'] = uri self.pollers[pol_id]['broks'] = broks self.pollers[pol_id]['instance_id'] = 0 # No use so all to 0 self.pollers[pol_id]['running_id'] = running_id self.pollers[pol_id]['last_connection'] = 0 # #And we connect to it # self.app.pynag_con_init(pol_id, 'poller') logger.info("We have our pollers: %s", self.pollers) # Now reactionners for rea_id in conf['reactionners']: # Must look if we already have it already_got = rea_id in self.reactionners if already_got: broks = self.reactionners[rea_id]['broks'] running_id = self.schedulers[sched_id]['running_id'] else: broks = {} running_id = 0 reac = conf['reactionners'][rea_id] self.reactionners[rea_id] = reac # replacing reactionner address and port by those defined in satellitemap if reac['name'] in g_conf['satellitemap']: reac = dict(reac) # make a copy reac.update(g_conf['satellitemap'][reac['name']]) proto = 'http' if reac['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, reac['address'], reac['port']) self.reactionners[rea_id]['uri'] = uri self.reactionners[rea_id]['broks'] = broks self.reactionners[rea_id]['instance_id'] = 0 # No use so all to 0 self.reactionners[rea_id]['running_id'] = running_id self.reactionners[rea_id]['last_connection'] = 0 # #And we connect to it # self.app.pynag_con_init(rea_id, 'reactionner') logger.info("We have our reactionners: %s", self.reactionners) # Now receivers for rec_id in conf['receivers']: # Must look if we already have it already_got = rec_id in self.receivers if already_got: broks = self.receivers[rec_id]['broks'] running_id = self.schedulers[sched_id]['running_id'] else: broks = {} running_id = 0 rec = conf['receivers'][rec_id] self.receivers[rec_id] = rec # replacing reactionner address and port by those defined in satellitemap if rec['name'] in g_conf['satellitemap']: rec = dict(rec) # make a copy rec.update(g_conf['satellitemap'][rec['name']]) proto = 'http' if rec['use_ssl']: proto = 'https' uri = '%s://%s:%s/' % (proto, rec['address'], rec['port']) self.receivers[rec_id]['uri'] = uri self.receivers[rec_id]['broks'] = broks self.receivers[rec_id]['instance_id'] = 0 # No use so all to 0 self.receivers[rec_id]['running_id'] = running_id self.receivers[rec_id]['last_connection'] = 0 if not self.have_modules: self.modules = mods = conf['global']['modules'] self.have_modules = True logger.info("We received modules %s ", mods) # Ok now start, or restart them! # Set modules, init them and start external ones self.do_load_modules(self.modules) self.modules_manager.start_external_instances() # Set our giving timezone from arbiter use_timezone = conf['global']['use_timezone'] if use_timezone != 'NOTSET': logger.info("Setting our timezone to %s", use_timezone) os.environ['TZ'] = use_timezone time.tzset() # Connection init with Schedulers for sched_id in self.schedulers: self.pynag_con_init(sched_id, i_type='scheduler') for pol_id in self.pollers: self.pynag_con_init(pol_id, i_type='poller') for rea_id in self.reactionners: self.pynag_con_init(rea_id, i_type='reactionner')
from alignak.daemons.brokerdaemon import Broker from alignak.daemons.arbiterdaemon import Arbiter from alignak.daemons.receiverdaemon import Receiver from logging import ERROR from alignak_tst_utils import safe_print # Modules are by default on the ../modules myself = os.path.abspath(__file__) class __DUMMY: def add(self, obj): pass logger.load_obj(__DUMMY()) logger.setLevel(ERROR) ############################################################################# # We overwrite the functions time() and sleep() # This way we can modify sleep() so that it immediately returns although # for a following time() it looks like thee was actually a delay. # This massively speeds up the tests. class TimeHacker(object): def __init__(self): self.my_offset = 0 self.my_starttime = time.time() self.my_oldtime = time.time self.original_time_time = time.time