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 :)
29 def __init__(self
, params
={}):
30 self
.id = self
.__class
__.id
31 self
.__class
__.id += 1
33 setattr(self
, key
, params
[key
])
40 #Copy the groups properties EXCEPT the members
41 #members need to be fill after manually
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
50 for prop
in cls
.properties
:
51 if prop
is not 'members':
53 val
= getattr(self
, prop
)
54 setattr(new_i
, prop
, val
)
60 #Change the members like item1 ,item2 to ['item1' , 'item2']
62 #We also strip elements because spaces Stinks!
64 if hasattr(self
, 'members') and self
.members
!= '':
65 mbrs
= self
.members
.split(',')
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
):
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
99 return str(self
.__dict
__)+'\n'
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
):
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
116 if self
.configuration_errors
!= []:
117 for err
in self
.configuration_errors
:
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
):
132 #Now config properties
133 for prop
in cls
.properties
:
134 if cls
.properties
[prop
].fill_brok
!= []:
136 data
[prop
] = getattr(self
, prop
)
137 #Here members is just a bunch of host, I need name in place
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
)
148 def __init__(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
:
162 def find_by_name(self
, name
):
163 id = self
.find_id_by_name(name
)
165 return self
.itemgroups
[id]
172 for id in self
.itemgroups
:
173 s
+= str(self
.itemgroups
[id])+'\n'
177 return self
.itemgroups
.itervalues()
181 return len(self
.itemgroups
)
184 #If a prop is absent and is not required, put the default value
185 def fill_default(self
):
191 self
.itemgroups
[ig
.id] = ig
199 def is_correct(self
):
200 #we are ok at the begining. Hope we still ok at the end...
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"
207 #Then look for individual ok
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
= {}
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
226 self
.twins
.append(id)