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/>.
23 Here is a node class for dependency_node(s) and a factory to create them
28 #pat = "(h1;db | h2;db | h3;db) & (h4;Apache & h5;Apache & h6;Apache) & (h7;lvs | h8;lvs)"
29 #pat2 = "h1;db | h2;db"
30 #pat3 = "(h1;db | h2;db | h3;db) & (h4;Apache & h5;Apache)"
31 #pat4 = "2 of: h1;db | h2;db | h3;db"
33 class DependencyNode(object):
38 self
.configuration_errors
= []
41 return "Op:'%s' Val:'%s' Sons:'[%s]'" % (self
.operand
, self
.of_values
, ','.join([str(s
) for s
in self
.sons
]))
44 # We will get the state of this node, by looking at the state of
45 # our sons, and apply our operand
47 #print "Ask state of me", self
49 # If we are a host or a service, wee just got the host/service
51 if self
.operand
in ['host', 'service']:
52 state
= self
.sons
[0].last_hard_state_id
53 #print "Get the hard state (%s) for the object %s" % (state, self.sons[0].get_name())
54 # Make DOWN look as CRITICAL (2 instead of 1)
55 if self
.operand
== 'host' and state
== 1:
59 # First we get teh state of all our sons
65 # We will surely need the worse state
66 worse_state
= max(states
)
68 # We look for the better state but not OK/UP
69 no_ok
= [s
for s
in states
if s
!= 0]
71 better_no_good
= min(no_ok
)
73 # Now look at the rule. For a or
74 if self
.operand
== '|':
76 #print "We find a OK/UP match in an OR", states
78 # no ok/UP-> return worse state
80 #print "I send the better no good state...in an OR", better_no_good, states
83 # With an AND, we just send the worse state
84 if self
.operand
== '&':
85 #print "We raise worse state for a AND", worse_state,states
88 # Ok we've got a 'of:' rule
89 nb_search
= self
.of_values
90 # Look if we've got enouth 0
91 if len([s
for s
in states
if s
== 0]) >= nb_search
:
92 #print "Good, we find at least %d 0 in states for a of:" % nb_search, states
95 # Now maybe at least enouth WARNING, still beter than CRITICAL...
96 if len([s
for s
in states
if s
== 1]) >= nb_search
:
97 #print "Beter than nothing, we find at least %d 1 in states for a of:" % nb_search, states
100 # Sic... not good, return 2
101 #print "ARG, not enough 1 or 0, return 2..."
105 #return a list of all host/service in our node and below
106 def list_all_elements(self
):
109 #We are a host/service
110 if self
.operand
in ['host', 'service']:
111 return [self
.sons
[0]]
114 r
.extend(s
.list_all_elements())
121 """Check for empty (= not found) leaf nodes"""
127 if isinstance(s
, DependencyNode
) and not s
.is_valid():
128 self
.configuration_errors
.extend(s
.configuration_errors
)
135 class DependencyNodeFactory(object):
139 # the () will be eval in a recursiv way, only one level of ()
140 def eval_cor_patern(self
, patern
, hosts
, services
):
141 patern
= patern
.strip()
142 #print "*****Loop", patern
145 # Look if it's a complex patern (with rule) or
146 # if it's a leef ofit, like a host/service
153 node
= DependencyNode()
154 p
= "^(\d+) *of: *(.+)"
158 #print "Match the of: thing N=", m.groups()
160 node
.of_values
= int(m
.groups()[0])
161 patern
= m
.groups()[1]
163 #print "Is so complex?", patern, complex_node
165 # if it's a single host/service
167 #print "Try to find?", patern
168 node
.operand
= 'object'
169 obj
, error
= self
.find_object(patern
, hosts
, services
)
171 # Set host or service
172 node
.operand
= obj
.__class
__.my_type
173 node
.sons
.append(obj
)
175 node
.configuration_errors
.append(error
)
187 o
= self
.eval_cor_patern(tmp
, hosts
, services
)
188 #print "1( I've %s got new sons" % patern , o
195 #print "Evaling sub pat", tmp
196 o
= self
.eval_cor_patern(tmp
, hosts
, services
)
197 #print "2) I've %s got new sons" % patern , o
200 #print "Fuck a node son!"
206 current_rule
= node
.operand
207 #print "Current rule", current_rule
208 if current_rule
!= None and current_rule
!= 'of:' and c
!= current_rule
:
209 #print "Fuck, you mix all dumbass!"
211 if current_rule
!= 'of:':
215 o
= self
.eval_cor_patern(tmp
, hosts
, services
)
216 #print "3&| I've %s got new sons" % patern , o
227 o
= self
.eval_cor_patern(tmp
, hosts
, services
)
228 #print "4end I've %s got new sons" % patern , o
231 #print "End, tmp", tmp
232 #print "R %s :" % patern, node
236 # We've got an object, like h1,db1 that mean the
237 # db1 service of the host db1, or just h1, that mean
239 def find_object(self
, patern
, hosts
, services
):
240 #print "Finding object", patern
244 # h_name, service_desc are , separated
245 elts
= patern
.split(',')
247 # Look if we have a service
250 service_description
= elts
[1]
252 obj
= services
.find_srv_by_name_and_hostname(host_name
, service_description
)
254 error
= "Business rule uses unknown service %s/%s" % (host_name
, service_description
)
256 obj
= hosts
.find_by_name(host_name
)
258 error
= "Business rule uses unknown host %s" % (host_name
,)