clean : comment unless print in discorules.
[shinken.git] / shinken / objects / hostdependency.py
blob281ec7e6ab0dad8d6e6eebf1ce4b235e324dae12
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
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 from shinken.objects.item import Item, Items
25 from shinken.property import BoolProp, 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 = {
41 'dependent_host_name': StringProp(),
42 'dependent_hostgroup_name': StringProp(default=''),
43 'host_name': StringProp(),
44 'hostgroup_name': StringProp(default=''),
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='')
51 running_properties = {}
53 #Give a nice name output, for debbuging purpose
54 #(debugging happens more often than expected...)
55 def get_name(self):
56 return self.dependent_host_name+'/'+self.host_name
60 class Hostdependencies(Items):
61 def delete_hostsdep_by_id(self, ids):
62 for id in ids:
63 del self.items[id]
66 #We create new servicedep if necessery (host groups and co)
67 def explode(self):
68 #The "old" dependencies will be removed. All dependencies with
69 #more than one host or a host group will be in it
70 hstdep_to_remove = []
72 #Then for every host create a copy of the dependency with just the host
73 #because we are adding services, we can't just loop in it
74 hostdeps = self.items.keys()
75 for id in hostdeps:
76 hd = self.items[id]
77 if hd.is_tpl(): #Exploding template is useless
78 continue
79 hnames = hd.dependent_host_name.split(',')
80 if len(hnames) >= 1:
81 for hname in hnames:
82 hname = hname.strip()
83 new_hd = hd.copy()
84 new_hd.dependent_host_name = hname
85 self.items[new_hd.id] = new_hd
86 hstdep_to_remove.append(id)
87 self.delete_hostsdep_by_id(hstdep_to_remove)
90 def linkify(self, hosts, timeperiods):
91 self.linkify_hd_by_h(hosts)
92 self.linkify_hd_by_tp(timeperiods)
93 self.linkify_h_by_hd()
96 def linkify_hd_by_h(self, hosts):
97 for hd in self:
98 try:
99 h_name = hd.host_name
100 dh_name = hd.dependent_host_name
101 h = hosts.find_by_name(h_name)
102 dh = hosts.find_by_name(dh_name)
103 hd.host_name = h
104 hd.dependent_host_name = dh
105 except AttributeError , exp:
106 print exp
109 #We just search for each hostdep the id of the host
110 #and replace the name by the id
111 def linkify_hd_by_tp(self, timeperiods):
112 for hd in self:
113 try:
114 tp_name = hd.dependency_period
115 tp = timeperiods.find_by_name(tp_name)
116 hd.dependency_period = tp
117 except AttributeError , exp:
118 print exp
121 #We backport host dep to host. So HD is not need anymore
122 def linkify_h_by_hd(self):
123 for hd in self:
124 depdt_hname = hd.dependent_host_name
125 if hd.is_tpl() or depdt_hname is None: continue
126 dp = getattr(hd, 'dependency_period', None)
127 depdt_hname.add_host_act_dependancy(hd.host_name, hd.notification_failure_criteria, dp, hd.inherits_parent)
128 depdt_hname.add_host_chk_dependancy(hd.host_name, hd.execution_failure_criteria, dp, hd.inherits_parent)
131 #Apply inheritance for all properties
132 def apply_inheritance(self):
133 #We check for all Host properties if the host has it
134 #if not, it check all host templates for a value
135 for prop in Hostdependency.properties:
136 self.apply_partial_inheritance(prop)
138 #Then implicit inheritance
139 #self.apply_implicit_inheritance(hosts)
140 for h in self:
141 h.get_customs_properties_by_inheritance(self)