/usr/lib/python2.7/dist-packages/shinken/downtime.py is in shinken-common 2.0.3-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
# Gregory Starck, g.starck@gmail.com
# Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
import datetime, time
from shinken.comment import Comment
from shinken.property import BoolProp, IntegerProp, StringProp
from shinken.brok import Brok
from shinken.log import logger
""" Schedules downtime for a specified service. If the "fixed" argument is set
to one (1), downtime will start and end at the times specified by the
"start" and "end" arguments.
Otherwise, downtime will begin between the "start" and "end" times and last
for "duration" seconds. The "start" and "end" arguments are specified
in time_t format (seconds since the UNIX epoch). The specified service
downtime can be triggered by another downtime entry if the "trigger_id"
is set to the ID of another scheduled downtime entry.
Set the "trigger_id" argument to zero (0) if the downtime for the
specified service should not be triggered by another downtime entry.
"""
class Downtime:
id = 1
# Just to list the properties we will send as pickle
# so to others daemons, so all but NOT REF
properties = {
'activate_me': StringProp(default=[]),
'entry_time': IntegerProp(default=0, fill_brok=['full_status']),
'fixed': BoolProp(default=True, fill_brok=['full_status']),
'start_time': IntegerProp(default=0, fill_brok=['full_status']),
'duration': IntegerProp(default=0, fill_brok=['full_status']),
'trigger_id': IntegerProp(default=0),
'end_time': IntegerProp(default=0, fill_brok=['full_status']),
'real_end_time': IntegerProp(default=0),
'author': StringProp(default='', fill_brok=['full_status']),
'comment': StringProp(default=''),
'is_in_effect': BoolProp(default=False),
'has_been_triggered': BoolProp(default=False),
'can_be_deleted': BoolProp(default=False),
# TODO: find a very good way to handle the downtime "ref".
# ref must effectively not be in properties because it points
# onto a real object.
#'ref': None
}
def __init__(self, ref, start_time, end_time, fixed, trigger_id, duration, author, comment):
now = datetime.datetime.now()
self.id = int(time.mktime(now.timetuple())*1e6 + now.microsecond)
self.__class__.id = self.id + 1
self.ref = ref # pointer to srv or host we are apply
self.activate_me = [] # The other downtimes i need to activate
self.entry_time = int(time.time())
self.fixed = fixed
self.start_time = start_time
self.duration = duration
self.trigger_id = trigger_id
if self.trigger_id != 0: # triggered plus fixed makes no sense
self.fixed = False
self.end_time = end_time
if fixed:
self.duration = end_time - start_time
# This is important for flexible downtimes. Here start_time and
# end_time mean: in this time interval it is possible to trigger
# the beginning of the downtime which lasts for duration.
# Later, when a non-ok event happens, real_end_time will be
# recalculated from now+duration
# end_time will be displayed in the web interface, but real_end_time
# is used internally
self.real_end_time = end_time
self.author = author
self.comment = comment
self.is_in_effect = False # fixed: start_time has been reached, flexible: non-ok checkresult
self.has_been_triggered = False # another downtime has triggered me
self.can_be_deleted = False
self.add_automatic_comment()
def __str__(self):
if self.is_in_effect == True:
active = "active"
else:
active = "inactive"
if self.fixed == True:
type = "fixed"
else:
type = "flexible"
return "%s %s Downtime id=%d %s - %s" % (active, type, self.id, time.ctime(self.start_time), time.ctime(self.end_time))
def trigger_me(self, other_downtime):
self.activate_me.append(other_downtime)
def in_scheduled_downtime(self):
return self.is_in_effect
# The referenced host/service object enters now a (or another) scheduled
# downtime. Write a log message only if it was not already in a downtime
def enter(self):
res = []
self.is_in_effect = True
if self.fixed == False:
now = time.time()
self.real_end_time = now + self.duration
if self.ref.scheduled_downtime_depth == 0:
self.ref.raise_enter_downtime_log_entry()
self.ref.create_notifications('DOWNTIMESTART')
self.ref.scheduled_downtime_depth += 1
self.ref.in_scheduled_downtime = True
for dt in self.activate_me:
res.extend(dt.enter())
return res
# The end of the downtime was reached.
def exit(self):
res = []
if self.is_in_effect == True:
# This was a fixed or a flexible+triggered downtime
self.is_in_effect = False
self.ref.scheduled_downtime_depth -= 1
if self.ref.scheduled_downtime_depth == 0:
self.ref.raise_exit_downtime_log_entry()
self.ref.create_notifications('DOWNTIMEEND')
self.ref.in_scheduled_downtime = False
else:
# This was probably a flexible downtime which was not triggered
# In this case it silently disappears
pass
self.del_automatic_comment()
self.can_be_deleted = True
# when a downtime ends and the service was critical
# a notification is sent with the next critical check
# So we should set a flag here which signals consume_result
# to send a notification
self.ref.in_scheduled_downtime_during_last_check = True
return res
# A scheduled downtime was prematurely canceled
def cancel(self):
res = []
self.is_in_effect = False
self.ref.scheduled_downtime_depth -= 1
if self.ref.scheduled_downtime_depth == 0:
self.ref.raise_cancel_downtime_log_entry()
self.ref.in_scheduled_downtime = False
self.del_automatic_comment()
self.can_be_deleted = True
self.ref.in_scheduled_downtime_during_last_check = True
# Nagios does not notify on canceled downtimes
#res.extend(self.ref.create_notifications('DOWNTIMECANCELLED'))
# Also cancel other downtimes triggered by me
for dt in self.activate_me:
res.extend(dt.cancel())
return res
# Scheduling a downtime creates a comment automatically
def add_automatic_comment(self):
if self.fixed == True:
text = "This %s has been scheduled for fixed downtime from %s to %s. Notifications for the %s will not be sent out during that time period." % (self.ref.my_type, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.start_time)), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.end_time)), self.ref.my_type)
else:
hours, remainder = divmod(self.duration, 3600)
minutes, seconds = divmod(remainder, 60)
text = "This %s has been scheduled for flexible downtime starting between %s and %s and lasting for a period of %d hours and %d minutes. Notifications for the %s will not be sent out during that time period." % (self.ref.my_type, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.start_time)), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.end_time)), hours, minutes, self.ref.my_type)
if self.ref.my_type == 'host':
comment_type = 1
else:
comment_type = 2
c = Comment(self.ref, False, "(Nagios Process)", text, comment_type, 2, 0, False, 0)
self.comment_id = c.id
self.extra_comment = c
self.ref.add_comment(c)
def del_automatic_comment(self):
# Extra comment can be None if we load it from a old version of Shinken
# TODO: remove it in a future version when every one got upgrade
if self.extra_comment is not None:
self.extra_comment.can_be_deleted = True
#self.ref.del_comment(self.comment_id)
# Fill data with info of item by looking at brok_type
# in props of properties or running_properties
def fill_data_brok_from(self, data, brok_type):
cls = self.__class__
# Now config properties
for prop, entry in cls.properties.items():
if hasattr(prop, 'fill_brok'):
if brok_type in entry['fill_brok']:
data[prop] = getattr(self, prop)
# Get a brok with initial status
def get_initial_status_brok(self):
data = {'id': self.id}
self.fill_data_brok_from(data, 'full_status')
b = Brok('downtime_raise', data)
return b
# Call by pickle for dataify the downtime
# because we DO NOT WANT REF in this pickleisation!
def __getstate__(self):
cls = self.__class__
# id is not in *_properties
res = {'id': self.id}
for prop in cls.properties:
if hasattr(self, prop):
res[prop] = getattr(self, prop)
return res
# Inverted function of getstate
def __setstate__(self, state):
cls = self.__class__
# Maybe it's not a dict but a list like in the old 0.4 format
# so we should call the 0.4 function for it
if isinstance(state, list):
self.__setstate_deprecated__(state)
return
self.id = state['id']
for prop in cls.properties:
if prop in state:
setattr(self, prop, state[prop])
if self.id >= cls.id:
cls.id = self.id + 1
# This function is DEPRECATED and will be removed in a future version of
# Shinken. It should not be useful any more after a first load/save pass.
# Inversed function of getstate
def __setstate_deprecated__(self, state):
cls = self.__class__
# Check if the len of this state is like the previous,
# if not, we will do errors!
# -1 because of the 'id' prop
if len(cls.properties) != (len(state) - 1):
logger.info("Passing downtime")
return
self.id = state.pop()
for prop in cls.properties:
val = state.pop()
setattr(self, prop, val)
if self.id >= cls.id:
cls.id = self.id + 1
|