Enabled ability to run in dry

This commit is contained in:
2024-12-05 20:30:47 -05:00
parent b0af9f8e7b
commit 58f3601c11

View File

@ -22,15 +22,18 @@ logger.addHandler(console_handler)
class SystemHealthMonitor: class SystemHealthMonitor:
def __init__(self, def __init__(self,
ticket_api_url: str = 'http://10.10.10.45/create_ticket_api.php', ticket_api_url: str = 'http://10.10.10.45/create_ticket_api.php',
state_file: str = '/tmp/last_health_check.json'): state_file: str = '/tmp/last_health_check.json',
dry_run: bool = False):
""" """
Initialize the system health monitor. Initialize the system health monitor.
:param ticket_api_url: URL for the ticket creation API. :param ticket_api_url: URL for the ticket creation API.
:param state_file: File path to track the last health check results. :param state_file: File path to track the last health check results.
:param dry_run: If True, simulate API calls without sending requests.
""" """
self.ticket_api_url = ticket_api_url self.ticket_api_url = ticket_api_url
self.state_file = state_file self.state_file = state_file
self.dry_run = dry_run
def run(self): def run(self):
""" """
@ -118,7 +121,7 @@ class SystemHealthMonitor:
def _create_tickets_for_issues(self, health_report: Dict[str, Any]): def _create_tickets_for_issues(self, health_report: Dict[str, Any]):
""" """
Create tickets for detected issues with dynamic parameters based on severity. Create tickets for detected issues with dynamic parameters based on severity.
:param health_report: The comprehensive health report from the checks. :param health_report: The comprehensive health report from the checks.
""" """
issues = self._detect_issues(health_report) issues = self._detect_issues(health_report)
@ -126,7 +129,6 @@ class SystemHealthMonitor:
print("No issues detected.") print("No issues detected.")
return return
# Initialize default ticket fields
hostname = socket.gethostname() # Get the current hostname hostname = socket.gethostname() # Get the current hostname
action_type = "[auto]" # Default action type for automatic checks action_type = "[auto]" # Default action type for automatic checks
scope = "[cluster-wide]" # Scope of the issues scope = "[cluster-wide]" # Scope of the issues
@ -138,7 +140,7 @@ class SystemHealthMonitor:
priority = "4" # Default to low priority priority = "4" # Default to low priority
category = "Other" category = "Other"
issue_type = "Task" issue_type = "Task"
if "Disk" in issue: if "Disk" in issue:
priority = "3" # Medium priority for disk issues priority = "3" # Medium priority for disk issues
category = "Hardware" category = "Hardware"
@ -158,9 +160,8 @@ class SystemHealthMonitor:
# Create the ticket title with relevant details # Create the ticket title with relevant details
ticket_title = f"[{hostname}]{action_type}[{issue_type}] {issue} {scope}{environment}{ticket_type}" ticket_title = f"[{hostname}]{action_type}[{issue_type}] {issue} {scope}{environment}{ticket_type}"
# Create a detailed description for the ticket
description = self._generate_detailed_description(issue, health_report) description = self._generate_detailed_description(issue, health_report)
ticket_payload = { ticket_payload = {
"title": ticket_title, "title": ticket_title,
"description": description, "description": description,
@ -170,29 +171,30 @@ class SystemHealthMonitor:
"type": issue_type "type": issue_type
} }
# Debug: Log the ticket payload being sent if self.dry_run:
print("Attempting to create ticket with payload:") # Dry-run mode: log the payload instead of sending it
print(json.dumps(ticket_payload, indent=4)) logger.info("Dry-run mode enabled. Simulating ticket creation:")
logger.info(json.dumps(ticket_payload, indent=4))
print("Dry-run: Ticket payload:")
print(json.dumps(ticket_payload, indent=4))
else:
# Perform actual API request
try:
response = requests.post(
self.ticket_api_url,
json=ticket_payload,
headers={'Content-Type': 'application/json'}
)
print(f"Response status code: {response.status_code}")
print(f"Response body: {response.text}")
# Attempt to create the ticket via the API if response.status_code in [200, 201]:
try: print(f"Ticket created successfully: {ticket_title}")
response = requests.post( else:
self.ticket_api_url, print(f"Failed to create ticket. Status code: {response.status_code}")
json=ticket_payload, print(f"Response: {response.text}")
headers={'Content-Type': 'application/json'} except Exception as e:
) print(f"Error creating ticket: {e}")
# Debug: Log the response from the server
print(f"Response status code: {response.status_code}")
print(f"Response body: {response.text}")
if response.status_code in [200, 201]:
print(f"Ticket created successfully: {ticket_title}")
else:
print(f"Failed to create ticket. Status code: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"Error creating ticket: {e}")
def _detect_issues(self, health_report: Dict[str, Any]) -> List[str]: def _detect_issues(self, health_report: Dict[str, Any]) -> List[str]:
""" """