Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of special on-demand macros (isvalidtime & nextvalidtime) #1452

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions shinken/macroresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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):
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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])
Expand Down
7 changes: 0 additions & 7 deletions shinken/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
except ImportError:
NodeSet = None

from shinken.macroresolver import MacroResolver
from shinken.log import logger

#from memoized import memoized
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions test/etc/standard/timeperiods.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
85 changes: 83 additions & 2 deletions test/test_macroresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down