lookyloo/tools/monitoring.py

111 lines
3.8 KiB
Python
Raw Normal View History

2021-08-31 14:57:12 +02:00
#!/usr/bin/env python3
2021-08-31 15:38:03 +02:00
import os
import sys
from typing import List, Tuple
2021-08-31 14:57:12 +02:00
from redis import Redis
2021-08-31 15:38:03 +02:00
from redis.exceptions import ConnectionError
2021-09-01 12:45:36 +02:00
from rich.console import Console
from rich.padding import Padding
2021-08-31 14:57:12 +02:00
2021-10-18 13:06:43 +02:00
from lookyloo.default import get_socket_path, AbstractManager
2021-08-31 14:57:12 +02:00
2021-09-01 12:45:36 +02:00
# NOTE: run with watch:
# watch --color tools/monitoring.py
console = Console(color_system="256")
2021-08-31 14:57:12 +02:00
class Monitoring():
def __init__(self) -> None:
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)
2021-08-31 15:38:03 +02:00
@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):
2021-09-01 12:45:36 +02:00
console.print(f'Socket path for the [blue]cache[/blue] redis DB [red]does not exists[/red] ({socket_path_cache}).')
2021-08-31 15:38:03 +02:00
backend_up = False
if not os.path.exists(socket_path_index):
2021-09-01 12:45:36 +02:00
console.print(f'Socket path for the [blue]indexing[/blue] redis DB [red]does not exists[/red] ({socket_path_index}).')
2021-08-31 15:38:03 +02:00
backend_up = False
if backend_up:
try:
cache_reachable = True if self.redis_cache.ping() else False
if not cache_reachable:
2021-09-01 12:45:36 +02:00
console.print('Unable to ping the redis cache db.')
2021-08-31 15:38:03 +02:00
backend_up = False
except ConnectionError:
2021-09-01 12:45:36 +02:00
console.print('Unable to connect to the redis cache db.')
2021-08-31 15:38:03 +02:00
backend_up = False
try:
indexing_reachable = True if self.redis_indexing.ping() else False
if not indexing_reachable:
2021-09-01 12:45:36 +02:00
console.print('Unable to ping the redis indexing db.')
2021-08-31 15:38:03 +02:00
backend_up = False
except ConnectionError:
2021-09-01 12:45:36 +02:00
console.print('Unable to connect to the redis indexing db.')
2021-08-31 15:38:03 +02:00
backend_up = False
return backend_up
2021-08-31 14:57:12 +02:00
@property
def queues(self):
return self.redis_cache.zrevrangebyscore('queues', 'Inf', '-Inf', withscores=True)
@property
def ongoing_captures(self):
2021-08-31 15:38:03 +02:00
captures_uuid: List[Tuple[str, float]] = self.redis_cache.zrevrangebyscore('to_capture', 'Inf', '-Inf', withscores=True)
2021-08-31 14:57:12 +02:00
if not captures_uuid:
return []
to_return = []
for uuid, rank in captures_uuid:
capture_params = self.redis_cache.hgetall(uuid)
if capture_params:
to_return.append((uuid, rank, capture_params))
return to_return
@property
def tree_cache(self):
return self.redis_cache.hgetall('tree_cache')
2021-08-31 14:57:12 +02:00
if __name__ == '__main__':
m = Monitoring()
2021-08-31 15:38:03 +02:00
backend_up = m.backend_status
if not backend_up:
2021-09-01 12:45:36 +02:00
console.print('[bold red]Backend not up, breaking.[/bold red]')
2021-08-31 15:38:03 +02:00
sys.exit()
2021-09-01 12:45:36 +02:00
console.print('Services currently running:')
2021-08-31 15:38:03 +02:00
running = AbstractManager.is_running()
for service, number in running:
2021-09-01 12:45:36 +02:00
s = Padding(f'{service} ({int(number)} service(s))', (0, 2))
console.print(s)
2021-08-31 15:38:03 +02:00
console.print('Current cache status:')
for name, status in m.tree_cache.items():
s = Padding(f'{name}: {status}', (0, 2))
console.print(s)
2021-09-01 12:45:36 +02:00
console.print('Current queues:')
2021-08-31 15:38:03 +02:00
for q, priority in m.queues:
2021-09-07 16:28:50 +02:00
s = Padding(f'{q} Recently enqueued captures: {int(priority)}', (0, 2))
2021-09-01 12:45:36 +02:00
console.print(s)
2021-08-31 14:57:12 +02:00
# ------------------
2021-09-01 12:45:36 +02:00
console.print('Captures details:')
2021-08-31 14:57:12 +02:00
captures = m.ongoing_captures
2021-09-01 12:45:36 +02:00
console.print(f'Queue length: [yellow]{len(captures)}[/yellow]')
2021-08-31 14:57:12 +02:00
for uuid, rank, d in captures:
2021-09-01 12:45:36 +02:00
a = Padding(f'{uuid} Rank: {int(rank)}', (0, 2))
console.print(a)
console.print(d)