Add : child_dependencies/ parent_dependencies in livestatus module.
[shinken.git] / shinken / servicedependency.py
blobc79c6eb3626a84a97a7ca323639abc8773ef49d6
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 from shinken.util import to_split, to_bool
23 from shinken.item import Item, Items
24 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
26 class Servicedependency(Item):
27 id = 0
29 #F is dep of D
30 #host_name Host B
31 # service_description Service D
32 # dependent_host_name Host C
33 # dependent_service_description Service F
34 # execution_failure_criteria o
35 # notification_failure_criteria w,u
36 # inherits_parent 1
37 # dependency_period 24x7
39 properties={'dependent_host_name': StringProp(),
40 'dependent_hostgroup_name': StringProp(default=''),
41 'dependent_service_description': StringProp(),
42 'host_name': StringProp(),
43 'hostgroup_name': StringProp(default=''),
44 'service_description': StringProp(),
45 'inherits_parent': BoolProp(default='0'),
46 'execution_failure_criteria': ListProp(default='n'),
47 'notification_failure_criteria': ListProp(default='n'),
48 'dependency_period': StringProp(default='')
50 running_properties = {}
52 #Give a nice name output, for debbuging purpose
53 #(Yes, debbuging CAN happen...)
54 def get_name(self):
55 return self.dependent_host_name+'/'+self.dependent_service_description+'..'+self.host_name+'/'+self.service_description
59 class Servicedependencies(Items):
60 def delete_servicesdep_by_id(self, ids):
61 for id in ids:
62 del self.items[id]
65 #Add a simple service dep from another (dep -> par)
66 def add_service_dependency(self, dep_host_name, dep_service_description, par_host_name, par_service_description):
67 #We create a "standard" service_dep
68 prop = {
69 'dependent_host_name' : dep_host_name,
70 'dependent_service_description' : dep_service_description,
71 'host_name' : par_host_name,
72 'service_description' : par_service_description,
73 'notification_failure_criteria' : 'u,c,w',
74 'inherits_parent' : '1',
76 sd = Servicedependency(prop)
77 self.items[sd.id] = sd
80 #We create new servicedep if necessery (host groups and co)
81 def explode(self):
82 #The "old" services will be removed. All services with
83 #more than one host or a host group will be in it
84 srvdep_to_remove = []
86 #Then for every host create a copy of the service with just the host
87 #because we are adding services, we can't just loop in it
88 servicedeps = self.items.keys()
89 for id in servicedeps:
90 sd = self.items[id]
91 if not sd.is_tpl(): #Exploding template is useless
92 if not hasattr(sd, 'dependent_host_name'):
93 sd.dependent_host_name = sd.host_name
94 hnames = sd.dependent_host_name.split(',')
95 snames = sd.dependent_service_description.split(',')
96 couples = []
97 for hname in hnames:
98 for sname in snames:
99 couples.append((hname, sname))
100 if len(couples) >= 2:
101 for (hname, sname) in couples:
102 hname = hname.strip()
103 sname = sname.strip()
104 new_sd = sd.copy()
105 new_sd.dependent_host_name = hname
106 new_sd.dependent_service_description = sname
107 self.items[new_sd.id] = new_sd
108 srvdep_to_remove.append(id)
109 self.delete_servicesdep_by_id(srvdep_to_remove)
112 def linkify(self, hosts, services, timeperiods):
113 self.linkify_sd_by_s(hosts, services)
114 self.linkify_sd_by_tp(timeperiods)
115 self.linkify_s_by_sd()
118 #We just search for each srvdep the id of the srv
119 #and replace the name by the id
120 def linkify_sd_by_s(self, hosts, services):
121 for sd in self:
122 try:
123 s_name = sd.dependent_service_description
124 hst_name = sd.dependent_host_name
126 #The new member list, in id
127 s = services.find_srv_by_name_and_hostname(hst_name, s_name)
128 sd.dependent_service_description = s
130 s_name = sd.service_description
131 hst_name = sd.host_name
133 #The new member list, in id
134 s = services.find_srv_by_name_and_hostname(hst_name, s_name)
135 sd.service_description = s
137 except AttributeError , exp:
138 print exp
141 #We just search for each srvdep the id of the srv
142 #and replace the name by the id
143 def linkify_sd_by_tp(self, timeperiods):
144 for sd in self:
145 try:
146 tp_name = sd.dependency_period
147 tp = timeperiods.find_by_name(tp_name)
148 sd.dependency_period = tp
149 except AttributeError , exp:
150 print exp
153 #We backport service dep to service. So SD is not need anymore
154 def linkify_s_by_sd(self):
155 for sd in self:
156 if not sd.is_tpl():
157 s = sd.dependent_service_description
158 if s is not None and sd.service_description != None:
159 if hasattr(sd, 'dependency_period'):
160 s.add_service_act_dependancy(sd.service_description, sd.notification_failure_criteria, sd.dependency_period, sd.inherits_parent)
161 s.add_service_chk_dependancy(sd.service_description, sd.execution_failure_criteria, sd.dependency_period, sd.inherits_parent)
162 else:
163 s.add_service_act_dependancy(sd.service_description, sd.notification_failure_criteria, None, sd.inherits_parent)
164 s.add_service_chk_dependancy(sd.service_description, sd.execution_failure_criteria, None, sd.inherits_parent)
167 #Apply inheritance for all properties
168 def apply_inheritance(self, hosts):
169 #We check for all Host properties if the host has it
170 #if not, it check all host templates for a value
171 for prop in Servicedependency.properties:
172 self.apply_partial_inheritance(prop)
174 #Then implicit inheritance
175 #self.apply_implicit_inheritance(hosts)
176 for s in self:
177 s.get_customs_properties_by_inheritance(self)