def on_inserted_service(items): """ What to do when a service is inserted in the backend ... """ livesynthesis_db = current_app.data.driver.db['livesynthesis'] for _, item in enumerate(items): if item['_is_template']: continue live_current = livesynthesis_db.find_one({'_realm': item['_realm']}) if live_current is None: ls = Livesynthesis() ls.recalculate() else: typecheck = 'services' if not item['active_checks_enabled'] and not item['passive_checks_enabled']: data = {"$inc": {"%s_not_monitored" % typecheck: 1, "%s_total" % typecheck: 1}} else: data = {"$inc": {"%s_%s_%s" % (typecheck, item['ls_state'].lower(), item['ls_state_type'].lower()): 1, "%s_total" % typecheck: 1}} current_app.logger.debug("LS - inserted service %s: %s...", item['name'], data) current_app.data.driver.db.livesynthesis.update({'_id': live_current['_id']}, data) # Send livesynthesis to TSDB live_current = livesynthesis_db.find_one({'_realm': item['_realm']}) Timeseries.send_livesynthesis_metrics(item['_realm'], live_current)
def on_updated_host(updated, original): """ What to do when an host live state is updated ... If the host monitored state is changing, recalculate the live synthesis, else simply update the livesynthesis """ if original['_is_template']: return # If the host is not monitored and we do not change its monitoring state if not original['active_checks_enabled'] and not original['passive_checks_enabled'] \ and 'active_checks_enabled' not in updated \ and 'passive_checks_enabled' not in updated: return minus, plus = Livesynthesis.livesynthesis_to_update('hosts', updated, original) if minus is not False: livesynthesis_db = current_app.data.driver.db['livesynthesis'] live_current = livesynthesis_db.find_one({'_realm': original['_realm']}) if live_current is None or 'not_monitored' in minus \ or (plus and 'not_monitored' in plus): ls = Livesynthesis() ls.recalculate() else: data = {"$inc": {minus: -1}} if plus is not False: data = {"$inc": {minus: -1, plus: 1}} current_app.logger.debug("LS - updated host %s: %s...", original['name'], data) current_app.data.driver.db.livesynthesis.update({'_id': live_current['_id']}, data) # Send livesynthesis to TSDB live_current = livesynthesis_db.find_one({'_realm': original['_realm']}) Timeseries.send_livesynthesis_metrics(original['_realm'], live_current)
def on_deleted_service(item): """When delete a service, decrement livesynthesis :param item: fields of the item deleted :type item: dict :return: None """ if item['_is_template']: return livesynthesis_db = current_app.data.driver.db['livesynthesis'] live_current = livesynthesis_db.find_one({'_realm': item['_realm']}) if live_current is None: ls = Livesynthesis() ls.recalculate() else: minus = Livesynthesis.livesynthesis_to_delete('services', item) data = {"$inc": {minus: -1, 'services_total': -1}} current_app.logger.debug("LS - Deleted service %s: %s", item['name'], data) current_app.data.driver.db.livesynthesis.update({'_id': live_current['_id']}, data) # Send livesynthesis to TSDB live_current = livesynthesis_db.find_one({'_realm': item['_realm']}) Timeseries.send_livesynthesis_metrics(item['_realm'], live_current)
def recalculate(): """ Recalculate all the live synthesis counters """ current_app.logger.debug("LS - Recalculating...") livesynthesis = current_app.data.driver.db['livesynthesis'] realmsdrv = current_app.data.driver.db['realm'] allrealms = realmsdrv.find() for _, realm in enumerate(allrealms): live_current = livesynthesis.find_one({'_realm': realm['_id']}) if live_current is None: current_app.logger.debug(" new LS for realm %s", realm['name']) data = { 'hosts_total': 0, 'hosts_not_monitored': 0, 'hosts_up_hard': 0, 'hosts_up_soft': 0, 'hosts_down_hard': 0, 'hosts_down_soft': 0, 'hosts_unreachable_hard': 0, 'hosts_unreachable_soft': 0, 'hosts_acknowledged': 0, 'hosts_in_downtime': 0, 'hosts_flapping': 0, 'services_total': 0, 'services_not_monitored': 0, 'services_ok_hard': 0, 'services_ok_soft': 0, 'services_warning_hard': 0, 'services_warning_soft': 0, 'services_critical_hard': 0, 'services_critical_soft': 0, 'services_unreachable_hard': 0, 'services_unreachable_soft': 0, 'services_unknown_hard': 0, 'services_unknown_soft': 0, 'services_acknowledged': 0, 'services_in_downtime': 0, 'services_flapping': 0, '_realm': realm['_id'] } livesynthesis.insert(data) live_current = livesynthesis.find_one({'_realm': realm['_id']}) # Update hosts live synthesis hosts = current_app.data.driver.db['host'] hosts_count = hosts.count({'_is_template': False, '_realm': realm['_id']}) data = {'hosts_total': hosts_count} data['hosts_not_monitored'] = hosts.count({ '_is_template': False, 'active_checks_enabled': False, 'passive_checks_enabled': False, '_realm': realm['_id'] }) data['hosts_up_hard'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UP', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['hosts_down_hard'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'DOWN', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['hosts_unreachable_hard'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UNREACHABLE', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['hosts_up_soft'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UP', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['hosts_down_soft'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'DOWN', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['hosts_unreachable_soft'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UNREACHABLE', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['hosts_acknowledged'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_acknowledged': True, '_realm': realm['_id'] }) data['hosts_in_downtime'] = hosts.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_downtimed': True, '_realm': realm['_id'] }) current_app.logger.debug(" realm %s, hosts LS: %s", realm['name'], data) lookup = {"_id": live_current['_id']} patch_internal('livesynthesis', data, False, False, **lookup) # Send livesynthesis to TSDB Timeseries.send_livesynthesis_metrics(realm['_id'], data) # Update services live synthesis services = current_app.data.driver.db['service'] services_count = services.count({'_is_template': False, '_realm': realm['_id']}) data = {'services_total': services_count} data['services_not_monitored'] = services.count({ '_is_template': False, 'active_checks_enabled': False, 'passive_checks_enabled': False, '_realm': realm['_id'] }) data['services_ok_hard'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'OK', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_warning_hard'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'WARNING', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_critical_hard'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'CRITICAL', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_unknown_hard'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UNKNOWN', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_unreachable_hard'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UNREACHABLE', 'ls_state_type': 'HARD', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_ok_soft'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'OK', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_warning_soft'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'WARNING', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_critical_soft'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'CRITICAL', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_unknown_soft'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UNKNOWN', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_unreachable_soft'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_state': 'UNREACHABLE', 'ls_state_type': 'SOFT', 'ls_acknowledged': False, '_realm': realm['_id'] }) data['services_acknowledged'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_acknowledged': True, '_realm': realm['_id'] }) data['services_in_downtime'] = services.count({ '_is_template': False, '$or': [{'active_checks_enabled': True}, {'passive_checks_enabled': True}], 'ls_downtimed': True, '_realm': realm['_id'] }) current_app.logger.debug(" realm %s, services LS: %s", realm['name'], data) lookup = {"_id": live_current['_id']} patch_internal('livesynthesis', data, False, False, **lookup) # Send livesynthesis to TSDB Timeseries.send_livesynthesis_metrics(realm['_id'], data)