Fix : bad host group member id in NDO.
[shinken.git] / shinken / hostdependency.py
blobec0f628d1acf91c7333ea0d8ddf063457b72b7ea
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 Hostdependency(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 'host_name': StringProp(),
42 'hostgroup_name': StringProp(default=''),
43 'inherits_parent': BoolProp(default='0'),
44 'execution_failure_criteria': ListProp(default='n'),
45 'notification_failure_criteria': ListProp(default='n'),
46 'dependency_period': StringProp(default='')
48 running_properties = {}
50 #Give a nice name output, for debbuging purpose
51 #(debugging happens more often than expected...)
52 def get_name(self):
53 return self.dependent_host_name+'/'+self.host_name
57 class Hostdependencies(Items):
58 def delete_hostsdep_by_id(self, ids):
59 for id in ids:
60 del self.items[id]
63 #We create new servicedep if necessery (host groups and co)
64 def explode(self):
65 #The "old" dependencies will be removed. All dependencies with
66 #more than one host or a host group will be in it
67 hstdep_to_remove = []
69 #Then for every host create a copy of the dependency with just the host
70 #because we are adding services, we can't just loop in it
71 hostdeps = self.items.keys()
72 for id in hostdeps:
73 hd = self.items[id]
74 if hd.is_tpl(): #Exploding template is useless
75 continue
76 hnames = hd.dependent_host_name.split(',')
77 if len(hnames) >= 1:
78 for hname in hnames:
79 hname = hname.strip()
80 new_hd = hd.copy()
81 new_hd.dependent_host_name = hname
82 self.items[new_hd.id] = new_hd
83 hstdep_to_remove.append(id)
84 self.delete_hostsdep_by_id(hstdep_to_remove)
87 def linkify(self, hosts, timeperiods):
88 self.linkify_hd_by_h(hosts)
89 self.linkify_hd_by_tp(timeperiods)
90 self.linkify_h_by_hd()
93 def linkify_hd_by_h(self, hosts):
94 for hd in self:
95 try:
96 h_name = hd.host_name
97 dh_name = hd.dependent_host_name
98 h = hosts.find_by_name(h_name)
99 dh = hosts.find_by_name(dh_name)
100 hd.host_name = h
101 hd.dependent_host_name = dh
102 except AttributeError , exp:
103 print exp
106 #We just search for each hostdep the id of the host
107 #and replace the name by the id
108 def linkify_hd_by_tp(self, timeperiods):
109 for hd in self:
110 try:
111 tp_name = hd.dependency_period
112 tp = timeperiods.find_by_name(tp_name)
113 hd.dependency_period = tp
114 except AttributeError , exp:
115 print exp
118 #We backport host dep to host. So HD is not need anymore
119 def linkify_h_by_hd(self):
120 for hd in self:
121 depdt_hname = hd.dependent_host_name
122 if hd.is_tpl() or depdt_hname is None: continue
123 dp = getattr(hd, 'dependency_period', None)
124 depdt_hname.add_host_act_dependancy(hd.host_name, hd.notification_failure_criteria, dp, hd.inherits_parent)
125 depdt_hname.add_host_chk_dependancy(hd.host_name, hd.execution_failure_criteria, dp, hd.inherits_parent)
128 #Apply inheritance for all properties
129 def apply_inheritance(self):
130 #We check for all Host properties if the host has it
131 #if not, it check all host templates for a value
132 for prop in Hostdependency.properties:
133 self.apply_partial_inheritance(prop)
135 #Then implicit inheritance
136 #self.apply_implicit_inheritance(hosts)
137 for h in self:
138 h.get_customs_properties_by_inheritance(self)