Merge branch 'master' of ssh://naparuba@shinken.git.sourceforge.net/gitroot/shinken...
[shinken.git] / shinken / resultmodulation.py
blobcc37e7c450f0c684bbeb93442774ecfe19c9c0c9
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 #The resultmodulation class is used for in scheduler modulation of resulsts
23 #like the return code or the output.
25 import time
27 from shinken.item import Item, Items
28 from shinken.util import to_split
29 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
31 class Resultmodulation(Item):
32 id = 1#0 is always special in database, so we do not take risk here
33 my_type = 'resultmodulation'
35 properties = {'resultmodulation_name': StringProp(),
36 'exit_codes_match': ListProp(
37 default=''),
38 'exit_code_modulation': StringProp(
39 default=None),
40 'modulation_period': StringProp(
41 default=None),
43 running_properties = {}
44 macros = {}
47 #For debugging purpose only (nice name)
48 def get_name(self):
49 return self.resultmodulation_name
52 def clean(self):
53 pass
56 #Make the return code modulation if need
57 def module_return(self, return_code):
58 #Only if in modulation_period of modulation_period == None
59 if self.modulation_period == None or self.modulation_period.is_time_valid(time.time()):
60 #Try to change the exit code only if a new one is defined
61 if self.exit_code_modulation != None:
62 #First with the exit_code_match
63 if return_code in self.exit_codes_match:
64 return_code = self.exit_code_modulation
66 return return_code
69 #We override the pythonize because we have special cases that we do not want
70 #to be do at running
71 def pythonize(self):
72 #First apply Item pythonize
73 super(self.__class__, self).pythonize()
75 #Then very special cases
76 # Intify the exit_codes_match, and make list
77 self.exit_codes_match = [ int(ec) for ec in getattr(self, 'exit_codes_match', []) ]
79 if hasattr(self, 'exit_code_modulation'):
80 self.exit_code_modulation = int(self.exit_code_modulation)
81 else:
82 self.exit_code_modulation = None
85 class Resultmodulations(Items):
86 name_property = "resultmodulation_name"
87 inner_class = Resultmodulation
90 def linkify(self, timeperiods):
91 self.linkify_rm_by_tp(timeperiods)
94 #We just search for each timeperiod the tp
95 #and replace the name by the tp
96 def linkify_rm_by_tp(self, timeperiods):
97 for rm in self:
98 mtp_name = rm.modulation_period
100 #The new member list, in id
101 mtp = timeperiods.find_by_name(mtp_name)
103 rm.modulation_period = mtp