Files
argus/src/master/app/config.py

51 lines
1.8 KiB
Python

from __future__ import annotations
import os
from dataclasses import dataclass
@dataclass(frozen=True)
class AppConfig:
db_path: str
metric_nodes_json_path: str
offline_threshold_seconds: int
online_threshold_seconds: int
scheduler_interval_seconds: int
node_id_prefix: str
auth_mode: str
target_prefer_net_cidrs: str
target_reachability_check: bool
def _get_int_env(name: str, default: int) -> int:
raw = os.environ.get(name)
if raw is None or raw.strip() == "":
return default
try:
return int(raw)
except ValueError as exc:
raise ValueError(f"Environment variable {name} must be an integer, got {raw!r}") from exc
def load_config() -> AppConfig:
"""读取环境变量生成配置对象,方便统一管理运行参数。"""
def _bool_env(name: str, default: bool) -> bool:
raw = os.environ.get(name)
if raw is None or raw.strip() == "":
return default
return raw.strip().lower() in ("1", "true", "yes", "on")
return AppConfig(
db_path=os.environ.get("DB_PATH", "/private/argus/master/db.sqlite3"),
metric_nodes_json_path=os.environ.get(
"METRIC_NODES_JSON_PATH", "/private/argus/metric/prometheus/nodes.json"
),
offline_threshold_seconds=_get_int_env("OFFLINE_THRESHOLD_SECONDS", 180),
online_threshold_seconds=_get_int_env("ONLINE_THRESHOLD_SECONDS", 120),
scheduler_interval_seconds=_get_int_env("SCHEDULER_INTERVAL_SECONDS", 30),
node_id_prefix=os.environ.get("NODE_ID_PREFIX", "A"),
auth_mode=os.environ.get("AUTH_MODE", "disabled"),
target_prefer_net_cidrs=os.environ.get("TARGET_PREFER_NET_CIDRS", "10.0.0.0/8,172.31.0.0/16"),
target_reachability_check=_bool_env("TARGET_REACHABILITY_CHECK", False),
)