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
):
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
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...)
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
):
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
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)
82 #The "old" services will be removed. All services with
83 #more than one host or a host group will be in it
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
:
91 if sd
.is_tpl(): #Exploding template is useless
93 if not hasattr(sd
, 'dependent_host_name'):
94 sd
.dependent_host_name
= sd
.host_name
95 hnames
= sd
.dependent_host_name
.split(',')
96 snames
= sd
.dependent_service_description
.split(',')
100 couples
.append((hname
, sname
))
101 if len(couples
) >= 2:
102 for (hname
, sname
) in couples
:
103 hname
= hname
.strip()
104 sname
= sname
.strip()
106 new_sd
.dependent_host_name
= hname
107 new_sd
.dependent_service_description
= sname
108 self
.items
[new_sd
.id] = new_sd
109 srvdep_to_remove
.append(id)
110 self
.delete_servicesdep_by_id(srvdep_to_remove
)
113 def linkify(self
, hosts
, services
, timeperiods
):
114 self
.linkify_sd_by_s(hosts
, services
)
115 self
.linkify_sd_by_tp(timeperiods
)
116 self
.linkify_s_by_sd()
119 #We just search for each srvdep the id of the srv
120 #and replace the name by the id
121 def linkify_sd_by_s(self
, hosts
, services
):
124 s_name
= sd
.dependent_service_description
125 hst_name
= sd
.dependent_host_name
127 #The new member list, in id
128 s
= services
.find_srv_by_name_and_hostname(hst_name
, s_name
)
129 sd
.dependent_service_description
= s
131 s_name
= sd
.service_description
132 hst_name
= sd
.host_name
134 #The new member list, in id
135 s
= services
.find_srv_by_name_and_hostname(hst_name
, s_name
)
136 sd
.service_description
= s
138 except AttributeError , exp
:
142 #We just search for each srvdep the id of the srv
143 #and replace the name by the id
144 def linkify_sd_by_tp(self
, timeperiods
):
147 tp_name
= sd
.dependency_period
148 tp
= timeperiods
.find_by_name(tp_name
)
149 sd
.dependency_period
= tp
150 except AttributeError , exp
:
154 #We backport service dep to service. So SD is not need anymore
155 def linkify_s_by_sd(self
):
157 if sd
.is_tpl(): continue
158 dsc
= sd
.dependent_service_description
159 sdval
= sd
.service_description
160 if dsc
is not None and sdval
is not None:
161 dp
= getattr(sd
, 'dependency_period', None)
162 dsc
.add_service_act_dependancy(sdval
, sd
.notification_failure_criteria
, dp
, sd
.inherits_parent
)
163 dsc
.add_service_chk_dependancy(sdval
, sd
.execution_failure_criteria
, dp
, sd
.inherits_parent
)
166 #Apply inheritance for all properties
167 def apply_inheritance(self
, hosts
):
168 #We check for all Host properties if the host has it
169 #if not, it check all host templates for a value
170 for prop
in Servicedependency
.properties
:
171 self
.apply_partial_inheritance(prop
)
173 #Then implicit inheritance
174 #self.apply_implicit_inheritance(hosts)
176 s
.get_customs_properties_by_inheritance(self
)