*Fix a bug in host.parents livestatus representation to make thruk happy
[shinken.git] / shinken / itemgroup.py
blob646366fae360355d3acb59d8da9a5707bd2a70fe
1 #!/usr/bin/env python
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/>.
22 #And itemgroup is like a item, but it's a group if items :)
24 from brok import Brok
26 class Itemgroup:
27 id = 0
29 def __init__(self, params={}):
30 self.id = self.__class__.id
31 self.__class__.id += 1
32 for key in params:
33 setattr(self, key, params[key])
36 def clean(self):
37 pass
40 #Copy the groups properties EXCEPT the members
41 #members need to be fill after manually
42 def copy_shell(self):
43 cls = self.__class__
44 old_id = cls.id
45 new_i = cls() # create a new group
46 new_i.id = self.id # with the same id
47 cls.id = old_id #Reset the Class counter
49 #Copy all properties
50 for prop in cls.properties:
51 if prop is not 'members':
52 if self.has(prop):
53 val = getattr(self, prop)
54 setattr(new_i, prop, val)
55 #but no members
56 new_i.members = []
57 return new_i
60 #Change the members like item1 ,item2 to ['item1' , 'item2']
61 #so a python list :)
62 #We also strip elements because spaces Stinks!
63 def pythonize(self):
64 if hasattr(self, 'members') and self.members != '':
65 mbrs = self.members.split(',')
66 else:
67 mbrs = []
68 self.members = []
69 for mbr in mbrs:
70 self.members.append(mbr.strip())
73 def replace_members(self, members):
74 self.members = members
78 #If a prop is absent and is not required, put the default value
79 def fill_default(self):
80 cls = self.__class__
81 properties = cls.properties
83 not_required = [prop for prop in properties \
84 if not properties[prop].required]
85 for prop in not_required:
86 if not hasattr(self, prop):
87 value = properties[prop].default
88 setattr(self, prop, value)
91 def add_string_member(self, member):
92 if hasattr(self, 'members'):
93 self.members += ','+member
94 else:
95 self.members = member
98 def __str__(self):
99 return str(self.__dict__)+'\n'
102 def __iter__(self):
103 return self.members.__iter__()
106 #a item group is correct if all members actually exists,
107 #so if unknown_members is still []
108 def is_correct(self):
109 b = True
111 if self.unknown_members != []:
112 for m in self.unknown_members:
113 print "Error : the", self.__class__.my_type, self.get_name(), "got a unknown member" , m
114 b = False
116 if self.configuration_errors != []:
117 for err in self.configuration_errors:
118 print err
119 b = False
120 return b
123 def has(self, prop):
124 return hasattr(self, prop)
127 #Get a brok with hostgroup info (like id, name)
128 #members is special : list of (id, host_name) for database info
129 def get_initial_status_brok(self):
130 cls = self.__class__
131 data = {}
132 #Now config properties
133 for prop in cls.properties:
134 if cls.properties[prop].fill_brok != []:
135 if self.has(prop):
136 data[prop] = getattr(self, prop)
137 #Here members is just a bunch of host, I need name in place
138 data['members'] = []
139 for i in self.members:
140 #it look like lisp! ((( ..))), sorry....
141 data['members'].append( (i.id, i.get_name()) )
142 b = Brok('initial_'+cls.my_type+'_status', data)
143 return b
147 class Itemgroups:
148 def __init__(self, itemgroups):
149 self.itemgroups = {}
150 for ig in itemgroups:
151 self.itemgroups[ig.id] = ig
154 def find_id_by_name(self, name):
155 for id in self.itemgroups:
156 name_property = self.__class__.name_property
157 if getattr(self.itemgroups[id], name_property) == name:
158 return id
159 return None
162 def find_by_name(self, name):
163 id = self.find_id_by_name(name)
164 if id is not None:
165 return self.itemgroups[id]
166 else:
167 return None
170 def __str__(self):
171 s = ''
172 for id in self.itemgroups:
173 s += str(self.itemgroups[id])+'\n'
174 return s
176 def __iter__(self):
177 return self.itemgroups.itervalues()
180 def __len__(self):
181 return len(self.itemgroups)
184 #If a prop is absent and is not required, put the default value
185 def fill_default(self):
186 for i in self:
187 i.fill_default()
190 def add(self, ig):
191 self.itemgroups[ig.id] = ig
194 def pythonize(self):
195 for ig in self:
196 ig.pythonize()
199 def is_correct(self):
200 #we are ok at the begining. Hope we still ok at the end...
201 r = True
202 #First look at no twins (it's bad!)
203 for id in self.twins:
204 i = self.itemgroups[id]
205 print "Error : the", i.__class__.my_type, i.get_name(), "is duplicated"
206 r = False
207 #Then look for individual ok
208 for ig in self:
209 r &= ig.is_correct()
210 return r
213 #We create the reversed list so search will be faster
214 #We also create a twins list with id of twins (not the original
215 #just the others, higher twins)
216 def create_reversed_list(self):
217 self.reversed_list = {}
218 self.twins = []
219 name_property = self.__class__.name_property
220 for id in self.itemgroups:
221 if hasattr(self.itemgroups[id], name_property):
222 name = getattr(self.itemgroups[id], name_property)
223 if name not in self.reversed_list:
224 self.reversed_list[name] = id
225 else:
226 self.twins.append(id)