Enabled ability to run in dry
This commit is contained in:
@ -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,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]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user