Skip to content

Commit

Permalink
Make the logcheckresult post update the corresponding item live state
Browse files Browse the repository at this point in the history
Make the logcheckresult allow to post with names for host and service
Add some print degug information for the LCR handling
  • Loading branch information
mohierf authored and ddurieux committed Nov 30, 2017
1 parent d4e9d4e commit 343a98e
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 81 deletions.
122 changes: 109 additions & 13 deletions alignak_backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ def pre_delete(resource, user_request, lookup):
resources_delete_custom = g.get('resources_delete_custom', {})
users_id = g.get('users_id', {})

print("delete: %s" % (resources_delete))
if resource not in resources_delete and resource not in resources_delete_custom:
abort(401, description='Not allowed to DELETE on this endpoint / resource.')
else:
Expand Down Expand Up @@ -452,7 +451,7 @@ def pre_history_post(items):
# Log checks results
def pre_logcheckresult_post(items):
"""
Hook before adding new forcecheck
Hook before adding new logcheckresult
:param items: logcheckresult fields
:type items: dict
Expand All @@ -461,17 +460,40 @@ def pre_logcheckresult_post(items):
hosts_drv = current_app.data.driver.db['host']
services_drv = current_app.data.driver.db['service']
for dummy, item in enumerate(items):
# Set _realm as host's _realm
host = hosts_drv.find_one({'_id': item['host']})
item['_realm'] = host['_realm']
item['host_name'] = host['name']
if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("LCR - got a check result: %s" % item)

# Find service_name
if item['service'] and 'service_name' not in item:
service = services_drv.find_one({'_id': item['service']})
item['service_name'] = service['name']
if not item.get('host') and not item.get('host_name'):
abort(make_response("Posting LCR without host information is not accepted.", 412))

# Find the concerned host
if not item.get('host'):
host = hosts_drv.find_one({'name': item['host_name']})
item['host'] = host['_id']
else:
host = hosts_drv.find_one({'_id': item['host']})
item['host_name'] = host['name']

if not item.get('service') and not item.get('service_name'):
# This is valid for an host check result
item['service'] = None
item['service_name'] = ''
else:
# We got a service check result
if item.get('service_name') and not item.get('service'):
service = services_drv.find_one(
{'host': item['host'], 'name': item['service_name']})
item['service'] = service['_id']

if item.get('service') and not item.get('service_name'):
service = services_drv.find_one({'_id': item['service']})
item['service_name'] = service['name']

# Set _realm as host's _realm
item['_realm'] = host['_realm']

if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("LCR - inserting an LCR for %s/%s..." % (item['host_name'], item['service_name']))


def after_insert_logcheckresult(items):
Expand All @@ -483,7 +505,78 @@ def after_insert_logcheckresult(items):
:return: None
"""
for dummy, item in enumerate(items):
# Create an history event for the new forcecheck
if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("LCR - inserted an LCR for %s/%s..." % (item['host_name'], item['service_name']))
print(" -> %s..." % item)
# # Update the livestate...
if item['service']:
# ...for a service
lookup = {"_id": item['service']}
data = {
'ls_state': item['state'],
'ls_state_type': item['state_type'],
'ls_state_id': item['state_id'],
'ls_acknowledged': item['acknowledged'],
'ls_acknowledgement_type': item['acknowledgement_type'],
'ls_downtimed': item['downtimed'],
'ls_last_check': item['last_check'],
'ls_last_state': item['last_state'],
'ls_last_state_type': item['last_state_type'],
'ls_output': item['output'],
'ls_long_output': item['long_output'],
'ls_perf_data': item['perf_data'],
'ls_current_attempt': item['current_attempt'],
'ls_max_attempts': item['max_attempts'],
'ls_latency': item['latency'],
'ls_execution_time': item['execution_time'],
'ls_passive_check': item['passive_check'],
'ls_state_changed': item['state_changed'],
'ls_last_state_changed': item['last_state_changed'],
'ls_last_hard_state_changed': item['last_hard_state_changed'],
'ls_last_time_ok': item['last_time_0'],
'ls_last_time_warning': item['last_time_1'],
'ls_last_time_critical': item['last_time_2'],
'ls_last_time_unknown': item['last_time_3'],
'ls_last_time_unreachable': item['last_time_4']
}
(pi_a, pi_b, pi_c, pi_d) = patch_internal('service', data, False, False, **lookup)
if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("LCR - internal patch result: %s, %s, %s, %s" % (pi_a, pi_b, pi_c, pi_d))
else:
# ...for an host
lookup = {"_id": item['host']}
data = {
'ls_state': item['state'],
'ls_state_type': item['state_type'],
'ls_state_id': item['state_id'],
'ls_acknowledged': item['acknowledged'],
'ls_acknowledgement_type': item['acknowledgement_type'],
'ls_downtimed': item['downtimed'],
'ls_last_check': item['last_check'],
'ls_last_state': item['last_state'],
'ls_last_state_type': item['last_state_type'],
'ls_output': item['output'],
'ls_long_output': item['long_output'],
'ls_perf_data': item['perf_data'],
'ls_current_attempt': item['current_attempt'],
'ls_max_attempts': item['max_attempts'],
'ls_latency': item['latency'],
'ls_execution_time': item['execution_time'],
'ls_passive_check': item['passive_check'],
'ls_state_changed': item['state_changed'],
'ls_last_state_changed': item['last_state_changed'],
'ls_last_hard_state_changed': item['last_hard_state_changed'],
'ls_last_time_up': item['last_time_0'],
'ls_last_time_down': item['last_time_1'],
# 'ls_last_time_2': item['last_time_2'],
# 'ls_last_time_3': item['last_time_3'],
'ls_last_time_unreachable': item['last_time_4']
}
(pi_a, pi_b, pi_c, pi_d) = patch_internal('host', data, False, False, **lookup)
if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("LCR - internal patch result: %s, %s, %s, %s" % (pi_a, pi_b, pi_c, pi_d))

# Create an history event for the new logcheckresult
message = "%s[%s] (%s/%s): %s" % (item['state'], item['state_type'],
item['acknowledged'], item['downtimed'],
item['output'])
Expand All @@ -500,6 +593,7 @@ def after_insert_logcheckresult(items):
post_internal("history", data, True)


# Actions
def pre_post_action_right(actrequestp):
"""Deny post on action* endpoint if the logged-in user do not have can_submit_commands
Expand Down Expand Up @@ -1188,7 +1282,8 @@ def pre_delete_host(item):
:type item: dict
:return: None
"""
print("Deleting host: %s" % item['name'])
if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("Deleting host: %s" % item['name'])
services_drv = current_app.data.driver.db['service']
services = services_drv.find({'host': item['_id']})
for service in services:
Expand All @@ -1204,7 +1299,8 @@ def after_delete_host(item):
:type item: dict
:return: None
"""
print("Deleted host: %s" % item['name'])
if 'ALIGNAK_BACKEND_PRINT' in os.environ:
print("Deleted host: %s" % item['name'])


# Alignak
Expand Down
53 changes: 31 additions & 22 deletions alignak_backend/models/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_schema():
'schema': {
'schema_version': {
'type': 'integer',
'default': 1,
'default': 2,
},
# Importation source
'imported_from': {
Expand Down Expand Up @@ -803,13 +803,6 @@ def get_schema():
'type': 'boolean',
'default': False
},
'ls_impact': {
'schema_version': 1,
'title': 'Impact',
'comment': 'Is an impact?',
'type': 'boolean',
'default': False
},
'ls_last_check': {
'schema_version': 1,
'title': 'Check timestamp',
Expand All @@ -833,13 +826,8 @@ def get_schema():
'default': 'HARD',
'allowed': ['HARD', 'SOFT']
},
'ls_last_state_changed': {
'schema_version': 1,
'title': 'Last state changed',
'comment': 'Last state changed timestamp',
'type': 'integer',
'default': 0
},

# Not in the host LCR
'ls_next_check': {
'schema_version': 1,
'title': 'Next check',
Expand Down Expand Up @@ -907,16 +895,21 @@ def get_schema():
'default': False
},

# todo - Attempt number - difference with ls_current_attemp?
'ls_attempt': {
# Last time state changed
'ls_state_changed': {
'schema_version': 2,
'title': 'State changed',
'comment': 'The state has changed with the last check?',
'type': 'integer',
'default': 0
},
'ls_last_state_changed': {
'schema_version': 1,
'title': 'Current attempt number',
'comment': '',
'title': 'Last state changed',
'comment': 'Last state changed timestamp',
'type': 'integer',
'default': 0
},

# Last time hard state changed
'ls_last_hard_state_changed': {
'schema_version': 1,
'title': 'Last time hard state changed',
Expand All @@ -926,6 +919,7 @@ def get_schema():
},

# Last time in the corresponding state
# Not in the host LCR
'ls_last_time_up': {
'schema_version': 1,
'title': 'Last time up',
Expand Down Expand Up @@ -1083,5 +1077,20 @@ def get_schema():
'default': []
},
},
'schema_deleted': {}
'schema_deleted': {
'ls_impact': {
'schema_version': 1,
'title': 'Impact',
'comment': 'Is an impact?',
'type': 'boolean',
'default': False
},
'ls_attempt': {
'schema_version': 1,
'title': 'Current attempt number',
'comment': '',
'type': 'integer',
'default': 0
},
},
}
Loading

0 comments on commit 343a98e

Please sign in to comment.