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 #SatelliteLink is a common Class for link to satellite for
23 #Arbiter with Conf Dispatcher.
26 import shinken
.pyro_wrapper
as pyro
30 from shinken
.item
import Item
, Items
32 class SatelliteLink(Item
):
33 #id = 0 each Class will have it's own id
34 #properties={'name' : {'required' : True },#, 'pythonize': None},
35 # 'address' : {'required' : True},#, 'pythonize': to_bool},
36 # 'port' : {'required': True, 'pythonize': to_int},
37 # 'spare' : {'required': False, 'default' : '0', 'pythonize': to_bool},
40 #running_properties = {
41 # 'con' : {'default' : None}
51 #Check is required prop are set:
52 #contacts OR contactgroups is need
54 state
= True #guilty or not? :)
57 special_properties
= ['realm']
58 for prop
in cls
.properties
:
59 if prop
not in special_properties
:
60 if not hasattr(self
, prop
) and cls
.properties
[prop
]['required']:
61 print self
.get_name(), " : I do not have", prop
62 state
= False #Bad boy...
63 # Ok now we manage special cases...
64 if getattr(self
, 'realm', None) is None:
65 print self
.get_name()," : I do not have a valid realm"
70 def create_connexion(self
):
71 self
.uri
= pyro
.create_uri(self
.address
, self
.port
, "ForArbiter", self
.__class
__.use_ssl
)
72 self
.con
= pyro
.getProxy(self
.uri
)
73 pyro
.set_timeout(self
.con
, self
.timeout
)
76 def put_conf(self
, conf
, use_ssl
):
79 self
.create_connexion()
80 #print "Connexion is OK, now we put conf", conf
81 #print "Try to put conf:", conf
84 pyro
.set_timeout(self
.con
, self
.data_timeout
)
85 #del conf[0].schedulerlinks
87 buf
=cPickle
.dumps(conf
)
88 print "DBG: put conf to", self
.con
.__dict
__
89 self
.con
.put_conf(conf
)
90 pyro
.set_timeout(self
.con
, self
.timeout
)
92 except Pyro
.errors
.URIError
, exp
:
95 except Pyro
.errors
.ProtocolError
, exp
:
98 except TypeError , exp
:
99 print ''.join(Pyro
.util
.getPyroTraceback(exp
))
100 except Pyro
.errors
.CommunicationError
, exp
:
105 #Get and clean all of our broks
106 def get_all_broks(self
):
112 #Set alive, reachable, and reset attemps.
113 #If we change state, raise a status brok update
115 was_alive
= self
.alive
118 self
.reachable
= True
120 #We came from dead to alive
121 #so we must add a brok update
123 b
= self
.get_update_status_brok()
128 print "Set dead for %s" % self
.get_name()
129 was_alive
= self
.alive
133 #We are dead now. Must raise
136 b
= self
.get_update_status_brok()
140 #Go in reachable=False and add a failed attempt
141 #if we reach the max, go dead
142 def add_failed_check_attempt(self
):
143 print "Add failed attempt to", self
.get_name()
144 self
.reachable
= False
146 self
.attempt
= min(self
.attempt
, self
.max_check_attempts
)
147 print "Attemps", self
.attempt
, self
.max_check_attempts
148 #check when we just go HARD (dead)
149 if self
.attempt
== self
.max_check_attempts
:
153 def ping(self
, use_ssl
):
154 print "Pinging %s" % self
.get_name()
157 self
.create_connexion()
160 except Pyro
.errors
.ProtocolError
, exp
:
161 self
.add_failed_check_attempt()
162 except Pyro
.errors
.URIError
, exp
:
164 self
.add_failed_check_attempt()
165 #Only pyro 4 but will be ProtocolError in 3
166 except Pyro
.errors
.CommunicationError
, exp
:
167 #print "Is not alive!", self.uri
168 self
.add_failed_check_attempt()
169 except Pyro
.errors
.DaemonError
, exp
:
171 self
.add_failed_check_attempt()
172 except Exception, exp
:
174 self
.add_failed_check_attempt()
177 def wait_new_conf(self
, use_ssl
):
179 self
.create_connexion()
181 self
.con
.wait_new_conf()
183 except Pyro
.errors
.URIError
, exp
:
186 except Pyro
.errors
.ProtocolError
, exp
:
189 except Exception, exp
:
195 #To know if the satellite have a conf (magic_hash = None)
196 #OR to know if the satellite have THIS conf (magic_hash != None)
197 def have_conf(self
, use_ssl
, magic_hash
=None):
199 self
.create_connexion()
202 if magic_hash
== None:
203 return self
.con
.have_conf()
205 return self
.con
.have_conf(magic_hash
)
206 except Pyro
.errors
.URIError
, exp
:
209 except Pyro
.errors
.ProtocolError
, exp
:
212 except Exception, exp
:
218 def remove_from_conf(self
, sched_id
, use_ssl
):
220 self
.create_connexion()
222 self
.con
.remove_from_conf(sched_id
)
224 except Pyro
.errors
.URIError
, exp
:
227 except Pyro
.errors
.ProtocolError
, exp
:
230 except Exception, exp
:
235 def what_i_managed(self
, use_ssl
):
237 self
.create_connexion()
239 tab
= self
.con
.what_i_managed()
240 #I don't know why, but tab can be a bool. Not good here
241 if isinstance(tab
, bool):
245 except Pyro
.errors
.URIError
, exp
:
248 except Pyro
.errors
.ProtocolError
, exp
:
251 except Exception, exp
:
256 def push_broks(self
, broks
, use_ssl
):
258 self
.create_connexion()
260 return self
.con
.push_broks(broks
)
261 except Pyro
.errors
.URIError
, exp
:
264 except Pyro
.errors
.ProtocolError
, exp
:
267 except AttributeError , exp
:
270 except Exception, exp
:
276 def get_external_commands(self
, use_ssl
):
278 self
.create_connexion()
280 tab
= self
.con
.get_external_commands()
281 if isinstance(tab
, bool):
284 except Pyro
.errors
.URIError
, exp
:
287 except Pyro
.errors
.ProtocolError
, exp
:
290 except AttributeError , exp
:
293 except Exception, exp
:
299 def prepare_for_conf(self
):
300 self
.cfg
= { 'global' : {}, 'schedulers' : {}, 'arbiters' : {}}
301 #cfg_for_satellite['modules'] = satellite.modules
302 properties
= self
.__class
__.properties
303 for prop
in properties
:
304 # if 'to_send' in properties[prop] and properties[prop]['to_send']:
305 if properties
[prop
].to_send
:
306 self
.cfg
['global'][prop
] = getattr(self
, prop
)
308 #Some parameters for satellites are not defined in the satellites conf
309 #but in the global configuration. We can pass them in the global
311 def add_global_conf_parameters(self
, params
):
313 print "Add global parameter", prop
, params
[prop
]
314 self
.cfg
['global'][prop
] = params
[prop
]
317 def get_my_type(self
):
318 return self
.__class
__.my_type
321 #Here for poller and reactionner. Scheduler have it's own function
322 def give_satellite_cfg(self
):
323 return {'port' : self
.port
, 'address' : self
.address
, 'name' : self
.get_name(), 'instance_id' : self
.id, 'active' : True}
327 #Call by picle for dataify the downtime
328 #because we DO NOT WANT REF in this pickleisation!
329 def __getstate__(self
):
331 # id is not in *_properties
332 res
= {'id' : self
.id}
333 for prop
in cls
.properties
:
335 if hasattr(self
, prop
):
336 res
[prop
] = getattr(self
, prop
)
337 for prop
in cls
.running_properties
:
339 if hasattr(self
, prop
):
340 res
[prop
] = getattr(self
, prop
)
344 #Inversed funtion of getstate
345 def __setstate__(self
, state
):
348 self
.id = state
['id']
349 for prop
in cls
.properties
:
351 setattr(self
, prop
, state
[prop
])
352 for prop
in cls
.running_properties
:
354 setattr(self
, prop
, state
[prop
])
358 class SatelliteLinks(Items
):
359 #name_property = "name"
360 #inner_class = SchedulerLink
362 #We must have a realm property, so we find our realm
363 def linkify(self
, realms
, modules
):
364 self
.linkify_s_by_p(realms
)
365 self
.linkify_s_by_plug(modules
)
368 def linkify_s_by_p(self
, realms
):
370 p_name
= s
.realm
.strip()
371 # If no realm name, take the default one
373 p
= realms
.get_default()
375 else: # find the realm one
376 p
= realms
.find_by_name(p_name
)
378 # Check if what we get is OK or not
380 print "Me", s
.get_name(), "is linked with realm", s
.realm
.get_name()
381 s
.register_to_my_realm()
383 err
= "The %s %s got a unknown realm '%s'" % (s
.__class
__.my_type
, s
.get_name(), p_name
)
384 s
.configuration_errors
.append(err
)
388 def linkify_s_by_plug(self
, modules
):
391 for plug_name
in s
.modules
:
392 plug
= modules
.find_by_name(plug_name
.strip())
394 new_modules
.append(plug
)
396 print "Error : the module %s is unknow for %s" % (plug_name
, s
.get_name())
397 s
.modules
= new_modules