zbx-tg-bot/config/config.py

89 lines
1.8 KiB
Python
Executable File

from pydantic import BaseModel
from pydantic_settings import (
BaseSettings,
SettingsConfigDict,
)
from pathlib import Path
import logging
BASE_DIR = Path(__file__).parent.parent
TEMPLATES_DIR = BASE_DIR / "web" / "templates"
STATIC_DIR = BASE_DIR / "web" / "static"
icon_dict = {
"0": "",
"1": "🟦",
"2": "🟨",
"3": "🟧",
"4": "🟥",
"5": "🟫",
"10": "",
}
class LogConfig(BaseModel):
level: int = 30
level_to_file: int = 30
dateformat: str = "%Y-%m-%d %H:%M:%S"
format: str = (
"[%(asctime)s.%(msecs)03d] %(module)-25s:%(lineno)4d | %(funcName)-20s| %(levelname)-8s | %(message)s"
)
class RedisConfig(BaseModel):
host: str = "localhost"
port: int = 6379
pwd: str | None = None
class TelegramBotConfig(BaseModel):
token: str
chat_id: int
tread_id: int
class ZabbixConfig(BaseModel):
url: str
token: str
min_severity: int
upd_interval: int
alert_fail_count: int
current_fail: int = 0
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=(
BASE_DIR / "config" / ".env-template",
BASE_DIR / "config" / ".env",
),
case_sensitive=False,
env_nested_delimiter="__",
env_prefix="CFG__",
)
log: LogConfig = LogConfig()
redis: RedisConfig
tgbot: TelegramBotConfig
zabbix: ZabbixConfig
conf = Settings()
logging.basicConfig(
level=conf.log.level,
datefmt=conf.log.dateformat,
format=conf.log.format,
)
file_handler = logging.FileHandler(BASE_DIR / "logfile.log")
file_handler.setLevel(conf.log.level_to_file)
file_handler.setFormatter(
logging.Formatter(
"[%(asctime)s.%(msecs)03d] %(module)s:%(lineno)4d | %(funcName)s| %(levelname)s | %(message)s"
)
)
logging.getLogger().addHandler(file_handler)