*Change the jenkins/hudson test scripts
[shinken.git] / shinken / hostdependency.py
blob1ebbf64638c78a1edf384ecd8288c1e3cab34833
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
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 from shinken.util import to_split, to_bool
24 from shinken.item import Item, Items
25 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
27 class Hostdependency(Item):
28 id = 0
30 #F is dep of D
31 #host_name Host B
32 # service_description Service D
33 # dependent_host_name Host C
34 # dependent_service_description Service F
35 # execution_failure_criteria o
36 # notification_failure_criteria w,u
37 # inherits_parent 1
38 # dependency_period 24x7
40 properties={'dependent_host_name': StringProp(),
41 'dependent_hostgroup_name': StringProp(default=''),
42 'host_name': StringProp(),
43 'hostgroup_name': StringProp(default=''),
44 'inherits_parent': BoolProp(default='0'),
45 'execution_failure_criteria': ListProp(default='n'),
46 'notification_failure_criteria': ListProp(default='n'),
47 'dependency_period': StringProp(default='')
49 running_properties = {}
51 #Give a nice name output, for debbuging purpose
52 #(debugging happens more often than expected...)
53 def get_name(self):
54 return self.dependent_host_name+'/'+self.host_name
58 class Hostdependencies(Items):
59 def delete_hostsdep_by_id(self, ids):
60 for id in ids:
61 del self.items[id]
64 #We create new servicedep if necessery (host groups and co)
65 def explode(self):
66 #The "old" dependencies will be removed. All dependencies with
67 #more than one host or a host group will be in it
68 hstdep_to_remove = []
70 #Then for every host create a copy of the dependency with just the host
71 #because we are adding services, we can't just loop in it
72 hostdeps = self.items.keys()
73 for id in hostdeps:
74 hd = self.items[id]
75 if hd.is_tpl(): #Exploding template is useless
76 continue
77 hnames = hd.dependent_host_name.split(',')
78 if len(hnames) >= 1:
79 for hname in hnames:
80 hname = hname.strip()
81 new_hd = hd.copy()
82 new_hd.dependent_host_name = hname
83 self.items[new_hd.id] = new_hd
84 hstdep_to_remove.append(id)
85 self.delete_hostsdep_by_id(hstdep_to_remove)
88 def linkify(self, hosts, timeperiods):
89 self.linkify_hd_by_h(hosts)
90 self.linkify_hd_by_tp(timeperiods)
91 self.linkify_h_by_hd()
94 def linkify_hd_by_h(self, hosts):
95 for hd in self:
96 try:
97 h_name = hd.host_name
98 dh_name = hd.dependent_host_name
99 h = hosts.find_by_name(h_name)
100 dh = hosts.find_by_name(dh_name)
101 hd.host_name = h
102 hd.dependent_host_name = dh
103 except AttributeError , exp:
104 print exp
107 #We just search for each hostdep the id of the host
108 #and replace the name by the id
109 def linkify_hd_by_tp(self, timeperiods):
110 for hd in self:
111 try:
112 tp_name = hd.dependency_period
113 tp = timeperiods.find_by_name(tp_name)
114 hd.dependency_period = tp
115 except AttributeError , exp:
116 print exp
119 #We backport host dep to host. So HD is not need anymore
120 def linkify_h_by_hd(self):
121 for hd in self:
122 depdt_hname = hd.dependent_host_name
123 if hd.is_tpl() or depdt_hname is None: continue
124 dp = getattr(hd, 'dependency_period', None)
125 depdt_hname.add_host_act_dependancy(hd.host_name, hd.notification_failure_criteria, dp, hd.inherits_parent)
126 depdt_hname.add_host_chk_dependancy(hd.host_name, hd.execution_failure_criteria, dp, hd.inherits_parent)
129 #Apply inheritance for all properties
130 def apply_inheritance(self):
131 #We check for all Host properties if the host has it
132 #if not, it check all host templates for a value
133 for prop in Hostdependency.properties:
134 self.apply_partial_inheritance(prop)
136 #Then implicit inheritance
137 #self.apply_implicit_inheritance(hosts)
138 for h in self:
139 h.get_customs_properties_by_inheritance(self)