monitoring
Monitor PostgreSQL database performance metrics, statistics, and health checks including connections, locks, replication, disk usage, query stats, and index usage over specified time ranges.
Instructions
Database monitoring: performance metrics, statistics, health checks
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of results | |
| metric | Yes | Metric type to retrieve | |
| timeRange | No | Time range for metrics | 1h |
Implementation Reference
- src/index.ts:1311-1372 (handler)The primary handler function for the 'monitoring' tool. Dispatches to different metrics (connections, performance, cache, security) and returns formatted JSON responses from various server components.private async handleMonitoring(args: any) { const { metric } = args; switch (metric) { case 'connections': return { content: [{ type: 'text', text: JSON.stringify(this.dbManager.getPoolStats(), null, 2) }] }; case 'performance': const operationalStats = this.dbManager.getOperationalStats(); const performanceMetrics = this.performanceMonitor.getMetrics(); const slowOperations = this.performanceMonitor.getSlowOperations(); return { content: [{ type: 'text', text: JSON.stringify({ operational: operationalStats, performance: performanceMetrics, slowOperations, cache: this.cache.getStats() }, null, 2) }] }; case 'cache': return { content: [{ type: 'text', text: JSON.stringify({ stats: this.cache.getStats(), recent: this.cache.getRecentEntries(5), popular: this.cache.getPopularEntries(5) }, null, 2) }] }; case 'security': const rateLimitEntries = Array.from(this.rateLimiter.getAllEntries().entries()).slice(0, 10); return { content: [{ type: 'text', text: JSON.stringify({ rateLimits: rateLimitEntries, securityEvents: 'Security event logging would be implemented here' }, null, 2) }] }; default: return { content: [{ type: 'text', text: JSON.stringify({ message: `Monitoring metric '${metric}' not yet implemented` }, null, 2) }] }; } }
- src/index.ts:363-388 (schema)Input schema definition for the 'monitoring' tool, defining parameters like 'metric' with supported enums.{ name: 'monitoring', description: 'Database monitoring: performance metrics, statistics, health checks', inputSchema: { type: 'object', properties: { metric: { type: 'string', enum: ['connections', 'performance', 'locks', 'replication', 'disk_usage', 'query_stats', 'index_usage'], description: 'Metric type to retrieve' }, timeRange: { type: 'string', enum: ['1h', '24h', '7d', '30d'], description: 'Time range for metrics', default: '1h' }, limit: { type: 'integer', description: 'Maximum number of results', default: 50 } }, required: ['metric'] } },
- src/index.ts:664-666 (registration)Registration of the 'monitoring' tool handler in the main CallToolRequestSchema switch statement.case 'monitoring': return await this.handleMonitoring(args);
- src/index.ts:634-636 (registration)Registration of tool definitions (including 'monitoring') for the ListToolsRequestSchema handler.this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: toolDefinitions, }));
- src/common/cache.ts:238-310 (helper)PerformanceMonitor class providing metrics collection and retrieval used by the monitoring tool's performance metric.export class PerformanceMonitor { private metrics: Map<string, { count: number; totalTime: number; minTime: number; maxTime: number; avgTime: number; lastExecution: number; }> = new Map(); /** * Record operation performance */ record(operation: string, duration: number): void { const existing = this.metrics.get(operation); if (existing) { existing.count++; existing.totalTime += duration; existing.minTime = Math.min(existing.minTime, duration); existing.maxTime = Math.max(existing.maxTime, duration); existing.avgTime = existing.totalTime / existing.count; existing.lastExecution = Date.now(); } else { this.metrics.set(operation, { count: 1, totalTime: duration, minTime: duration, maxTime: duration, avgTime: duration, lastExecution: Date.now() }); } } /** * Get performance metrics */ getMetrics(): Record<string, any> { const result: Record<string, any> = {}; for (const [operation, metrics] of this.metrics) { result[operation] = { ...metrics, avgTime: Math.round(metrics.avgTime * 100) / 100 }; } return result; } /** * Get slow operations */ getSlowOperations(threshold: number = 1000): Array<{ operation: string; metrics: any }> { const slow: Array<{ operation: string; metrics: any }> = []; for (const [operation, metrics] of this.metrics) { if (metrics.avgTime > threshold || metrics.maxTime > threshold * 2) { slow.push({ operation, metrics }); } } return slow.sort((a, b) => b.metrics.avgTime - a.metrics.avgTime); } /** * Reset metrics */ reset(): void { this.metrics.clear(); } }