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 :)
30 def __init__(self
, params
={}):
31 self
.id = self
.__class
__.id
32 self
.__class
__.id += 1
34 setattr(self
, key
, params
[key
])
41 #Copy the groups properties EXCEPT the members
42 #members need to be fill after manually
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
51 for prop
in cls
.properties
:
52 if prop
is not 'members':
54 val
= getattr(self
, prop
)
55 setattr(new_i
, prop
, val
)
61 #Change the members like item1 ,item2 to ['item1' , 'item2']
63 #We also strip elements because spaces Stinks!
65 self
.members
= [ mbr
for mbr
in
66 ( m
.strip() for m
in getattr(self
, 'members', '').split(',') )
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
):
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
96 return str(self
.__dict
__)+'\n'
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
):
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
113 if self
.configuration_errors
!= []:
114 for err
in self
.configuration_errors
:
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
):
129 #Now config properties
130 for prop
in cls
.properties
:
131 if cls
.properties
[prop
].fill_brok
!= []:
133 data
[prop
] = getattr(self
, prop
)
134 #Here members is just a bunch of host, I need name in place
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
)
145 def __init__(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
:
159 def find_by_name(self
, name
):
160 id = self
.find_id_by_name(name
)
162 return self
.itemgroups
[id]
169 for id in self
.itemgroups
:
170 s
+= str(self
.itemgroups
[id])+'\n'
174 return self
.itemgroups
.itervalues()
178 return len(self
.itemgroups
)
181 #If a prop is absent and is not required, put the default value
182 def fill_default(self
):
188 self
.itemgroups
[ig
.id] = ig
196 def is_correct(self
):
197 #we are ok at the begining. Hope we still ok at the end...
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"
204 #Then look for individual ok
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
= {}
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
223 self
.twins
.append(id)