Skip to content

Commit

Permalink
Improve manage of problems and their update
Browse files Browse the repository at this point in the history
Ref #287
  • Loading branch information
algorys committed Mar 24, 2018
1 parent 49b9b6b commit e167121
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 66 deletions.
90 changes: 40 additions & 50 deletions alignak_app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def query_user(self):

def query_hosts(self):
"""
Launch request on "host" endpoint
Launch request on "host" endpoint, add hosts in problems if needed
"""

Expand All @@ -351,6 +351,15 @@ def query_hosts(self):
)
hosts_list.append(host)

# If host is a problem, add / update it
if host.is_problem():
if data_manager.get_item('problems', host.item_id):
data_manager.update_item_data('problems', host.item_id, host.data)
else:
data_manager.database['problems'].append(host)

data_manager.db_is_ready['problems']['host'] = True

if hosts_list:
data_manager.update_database('host', hosts_list)
if 'OK' in request['_status']:
Expand Down Expand Up @@ -398,65 +407,46 @@ def query_services(self, host_id=None):
if 'OK' in request['_status']:
data_manager.db_is_ready[request_data['endpoint']] = True

def query_problems(self, states):
def query_services_problems(self, state):
"""
Launch requests on "service" endpoint to get items in problems and add hosts in problems:
Launch requests on "service" endpoint to get items with "ls_state = state"
* **Hosts**: ``DOWN``, ``UNREACHABLE``
* **Services**: ``WARNING``, ``CRITICAL``, ``UNKNOWN``
"""
Wanted states are: ``WARNING``, ``CRITICAL`` and ``UNKNOWN``
# Reset if states is equal to 3 (WARNING, CRITICAL, UNKNOWN)
if len(states) == 3:
data_manager.new_database['problems'] = []
:param state: state of service
:type state: str
"""

# Services
services_projection = [
'name', 'host', 'alias', 'ls_state', 'ls_output', 'ls_acknowledged', 'ls_downtimed'
]

for state in states:
params = {'where': json.dumps({'_is_template': False, 'ls_state': state})}
request = self.get(
'service',
params,
services_projection,
all_items=True
)
if request:
for item in request['_items']:
if not item['ls_acknowledged'] and not item['ls_downtimed']:
service = Service()
service.create(
item['_id'],
item,
item['name']
)
data_manager.new_database['problems'].append(service)

# If new problems
if data_manager.new_database['problems']:
# Append states
data_manager.db_is_ready['problems'].append(states)

# If problems is the last, add hosts to problems
if len(data_manager.db_is_ready['problems']) > 2:
for host in data_manager.database['host']:
if host.data['ls_state'] in ['DOWN', 'UNREACHABLE'] and \
not host.data['ls_acknowledged'] and \
not host.data['ls_downtimed'] and \
not data_manager.get_item('problems', host.item_id):
data_manager.new_database['problems'].append(host)

# Update "problems" database
data_manager.database['problems'] = []
while data_manager.new_database['problems']:
data_manager.database['problems'].append(
data_manager.new_database['problems'].pop()
params = {'where': json.dumps({'_is_template': False, 'ls_state': state})}
request = self.get(
'service',
params,
services_projection,
all_items=True
)

if request:
for item in request['_items']:
if not item['ls_acknowledged'] and not item['ls_downtimed']:
service = Service()
service.create(
item['_id'],
item,
item['name']
)
logger.info("Update database[problems]...")
if len(data_manager.db_is_ready['problems']) > 3:
data_manager.db_is_ready['problems'] = []

if data_manager.get_item('problems', service.item_id):
data_manager.update_item_data('problems', service.item_id, service.data)
else:
data_manager.database['problems'].append(service)
# Problems state is ready
data_manager.db_is_ready['problems'][state] = True
logger.info("Update database[problems] for services %s..", state)

def query_alignakdaemons(self):
"""
Expand Down
13 changes: 8 additions & 5 deletions alignak_app/backend/datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
++++++++++++
DataManager manage alignak data provided by
:class:`Client <alignak_app.backend.backend.BackendClient>`.
* ``database`` fied contains all data collected by App
* ``db_is_ready`` fied says to App if database has been filled or not (needed on start)
* ``old_notifications`` fied store old notifications from backend to avoid sending them again
"""

from logging import getLogger
Expand Down Expand Up @@ -52,17 +57,14 @@ def __init__(self):
'timeperiod': [],
'problems': [],
}
self.new_database = {
'problems': []
}
self.db_is_ready = {
'livesynthesis': False,
'alignakdaemon': False,
'host': False,
'user': False,
'realm': False,
'timeperiod': False,
'problems': [],
'problems': {'CRITICAL': False, 'WARNING': False, 'UNKNOWN': False, 'host': False},
}
self.old_notifications = []

Expand All @@ -80,7 +82,8 @@ def is_ready(self):
if db_name not in 'problems':
assert self.db_is_ready[db_name]
else:
assert len(self.db_is_ready[db_name]) > 2
for problem in self.db_is_ready['problems']:
assert self.db_is_ready['problems'][problem]
cur_collected = _('READY')
except AssertionError:
cur_collected = _('Collecting %s...') % db_name
Expand Down
14 changes: 14 additions & 0 deletions alignak_app/items/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ def get_display_name(self):
return self.data['alias'].title()

return self.name.title()

def is_problem(self):
"""
Return True if host is a problem, else return False
:return: if host in problem or not
:rtype: bool
"""

if self.data['ls_state'] in ['DOWN', 'UNREACHABLE'] and not self.data['ls_acknowledged'] \
and not self.data['ls_downtimed']:
return True

return False
4 changes: 1 addition & 3 deletions alignak_app/qobjects/threads/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def run(self): # pragma: no cover
elif 'service' in self.thread_name:
app_backend.query_services()
elif self.thread_name in self.problem_states:
app_backend.query_problems([self.thread_name])
elif 'problems' in self.thread_name:
app_backend.query_problems(self.problem_states)
app_backend.query_services_problems(self.thread_name)
elif 'alignakdaemon' in self.thread_name:
app_backend.query_alignakdaemons()
elif 'livesynthesis' in self.thread_name:
Expand Down
4 changes: 2 additions & 2 deletions alignak_app/qobjects/threads/threadmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def get_threads_to_launch(self):
threads_to_launch = []

# Add BackendQThread only if they are not already running
for cur_thread in ['livesynthesis', 'host', 'user', 'realm', 'timeperiod',
'alignakdaemon', 'notifications', 'history', 'problems']:
for cur_thread in ['livesynthesis', 'host', 'user', 'realm', 'timeperiod', 'alignakdaemon',
'notifications', 'history', 'CRITICAL', 'WARNING', 'UNKNOWN']:
if self.current_thread:
if cur_thread != self.current_thread.thread_name:
threads_to_launch.append(cur_thread)
Expand Down
4 changes: 2 additions & 2 deletions test/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ def test_query_problems(self):
)

from alignak_app.backend.datamanager import data_manager
under_test.query_problems(['WARNING', 'UNKNOWN'])
under_test.query_services_problems('WARNING')

self.assertIsNotNone(data_manager.database['problems'])

self.assertTrue(data_manager.db_is_ready['problems'])
self.assertTrue(data_manager.db_is_ready['problems']['WARNING'])

def test_query_daemons_data(self):
"""Query Daemons Data"""
Expand Down
8 changes: 4 additions & 4 deletions test/test_thread_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_initialize_thread_manager(self):

self.assertFalse(under_test.current_thread)
self.assertFalse(under_test.priority_threads)
for thread in ['livesynthesis', 'host', 'problems', 'user',
'alignakdaemon', 'notifications', 'history']:
for thread in ['livesynthesis', 'host', 'user', 'realm', 'timeperiod', 'alignakdaemon',
'notifications', 'history', 'CRITICAL', 'WARNING', 'UNKNOWN']:
self.assertTrue(thread in under_test.threads_to_launch)

def test_get_threads_to_launch(self):
Expand All @@ -56,8 +56,8 @@ def test_get_threads_to_launch(self):

# If there is no current thread, all threads are added
self.assertIsNone(thread_mgr_test.current_thread)
for thread in ['livesynthesis', 'host', 'problems', 'user', 'alignakdaemon',
'notifications', 'history']:
for thread in ['livesynthesis', 'host', 'user', 'realm', 'timeperiod', 'alignakdaemon',
'notifications', 'history', 'CRITICAL', 'WARNING', 'UNKNOWN']:
self.assertTrue(thread in under_test)

thread_mgr_test.current_thread = BackendQThread('user')
Expand Down

0 comments on commit e167121

Please sign in to comment.