""" Модели данных для Grafana webhooks. Автор: Сергей Антропов Сайт: https://devops.org.ru """ from typing import List, Optional, Dict, Any from pydantic import BaseModel, Field class EvalMatch(BaseModel): """Модель для evalMatches из Grafana.""" value: float = Field(..., description="Значение метрики", examples=[95.5, 87.2, 45.2]) metric: str = Field(..., description="Название метрики", examples=["cpu_usage_percent", "memory_usage_percent", "disk_usage_percent"]) tags: Optional[Dict[str, Any]] = Field(None, description="Теги метрики", examples=[{"host": "server01", "instance": "production"}, None]) class GrafanaAlert(BaseModel): """Модель данных вебхука из Grafana.""" title: str = Field(..., description="Заголовок алерта", examples=["[Alerting] Test notification"]) ruleId: int = Field(..., description="ID правила алерта", examples=[674180201771804383]) ruleName: str = Field(..., description="Название правила", examples=["Test notification"]) state: str = Field(..., description="Состояние алерта (alerting, ok, paused, pending, no_data)", examples=["alerting"]) evalMatches: List[EvalMatch] = Field(default_factory=list, description="Совпадения метрик") orgId: int = Field(..., description="ID организации", examples=[0]) dashboardId: int = Field(..., description="ID дашборда", examples=[1]) panelId: int = Field(..., description="ID панели", examples=[1]) tags: Dict[str, str] = Field(default_factory=dict, description="Теги алерта") ruleUrl: str = Field(..., description="URL правила алерта", examples=["http://grafana.cism-ms.ru/"]) message: Optional[str] = Field(None, description="Сообщение алерта", examples=["Someone is testing the alert notification within Grafana."]) model_config = { "json_schema_extra": { "examples": [ { "title": "[Alerting] High CPU Usage", "ruleId": 674180201771804383, "ruleName": "High CPU Usage Alert", "state": "alerting", "evalMatches": [ { "value": 95.5, "metric": "cpu_usage_percent", "tags": {"host": "server01", "instance": "production"} }, { "value": 87.2, "metric": "memory_usage_percent", "tags": {"host": "server01", "instance": "production"} } ], "orgId": 1, "dashboardId": 123, "panelId": 456, "tags": {"severity": "critical", "environment": "production"}, "ruleUrl": "http://grafana.cism-ms.ru/alerting/list", "message": "CPU usage is above 90% threshold for more than 5 minutes" }, { "title": "[OK] High CPU Usage", "ruleId": 674180201771804383, "ruleName": "High CPU Usage Alert", "state": "ok", "evalMatches": [ { "value": 45.2, "metric": "cpu_usage_percent", "tags": {"host": "server01", "instance": "production"} } ], "orgId": 1, "dashboardId": 123, "panelId": 456, "tags": {"severity": "critical", "environment": "production"}, "ruleUrl": "http://grafana.cism-ms.ru/alerting/list", "message": "CPU usage has returned to normal levels" } ] } }