Some checks failed
System Monitoring / Health Checks (push) Has been cancelled
System Monitoring / Performance Monitoring (push) Has been cancelled
System Monitoring / Database Monitoring (push) Has been cancelled
System Monitoring / Cache Monitoring (push) Has been cancelled
System Monitoring / Log Monitoring (push) Has been cancelled
System Monitoring / Resource Monitoring (push) Has been cancelled
System Monitoring / Uptime Monitoring (push) Has been cancelled
System Monitoring / Backup Monitoring (push) Has been cancelled
System Monitoring / Security Monitoring (push) Has been cancelled
System Monitoring / Monitoring Dashboard (push) Has been cancelled
System Monitoring / Alerting (push) Has been cancelled
Security Scanning / Dependency Scanning (push) Has been cancelled
Security Scanning / Code Security Scanning (push) Has been cancelled
Security Scanning / Secrets Scanning (push) Has been cancelled
Security Scanning / Container Security Scanning (push) Has been cancelled
Security Scanning / Compliance Checking (push) Has been cancelled
Security Scanning / Security Dashboard (push) Has been cancelled
Security Scanning / Security Remediation (push) Has been cancelled
132 lines
4.2 KiB
Python
132 lines
4.2 KiB
Python
"""
|
|
Django management command to start metrics collection.
|
|
"""
|
|
|
|
import time
|
|
import signal
|
|
import sys
|
|
from django.core.management.base import BaseCommand
|
|
from django.conf import settings
|
|
|
|
from ..exporters import metrics_collector
|
|
from ..alerts import alert_manager
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Start metrics collection and alert monitoring'
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
'--interval',
|
|
type=int,
|
|
default=30,
|
|
help='Metrics collection interval in seconds (default: 30)'
|
|
)
|
|
parser.add_argument(
|
|
'--alert-interval',
|
|
type=int,
|
|
default=60,
|
|
help='Alert checking interval in seconds (default: 60)'
|
|
)
|
|
parser.add_argument(
|
|
'--port',
|
|
type=int,
|
|
default=8001,
|
|
help='Metrics server port (default: 8001)'
|
|
)
|
|
parser.add_argument(
|
|
'--metrics-only',
|
|
action='store_true',
|
|
help='Only collect metrics, no alerts'
|
|
)
|
|
parser.add_argument(
|
|
'--alerts-only',
|
|
action='store_true',
|
|
help='Only check alerts, no metrics collection'
|
|
)
|
|
parser.add_argument(
|
|
'--quiet',
|
|
action='store_true',
|
|
help='Run quietly'
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
self.interval = options['interval']
|
|
self.alert_interval = options['alert_interval']
|
|
self.port = options['port']
|
|
self.metrics_only = options['metrics_only']
|
|
self.alerts_only = options['alerts_only']
|
|
self.quiet = options['quiet']
|
|
|
|
# Set up signal handlers for graceful shutdown
|
|
signal.signal(signal.SIGINT, self.signal_handler)
|
|
signal.signal(signal.SIGTERM, self.signal_handler)
|
|
|
|
self.running = True
|
|
|
|
if not self.quiet:
|
|
self.stdout.write(
|
|
self.style.SUCCESS('Starting metrics collection and alert monitoring...')
|
|
)
|
|
self.stdout.write(f'Metrics interval: {self.interval} seconds')
|
|
self.stdout.write(f'Alert interval: {self.alert_interval} seconds')
|
|
|
|
try:
|
|
# Start metrics collection
|
|
if not self.alerts_only:
|
|
if not self.quiet:
|
|
self.stdout.write('Starting metrics collection...')
|
|
metrics_collector.start_collection(self.interval)
|
|
|
|
# Start alert monitoring
|
|
if not self.metrics_only:
|
|
if not self.quiet:
|
|
self.stdout.write('Starting alert monitoring...')
|
|
self.start_alert_monitoring()
|
|
|
|
# Keep the command running
|
|
if not self.quiet:
|
|
self.stdout.write('Monitoring started. Press Ctrl+C to stop.')
|
|
|
|
while self.running:
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
if not self.quiet:
|
|
self.stdout.write('\nReceived interrupt signal, stopping...')
|
|
finally:
|
|
self.shutdown()
|
|
|
|
def start_alert_monitoring(self):
|
|
"""Start alert monitoring in a separate thread."""
|
|
import threading
|
|
|
|
def alert_monitor():
|
|
while self.running:
|
|
try:
|
|
alert_manager.check_rules()
|
|
time.sleep(self.alert_interval)
|
|
except Exception as e:
|
|
if not self.quiet:
|
|
self.stdout.write(
|
|
self.style.ERROR(f'Error in alert monitoring: {e}')
|
|
)
|
|
time.sleep(self.alert_interval)
|
|
|
|
alert_thread = threading.Thread(target=alert_monitor, daemon=True)
|
|
alert_thread.start()
|
|
|
|
def signal_handler(self, signum, frame):
|
|
"""Handle shutdown signals."""
|
|
if not self.quiet:
|
|
self.stdout.write(f'\nReceived signal {signum}, shutting down...')
|
|
self.running = False
|
|
|
|
def shutdown(self):
|
|
"""Shutdown the monitoring system."""
|
|
if not self.quiet:
|
|
self.stdout.write('Shutting down metrics collection...')
|
|
metrics_collector.stop_collection()
|
|
|
|
if not self.quiet:
|
|
self.stdout.write(self.style.SUCCESS('Monitoring stopped.')) |