Use UptimeKumaException exceptions
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
import fnmatch
|
import fnmatch
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from uptime_kuma_api import UptimeKumaApi
|
from uptime_kuma_api import UptimeKumaApi, UptimeKumaException
|
||||||
|
|
||||||
|
|
||||||
class KumaClient:
|
class KumaClient:
|
||||||
@@ -77,7 +77,7 @@ class KumaClient:
|
|||||||
self.api.login(self.username, self.password)
|
self.api.login(self.username, self.password)
|
||||||
print(f"Connected to {self.url}")
|
print(f"Connected to {self.url}")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Failed to connect: {e}")
|
print(f"Failed to connect: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ class KumaClient:
|
|||||||
|
|
||||||
return unique_monitors
|
return unique_monitors
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error finding monitors: {e}")
|
print(f"Error finding monitors: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ class KumaClient:
|
|||||||
|
|
||||||
return unique_groups
|
return unique_groups
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error finding groups: {e}")
|
print(f"Error finding groups: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ class KumaClient:
|
|||||||
|
|
||||||
return group_members
|
return group_members
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error getting group members: {e}")
|
print(f"Error getting group members: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -226,7 +226,7 @@ class KumaClient:
|
|||||||
# Return list of monitor IDs
|
# Return list of monitor IDs
|
||||||
return [monitor["id"] for monitor in matched_monitors]
|
return [monitor["id"] for monitor in matched_monitors]
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error finding monitors by globs: {e}")
|
print(f"Error finding monitors by globs: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -255,7 +255,7 @@ class KumaClient:
|
|||||||
}
|
}
|
||||||
for mid in monitor_ids
|
for mid in monitor_ids
|
||||||
]
|
]
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error getting monitor details: {e}")
|
print(f"Error getting monitor details: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Info command implementations for Uptime Kuma CLI."""
|
"""Info command implementations for Uptime Kuma CLI."""
|
||||||
|
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
from ..client import KumaClient
|
from ..client import KumaClient
|
||||||
|
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ class InfoCommands:
|
|||||||
for key, value in info.items():
|
for key, value in info.items():
|
||||||
print(f" {key}: {value}")
|
print(f" {key}: {value}")
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error getting server info: {e}")
|
print(f"Error getting server info: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
"""Maintenance command implementations for Uptime Kuma CLI."""
|
"""Maintenance command implementations for Uptime Kuma CLI."""
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
from ..client import KumaClient
|
from ..client import KumaClient
|
||||||
|
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ class MaintenanceCommands:
|
|||||||
f"{maintenance_id:<5} {title:<30} {strategy:<15} {active:<10} {description:<50}"
|
f"{maintenance_id:<5} {title:<30} {strategy:<15} {active:<10} {description:<50}"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error listing maintenances: {e}")
|
print(f"Error listing maintenances: {e}")
|
||||||
|
|
||||||
def add_maintenance(
|
def add_maintenance(
|
||||||
@@ -104,13 +105,13 @@ class MaintenanceCommands:
|
|||||||
f"Successfully added {len(matched_monitors)} monitors to maintenance"
|
f"Successfully added {len(matched_monitors)} monitors to maintenance"
|
||||||
)
|
)
|
||||||
print(f"API response: {result}")
|
print(f"API response: {result}")
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error: Failed to add monitors to maintenance: {e}")
|
print(f"Error: Failed to add monitors to maintenance: {e}")
|
||||||
print(
|
print(
|
||||||
"This might be due to API compatibility issues or server configuration"
|
"This might be due to API compatibility issues or server configuration"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error adding maintenance: {e}")
|
print(f"Error adding maintenance: {e}")
|
||||||
|
|
||||||
def delete_maintenance(self, maintenance_id=None, delete_all=False):
|
def delete_maintenance(self, maintenance_id=None, delete_all=False):
|
||||||
@@ -142,7 +143,7 @@ class MaintenanceCommands:
|
|||||||
f"(ID: {maintenance.get('id')})"
|
f"(ID: {maintenance.get('id')})"
|
||||||
)
|
)
|
||||||
deleted_count += 1
|
deleted_count += 1
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(
|
print(
|
||||||
f"Failed to delete maintenance '{maintenance.get('title', 'N/A')}': {e}"
|
f"Failed to delete maintenance '{maintenance.get('title', 'N/A')}': {e}"
|
||||||
)
|
)
|
||||||
@@ -165,14 +166,14 @@ class MaintenanceCommands:
|
|||||||
)
|
)
|
||||||
print(f"API response: {result}")
|
print(f"API response: {result}")
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Failed to delete maintenance ID {maintenance_id}: {e}")
|
print(f"Failed to delete maintenance ID {maintenance_id}: {e}")
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
"Error: Either --id or --all flag is required for delete operation"
|
"Error: Either --id or --all flag is required for delete operation"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error during maintenance deletion: {e}")
|
print(f"Error during maintenance deletion: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Monitor command implementations for Uptime Kuma CLI."""
|
"""Monitor command implementations for Uptime Kuma CLI."""
|
||||||
|
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
from ..client import KumaClient
|
from ..client import KumaClient
|
||||||
|
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ class MonitorCommands:
|
|||||||
f"{parent_name:<20} {url:<35} {active:<10}"
|
f"{parent_name:<20} {url:<35} {active:<10}"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error listing monitors: {e}")
|
print(f"Error listing monitors: {e}")
|
||||||
|
|
||||||
def pause_monitors(self, monitor_patterns=None, group_patterns=None):
|
def pause_monitors(self, monitor_patterns=None, group_patterns=None):
|
||||||
@@ -91,14 +92,14 @@ class MonitorCommands:
|
|||||||
self.client.api.pause_monitor(monitor["id"])
|
self.client.api.pause_monitor(monitor["id"])
|
||||||
print(f"Paused monitor '{monitor['name']}' (ID: {monitor['id']})")
|
print(f"Paused monitor '{monitor['name']}' (ID: {monitor['id']})")
|
||||||
paused_count += 1
|
paused_count += 1
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Failed to pause monitor '{monitor['name']}': {e}")
|
print(f"Failed to pause monitor '{monitor['name']}': {e}")
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"Successfully paused {paused_count} out of {len(matched_monitors)} monitors"
|
f"Successfully paused {paused_count} out of {len(matched_monitors)} monitors"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error pausing monitors: {e}")
|
print(f"Error pausing monitors: {e}")
|
||||||
|
|
||||||
def resume_monitors(
|
def resume_monitors(
|
||||||
@@ -147,14 +148,14 @@ class MonitorCommands:
|
|||||||
self.client.api.resume_monitor(monitor["id"])
|
self.client.api.resume_monitor(monitor["id"])
|
||||||
print(f"Resumed monitor '{monitor['name']}' (ID: {monitor['id']})")
|
print(f"Resumed monitor '{monitor['name']}' (ID: {monitor['id']})")
|
||||||
resumed_count += 1
|
resumed_count += 1
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Failed to resume monitor '{monitor['name']}': {e}")
|
print(f"Failed to resume monitor '{monitor['name']}': {e}")
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"Successfully resumed {resumed_count} out of {len(matched_monitors)} monitors"
|
f"Successfully resumed {resumed_count} out of {len(matched_monitors)} monitors"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except UptimeKumaException as e:
|
||||||
print(f"Error resuming monitors: {e}")
|
print(f"Error resuming monitors: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user