* Fix : remove hook_late_configuration from BaseModule, if a module do not have,...
[shinken.git] / shinken / contactdowntime.py
blobf7f4fd4b4a721e70557594b3388f55b11a30996a
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
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 import time
27 class ContactDowntime:
28 id = 1
30 #Just to list the properties we will send as pickle
31 #so to others daemons, so all but NOT REF
32 properties = {
33 #'activate_me': None,
34 #'entry_time': None,
35 #'fixed': None,
36 'start_time': None,
37 #'duration': None,
38 #'trigger_id': None,
39 'end_time': None,
40 #'real_end_time': None,
41 'author': None,
42 'comment': None,
43 'is_in_effect': None,
44 #'has_been_triggered': None,
45 'can_be_deleted': None,
50 # Scheduler a contactr downtime. It's far mroe easy than a host/service
51 # one because we got a start, and a end. That's all for running.
52 # got also an author and a comment for logging purpose.
53 def __init__(self, ref, start_time, end_time, author, comment):
54 self.id = self.__class__.id
55 self.__class__.id += 1
56 self.ref = ref #pointer to srv or host we are apply
57 self.start_time = start_time
58 self.end_time = end_time
59 self.author = author
60 self.comment = comment
61 self.is_in_effect = False
62 self.can_be_deleted = False
63 #self.add_automatic_comment()
66 # Check if we came into the activation of this downtime
67 def check_activation(self):
68 now = time.time()
69 was_is_in_effect = self.is_in_effect
70 self.is_in_effect = (self.start_time <= now <= self.end_time)
71 print "CHECK ACTIVATION:", self.is_in_effect
73 # Raise a log entry when we get in the downtime
74 if not was_is_in_effect and self.is_in_effect:
75 self.enter()
77 # Same for exit purpose
78 if was_is_in_effect and not self.is_in_effect:
79 self.exit()
82 def in_scheduled_downtime(self):
83 return self.is_in_effect
86 #The referenced host/service object enters now a (or another) scheduled
87 #downtime. Write a log message only if it was not already in a downtime
88 def enter(self):
89 self.ref.raise_enter_downtime_log_entry()
92 #The end of the downtime was reached.
93 def exit(self):
94 self.ref.raise_exit_downtime_log_entry()
95 self.can_be_deleted = True
98 # A scheduled downtime was prematurely cancelled
99 def cancel(self):
100 self.is_in_effect = False
101 self.ref.raise_cancel_downtime_log_entry()
102 self.can_be_deleted = True
106 #Call by picle for dataify the coment
107 #because we DO NOT WANT REF in this pickleisation!
108 def __getstate__(self):
109 # print "Asking a getstate for a downtime on", self.ref.get_dbg_name()
110 cls = self.__class__
111 #id is not in *_properties
112 res = [self.id]
113 for prop in cls.properties:
114 res.append(getattr(self, prop))
115 #We reverse because we want to recreate
116 #By check at properties in the same order
117 res.reverse()
118 return res
121 #Inversed funtion of getstate
122 def __setstate__(self, state):
123 cls = self.__class__
124 self.id = state.pop()
125 for prop in cls.properties:
126 val = state.pop()
127 setattr(self, prop, val)
128 if self.id >= cls.id:
129 cls.id = self.id + 1