chg: Improve monitoring script

pull/254/head
Raphaël Vinot 2021-08-31 15:38:03 +02:00
parent 0535614c32
commit 11dd288bc4
1 changed files with 51 additions and 4 deletions

View File

@ -3,10 +3,16 @@
import json
import logging
import os
import sys
from typing import List, Tuple
from redis import Redis
from redis.exceptions import ConnectionError
from lookyloo.helpers import get_config, get_socket_path, splash_status
from lookyloo.abstractmanager import AbstractManager
class Monitoring():
@ -18,13 +24,44 @@ class Monitoring():
self.redis_cache: Redis = Redis(unix_socket_path=get_socket_path('cache'), decode_responses=True)
self.redis_indexing: Redis = Redis(unix_socket_path=get_socket_path('indexing'), decode_responses=True)
@property
def backend_status(self):
socket_path_cache = get_socket_path('cache')
socket_path_index = get_socket_path('indexing')
backend_up = True
if not os.path.exists(socket_path_cache):
print(f'Socket path for the cache redis DB does not exists ({socket_path_cache}).')
backend_up = False
if not os.path.exists(socket_path_index):
print(f'Socket path for the indexing redis DB does not exists ({socket_path_index}).')
backend_up = False
if backend_up:
try:
cache_reachable = True if self.redis_cache.ping() else False
if not cache_reachable:
print('Unable to ping the redis cache db.')
backend_up = False
except ConnectionError:
print('Unable to connect to the redis cache db.')
backend_up = False
try:
indexing_reachable = True if self.redis_indexing.ping() else False
if not indexing_reachable:
print('Unable to ping the redis indexing db.')
backend_up = False
except ConnectionError:
print('Unable to connect to the redis indexing db.')
backend_up = False
return backend_up
@property
def queues(self):
return self.redis_cache.zrevrangebyscore('queues', 'Inf', '-Inf', withscores=True)
@property
def ongoing_captures(self):
captures_uuid = self.redis_cache.zrevrangebyscore('to_capture', 'Inf', '-Inf', withscores=True)
captures_uuid: List[Tuple[str, float]] = self.redis_cache.zrevrangebyscore('to_capture', 'Inf', '-Inf', withscores=True)
if not captures_uuid:
return []
to_return = []
@ -45,13 +82,23 @@ if __name__ == '__main__':
print('Splash is down: ', message)
m = Monitoring()
backend_up = m.backend_status
if not backend_up:
print('Backend not up, breaking.')
sys.exit()
print('Services currently running:')
running = AbstractManager.is_running()
for service, number in running:
print(service, f'({int(number)} service(s))')
print('Current queues:')
for q in m.queues:
print(q)
for q, priority in m.queues:
print(q, f'Priority: {int(priority)}')
# ------------------
print('Captures details:')
captures = m.ongoing_captures
print('Queue length', len(captures))
for uuid, rank, d in captures:
print(uuid, rank)
print(uuid, f'Rank: {int(rank)}')
print(json.dumps(d, indent=2))