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 not hasattr(self
, 'realm') or hasattr(self
, 'realm') and self
.realm
== 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")
72 self
.con
= pyro
.getProxy(self
.uri
)
73 pyro
.set_timeout(self
.con
, self
.timeout
)
76 def put_conf(self
, conf
):
78 self
.create_connexion()
79 #print "Connexion is OK, now we put conf", conf
80 #print "Try to put conf:", conf
82 pyro
.set_timeout(self
.con
, self
.data_timeout
)
83 self
.con
.put_conf(conf
)
84 pyro
.set_timeout(self
.con
, self
.timeout
)
86 except Pyro
.errors
.URIError
, exp
:
89 except Pyro
.errors
.ProtocolError
, exp
:
92 except TypeError , exp
:
93 print ''.join(Pyro
.util
.getPyroTraceback(exp
))
94 except Pyro
.errors
.CommunicationError
, exp
:
99 #Get and clean all of our broks
100 def get_all_broks(self
):
106 #Set alive, reachable, and reset attemps.
107 #If we change state, raise a status brok update
109 was_alive
= self
.alive
112 self
.reachable
= True
114 #We came from dead to alive
115 #so we must add a brok update
117 b
= self
.get_update_status_brok()
122 print "Set dead for %s" % self
.get_name()
123 was_alive
= self
.alive
127 #We are dead now. Must raise
130 b
= self
.get_update_status_brok()
134 #Go in reachable=False and add a failed attempt
135 #if we reach the max, go dead
136 def add_failed_check_attempt(self
):
137 print "Add failed attempt to", self
.get_name()
138 self
.reachable
= False
140 self
.attempt
= min(self
.attempt
, self
.max_check_attempts
)
141 print "Attemps", self
.attempt
, self
.max_check_attempts
142 #check when we just go HARD (dead)
143 if self
.attempt
== self
.max_check_attempts
:
148 print "Pinging %s" % self
.get_name()
151 self
.create_connexion()
154 except Pyro
.errors
.ProtocolError
, exp
:
155 self
.add_failed_check_attempt()
156 except Pyro
.errors
.URIError
, exp
:
158 self
.add_failed_check_attempt()
159 #Only pyro 4 but will be ProtocolError in 3
160 except Pyro
.errors
.CommunicationError
, exp
:
161 #print "Is not alive!", self.uri
162 self
.add_failed_check_attempt()
163 except Pyro
.errors
.DaemonError
, exp
:
165 self
.add_failed_check_attempt()
168 def wait_new_conf(self
):
170 self
.create_connexion()
172 self
.con
.wait_new_conf()
174 except Pyro
.errors
.URIError
, exp
:
177 except Pyro
.errors
.ProtocolError
, exp
:
182 #To know if the satellite have a conf (magic_hash = None)
183 #OR to know if the satellite have THIS conf (magic_hash != None)
184 def have_conf(self
, magic_hash
=None):
186 self
.create_connexion()
189 if magic_hash
== None:
190 return self
.con
.have_conf()
192 return self
.con
.have_conf(magic_hash
)
193 except Pyro
.errors
.URIError
, exp
:
196 except Pyro
.errors
.ProtocolError
, exp
:
201 def remove_from_conf(self
, sched_id
):
203 self
.create_connexion()
205 self
.con
.remove_from_conf(sched_id
)
207 except Pyro
.errors
.URIError
, exp
:
210 except Pyro
.errors
.ProtocolError
, exp
:
215 def what_i_managed(self
):
217 self
.create_connexion()
219 tab
= self
.con
.what_i_managed()
220 #I don't know why, but tab can be a bool. Not good here
221 if isinstance(tab
, bool):
225 except Pyro
.errors
.URIError
, exp
:
228 except Pyro
.errors
.ProtocolError
, exp
:
233 def push_broks(self
, broks
):
235 self
.create_connexion()
237 return self
.con
.push_broks(broks
)
238 except Pyro
.errors
.URIError
, exp
:
241 except Pyro
.errors
.ProtocolError
, exp
:
244 except AttributeError , exp
:
249 def get_external_commands(self
):
251 self
.create_connexion()
253 tab
= self
.con
.get_external_commands()
254 if isinstance(tab
, bool):
257 except Pyro
.errors
.URIError
, exp
:
260 except Pyro
.errors
.ProtocolError
, exp
:
263 except AttributeError , exp
:
269 def prepare_for_conf(self
):
270 self
.cfg
= { 'global' : {}, 'schedulers' : {}, 'arbiters' : {}}
271 #cfg_for_satellite['modules'] = satellite.modules
272 properties
= self
.__class
__.properties
273 for prop
in properties
:
274 # if 'to_send' in properties[prop] and properties[prop]['to_send']:
275 if properties
[prop
].to_send
:
276 self
.cfg
['global'][prop
] = getattr(self
, prop
)
278 #Some parameters for satellites are not defined in the satellites conf
279 #but in the global configuration. We can pass them in the global
281 def add_global_conf_parameters(self
, params
):
283 print "Add global parameter", prop
, params
[prop
]
284 self
.cfg
['global'][prop
] = params
[prop
]
287 def get_my_type(self
):
288 return self
.__class
__.my_type
291 #Here for poller and reactionner. Scheduler have it's own function
292 def give_satellite_cfg(self
):
293 return {'port' : self
.port
, 'address' : self
.address
, 'name' : self
.get_name(), 'instance_id' : self
.id, 'active' : True}
297 class SatelliteLinks(Items
):
298 #name_property = "name"
299 #inner_class = SchedulerLink
301 #We must have a realm property, so we find our realm
302 def linkify(self
, realms
, modules
):
303 self
.linkify_s_by_p(realms
)
304 self
.linkify_s_by_plug(modules
)
307 def linkify_s_by_p(self
, realms
):
309 p_name
= s
.realm
.strip()
310 # If no realm name, take the default one
312 p
= realms
.get_default()
314 else: # find the realm one
315 p
= realms
.find_by_name(p_name
)
317 # Check if what we get is OK or not
319 print "Me", s
.get_name(), "is linked with realm", s
.realm
.get_name()
320 s
.register_to_my_realm()
322 err
= "The %s %s got a unknown realm '%s'" % (s
.__class
__.my_type
, s
.get_name(), p_name
)
323 s
.configuration_errors
.append(err
)
327 def linkify_s_by_plug(self
, modules
):
330 for plug_name
in s
.modules
:
331 plug
= modules
.find_by_name(plug_name
.strip())
333 new_modules
.append(plug
)
335 print "Error : the module %s is unknow for %s" % (plug_name
, s
.get_name())
336 s
.modules
= new_modules