Skip to content

Commit

Permalink
Application try to etablish connection if failed.
Browse files Browse the repository at this point in the history
Ref #23
Run even the connection failed
Retry to connect if needed

Ref #22
A Notification appears if backend is not available.
  • Loading branch information
algorys committed Oct 13, 2016
1 parent 0acd6c0 commit 9fa5813
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
28 changes: 20 additions & 8 deletions alignak_app/alignak_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

from logging import getLogger
from alignak_backend_client.client import Backend, BackendException
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify # pylint: disable=wrong-import-position


logger = getLogger(__name__)
Expand All @@ -51,6 +54,7 @@ def log_to_backend(self, config):
:param config: parser config who contains settings
:type config: :class:`~configparser.ConfigParser`
"""

# Credentials
username = config.get('Backend', 'username')
password = config.get('Backend', 'password')
Expand All @@ -64,15 +68,25 @@ def log_to_backend(self, config):
connect = self.backend.login(username, password)
if connect:
logger.info('Connection to backend : OK.')

else:
logger.warn('Connection to backend failed !')
Notify.init('appalignak')
Notify.Notification.new(
'Connection to backend failed !',
None,
gi.repository.Gtk.STOCK_DIALOG_ERROR,
).show()
except BackendException as e:
logger.error(
'Can\'t connect to Backend. ' +
'Connection to Backend has failed. ' +
str(e) +
'\nCheck your [settings.cfg] or your backend status.'
)
sys.exit()
sys.exit(
'Alignak-app will close :( ...' +
'\nCheck your logs.'
)

def get_host_state(self):
"""
Expand All @@ -88,14 +102,13 @@ def get_host_state(self):
all_host = self.backend.get_all(
self.backend.url_endpoint_root + '/host', params)
except BackendException as e:
logger.error('Can\'t get hosts state \n' + str(e))
logger.warn('Alignak-app failed to collect hosts... \n' + str(e))

# Store Data
if all_host:
for host in all_host['_items']:
self.current_hosts[host['name']] = host['ls_state']
else:
logger.error('AlignakApp has collected 0 hosts !')

return self.current_hosts

def get_service_state(self):
Expand All @@ -112,12 +125,11 @@ def get_service_state(self):
all_services = self.backend.get_all(
self.backend.url_endpoint_root + '/service', params)
except BackendException as e:
logger.error('Can\'t get services state \n' + str(e))
logger.warn('Alignak-app failed to collect services... \n' + str(e))

# Store Data
if all_services:
for service in all_services['_items']:
self.current_services[service['name']] = service['ls_state']
else:
logger.error('AlignakApp has collected 0 services !')

return self.current_services
6 changes: 4 additions & 2 deletions alignak_app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ def get_state(self):
:rtype: dict
"""

if not self.alignak_data.backend.authenticated:
logger.warn('Connection to backend is lost, application will try to reconnect !')
self.alignak_data.log_to_backend(self.config)

logger.info('Get state of Host and Services...')
# Dicts for states
hosts_states = {
Expand All @@ -232,7 +236,6 @@ def get_state(self):

if not hosts_data:
hosts_states['up'] = -1
logger.warning('Alignak-app failed to collect hosts states...')
else:
for _, v in hosts_data.items():
if 'UP' in v:
Expand All @@ -250,7 +253,6 @@ def get_state(self):
services_data = self.alignak_data.get_service_state()
if not services_data:
services_states['ok'] = -1
logger.warning('Alignak-app failed to collect services states...')
else:
for _, v in services_data.items():
if 'OK' in v:
Expand Down

0 comments on commit 9fa5813

Please sign in to comment.