Implement pause_monitor()
This commit is contained in:
@@ -67,6 +67,71 @@ class MonitorCommands:
|
||||
except Exception as e:
|
||||
print(f"Error listing monitors: {e}")
|
||||
|
||||
def pause_monitors(self, monitor_patterns=None, group_patterns=None):
|
||||
"""Pause monitors by patterns and/or groups"""
|
||||
try:
|
||||
# Check if we have either monitor patterns or group patterns
|
||||
if not monitor_patterns and not group_patterns:
|
||||
print(
|
||||
"Error: Either --monitor or --group flag is required. Specify at least one pattern."
|
||||
)
|
||||
return
|
||||
|
||||
matched_monitors = []
|
||||
|
||||
# Find monitors by patterns if specified
|
||||
if monitor_patterns:
|
||||
pattern_monitors = self.client.find_monitors_by_pattern(
|
||||
monitor_patterns
|
||||
)
|
||||
matched_monitors.extend(pattern_monitors)
|
||||
|
||||
# Find monitors by groups if specified
|
||||
if group_patterns:
|
||||
group_monitors = self.client.get_monitors_in_groups(group_patterns)
|
||||
# Convert to the same format as find_monitors_by_pattern
|
||||
group_monitor_objs = [
|
||||
{"id": m.get("id"), "name": m.get("name")} for m in group_monitors
|
||||
]
|
||||
matched_monitors.extend(group_monitor_objs)
|
||||
|
||||
# Remove duplicates while preserving order
|
||||
seen = set()
|
||||
unique_monitors = []
|
||||
for monitor in matched_monitors:
|
||||
if monitor["id"] not in seen:
|
||||
seen.add(monitor["id"])
|
||||
unique_monitors.append(monitor)
|
||||
|
||||
matched_monitors = unique_monitors
|
||||
|
||||
if not matched_monitors:
|
||||
print(
|
||||
"Error: No monitors found matching the specified patterns or groups"
|
||||
)
|
||||
return
|
||||
|
||||
print(f"Found {len(matched_monitors)} matching monitors to pause:")
|
||||
for monitor in matched_monitors:
|
||||
print(f" - {monitor['name']} (ID: {monitor['id']})")
|
||||
|
||||
# Pause each monitor
|
||||
paused_count = 0
|
||||
for monitor in matched_monitors:
|
||||
try:
|
||||
result = self.client.api.pause_monitor(monitor["id"])
|
||||
print(f"Paused monitor '{monitor['name']}' (ID: {monitor['id']})")
|
||||
paused_count += 1
|
||||
except Exception as e:
|
||||
print(f"Failed to pause monitor '{monitor['name']}': {e}")
|
||||
|
||||
print(
|
||||
f"Successfully paused {paused_count} out of {len(matched_monitors)} monitors"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error pausing monitors: {e}")
|
||||
|
||||
|
||||
def setup_monitor_parser(subparsers):
|
||||
"""Setup monitor command parser"""
|
||||
@@ -90,6 +155,21 @@ def setup_monitor_parser(subparsers):
|
||||
help="Group name pattern to filter by (supports wildcards, can be repeated)",
|
||||
)
|
||||
|
||||
# Pause monitors command
|
||||
pause_monitors_parser = monitor_subparsers.add_parser(
|
||||
"pause", help="Pause monitors"
|
||||
)
|
||||
pause_monitors_parser.add_argument(
|
||||
"--monitor",
|
||||
action="append",
|
||||
help="Monitor name pattern to pause (supports wildcards, can be repeated)",
|
||||
)
|
||||
pause_monitors_parser.add_argument(
|
||||
"--group",
|
||||
action="append",
|
||||
help="Group name pattern to pause all group members (supports wildcards, can be repeated)",
|
||||
)
|
||||
|
||||
return monitor_parser
|
||||
|
||||
|
||||
@@ -101,6 +181,10 @@ def handle_monitor_command(args, client):
|
||||
monitor_commands.list_monitors(
|
||||
monitor_patterns=args.monitor, group_patterns=args.group
|
||||
)
|
||||
elif args.monitor_action == "pause":
|
||||
monitor_commands.pause_monitors(
|
||||
monitor_patterns=args.monitor, group_patterns=args.group
|
||||
)
|
||||
else:
|
||||
print("Unknown monitor action. Use --help for usage information.")
|
||||
return False
|
||||
|
Reference in New Issue
Block a user