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/>.
25 from shinken
.objects
.item
import Item
, Items
26 from shinken
.property import IntegerProp
, StringProp
, ListProp
28 class Discoveryrule(Item
):
29 id = 1 #0 is always special in database, so we do not take risk here
30 my_type
= 'discoveryrule'
33 'discoveryrule_name': StringProp (),
34 'check_command': StringProp (),
35 'service_description': StringProp (),
39 running_properties
= {}
44 # The init of a discovery will set the property of
45 # Discoveryrule.properties as in setattr, but all others
46 # will be in a list because we need to have all names
47 # and not lost all in __dict__
48 def __init__(self
, params
={}):
51 # We have our own id of My Class type :)
52 # use set attr for going into the slots
53 # instead of __dict__ :)
54 setattr(self
, 'id', cls
.id)
57 self
.matches
= {} # for matching rules
58 self
.not_matches
= {} # for rules that should NOT match
60 # In property : in __dict__
61 # if not, in matches or not match (if key starts
62 # with a !, it's a not rule)
64 if key
in cls
.properties
:
65 setattr(self
, key
, params
[key
])
67 if key
.startswith('!'):
68 key
= key
.split('!')[1]
69 self
.not_matches
[key
] = params
['!'+key
]
71 self
.matches
[key
] = params
[key
]
76 return self
.discoveryrule_name
79 # Try to see if the key,value is matching one or
80 # our rule. If value got ',' we must look for each value
81 # If one match, we quit
82 # We can find in matches or not_matches
83 def is_matching(self
, key
, value
, look_in
='matches'):
84 if look_in
== 'matches':
88 # If we do not even have the key, we bailout
89 if not key
.strip() in d
:
92 # Get my matching patern
95 matchings
= [mt
.strip() for mt
in m
.split(',')]
99 if re
.search(m
, value
):
104 # Look if we match all discovery data or not
105 # a disco data look as a list of (key, values)
106 def is_matching_disco_datas(self
, datas
):
107 # If we got not data, no way we can match
111 # First we look if it's possible to match
112 # we must match All self.matches things
113 for m
in self
.matches
:
114 #print "Compare to", m
117 # We found at least one of our match key
119 if self
.is_matching(k
, v
):
120 #print "Got matching with", m, k, v
125 #print "Match none, FAlse"
127 #print "It's possible to be OK"
129 # And now look if ANY of not_matches is reach. If so
131 for m
in self
.not_matches
:
132 #print "Compare to NOT", m
136 # We found at least one of our match key
139 if self
.is_matching(k
, v
, look_in
='not_matches'):
140 #print "Got matching with", m, k, v
144 #print "I match one, I quit"
147 # Ok we match ALL rules in self.matches
148 # and NONE of self.not_matches, we can go :)
155 class Discoveryrules(Items
):
156 name_property
= "discoveryrule_name"
157 inner_class
= Discoveryrule