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):
""" """
@ -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
@ -158,7 +160,6 @@ 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 = {
@ -170,19 +171,20 @@ 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
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)) print(json.dumps(ticket_payload, indent=4))
else:
# Attempt to create the ticket via the API # Perform actual API request
try: try:
response = requests.post( response = requests.post(
self.ticket_api_url, self.ticket_api_url,
json=ticket_payload, json=ticket_payload,
headers={'Content-Type': 'application/json'} headers={'Content-Type': 'application/json'}
) )
# Debug: Log the response from the server
print(f"Response status code: {response.status_code}") print(f"Response status code: {response.status_code}")
print(f"Response body: {response.text}") print(f"Response body: {response.text}")