add api key support

This commit is contained in:
2026-01-01 16:01:55 -05:00
parent cc62aabfe4
commit 0577c7fc1b

View File

@@ -41,6 +41,7 @@ class SystemHealthMonitor:
CONFIG = { CONFIG = {
'TICKET_API_URL': 'http://10.10.10.45/create_ticket_api.php', 'TICKET_API_URL': 'http://10.10.10.45/create_ticket_api.php',
'TICKET_API_KEY': None, # Will be loaded from .env file
'THRESHOLDS': { 'THRESHOLDS': {
'DISK_CRITICAL': 90, 'DISK_CRITICAL': 90,
'DISK_WARNING': 80, 'DISK_WARNING': 80,
@@ -70,6 +71,41 @@ class SystemHealthMonitor:
'HISTORY_DIR': '/var/log/hwmonDaemon', 'HISTORY_DIR': '/var/log/hwmonDaemon',
'HISTORY_RETENTION_DAYS': 30 'HISTORY_RETENTION_DAYS': 30
} }
@classmethod
def load_env_config(cls):
"""Load configuration from .env file in /etc/hwmonDaemon/"""
# Check for .env file in standard system location
env_file = '/etc/hwmonDaemon/.env'
if not os.path.exists(env_file):
logger.warning(f".env file not found at {env_file} - API key required for ticket creation")
return
try:
with open(env_file, 'r') as f:
for line in f:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith('#'):
continue
# Parse KEY=VALUE format
if '=' in line:
key, value = line.split('=', 1)
key = key.strip()
value = value.strip()
# Update CONFIG if key exists
if key == 'TICKET_API_KEY':
cls.CONFIG['TICKET_API_KEY'] = value
logger.info("✓ Loaded TICKET_API_KEY from .env")
elif key == 'TICKET_API_URL':
cls.CONFIG['TICKET_API_URL'] = value
logger.info(f"✓ Loaded TICKET_API_URL: {value}")
except Exception as e:
logger.error(f"Failed to load .env file: {e}")
TICKET_TEMPLATES = { TICKET_TEMPLATES = {
'ACTION_TYPE': { 'ACTION_TYPE': {
@@ -481,9 +517,12 @@ class SystemHealthMonitor:
:param ticket_api_url: URL for the ticket creation API. :param ticket_api_url: URL for the ticket creation API.
:param dry_run: If True, simulate API calls without sending requests. :param dry_run: If True, simulate API calls without sending requests.
""" """
# Load environment configuration first (API keys, etc.)
self.load_env_config()
self.ticket_api_url = ticket_api_url self.ticket_api_url = ticket_api_url
self.dry_run = dry_run self.dry_run = dry_run
# Ensure history directory exists # Ensure history directory exists
os.makedirs(self.CONFIG['HISTORY_DIR'], exist_ok=True) os.makedirs(self.CONFIG['HISTORY_DIR'], exist_ok=True)