File size: 25,124 Bytes
2998089 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
import json
import time
import smtplib
import logging
import numpy as np
from pathlib import Path
from email.mime.text import MIMEText
from datetime import datetime, timedelta
from dataclasses import dataclass, asdict
from collections import defaultdict, deque
from email.mime.multipart import MIMEMultipart
from typing import Dict, List, Optional, Any, Callable
logger = logging.getLogger(__name__)
@dataclass
class Alert:
"""Alert data structure"""
id: str
timestamp: str
type: str # 'info', 'warning', 'critical'
category: str # 'system', 'api', 'model', 'prediction'
title: str
message: str
source: str
severity_score: float
metadata: Dict[str, Any]
acknowledged: bool = False
resolved: bool = False
resolution_time: Optional[str] = None
@dataclass
class AlertRule:
"""Alert rule configuration"""
id: str
name: str
category: str
condition: Dict[str, Any]
threshold: float
severity: str
cooldown_minutes: int
enabled: bool = True
class AlertSystem:
"""Comprehensive alerting and notification system"""
def __init__(self, base_dir: Path):
self.base_dir = Path(base_dir)
self.monitor_dir = self.base_dir / "monitor"
self.monitor_dir.mkdir(parents=True, exist_ok=True)
# Storage paths
self.alerts_log_path = self.monitor_dir / "alerts.json"
self.alert_rules_path = self.monitor_dir / "alert_rules.json"
self.alert_config_path = self.monitor_dir / "alert_config.json"
# In-memory storage
self.active_alerts = {} # alert_id -> Alert
self.alert_history = deque(maxlen=10000)
self.alert_rules = {} # rule_id -> AlertRule
self.alert_cooldowns = defaultdict(float) # rule_id -> last_triggered_time
# Notification channels
self.notification_handlers = {}
# Alert statistics
self.alert_stats = {
'total_alerts': 0,
'alerts_by_type': defaultdict(int),
'alerts_by_category': defaultdict(int),
'resolution_times': []
}
# Load configuration and rules
self.load_alert_configuration()
self.load_alert_rules()
self.load_alert_history()
def add_notification_handler(self, name: str, handler: Callable):
"""Add a custom notification handler"""
self.notification_handlers[name] = handler
logger.info(f"Added notification handler: {name}")
def create_alert(self,
alert_type: str,
category: str,
title: str,
message: str,
source: str,
metadata: Dict[str, Any] = None,
severity_score: float = 0.5) -> str:
"""Create a new alert"""
alert_id = self._generate_alert_id(category, title)
# Check if similar alert already exists
if self._is_duplicate_alert(alert_id, category, title):
logger.debug(f"Duplicate alert suppressed: {title}")
return alert_id
alert = Alert(
id=alert_id,
timestamp=datetime.now().isoformat(),
type=alert_type,
category=category,
title=title,
message=message,
source=source,
severity_score=severity_score,
metadata=metadata or {},
acknowledged=False,
resolved=False
)
# Store alert
self.active_alerts[alert_id] = alert
self.alert_history.append(alert)
# Update statistics
self.alert_stats['total_alerts'] += 1
self.alert_stats['alerts_by_type'][alert_type] += 1
self.alert_stats['alerts_by_category'][category] += 1
# Save to log
self._append_to_log(self.alerts_log_path, asdict(alert))
# Send notifications
self._send_notifications(alert)
logger.info(f"Created {alert_type} alert: {title}")
return alert_id
def acknowledge_alert(self, alert_id: str, acknowledger: str = "system") -> bool:
"""Acknowledge an alert"""
if alert_id in self.active_alerts:
alert = self.active_alerts[alert_id]
alert.acknowledged = True
alert.metadata['acknowledged_by'] = acknowledger
alert.metadata['acknowledged_at'] = datetime.now().isoformat()
self._append_to_log(self.alerts_log_path, {
'action': 'acknowledge',
'alert_id': alert_id,
'acknowledger': acknowledger,
'timestamp': datetime.now().isoformat()
})
logger.info(f"Alert acknowledged: {alert_id} by {acknowledger}")
return True
return False
def resolve_alert(self, alert_id: str, resolver: str = "system", resolution_note: str = "") -> bool:
"""Resolve an alert"""
if alert_id in self.active_alerts:
alert = self.active_alerts[alert_id]
alert.resolved = True
alert.resolution_time = datetime.now().isoformat()
alert.metadata['resolved_by'] = resolver
alert.metadata['resolution_note'] = resolution_note
# Calculate resolution time
alert_time = datetime.fromisoformat(alert.timestamp)
resolution_time = datetime.now()
resolution_duration = (resolution_time - alert_time).total_seconds() / 60 # minutes
self.alert_stats['resolution_times'].append(resolution_duration)
# Remove from active alerts
del self.active_alerts[alert_id]
self._append_to_log(self.alerts_log_path, {
'action': 'resolve',
'alert_id': alert_id,
'resolver': resolver,
'resolution_note': resolution_note,
'resolution_duration_minutes': resolution_duration,
'timestamp': datetime.now().isoformat()
})
logger.info(f"Alert resolved: {alert_id} by {resolver}")
return True
return False
def check_metric_thresholds(self, metrics: Dict[str, Any]):
"""Check metrics against alert rules"""
for rule_id, rule in self.alert_rules.items():
if not rule.enabled:
continue
# Check cooldown
if self._is_in_cooldown(rule_id, rule.cooldown_minutes):
continue
# Evaluate rule condition
if self._evaluate_rule_condition(rule, metrics):
self._trigger_rule_alert(rule, metrics)
def check_anomaly_detection(self,
current_metrics: Dict[str, Any],
historical_metrics: List[Dict[str, Any]]):
"""Check for anomalies using statistical methods"""
if len(historical_metrics) < 10: # Need sufficient history
return
# Define metrics to monitor for anomalies
anomaly_metrics = {
'response_time': 'api.avg_response_time',
'error_rate': 'api.error_rate',
'cpu_usage': 'system.cpu_percent',
'memory_usage': 'system.memory_percent',
'confidence': 'model.avg_confidence'
}
for metric_name, metric_path in anomaly_metrics.items():
try:
# Extract historical values
historical_values = []
for hist_metric in historical_metrics:
value = self._get_nested_value(hist_metric, metric_path)
if value is not None:
historical_values.append(value)
if len(historical_values) < 5:
continue
# Get current value
current_value = self._get_nested_value(current_metrics, metric_path)
if current_value is None:
continue
# Statistical anomaly detection
mean_val = np.mean(historical_values)
std_val = np.std(historical_values)
# Z-score based detection
if std_val > 0:
z_score = abs(current_value - mean_val) / std_val
if z_score > 3: # 3 sigma threshold
self.create_alert(
alert_type='warning',
category='anomaly',
title=f'Anomaly Detected: {metric_name}',
message=f'{metric_name} value {current_value:.2f} is {z_score:.1f} standard deviations from normal',
source='anomaly_detection',
metadata={
'metric_name': metric_name,
'current_value': current_value,
'historical_mean': mean_val,
'historical_std': std_val,
'z_score': z_score
},
severity_score=min(z_score / 5, 1.0)
)
except Exception as e:
logger.error(f"Error in anomaly detection for {metric_name}: {e}")
def get_active_alerts(self) -> List[Alert]:
"""Get all active alerts"""
return list(self.active_alerts.values())
def get_alerts_by_category(self, category: str, hours: int = 24) -> List[Alert]:
"""Get alerts by category within time period"""
cutoff_time = datetime.now() - timedelta(hours=hours)
return [
alert for alert in self.alert_history
if (alert.category == category and
datetime.fromisoformat(alert.timestamp) > cutoff_time)
]
def get_alert_statistics(self) -> Dict[str, Any]:
"""Get alert statistics and metrics"""
active_count = len(self.active_alerts)
# Recent alerts (last 24 hours)
recent_alerts = self.get_recent_alerts(hours=24)
# Resolution time statistics
resolution_times = self.alert_stats['resolution_times']
resolution_stats = {}
if resolution_times:
resolution_stats = {
'avg_resolution_time_minutes': float(np.mean(resolution_times)),
'median_resolution_time_minutes': float(np.median(resolution_times)),
'max_resolution_time_minutes': float(np.max(resolution_times)),
'min_resolution_time_minutes': float(np.min(resolution_times))
}
return {
'active_alerts': active_count,
'total_alerts_24h': len(recent_alerts),
'alerts_by_type': dict(self.alert_stats['alerts_by_type']),
'alerts_by_category': dict(self.alert_stats['alerts_by_category']),
'resolution_statistics': resolution_stats,
'alert_rate_per_hour': len(recent_alerts) / 24.0,
'critical_alerts_active': len([a for a in self.active_alerts.values() if a.type == 'critical']),
'unacknowledged_alerts': len([a for a in self.active_alerts.values() if not a.acknowledged])
}
def get_recent_alerts(self, hours: int = 24) -> List[Alert]:
"""Get recent alerts within time period"""
cutoff_time = datetime.now() - timedelta(hours=hours)
return [
alert for alert in self.alert_history
if datetime.fromisoformat(alert.timestamp) > cutoff_time
]
def create_default_alert_rules(self):
"""Create default alert rules"""
default_rules = [
{
'id': 'high_response_time',
'name': 'High Response Time',
'category': 'api',
'condition': {'metric': 'avg_response_time', 'operator': '>', 'value': 5.0},
'threshold': 5.0,
'severity': 'warning',
'cooldown_minutes': 5
},
{
'id': 'critical_response_time',
'name': 'Critical Response Time',
'category': 'api',
'condition': {'metric': 'avg_response_time', 'operator': '>', 'value': 10.0},
'threshold': 10.0,
'severity': 'critical',
'cooldown_minutes': 2
},
{
'id': 'high_error_rate',
'name': 'High Error Rate',
'category': 'api',
'condition': {'metric': 'error_rate', 'operator': '>', 'value': 0.05},
'threshold': 0.05,
'severity': 'warning',
'cooldown_minutes': 5
},
{
'id': 'critical_error_rate',
'name': 'Critical Error Rate',
'category': 'api',
'condition': {'metric': 'error_rate', 'operator': '>', 'value': 0.1},
'threshold': 0.1,
'severity': 'critical',
'cooldown_minutes': 2
},
{
'id': 'high_cpu_usage',
'name': 'High CPU Usage',
'category': 'system',
'condition': {'metric': 'cpu_percent', 'operator': '>', 'value': 80.0},
'threshold': 80.0,
'severity': 'warning',
'cooldown_minutes': 10
},
{
'id': 'critical_cpu_usage',
'name': 'Critical CPU Usage',
'category': 'system',
'condition': {'metric': 'cpu_percent', 'operator': '>', 'value': 95.0},
'threshold': 95.0,
'severity': 'critical',
'cooldown_minutes': 5
},
{
'id': 'high_memory_usage',
'name': 'High Memory Usage',
'category': 'system',
'condition': {'metric': 'memory_percent', 'operator': '>', 'value': 85.0},
'threshold': 85.0,
'severity': 'warning',
'cooldown_minutes': 10
},
{
'id': 'low_model_confidence',
'name': 'Low Model Confidence',
'category': 'model',
'condition': {'metric': 'avg_confidence', 'operator': '<', 'value': 0.6},
'threshold': 0.6,
'severity': 'warning',
'cooldown_minutes': 15
}
]
for rule_data in default_rules:
rule = AlertRule(**rule_data)
self.alert_rules[rule.id] = rule
self.save_alert_rules()
logger.info(f"Created {len(default_rules)} default alert rules")
def _generate_alert_id(self, category: str, title: str) -> str:
"""Generate unique alert ID"""
import hashlib
content = f"{category}_{title}_{datetime.now().isoformat()}"
return hashlib.md5(content.encode()).hexdigest()[:12]
def _is_duplicate_alert(self, alert_id: str, category: str, title: str, window_minutes: int = 10) -> bool:
"""Check if similar alert exists within time window"""
cutoff_time = datetime.now() - timedelta(minutes=window_minutes)
for alert in self.alert_history:
if (alert.category == category and
alert.title == title and
datetime.fromisoformat(alert.timestamp) > cutoff_time and
not alert.resolved):
return True
return False
def _is_in_cooldown(self, rule_id: str, cooldown_minutes: int) -> bool:
"""Check if rule is in cooldown period"""
if rule_id not in self.alert_cooldowns:
return False
last_triggered = self.alert_cooldowns[rule_id]
cooldown_period = cooldown_minutes * 60 # Convert to seconds
return (time.time() - last_triggered) < cooldown_period
def _evaluate_rule_condition(self, rule: AlertRule, metrics: Dict[str, Any]) -> bool:
"""Evaluate if rule condition is met"""
try:
condition = rule.condition
metric_value = self._get_nested_value(metrics, condition['metric'])
if metric_value is None:
return False
operator = condition['operator']
threshold_value = condition['value']
if operator == '>':
return metric_value > threshold_value
elif operator == '<':
return metric_value < threshold_value
elif operator == '>=':
return metric_value >= threshold_value
elif operator == '<=':
return metric_value <= threshold_value
elif operator == '==':
return metric_value == threshold_value
elif operator == '!=':
return metric_value != threshold_value
return False
except Exception as e:
logger.error(f"Error evaluating rule condition for {rule.id}: {e}")
return False
def _trigger_rule_alert(self, rule: AlertRule, metrics: Dict[str, Any]):
"""Trigger alert based on rule"""
metric_value = self._get_nested_value(metrics, rule.condition['metric'])
alert_id = self.create_alert(
alert_type=rule.severity,
category=rule.category,
title=rule.name,
message=f"{rule.name}: {rule.condition['metric']} = {metric_value} (threshold: {rule.threshold})",
source=f"rule_{rule.id}",
metadata={
'rule_id': rule.id,
'metric_name': rule.condition['metric'],
'metric_value': metric_value,
'threshold': rule.threshold,
'operator': rule.condition['operator']
}
)
# Set cooldown
self.alert_cooldowns[rule.id] = time.time()
logger.info(f"Rule alert triggered: {rule.name} (ID: {alert_id})")
def _get_nested_value(self, data: Dict, path: str):
"""Get nested value from dictionary using dot notation"""
try:
keys = path.split('.')
value = data
for key in keys:
if isinstance(value, dict) and key in value:
value = value[key]
else:
return None
return value
except Exception:
return None
def _send_notifications(self, alert: Alert):
"""Send notifications for alert"""
for handler_name, handler in self.notification_handlers.items():
try:
handler(alert)
except Exception as e:
logger.error(f"Error in notification handler {handler_name}: {e}")
def _append_to_log(self, log_path: Path, data: Dict):
"""Append data to log file"""
try:
with open(log_path, 'a') as f:
f.write(json.dumps(data) + '\n')
except Exception as e:
logger.error(f"Failed to write to log {log_path}: {e}")
def load_alert_configuration(self):
"""Load alert system configuration"""
try:
if self.alert_config_path.exists():
with open(self.alert_config_path, 'r') as f:
config = json.load(f)
# Update notification settings, thresholds, etc.
logger.info("Loaded alert configuration")
else:
# Create default configuration
default_config = {
'notification_channels': ['console'],
'alert_retention_days': 30,
'auto_resolve_after_hours': 24,
'duplicate_suppression_minutes': 10
}
with open(self.alert_config_path, 'w') as f:
json.dump(default_config, f, indent=2)
logger.info("Created default alert configuration")
except Exception as e:
logger.error(f"Failed to load alert configuration: {e}")
def load_alert_rules(self):
"""Load alert rules from file"""
try:
if self.alert_rules_path.exists():
with open(self.alert_rules_path, 'r') as f:
rules_data = json.load(f)
for rule_id, rule_data in rules_data.items():
rule = AlertRule(**rule_data)
self.alert_rules[rule_id] = rule
logger.info(f"Loaded {len(self.alert_rules)} alert rules")
else:
# Create default rules
self.create_default_alert_rules()
except Exception as e:
logger.error(f"Failed to load alert rules: {e}")
# Create default rules as fallback
self.create_default_alert_rules()
def save_alert_rules(self):
"""Save alert rules to file"""
try:
rules_data = {}
for rule_id, rule in self.alert_rules.items():
rules_data[rule_id] = asdict(rule)
with open(self.alert_rules_path, 'w') as f:
json.dump(rules_data, f, indent=2)
logger.info(f"Saved {len(self.alert_rules)} alert rules")
except Exception as e:
logger.error(f"Failed to save alert rules: {e}")
def load_alert_history(self):
"""Load recent alert history"""
try:
if self.alerts_log_path.exists():
cutoff_time = datetime.now() - timedelta(days=7) # Last 7 days
with open(self.alerts_log_path, 'r') as f:
for line in f:
try:
data = json.loads(line.strip())
# Skip action logs
if 'action' in data:
continue
alert = Alert(**data)
# Only load recent alerts
if datetime.fromisoformat(alert.timestamp) > cutoff_time:
self.alert_history.append(alert)
# Add to active alerts if not resolved
if not alert.resolved:
self.active_alerts[alert.id] = alert
except Exception:
continue
logger.info(f"Loaded {len(self.alert_history)} recent alerts, "
f"{len(self.active_alerts)} active")
except Exception as e:
logger.error(f"Failed to load alert history: {e}")
# Default notification handlers
def console_notification_handler(alert: Alert):
"""Simple console notification handler"""
icon = "π΄" if alert.type == "critical" else "π‘" if alert.type == "warning" else "π΅"
print(f"{icon} [{alert.type.upper()}] {alert.title}: {alert.message}")
def email_notification_handler(alert: Alert,
smtp_server: str,
smtp_port: int,
username: str,
password: str,
recipients: List[str]):
"""Email notification handler"""
try:
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = ', '.join(recipients)
msg['Subject'] = f"[{alert.type.upper()}] {alert.title}"
body = f"""
Alert Details:
- Type: {alert.type}
- Category: {alert.category}
- Timestamp: {alert.timestamp}
- Source: {alert.source}
- Message: {alert.message}
Metadata:
{json.dumps(alert.metadata, indent=2)}
"""
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(username, password)
text = msg.as_string()
server.sendmail(username, recipients, text)
server.quit()
logger.info(f"Email notification sent for alert: {alert.id}")
except Exception as e:
logger.error(f"Failed to send email notification: {e}") |