Merge branch 'master' of ssh://naparuba@shinken.git.sourceforge.net/gitroot/shinken...
[shinken.git] / shinken / itemgroup.py
blob668a69b2c8bc39d998d2aeb0dae56930738f545a
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
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 #And itemgroup is like a item, but it's a group if items :)
25 from brok import Brok
27 class Itemgroup:
28 id = 0
30 def __init__(self, params={}):
31 self.id = self.__class__.id
32 self.__class__.id += 1
33 for key in params:
34 setattr(self, key, params[key])
37 def clean(self):
38 pass
41 #Copy the groups properties EXCEPT the members
42 #members need to be fill after manually
43 def copy_shell(self):
44 cls = self.__class__
45 old_id = cls.id
46 new_i = cls() # create a new group
47 new_i.id = self.id # with the same id
48 cls.id = old_id #Reset the Class counter
50 #Copy all properties
51 for prop in cls.properties:
52 if prop is not 'members':
53 if self.has(prop):
54 val = getattr(self, prop)
55 setattr(new_i, prop, val)
56 #but no members
57 new_i.members = []
58 return new_i
61 #Change the members like item1 ,item2 to ['item1' , 'item2']
62 #so a python list :)
63 #We also strip elements because spaces Stinks!
64 def pythonize(self):
65 self.members = [ mbr for mbr in
66 ( m.strip() for m in getattr(self, 'members', '').split(',') )
67 if mbr != '' ]
70 def replace_members(self, members):
71 self.members = members
75 #If a prop is absent and is not required, put the default value
76 def fill_default(self):
77 cls = self.__class__
78 properties = cls.properties
80 not_required = tuple( prop for prop in properties
81 if not properties[prop].required )
82 for prop in not_required:
83 if not hasattr(self, prop):
84 value = properties[prop].default
85 setattr(self, prop, value)
88 def add_string_member(self, member):
89 if hasattr(self, 'members'):
90 self.members += ','+member
91 else:
92 self.members = member
95 def __str__(self):
96 return str(self.__dict__)+'\n'
99 def __iter__(self):
100 return self.members.__iter__()
103 #a item group is correct if all members actually exists,
104 #so if unknown_members is still []
105 def is_correct(self):
106 b = True
108 if self.unknown_members != []:
109 for m in self.unknown_members:
110 print "Error : the", self.__class__.my_type, self.get_name(), "got a unknown member" , m
111 b = False
113 if self.configuration_errors != []:
114 for err in self.configuration_errors:
115 print err
116 b = False
117 return b
120 def has(self, prop):
121 return hasattr(self, prop)
124 #Get a brok with hostgroup info (like id, name)
125 #members is special : list of (id, host_name) for database info
126 def get_initial_status_brok(self):
127 cls = self.__class__
128 data = {}
129 #Now config properties
130 for prop in cls.properties:
131 if cls.properties[prop].fill_brok != []:
132 if self.has(prop):
133 data[prop] = getattr(self, prop)
134 #Here members is just a bunch of host, I need name in place
135 data['members'] = []
136 for i in self.members:
137 #it look like lisp! ((( ..))), sorry....
138 data['members'].append( (i.id, i.get_name()) )
139 b = Brok('initial_'+cls.my_type+'_status', data)
140 return b
144 class Itemgroups:
145 def __init__(self, itemgroups):
146 self.itemgroups = {}
147 for ig in itemgroups:
148 self.itemgroups[ig.id] = ig
151 def find_id_by_name(self, name):
152 for id in self.itemgroups:
153 name_property = self.__class__.name_property
154 if getattr(self.itemgroups[id], name_property) == name:
155 return id
156 return None
159 def find_by_name(self, name):
160 id = self.find_id_by_name(name)
161 if id is not None:
162 return self.itemgroups[id]
163 else:
164 return None
167 def __str__(self):
168 s = ''
169 for id in self.itemgroups:
170 s += str(self.itemgroups[id])+'\n'
171 return s
173 def __iter__(self):
174 return self.itemgroups.itervalues()
177 def __len__(self):
178 return len(self.itemgroups)
181 #If a prop is absent and is not required, put the default value
182 def fill_default(self):
183 for i in self:
184 i.fill_default()
187 def add(self, ig):
188 self.itemgroups[ig.id] = ig
191 def pythonize(self):
192 for ig in self:
193 ig.pythonize()
196 def is_correct(self):
197 #we are ok at the begining. Hope we still ok at the end...
198 r = True
199 #First look at no twins (it's bad!)
200 for id in self.twins:
201 i = self.itemgroups[id]
202 print "Error : the", i.__class__.my_type, i.get_name(), "is duplicated"
203 r = False
204 #Then look for individual ok
205 for ig in self:
206 r &= ig.is_correct()
207 return r
210 #We create the reversed list so search will be faster
211 #We also create a twins list with id of twins (not the original
212 #just the others, higher twins)
213 def create_reversed_list(self):
214 self.reversed_list = {}
215 self.twins = []
216 name_property = self.__class__.name_property
217 for id in self.itemgroups:
218 if hasattr(self.itemgroups[id], name_property):
219 name = getattr(self.itemgroups[id], name_property)
220 if name not in self.reversed_list:
221 self.reversed_list[name] = id
222 else:
223 self.twins.append(id)