docker_monitoring_advanced
Monitor Docker containers with health checks, events, and detailed statistics to track performance and system information.
Instructions
Enhanced monitoring with health checks, events, and detailed statistics
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Monitoring action | |
| container | No | Container name (for container-specific actions) | |
| since | No | Time period for events (e.g., '1h', '30m', '1d') | |
| format | No | Output format | table |
Implementation Reference
- src/index.ts:982-1072 (registration)Registration of the 'docker_monitoring_advanced' tool, including input schema definition and the complete handler function that implements various advanced monitoring features like live stats, health checks, events, system info, and performance overview.server.registerTool( "docker_monitoring_advanced", { title: "Advanced Docker Monitoring", description: "Enhanced monitoring with health checks, events, and detailed statistics", inputSchema: { action: z.enum(["live_stats", "health", "events", "system_info", "performance"]).describe("Monitoring action"), container: z.string().optional().describe("Container name (for container-specific actions)"), since: z.string().optional().describe("Time period for events (e.g., '1h', '30m', '1d')"), format: z.enum(["table", "json"]).optional().default("table").describe("Output format") } }, async ({ action, container, since, format }) => { try { switch (action) { case "live_stats": const stats = await DockerMonitor.getLiveStats(container); return { content: [ { type: "text", text: `## Live Docker Statistics\n\n\`\`\`\n${stats}\n\`\`\`` } ] }; case "health": if (!container) { throw new Error("Container name is required for health check"); } const health = await DockerMonitor.getContainerHealth(container); return { content: [ { type: "text", text: `## Container Health Check\n\n${health}` } ] }; case "events": const events = await DockerMonitor.getSystemEvents(since || "1h"); return { content: [ { type: "text", text: `## Docker System Events (last ${since || "1h"})\n\n\`\`\`\n${events}\n\`\`\`` } ] }; case "system_info": const systemInfo = await executeDockerCommand("docker system info"); return { content: [ { type: "text", text: `## Docker System Information\n\n\`\`\`\n${systemInfo.stdout}\n\`\`\`` } ] }; case "performance": const [diskUsage, version, containers] = await Promise.all([ executeDockerCommand("docker system df -v"), executeDockerCommand("docker version"), executeDockerCommand("docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'") ]); return { content: [ { type: "text", text: `## Docker Performance Overview\n\n### Disk Usage\n\`\`\`\n${diskUsage.stdout}\n\`\`\`\n\n### Running Containers\n\`\`\`\n${containers.stdout}\n\`\`\`\n\n### Version\n\`\`\`\n${version.stdout}\n\`\`\`` } ] }; } } catch (error) { return { content: [ { type: "text", text: `Error with advanced monitoring: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } );
- src/index.ts:994-1071 (handler)The handler function for the tool that handles different monitoring actions: live_stats, health, events, system_info, and performance by calling DockerMonitor methods and executeDockerCommand.async ({ action, container, since, format }) => { try { switch (action) { case "live_stats": const stats = await DockerMonitor.getLiveStats(container); return { content: [ { type: "text", text: `## Live Docker Statistics\n\n\`\`\`\n${stats}\n\`\`\`` } ] }; case "health": if (!container) { throw new Error("Container name is required for health check"); } const health = await DockerMonitor.getContainerHealth(container); return { content: [ { type: "text", text: `## Container Health Check\n\n${health}` } ] }; case "events": const events = await DockerMonitor.getSystemEvents(since || "1h"); return { content: [ { type: "text", text: `## Docker System Events (last ${since || "1h"})\n\n\`\`\`\n${events}\n\`\`\`` } ] }; case "system_info": const systemInfo = await executeDockerCommand("docker system info"); return { content: [ { type: "text", text: `## Docker System Information\n\n\`\`\`\n${systemInfo.stdout}\n\`\`\`` } ] }; case "performance": const [diskUsage, version, containers] = await Promise.all([ executeDockerCommand("docker system df -v"), executeDockerCommand("docker version"), executeDockerCommand("docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'") ]); return { content: [ { type: "text", text: `## Docker Performance Overview\n\n### Disk Usage\n\`\`\`\n${diskUsage.stdout}\n\`\`\`\n\n### Running Containers\n\`\`\`\n${containers.stdout}\n\`\`\`\n\n### Version\n\`\`\`\n${version.stdout}\n\`\`\`` } ] }; } } catch (error) { return { content: [ { type: "text", text: `Error with advanced monitoring: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/index.ts:985-992 (schema)Input schema definition using Zod for the tool's parameters: action (required enum), optional container, since, and format.title: "Advanced Docker Monitoring", description: "Enhanced monitoring with health checks, events, and detailed statistics", inputSchema: { action: z.enum(["live_stats", "health", "events", "system_info", "performance"]).describe("Monitoring action"), container: z.string().optional().describe("Container name (for container-specific actions)"), since: z.string().optional().describe("Time period for events (e.g., '1h', '30m', '1d')"), format: z.enum(["table", "json"]).optional().default("table").describe("Output format") }
- src/index.ts:351-380 (helper)DockerMonitor class providing helper methods: getLiveStats, getSystemEvents, getContainerHealth, which are called by the tool handler to perform the actual monitoring tasks.class DockerMonitor { static async getLiveStats(containerName?: string): Promise<string> { const command = containerName ? `docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" ${containerName}` : `docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"`; const result = await executeDockerCommand(command); return result.stdout; } static async getSystemEvents(since: string = "1h"): Promise<string> { const command = `docker events --since ${since} --until now`; const result = await executeDockerCommand(command); return result.stdout; } static async getContainerHealth(containerName: string): Promise<string> { try { const inspect = await executeDockerCommand(`docker inspect ${containerName}`); const data = JSON.parse(inspect.stdout)[0]; const health = data.State.Health || { Status: "none" }; const state = data.State; return `Container: ${containerName}\nStatus: ${state.Status}\nHealth: ${health.Status}\nStarted: ${state.StartedAt}\nFinished: ${state.FinishedAt || 'N/A'}`; } catch (error) { throw new Error(`Failed to get health info: ${error instanceof Error ? error.message : String(error)}`); } } }