"""Configuration loader and defaults for hbd (HeartBeat Daemon/Server).""" import logging import os try: import yaml except Exception: yaml = None SERVER_DEFAULTS = { # Network settings "hb_port": 50003, # Port to listen for heartbeats "hbd_port": 50004, # HTTP API port "hbd_host": "", # Bind address (empty = all interfaces) # Persistence "pickfile": "/tmp/hb.pick", # Logging "logfile": "/var/log/heartbeat.log", "logfmt": "text", # text or msg or json # Notification settings "pushsrv": "pushover", # pushover, mattermost, or all "pushover_token": "", "pushover_user": "", # Monitoring settings "interval": 20, # Expected heartbeat interval (for server checks) "grace": 2, # Grace multiplier (interval * grace = timeout) "threshold_renotify_interval": 3600, # Seconds between threshold re-notifications # Host management "watchhosts": [], # Hosts to monitor and notify about "dyndnshosts": [], # Hosts with dynamic DNS "drophosts": [], # Hosts to ignore "dyndomains": ["wrede.org"], # DNS updates "nsupdate_bin": "/usr/bin/nsupdate", # Email settings "smtpserver": "smtp.fastmail.com", "smtpuser": "andreas@wrede.ca", "smtppassword": "pvtvefyp5gbhnch2", "smtpport": 587, "toemail": ["aew.hbd.notify@wrede.ca"], "fromemail": "aew.hbd@wrede.ca", # WebSocket settings "ws_port": 50005, "wss_port": None, "cert_path": "/usr/local/etc/ssl/", "wss_pem": "fullchain.pem", "wss_key": "privkey.pem", # Message journal configuration "journal_enabled": True, "journal_dir": "/var/log/heartbeat", "journal_file": "messages.journal", "journal_max_size": 100 * 1024 * 1024, # 100MB "journal_max_backups": 10, # Runtime flags "foreground": False, "verbose": False, "debug": 0, # Plugin/threshold configs (for clients reporting to this server) "plugins": {}, "thresholds": {}, } def load_config(path=None): """Load configuration from a YAML file and merge with server defaults. If YAML is not available or the file does not exist, defaults are returned. Args: path: Path to YAML config file (default: ~/.hb.yaml) Returns: Dictionary with configuration """ cfg = SERVER_DEFAULTS.copy() if not path: # default path (~/.hb.yaml) path = os.path.join(os.path.expanduser("~"), ".hb.yaml") if os.path.exists(path): if yaml: with open(path) as fh: data = yaml.safe_load(fh) # Merge YAML data with defaults # Keep all keys from YAML to support plugin configs and future extensions for k, v in data.items(): cfg[k] = v else: # yaml not installed: do not attempt to parse; user must ensure defaults pass return cfg