diff --git a/src/kumacli/cmd/monitor.py b/src/kumacli/cmd/monitor.py index 3d06c81..7e2efca 100644 --- a/src/kumacli/cmd/monitor.py +++ b/src/kumacli/cmd/monitor.py @@ -132,6 +132,88 @@ class MonitorCommands: except Exception as e: print(f"Error pausing monitors: {e}") + def resume_monitors( + self, monitor_patterns=None, group_patterns=None, resume_all=False + ): + """Resume monitors by patterns and/or groups, or all paused monitors""" + try: + # Check if we have either monitor patterns, group patterns, or --all flag + if not monitor_patterns and not group_patterns and not resume_all: + print("Error: Either --monitor, --group, or --all flag is required.") + return + + matched_monitors = [] + + if resume_all: + # Get all monitors and filter for inactive (paused) ones + all_monitors = self.client.api.get_monitors() + paused_monitors = [ + {"id": m.get("id"), "name": m.get("name")} + for m in all_monitors + if not m.get("active", True) + ] + matched_monitors.extend(paused_monitors) + else: + # 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: + if resume_all: + print("No paused monitors found to resume") + else: + print( + "Error: No monitors found matching the specified patterns or groups" + ) + return + + if resume_all: + print(f"Found {len(matched_monitors)} paused monitors to resume:") + else: + print(f"Found {len(matched_monitors)} matching monitors to resume:") + for monitor in matched_monitors: + print(f" - {monitor['name']} (ID: {monitor['id']})") + + # Resume each monitor + resumed_count = 0 + for monitor in matched_monitors: + try: + result = self.client.api.resume_monitor(monitor["id"]) + print(f"Resumed monitor '{monitor['name']}' (ID: {monitor['id']})") + resumed_count += 1 + except Exception as e: + print(f"Failed to resume monitor '{monitor['name']}': {e}") + + print( + f"Successfully resumed {resumed_count} out of {len(matched_monitors)} monitors" + ) + + except Exception as e: + print(f"Error resuming monitors: {e}") + def setup_monitor_parser(subparsers): """Setup monitor command parser""" @@ -170,6 +252,24 @@ def setup_monitor_parser(subparsers): help="Group name pattern to pause all group members (supports wildcards, can be repeated)", ) + # Resume monitors command + resume_monitors_parser = monitor_subparsers.add_parser( + "resume", help="Resume monitors" + ) + resume_monitors_parser.add_argument( + "--monitor", + action="append", + help="Monitor name pattern to resume (supports wildcards, can be repeated)", + ) + resume_monitors_parser.add_argument( + "--group", + action="append", + help="Group name pattern to resume all group members (supports wildcards, can be repeated)", + ) + resume_monitors_parser.add_argument( + "--all", action="store_true", help="Resume all paused monitors" + ) + return monitor_parser @@ -185,6 +285,12 @@ def handle_monitor_command(args, client): monitor_commands.pause_monitors( monitor_patterns=args.monitor, group_patterns=args.group ) + elif args.monitor_action == "resume": + monitor_commands.resume_monitors( + monitor_patterns=args.monitor, + group_patterns=args.group, + resume_all=args.all, + ) else: print("Unknown monitor action. Use --help for usage information.") return False