diff --git a/shinken/macroresolver.py b/shinken/macroresolver.py index 57b3983d69..f6772f8d4e 100644 --- a/shinken/macroresolver.py +++ b/shinken/macroresolver.py @@ -35,7 +35,7 @@ import time from shinken.borg import Borg - +from shinken.objects.timeperiod import Timeperiod class MacroResolver(Borg): """Please Add a Docstring to describe the class here""" @@ -79,6 +79,11 @@ class MacroResolver(Borg): 'SERVICEACKCOMMENT' ] + validtime_macros = { + 'ISVALIDTIME': ('', Timeperiod.is_time_valid), + 'NEXTVALIDTIME': ('0', Timeperiod.get_next_valid_time_from_t) + } + # This must be called ONCE. It just put links for elements # by scheduler def init(self, conf): @@ -101,6 +106,9 @@ def init(self, conf): self.contactgroups = conf.contactgroups self.lists_on_demand.append(self.contactgroups) self.illegal_macro_output_chars = conf.illegal_macro_output_chars + self.timeperiods = conf.timeperiods + self.lists_on_demand.append(self.timeperiods) + # Try cache :) #self.cache = {} @@ -324,8 +332,29 @@ def _resolve_ondemand(self, macro, data): elts = macro.split(':') nb_parts = len(elts) macro_name = elts[0] + + # Look first for the special validtime macros + validtime_macro = self.validtime_macros.get(macro_name) + if validtime_macro: + # Get the right infos + failed_output = validtime_macro[0] + func_to_call = validtime_macro[1] + + timeperiod_arg = elts[1] + timestamp = time.time() + # If there a timestamp specifed ? + if nb_parts == 3: + try: + timestamp = float(elts[2]) + except ValueError: + return failed_output + + for timeperiod in self.timeperiods: + if timeperiod.get_name() == timeperiod_arg: + return str(int(func_to_call(timeperiod, timestamp))) + return failed_output # Len 3 == service, 2 = all others types... - if nb_parts == 3: + elif nb_parts == 3: val = '' #print "Got a Service on demand asking...", elts (host_name, service_description) = (elts[1], elts[2]) diff --git a/shinken/util.py b/shinken/util.py index 9748378200..e590bf728c 100644 --- a/shinken/util.py +++ b/shinken/util.py @@ -35,7 +35,6 @@ except ImportError: NodeSet = None -from shinken.macroresolver import MacroResolver from shinken.log import logger #from memoized import memoized @@ -353,12 +352,6 @@ def to_svc_hst_distinct_lists(ref, tab): return r -# Will expand the value with macros from the -# host/service ref before brok it -def expand_with_macros(ref, value): - return MacroResolver().resolve_simple_macros_in_string(value, ref.get_data_for_checks()) - - # Just get the string name of the object # (like for realm) def get_obj_name(obj): diff --git a/test/etc/standard/timeperiods.cfg b/test/etc/standard/timeperiods.cfg index bfdb9a832e..fe5ec65dfb 100644 --- a/test/etc/standard/timeperiods.cfg +++ b/test/etc/standard/timeperiods.cfg @@ -9,3 +9,13 @@ define timeperiod{ friday 00:00-24:00 saturday 00:00-24:00 } + +define timeperiod{ + timeperiod_name workhours + alias Normal Work Hours + monday 09:00-17:00 + tuesday 09:00-17:00 + wednesday 09:00-17:00 + thursday 09:00-17:00 + friday 09:00-17:00 + } diff --git a/test/test_macroresolver.py b/test/test_macroresolver.py index 7b5476a449..efca1118d0 100644 --- a/test/test_macroresolver.py +++ b/test/test_macroresolver.py @@ -201,8 +201,89 @@ def test_ondemand_macros(self): com = mr.resolve_command(cc, data) print com self.assertEqual('plugins/nothing you should not pass', com) - - + + + # Look at special on demand macros (ISVALIDTIME and NEXTVALIDTIME) + def test_validtime_ondemand_macros(self): + mr = self.get_mr() + (svc, hst) = self.get_hst_svc() + data = hst.get_data_for_checks() + + # Get the 18 of December 2014 at 15:00, thursday + dec_the_18_15h = int(time.mktime(time.strptime("18 Dec 2014 15:00:00", "%d %b %Y %H:%M:%S"))) + # Get the 18 of December 2014 at 20:00, thursday + dec_the_18_20h = int(time.mktime(time.strptime("18 Dec 2014 20:00:00", "%d %b %Y %H:%M:%S"))) + # Get the 18 of December 2014 at 09:00, friday + dec_the_19_9h = int(time.mktime(time.strptime("19 Dec 2014 09:00:00", "%d %b %Y %H:%M:%S"))) + + + #with a 24x7 timeperiod + data = svc.get_data_for_checks() + dummy_call = "special_macro!$ISVALIDTIME:24x7$" + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing 1', com) + + #with workhours timeperiod and a timestamp (equals to 18 Dec 2014 15:00:00) + data = svc.get_data_for_checks() + dummy_call = "special_macro!$ISVALIDTIME:workhours:{0}$".format(dec_the_18_15h) + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing 1', com) + + #with a timestamp off workhours (equals to 18 Dec 2014 20:00:00) + data = svc.get_data_for_checks() + dummy_call = "special_macro!$ISVALIDTIME:workhours:{0}$".format(dec_the_18_20h) + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing 0', com) + + # with bad timestamp + data = svc.get_data_for_checks() + dummy_call = "special_macro!$ISVALIDTIME:workhours:t$" + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing', com) + + # with a 24x7 timeperiod and a timestamp (equals to 18 Dec 2014 15:00:00) + # Next validtime will be equal to the timestamp + data = svc.get_data_for_checks() + dummy_call = "special_macro!$NEXTVALIDTIME:24x7:{0}$".format(dec_the_18_15h) + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing {0}'.format(dec_the_18_15h), com) + + # with workhours timeperiod and a timestamp (equals to 18 Dec 2014 15:00:00) + # Next validtime will be equal to the timestamp + data = svc.get_data_for_checks() + dummy_call = "special_macro!$NEXTVALIDTIME:workhours:{0}$".format(dec_the_18_15h) + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing {0}'.format(dec_the_18_15h), com) + + # with a timestamp off workhours (equals to 18 Dec 2014 20:00:00) + # Next valid time will be 1418976000 (19 Dec 2014 09:00:00) + data = svc.get_data_for_checks() + dummy_call = "special_macro!$NEXTVALIDTIME:workhours:{0}$".format(dec_the_18_20h) + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing {0}'.format(dec_the_19_9h), com) + + # with bad timestamp + data = svc.get_data_for_checks() + dummy_call = "special_macro!$NEXTVALIDTIME:workhours:string$" + cc = CommandCall(self.conf.commands, dummy_call) + com = mr.resolve_command(cc, data) + print com + self.assertEqual('plugins/nothing 0', com) + # Look at on demand macros def test_hostadressX_macros(self):