forecast-operational-metrics
정보
이 스킬은 Prophet 또는 statsmodels를 사용하여 인프라 및 애플리케이션 메트릭(예: CPU 또는 비용 추세)을 예측하여 용량 계획과 비용 최적화를 지원합니다. Grafana에서 예측 결과를 시각화하고, 예상되는 자원 고갈에 대한 알림을 설정할 수 있습니다. 향후 용량 필요성을 예측하거나, 클라우드 지출을 최적화하거나, 사전 확장 정책을 수립해야 할 때 사용하세요.
빠른 설치
Claude Code
추천npx skills add pjt222/agent-almanac -a claude-code/plugin add https://github.com/pjt222/agent-almanacgit clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/forecast-operational-metricsClaude Code에서 이 명령을 복사하여 붙여넣어 스킬을 설치하세요
문서
預運營指標
以 Prophet 或 statsmodels 預基礎設施與應用之指標,供容量規劃、成本優化與主動擴展。於 Grafana 示預測並設告警於所預之資源耗盡。
見 擴展範例 查完整配置文件與模板。
適用時機
- 需預基礎設施容量之需(CPU、記憶體、盤、網)
- 為下季規劃硬體/雲資源採購
- 欲預成本趨並優化雲支出
- 需基於所預負載設主動擴展策
- 為活動規劃預使用者流量
- 為備份規劃預資料庫存儲增長
- 為速限配置估 API 使用
輸入
- 必要:歷史時序指標(至少 3-12 月)
- 必要:指標類(CPU、記憶體、req/秒、成本等)
- 必要:預範圍(日、週或月後)
- 選擇性:已知之未來事件(部署、營銷活動、假日)
- 選擇性:季節性信息(日、週、年模式)
- 選擇性:外部回歸(如營銷支出、使用者註冊)
步驟
步驟一:設環境並載數據
裝預函式庫並備時序數據。
# Create virtual environment
python -m venv venv
source venv/bin/activate
# Install forecasting libraries
pip install prophet statsmodels pandas numpy
pip install plotly matplotlib seaborn
pip install prometheus-api-client influxdb-client
pip install grafana-api
以 MetricsLoader 載並備數據:
# forecasting/data_loader.py (abbreviated)
import pandas as pd
from datetime import datetime, timedelta
class MetricsLoader:
def load_from_prometheus(self, query: str, lookback_days: int = 90, step: str = "1h"):
"""Load historical metrics from Prometheus."""
# ... implementation (see EXAMPLES.md for complete code)
def resample_and_aggregate(self, df: pd.DataFrame, freq: str = "1H"):
"""Resample time series to regular intervals."""
# ... implementation (see EXAMPLES.md)
# Example usage
loader = MetricsLoader(prometheus_url="http://prometheus:9090")
df = loader.load_from_prometheus(
query='avg(rate(container_cpu_usage_seconds_total[5m]))',
lookback_days=90,
)
df_daily = loader.resample_and_aggregate(df, freq="1D")
見 EXAMPLES.md Step 1 查完整之 MetricsLoader 實作。
預期: 時序數據已載於規則間隔,缺值已填,備以預。
失敗時: 若數據缺,用 forward-fill 或插值;確回溯期足(建議 90+ 日);驗時戳時區一致;察或偏預之離群(>5 sigma)。
步驟二:行 Prophet 預
用 Facebook Prophet 以自動偵季節性並預。
# forecasting/prophet_forecaster.py (abbreviated)
from prophet import Prophet
class ProphetForecaster:
def __init__(self, growth: str = "linear", seasonality_mode: str = "multiplicative"):
self.growth = growth
self.prophet_params = {
"growth": growth,
"seasonality_mode": seasonality_mode,
# ... additional parameters (see EXAMPLES.md)
}
def fit(self, df: pd.DataFrame, regressors=None, holidays=None):
"""Train Prophet model on historical data."""
# ... implementation (see EXAMPLES.md)
def forecast(self, periods: int, freq: str = "D"):
"""Generate forecast for future periods."""
# ... implementation (see EXAMPLES.md)
# Example usage
forecaster = ProphetForecaster(growth="linear", seasonality_mode="multiplicative")
forecaster.fit(df_daily)
forecast = forecaster.forecast(periods=30, freq="D")
forecaster.plot_forecast(forecast, save_path="results/cpu_forecast.png")
見 EXAMPLES.md Step 2 查完整之 ProphetForecaster 實作。
預期: 為 30+ 日生含信區間之預測,於組件圖捕季節模式,交叉驗證 MAPE < 15%。
失敗時: 若預測似不實,試異生長模型(線性 vs 邏輯);若缺季節性調 seasonality_mode;若準度差(<70% MAPE)加更多歷史數據或外部回歸;察數據品質問題。
步驟三:行 ARIMA/SARIMAX 預(替)
用 statsmodels 行傳統時序預。
# forecasting/arima_forecaster.py (abbreviated)
from statsmodels.tsa.statespace.sarimax import SARIMAX
class ARIMAForecaster:
def __init__(self, order: tuple = (1, 1, 1), seasonal_order: tuple = (1, 1, 1, 7)):
self.order = order
self.seasonal_order = seasonal_order
def fit(self, df: pd.DataFrame, exog=None):
"""Train SARIMAX model."""
series = df.set_index("timestamp")["value"]
self.model = SARIMAX(series, exog=exog, order=self.order, seasonal_order=self.seasonal_order)
self.fitted_model = self.model.fit(disp=False)
# ... implementation (see EXAMPLES.md)
def forecast(self, steps: int, exog_future=None):
"""Generate forecast for future periods."""
# ... implementation (see EXAMPLES.md)
# Auto-select parameters
best_order, best_seasonal = auto_arima(series, seasonal=True)
forecaster = ARIMAForecaster(order=best_order, seasonal_order=best_seasonal)
forecaster.fit(df_hourly)
forecast = forecaster.forecast(steps=168) # 7 days
見 EXAMPLES.md Step 3 查完整之 ARIMAForecaster 實作與 auto_arima 函數。
預期: ARIMA 模型以最優參擬,生含信區間之預,診圖示白噪殘差。
失敗時: 若模型不收,簡參(減 p、q、P、Q);若預有誤趨察差分階(d、D);若殘差非白噪加更多 AR/MA 項;確序長 >2x 季節期。
步驟四:識容量閾與告警
析預以預資源何時耗盡。
# forecasting/capacity_planning.py (abbreviated)
from datetime import datetime
class CapacityPlanner:
def __init__(self, capacity_limit: float, warning_threshold: float = 0.8):
self.capacity_limit = capacity_limit
self.warning_threshold = warning_threshold
def find_exhaustion_date(self, forecast: pd.DataFrame):
"""Find when forecast exceeds capacity limit."""
exceeded = forecast[forecast["yhat"] >= self.capacity_limit]
# ... implementation (see EXAMPLES.md)
def generate_capacity_report(self, forecast: pd.DataFrame):
"""Generate comprehensive capacity planning report."""
# ... implementation (see EXAMPLES.md)
# Example usage
planner = CapacityPlanner(capacity_limit=1000, warning_threshold=0.8)
report = planner.generate_capacity_report(forecast)
print(f"Warning Date: {report['warning_date']}")
print(f"Exhaustion Date: {report['exhaustion_date']}")
recommendation = planner.recommend_scaling_action(report)
見 EXAMPLES.md Step 4 查完整之 CapacityPlanner 實作。
預期: 報告示容量限將達之時、予帶急度之建議、算增長率。
失敗時: 若耗盡日不實,驗 capacity_limit 正;若增長率過高察歷史數據之離群;於成熟系統考非線性增長模型。
步驟五:於 Grafana 示預
推預數據至 Grafana 以供實時監。
# forecasting/grafana_integration.py (abbreviated)
import requests
class GrafanaForecaster:
def __init__(self, grafana_url: str, api_key: str, dashboard_uid: str = None):
self.grafana_url = grafana_url.rstrip("/")
self.api_key = api_key
self.dashboard_uid = dashboard_uid
def create_annotation(self, text: str, tags: list, time: datetime = None):
"""Create annotation in Grafana for forecast events."""
# ... implementation (see EXAMPLES.md)
def create_capacity_alert_annotation(self, capacity_report: dict):
"""Create Grafana annotation for capacity warnings."""
# ... implementation (see EXAMPLES.md)
# Export to CSV for Grafana datasource
def export_forecast_to_csv(forecast: pd.DataFrame, output_path: str):
"""Export forecast in format compatible with Grafana CSV datasource."""
# ... implementation (see EXAMPLES.md)
# Example usage
grafana = GrafanaForecaster(
grafana_url="http://grafana:3000",
api_key="YOUR_API_KEY",
dashboard_uid="your-dashboard-uid",
)
grafana.create_capacity_alert_annotation(report)
export_forecast_to_csv(forecast, "grafana/forecasts/cpu_forecast.csv")
見 EXAMPLES.md Step 5 查完整之 GrafanaForecaster 實作。
預期: 預註見於 Grafana 儀表板,容量警以直標示可見,預數據藉 CSV datasource 可得。
失敗時: 驗 Grafana API key 有正權限;察 dashboard UID 正;確註之時戳為毫秒;整合前以 curl 測 API。
步驟六:自動化預生
設定期工以規律生預。
# forecasting/scheduler.py (abbreviated)
import schedule
import time
def generate_daily_forecast():
"""Generate forecast for all monitored metrics."""
logger.info("Starting daily forecast generation")
metrics_config = [
{"name": "cpu_usage", "query": "...", "capacity_limit": 0.8, "forecast_days": 30},
{"name": "memory_usage", "query": "...", "capacity_limit": 32, "forecast_days": 30},
{"name": "disk_usage", "query": "...", "capacity_limit": 500, "forecast_days": 90},
]
loader = MetricsLoader(prometheus_url="http://prometheus:9090")
for metric_config in metrics_config:
df = loader.load_from_prometheus(query=metric_config["query"], lookback_days=90)
forecaster = ProphetForecaster()
forecaster.fit(df)
forecast = forecaster.forecast(periods=metric_config["forecast_days"])
planner = CapacityPlanner(capacity_limit=metric_config["capacity_limit"])
report = planner.generate_capacity_report(forecast)
export_forecast_to_csv(forecast, f"grafana/forecasts/{metric_config['name']}_forecast.csv")
# ... (see EXAMPLES.md for complete implementation)
# Schedule daily at 2 AM
schedule.every().day.at("02:00").do(generate_daily_forecast)
while True:
schedule.run_pending()
time.sleep(60)
見 EXAMPLES.md Step 6 查完整之 scheduler 實作。
預期: 預每日生於所有指標,容量報告已記,CSV 檔已匯出供 Grafana,關鍵容量警告已送。
失敗時: 驗 scheduler 進程續行(用 systemd/supervisor);察 Prometheus 連通;確盤空足以匯預;為瞬時敗行重試;為 scheduler 自身設監。
驗證
- 歷史數據已載,有 90+ 日連續指標
- Prophet 預於組件圖捕日/週季節性
- 於驗證中預信區間含 85-95% 之實值
- 於已知情境容量耗盡日正算
- 於診圖 ARIMA 模型殘差顯為白噪
- Grafana 註於所預之警/耗盡日顯
- 自動預每日無手介入而行
- 於驗證集預準(MAPE)< 15%
常見陷阱
- 歷史數據不足:需 3-12 月以可靠偵季節性;勿以 <60 日預
- 忽已知事件:假日、部署、營銷活動偏預;加為外部回歸或假日
- 長期預之過信:準度於 30-90 日後降;以為方向導,非準預
- 靜容量限:基礎設施隨時變;加資源時更 capacity_limit
- 預異常:訓數據中離群傳於預;清數據或用穩健法
- 不更模型:系統變後預陳;每週或重大架構變後重訓
- 忽信區間:點預誤導;恒用下/上界供規劃
- 誤季節期:時數據用日、日數據用週;不配致差預
相關技能
detect-anomalies-aiops- 異常偵輔預以主動監plan-capacity- 基礎設施容量規劃工作流build-grafana-dashboards- 示預與容量趨
GitHub 저장소
연관 스킬
executing-plans
디자인executing-plans 스킬은 검토 체크포인트가 포함된 통제된 배치로 실행할 완전한 구현 계획이 있을 때 사용합니다. 이 스킬은 계획을 불러와 비판적으로 검토한 후, 소규모 배치(기본값 3개 작업)로 작업을 실행하면서 각 배치 사이에 진행 상황을 아키텍트 검토를 위해 보고합니다. 이를 통해 내재된 품질 관리 체크포인트를 갖춘 체계적인 구현이 보장됩니다.
requesting-code-review
디자인이 스킬은 코드 변경 사항을 요구 사항에 따라 분석하기 위해 코드 리뷰어 하위 에이전트를 호출합니다. 작업 완료 후, 주요 기능 구현 후, 또는 메인 브랜치에 병합하기 전에 사용해야 합니다. 이 리뷰는 현재 구현체와 원래 계획을 비교하여 문제를 조기에 발견하는 데 도움이 됩니다.
connect-mcp-server
디자인이 스킬은 개발자들이 HTTP, stdio 또는 SSE 전송 방식을 통해 MCP 서버를 Claude Code에 연결하는 포괄적인 가이드를 제공합니다. GitHub, Notion 및 사용자 정의 API와 같은 외부 서비스를 통합하기 위한 설치, 구성, 인증 및 보안을 다룹니다. MCP 통합 설정, 외부 도구 구성 또는 Claude의 모델 컨텍스트 프로토콜 작업 시 활용하세요.
web-cli-teleport
디자인이 스킬은 작업 분석을 기반으로 개발자가 Claude Code 웹 인터페이스와 CLI 인터페이스 중 선택할 수 있도록 돕고, 두 환경 간 원활한 세션 텔레포트를 가능하게 합니다. 웹, CLI 또는 모바일 환경 전환 시 세션 상태와 컨텍스트를 관리하여 워크플로를 최적화합니다. 다양한 단계에서 서로 다른 도구가 필요한 복잡한 프로젝트에 사용하세요.
