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:
def __init__(self,
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.
:param ticket_api_url: URL for the ticket creation API.
: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.state_file = state_file
self.dry_run = dry_run
def run(self):
"""
@ -118,7 +121,7 @@ class SystemHealthMonitor:
def _create_tickets_for_issues(self, health_report: Dict[str, Any]):
"""
Create tickets for detected issues with dynamic parameters based on severity.
:param health_report: The comprehensive health report from the checks.
"""
issues = self._detect_issues(health_report)
@ -126,7 +129,6 @@ class SystemHealthMonitor:
print("No issues detected.")
return
# Initialize default ticket fields
hostname = socket.gethostname() # Get the current hostname
action_type = "[auto]" # Default action type for automatic checks
scope = "[cluster-wide]" # Scope of the issues
@ -138,7 +140,7 @@ class SystemHealthMonitor:
priority = "4" # Default to low priority
category = "Other"
issue_type = "Task"
if "Disk" in issue:
priority = "3" # Medium priority for disk issues
category = "Hardware"
@ -158,9 +160,8 @@ class SystemHealthMonitor:
# Create the ticket title with relevant details
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)
ticket_payload = {
"title": ticket_title,
"description": description,
@ -170,29 +171,30 @@ class SystemHealthMonitor:
"type": issue_type
}
# Debug: Log the ticket payload being sent
print("Attempting to create ticket with payload:")
print(json.dumps(ticket_payload, indent=4))
if self.dry_run:
# Dry-run mode: log the payload instead of sending it
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
try:
response = requests.post(
self.ticket_api_url,
json=ticket_payload,
headers={'Content-Type': 'application/json'}
)
# 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}")
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]:
"""