clean : comment unless print in discorules.
[shinken.git] / shinken / objects / discoveryrule.py
blob799a10f93f395adf30ddc6f2d25127a39a22a846
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 import re
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'
32 properties = {
33 'discoveryrule_name': StringProp (),
34 'check_command': StringProp (),
35 'service_description': StringProp (),
36 'use': StringProp(),
39 running_properties = {}
41 macros = {}
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={}):
49 cls = self.__class__
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)
55 cls.id += 1
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)
63 for key in params:
64 if key in cls.properties:
65 setattr(self, key, params[key])
66 else:
67 if key.startswith('!'):
68 key = key.split('!')[1]
69 self.not_matches[key] = params['!'+key]
70 else:
71 self.matches[key] = params[key]
74 # Output name
75 def get_name(self):
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':
85 d = self.matches
86 else:
87 d = self.not_matches
88 # If we do not even have the key, we bailout
89 if not key.strip() in d:
90 return False
92 # Get my matching patern
93 m = d[key]
94 if ',' in m:
95 matchings = [mt.strip() for mt in m.split(',')]
96 else:
97 matchings = [m]
98 for m in matchings:
99 if re.search(m, value):
100 return True
101 return False
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
108 if len(datas) == 0:
109 return False
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
115 match_one = False
116 for (k, v) in datas:
117 # We found at least one of our match key
118 if m == k:
119 if self.is_matching(k, v):
120 #print "Got matching with", m, k, v
121 match_one = True
122 continue
123 if not match_one:
124 # It match none
125 #print "Match none, FAlse"
126 return False
127 #print "It's possible to be OK"
129 # And now look if ANY of not_matches is reach. If so
130 # it's False
131 for m in self.not_matches:
132 #print "Compare to NOT", m
133 match_one = False
134 for (k, v) in datas:
135 #print "K,V", k,v
136 # We found at least one of our match key
137 if m == k:
138 #print "Go loop"
139 if self.is_matching(k, v, look_in='not_matches'):
140 #print "Got matching with", m, k, v
141 match_one = True
142 continue
143 if match_one:
144 #print "I match one, I quit"
145 return False
147 # Ok we match ALL rules in self.matches
148 # and NONE of self.not_matches, we can go :)
149 return True
155 class Discoveryrules(Items):
156 name_property = "discoveryrule_name"
157 inner_class = Discoveryrule