Skip to content

Commit d3d6b39

Browse files
committed
Python3 version:
- some cleanings in the monitoring logs: ordering is better (checks before alerts!)
1 parent 879ec89 commit d3d6b39

21 files changed

+425
-309
lines changed

alignak/objects/config.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1577,8 +1577,7 @@ def explode(self):
15771577
# Serviceescalations hostescalations will create new escalations
15781578
self.serviceescalations.explode(self.escalations)
15791579
self.hostescalations.explode(self.escalations)
1580-
self.escalations.explode(self.hosts, self.hostgroups,
1581-
self.contactgroups)
1580+
self.escalations.explode(self.hosts, self.hostgroups, self.contactgroups)
15821581

15831582
# Now the architecture part
15841583
self.realms.explode()

alignak/objects/escalation.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -330,25 +330,27 @@ def linkify_es_by_s(self, services):
330330
:type services: alignak.objects.service.Services
331331
:return: None
332332
"""
333-
for escal in self:
333+
for escalation in self:
334334
# If no host, no hope of having a service
335-
if not (hasattr(escal, 'host_name') and hasattr(escal, 'service_description')):
335+
if not (hasattr(escalation, 'host_name')):
336336
continue
337-
es_hname, sdesc = escal.host_name, escal.service_description
338-
if '' in (es_hname.strip(), sdesc.strip()):
337+
338+
es_hname, sdesc = escalation.host_name, escalation.service_description
339+
if not es_hname.strip() or not sdesc.strip():
339340
continue
341+
340342
for hname in strip_and_uniq(es_hname.split(',')):
341343
if sdesc.strip() == '*':
342344
slist = services.find_srvs_by_hostname(hname)
343345
if slist is not None:
344346
slist = [services[serv] for serv in slist]
345347
for serv in slist:
346-
serv.escalations.append(escal.uuid)
348+
serv.escalations.append(escalation.uuid)
347349
else:
348350
for sname in strip_and_uniq(sdesc.split(',')):
349351
serv = services.find_srv_by_name_and_hostname(hname, sname)
350352
if serv is not None:
351-
serv.escalations.append(escal.uuid)
353+
serv.escalations.append(escalation.uuid)
352354

353355
def linkify_es_by_h(self, hosts):
354356
"""Add each escalation object into host.escalation attribute

alignak/objects/host.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,8 @@ def raise_check_result(self):
600600
elif self.state == 'UNREACHABLE':
601601
log_level = 'warning'
602602
brok = make_monitoring_log(
603-
log_level, 'ACTIVE HOST CHECK: %s;%s;%s;%d;%s' % (
604-
self.get_name(), self.state, self.state_type, self.attempt, self.output
605-
)
603+
log_level, 'ACTIVE HOST CHECK: %s;%s;%d;%s' % (self.get_name(), self.state,
604+
self.attempt, self.output)
606605
)
607606
self.broks.append(brok)
608607

alignak/objects/hostescalation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def explode(self, escalations):
119119
name = getattr(escalation, 'host_name', getattr(escalation, 'hostgroup_name', ''))
120120
creation_dict = {
121121
'escalation_name':
122-
'Generated-HostEscalation-%s-%s' % (name, escalation.uuid)
122+
'Generated-HE-%s-%s' % (name, escalation.uuid)
123123
}
124124
for prop in properties:
125125
if hasattr(escalation, prop):

alignak/objects/item.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def __init__(self, params=None, parsing=True):
237237

238238
# Simply moved all the __ functions near the initialization
239239
def __str__(self): # pragma: no cover
240-
return '<%s name=%r />' % (self.__class__.__name__, self.get_name())
240+
return '<%s name=%s />' % (self.__class__.__name__, self.get_name())
241241
__repr__ = __str__
242242

243243
def init_running_properties(self):

alignak/objects/schedulingitem.py

+58-52
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,6 @@ def schedule(self, hosts, services, timeperiods, macromodulations, checkmodulati
12441244
self.get_full_name(),
12451245
datetime.datetime.fromtimestamp(self.next_chk).strftime('%Y-%m-%d %H:%M:%S'),
12461246
interval, time_add)
1247-
print("-> schedule: %s / %s (interval: %d, added: %d)",
1248-
self.get_full_name(),
1249-
datetime.datetime.fromtimestamp(self.next_chk).strftime('%Y-%m-%d %H:%M:%S'),
1250-
interval, time_add)
12511247
# Get the command to launch, and put it in queue
12521248
return self.launch_check(self.next_chk, hosts, services, timeperiods, macromodulations,
12531249
checkmodulations, checks, force=force)
@@ -1526,7 +1522,7 @@ def update_hard_unknown_phase_state(self):
15261522

15271523
def consume_result(self, chk, notif_period, hosts,
15281524
services, timeperiods, macromodulations, checkmodulations, bi_modulations,
1529-
res_modulations, checks):
1525+
res_modulations, checks, raise_log):
15301526
# pylint: disable=too-many-locals, too-many-arguments
15311527
# pylint: disable=too-many-branches, too-many-statements
15321528
"""Consume a check return and send action in return
@@ -1662,6 +1658,10 @@ def consume_result(self, chk, notif_period, hosts,
16621658
self.last_state_type = self.state_type
16631659
self.return_code = chk.exit_status
16641660

1661+
# Raise the log only when the item information are up-to-date :/
1662+
if raise_log:
1663+
self.raise_check_result()
1664+
16651665
# we change the state, do whatever we are or not in
16661666
# an impact mode, we can put it
16671667
self.state_changed_since_impact = True
@@ -1889,6 +1889,7 @@ def consume_result(self, chk, notif_period, hosts,
18891889
self.get_perfdata_command(hosts, macromodulations, timeperiods)
18901890
# Also snapshot if need :)
18911891
self.get_snapshot(hosts, macromodulations, timeperiods)
1892+
18921893
return []
18931894

18941895
def update_event_and_problem_id(self):
@@ -1977,12 +1978,12 @@ def update_notification_command(self, notif, contact, macromodulations, timeperi
19771978
if cls.enable_environment_macros or notif.enable_environment_macros:
19781979
notif.env = macrosolver.get_env_macros(data)
19791980

1980-
def is_escalable(self, notif, escalations, timeperiods):
1981+
def is_escalable(self, notification, escalations, timeperiods):
19811982
"""Check if a notification can be escalated.
19821983
Basically call is_eligible for each escalation
19831984
1984-
:param notif: notification we would like to escalate
1985-
:type notif: alignak.objects.notification.Notification
1985+
:param notification: notification we would like to escalate
1986+
:type notification: alignak.objects.notification.Notification
19861987
:param escalations: Esclations objects, used to get escalation objects (period)
19871988
:type escalations: alignak.objects.escalation.Escalations
19881989
:param timeperiods: Timeperiods objects, used to get escalation period
@@ -1994,14 +1995,14 @@ def is_escalable(self, notif, escalations, timeperiods):
19941995

19951996
# We search since when we are in notification for escalations
19961997
# that are based on time
1997-
in_notif_time = time.time() - notif.creation_time
1998+
in_notif_time = time.time() - notification.creation_time
19981999

19992000
# Check is an escalation match the current_notification_number
2000-
for escal_id in self.escalations:
2001-
escal = escalations[escal_id]
2002-
escal_period = timeperiods[escal.escalation_period]
2003-
if escal.is_eligible(notif.t_to_go, self.state, notif.notif_nb,
2004-
in_notif_time, cls.interval_length, escal_period):
2001+
for escalation_id in self.escalations:
2002+
escalation = escalations[escalation_id]
2003+
escalation_period = timeperiods[escalation.escalation_period]
2004+
if escalation.is_eligible(notification.t_to_go, self.state, notification.notif_nb,
2005+
in_notif_time, cls.interval_length, escalation_period):
20052006
return True
20062007

20072008
return False
@@ -2030,14 +2031,14 @@ def get_next_notification_time(self, notif, escalations, timeperiods):
20302031
# and then look for currently active notifications, and take notification_interval
20312032
# if filled and less than the self value
20322033
in_notif_time = time.time() - notif.creation_time
2033-
for escal_id in self.escalations:
2034-
escal = escalations[escal_id]
2035-
escal_period = timeperiods[escal.escalation_period]
2036-
if escal.is_eligible(notif.t_to_go, self.state, notif.notif_nb,
2037-
in_notif_time, cls.interval_length, escal_period):
2038-
if escal.notification_interval != -1 and \
2039-
escal.notification_interval < notification_interval:
2040-
notification_interval = escal.notification_interval
2034+
for escalation_id in self.escalations:
2035+
escalation = escalations[escalation_id]
2036+
escalation_period = timeperiods[escalation.escalation_period]
2037+
if escalation.is_eligible(notif.t_to_go, self.state, notif.notif_nb,
2038+
in_notif_time, cls.interval_length, escalation_period):
2039+
if escalation.notification_interval != -1 and \
2040+
escalation.notification_interval < notification_interval:
2041+
notification_interval = escalation.notification_interval
20412042

20422043
# So take the by default time
20432044
std_time = notif.t_to_go + notification_interval * cls.interval_length
@@ -2054,25 +2055,26 @@ def get_next_notification_time(self, notif, escalations, timeperiods):
20542055
creation_time = notif.creation_time
20552056
in_notif_time = now - notif.creation_time
20562057

2057-
for escal_id in self.escalations:
2058-
escal = escalations[escal_id]
2058+
for escalation_id in self.escalations:
2059+
escalation = escalations[escalation_id]
20592060
# If the escalation was already raised, we do not look for a new "early start"
2060-
if escal.get_name() not in notif.already_start_escalations:
2061-
escal_period = timeperiods[escal.escalation_period]
2062-
next_t = escal.get_next_notif_time(std_time, self.state,
2063-
creation_time, cls.interval_length, escal_period)
2061+
if escalation.get_name() not in notif.already_start_escalations:
2062+
escalation_period = timeperiods[escalation.escalation_period]
2063+
next_t = escalation.get_next_notif_time(std_time, self.state,
2064+
creation_time, cls.interval_length,
2065+
escalation_period)
20642066
# If we got a real result (time base escalation), we add it
20652067
if next_t is not None and now < next_t < res:
20662068
res = next_t
20672069

20682070
# And we take the minimum of this result. Can be standard or escalation asked
20692071
return res
20702072

2071-
def get_escalable_contacts(self, notif, escalations, timeperiods):
2073+
def get_escalable_contacts(self, notification, escalations, timeperiods):
20722074
"""Get all contacts (uniq) from eligible escalations
20732075
2074-
:param notif: Notification to get data from (notif number...)
2075-
:type notif: alignak.objects.notification.Notification
2076+
:param notification: Notification to get data from (notif number...)
2077+
:type notification: alignak.objects.notification.Notification
20762078
:param escalations: Esclations objects, used to get escalation objects (contact, period)
20772079
:type escalations: alignak.objects.escalation.Escalations
20782080
:param timeperiods: Timeperiods objects, used to get escalation period
@@ -2085,17 +2087,18 @@ def get_escalable_contacts(self, notif, escalations, timeperiods):
20852087

20862088
# We search since when we are in notification for escalations
20872089
# that are based on this time
2088-
in_notif_time = time.time() - notif.creation_time
2090+
in_notif_time = time.time() - notification.creation_time
20892091

20902092
contacts = set()
2091-
for escal_id in self.escalations:
2092-
escal = escalations[escal_id]
2093-
escal_period = timeperiods[escal.escalation_period]
2094-
if escal.is_eligible(notif.t_to_go, self.state, notif.notif_nb,
2095-
in_notif_time, cls.interval_length, escal_period):
2096-
contacts.update(escal.contacts)
2093+
for escalation_id in self.escalations:
2094+
escalation = escalations[escalation_id]
2095+
2096+
escalation_period = timeperiods[escalation.escalation_period]
2097+
if escalation.is_eligible(notification.t_to_go, self.state, notification.notif_nb,
2098+
in_notif_time, cls.interval_length, escalation_period):
2099+
contacts.update(escalation.contacts)
20972100
# And we tag this escalations as started now
2098-
notif.already_start_escalations.add(escal.get_name())
2101+
notification.already_start_escalations.add(escalation.get_name())
20992102

21002103
return list(contacts)
21012104

@@ -2219,55 +2222,59 @@ def scatter_notification(self, notif, contacts, notifways, timeperiods, macromod
22192222
cls = self.__class__
22202223
childnotifications = []
22212224
escalated = False
2222-
notif_contacts = []
2225+
notification_contacts = []
22232226
if notif.type == u'RECOVERY':
22242227
if self.first_notification_delay != 0 and not self.notified_contacts:
22252228
# Recovered during first_notification_delay. No notifications
22262229
# have been sent yet, so we keep quiet
2227-
notif_contacts = []
2230+
notification_contacts = []
22282231
else:
22292232
# The old way. Only send recover notifications to those contacts
22302233
# who also got problem notifications
2231-
notif_contacts = [c_id for c_id in self.notified_contacts]
2234+
notification_contacts = [c_id for c_id in self.notified_contacts]
22322235
self.notified_contacts.clear()
22332236
else:
22342237
# Check is an escalation match. If yes, get all contacts from escalations
22352238
if self.is_escalable(notif, escalations, timeperiods):
2236-
notif_contacts = self.get_escalable_contacts(notif, escalations, timeperiods)
2239+
notification_contacts = self.get_escalable_contacts(notif, escalations, timeperiods)
22372240
escalated = True
22382241
# else take normal contacts
22392242
else:
22402243
# notif_contacts = [contacts[c_id] for c_id in self.contacts]
2241-
notif_contacts = self.contacts
2244+
notification_contacts = self.contacts
22422245

2243-
for contact_id in notif_contacts:
2244-
contact = contacts[contact_id]
2246+
recipients = []
2247+
recipients_names = []
2248+
for contact_uuid in notification_contacts:
22452249
# We do not want to notify again a contact with notification interval == 0
22462250
# if has been already notified except if the item hard state changed!
22472251
# This can happen when a service exits a downtime and it is still in
22482252
# critical/warning (and not acknowledge)
2249-
if notif.type == u'PROBLEM' and \
2250-
self.notification_interval == 0 \
2253+
if notif.type == u'PROBLEM' and self.notification_interval == 0 \
22512254
and self.state_type == 'HARD' and self.last_state_type == self.state_type \
22522255
and self.state == self.last_state \
22532256
and contact.uuid in self.notified_contacts:
22542257
# Do not send notification
22552258
continue
2259+
recipients.append(contact_uuid)
2260+
recipients_names.append(contacts[contact_uuid].contact_name)
2261+
2262+
for contact_uuid in recipients:
2263+
contact = contacts[contact_uuid]
2264+
22562265
# Get the property name for notification commands, like
22572266
# service_notification_commands for service
22582267
notif_commands = contact.get_notification_commands(notifways, cls.my_type)
22592268

22602269
for cmd in notif_commands:
2261-
# Get the notification recipients list
2262-
recipients = ','.join([contacts[c_uuid].contact_name for c_uuid in notif_contacts])
22632270
data = {
22642271
'type': notif.type,
22652272
'command': u'VOID',
22662273
'command_call': cmd,
22672274
'ref': self.uuid,
22682275
'contact': contact.uuid,
22692276
'contact_name': contact.contact_name,
2270-
'recipients': recipients,
2277+
'recipients': recipients_names,
22712278
't_to_go': notif.t_to_go,
22722279
'escalated': escalated,
22732280
'timeout': cls.notification_timeout,
@@ -2429,7 +2436,6 @@ def launch_check(self, timestamp, hosts, services, timeperiods,
24292436
self.checks_in_progress.append(chk.uuid)
24302437

24312438
self.update_in_checking()
2432-
print("Check: %s" % chk)
24332439

24342440
# We need to put this new check in our actions queue
24352441
# so scheduler can take it

alignak/objects/service.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,9 @@ def raise_check_result(self):
634634
elif self.state == 'CRITICAL':
635635
log_level = 'error'
636636
brok = make_monitoring_log(
637-
log_level, 'ACTIVE SERVICE CHECK: %s;%s;%s;%s;%d;%s' % (
638-
self.host_name, self.get_name(),
639-
self.state, self.state_type,
640-
self.attempt, self.output
641-
)
637+
log_level, 'ACTIVE SERVICE CHECK: %s;%s;%s;%d;%s' % (self.host_name, self.get_name(),
638+
self.state, self.attempt,
639+
self.output)
642640
)
643641
self.broks.append(brok)
644642

@@ -1116,16 +1114,12 @@ def is_blocking_notifications(self, notification_period, hosts, services, n_type
11161114
'n' in self.notification_options:
11171115
logger.debug("Service: %s, notification %s sending is blocked by configuration",
11181116
self.get_name(), n_type)
1119-
print("Service: %s, notification %s sending is blocked by configuration"
1120-
% (self.get_name(), n_type))
11211117
return True
11221118

11231119
# Does the notification period allow sending out this notification?
11241120
if notification_period is not None and not notification_period.is_time_valid(t_wished):
11251121
logger.debug("Service: %s, notification %s sending is blocked by globals",
11261122
self.get_name(), n_type)
1127-
print("Service: %s, notification %s sending is blocked by notification period"
1128-
% (self.get_name(), n_type))
11291123
return True
11301124

11311125
if n_type in (u'PROBLEM', u'RECOVERY') and (

alignak/objects/serviceescalation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def explode(self, escalations):
120120
host_name = getattr(escalation, 'host_name', '')
121121
creation_dict = {
122122
'escalation_name':
123-
'Generated-ServiceEscalation-%s-%s' % (host_name, escalation.uuid)
123+
'Generated-SE-%s-%s' % (host_name, escalation.uuid)
124124
}
125125
for prop in properties:
126126
if hasattr(escalation, prop):

0 commit comments

Comments
 (0)