---
name: monitoring-database-health
description: 'Monitor use when you need to work with monitoring and observability.

  This skill provides health monitoring and alerting with comprehensive guidance and automation.

  Trigger with phrases like "monitor system health", "set up alerts",

  or "track metrics".'
allowed-tools: Read, Write, Edit, Grep, Glob, Bash(psql:*), Bash(mysql:*), Bash(mongosh:*)
version: 1.25.0
author: Jeremy Longshore <jeremy@intentsolutions.io>
license: MIT
tags:
  - database
  - monitoring
  - observability
compatibility: Designed for Claude Code
title: monitoring-database-health
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/monitoring-database-health
last_updated: 2026-10-07T06:06:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [assets/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/monitoring-database-health/assets/README.md), [references/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/monitoring-database-health/references/README.md), [scripts/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/monitoring-database-health/scripts/README.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/monitoring-database-health`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/monitoring-database-health`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/monitoring-database-health) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/monitoring-database-health`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/monitoring-database-health --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/monitoring-database-health --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Database Health Monitor

## Overview

Monitor database server health across PostgreSQL, MySQL, and MongoDB by tracking key performance indicators including connection utilization, query throughput, replication lag, disk usage, cache hit ratios, vacuum activity, and lock contention.

## Prerequisites

- Database credentials with access to system statistics views (`pg_stat_*`, `performance_schema`, `serverStatus`)
- `psql`, `mysql`, or `mongosh` CLI tools for running health check queries
- Permissions: `pg_monitor` role (PostgreSQL), `PROCESS` privilege (MySQL)
- Baseline metrics from a period of normal operation for threshold calibration
- Alerting channel configured (email, Slack webhook, PagerDuty)

## Instructions

1. Check connection utilization:
   - PostgreSQL: `SELECT count(*) AS active_connections, (SELECT setting::int FROM pg_settings WHERE name = 'max_connections') AS max_connections, round(count(*)::numeric / (SELECT setting::int FROM pg_settings WHERE name = 'max_connections') * 100, 1) AS utilization_pct FROM pg_stat_activity`
   - MySQL: `SELECT VARIABLE_VALUE AS connections FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Threads_connected'`
   - Alert threshold: utilization above 80%

2. Monitor query throughput and error rate:
   - PostgreSQL: `SELECT datname, xact_commit AS commits_total, xact_rollback AS rollbacks_total, xact_rollback::float / GREATEST(xact_commit, 1) AS rollback_ratio FROM pg_stat_database WHERE datname = current_database()`
   - MySQL: `SHOW GLOBAL STATUS LIKE 'Com_commit'` and `SHOW GLOBAL STATUS LIKE 'Com_rollback'`
   - Alert threshold: rollback ratio above 5% or throughput drops more than 50% from baseline

3. Check disk usage and growth:
   - PostgreSQL: `SELECT pg_size_pretty(pg_database_size(current_database())) AS db_size` and `SELECT tablename, pg_size_pretty(pg_total_relation_size(tablename::text)) AS size FROM pg_tables WHERE schemaname = 'public' ORDER BY pg_total_relation_size(tablename::text) DESC LIMIT 10`
   - Alert threshold: disk usage above 80% or growth rate projecting full disk within 7 days

4. Monitor cache hit ratio:
   - PostgreSQL: `SELECT sum(heap_blks_hit)::float / GREATEST(sum(heap_blks_hit) + sum(heap_blks_read), 1) AS cache_hit_ratio FROM pg_statio_user_tables`
   - MySQL: `SELECT (1 - (VARIABLE_VALUE / (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Innodb_buffer_pool_read_requests'))) AS hit_ratio FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Innodb_buffer_pool_reads'`
   - Alert threshold: cache hit ratio below 95% indicates shared_buffers or innodb_buffer_pool_size needs increasing

5. Check vacuum and autovacuum health (PostgreSQL):
   - `SELECT relname, last_vacuum, last_autovacuum, n_dead_tup, n_live_tup, round(n_dead_tup::numeric / GREATEST(n_live_tup, 1) * 100, 1) AS dead_pct FROM pg_stat_user_tables WHERE n_dead_tup > 1000 ORDER BY n_dead_tup DESC LIMIT 10`
   - Alert threshold: dead tuple percentage above 20% or autovacuum not running for more than 24 hours on active tables

6. Monitor replication lag (if replicas exist):
   - PostgreSQL: `SELECT client_addr, state, pg_wal_lsn_diff(sent_lsn, replay_lsn) AS lag_bytes FROM pg_stat_replication`
   - MySQL: `SHOW REPLICA STATUS\G` - check `Seconds_Behind_Source`
   - Alert threshold: lag above 30 seconds or replication stopped

7. Check for long-running queries:
   - PostgreSQL: `SELECT pid, now() - query_start AS duration, state, query FROM pg_stat_activity WHERE state != 'idle' AND now() - query_start > interval '5 minutes' ORDER BY duration DESC`
   - Alert threshold: any query running longer than 10 minutes (OLTP) or 1 hour (analytics)

8. Monitor lock contention:
   - PostgreSQL: `SELECT count(*) AS waiting_queries FROM pg_stat_activity WHERE wait_event_type = 'Lock'`
   - Alert threshold: more than 10 queries waiting for locks simultaneously

9. Compile all health checks into a single monitoring script that runs via cron every 60 seconds, outputs metrics in a structured format (JSON), and triggers alerts when thresholds are breached.

10. Create a health summary dashboard query that returns a single-row result with RAG (Red/Amber/Green) status for each health dimension: connections, throughput, disk, cache, vacuum, replication, queries, and locks.

## Output

- **Health check queries** tailored to the specific database engine
- **Monitoring script** (shell or Python) for scheduled health checks with alerting
- **Threshold configuration** with default values and tuning guidance
- **Dashboard summary query** providing RAG status across all health dimensions
- **Alert notification templates** for Slack, email, or PagerDuty integration

## Error Handling

| Error | Cause | Solution |
|-------|-------|---------|
| `pg_stat_activity` returns incomplete data | `track_activities = off` in postgresql.conf | Enable `track_activities = on` and `track_counts = on`; reload configuration |
| Health check query itself times out | Database under heavy load or lock contention | Set `statement_timeout = '5s'` for monitoring queries; use a dedicated monitoring connection |
| False alerts during maintenance windows | Planned maintenance triggers threshold breaches | Implement alert suppression windows; add maintenance mode flag to monitoring script |
| Disk usage alert but no obvious growth | WAL files, temporary files, or pg_stat_tmp consuming space | Check `pg_wal` directory size; check for orphaned temporary files; verify `wal_keep_size` setting |
| Cache hit ratio drops after restart | Buffer pool/shared_buffers cold after database restart | Implement cache warming script that runs key queries after restart; alert will self-resolve as cache warms |

## Examples

**PostgreSQL health dashboard for a production SaaS application**: A single cron-based script checks 8 health dimensions every 60 seconds, writing results to a metrics table. A dashboard query shows: connections 45/200 (GREEN), cache hit 98.5% (GREEN), dead tuples 2.1% (GREEN), disk 62% (GREEN), replication lag 0.5s (GREEN), long queries 0 (GREEN), lock waiters 1 (GREEN), rollback ratio 0.3% (GREEN).

**Detecting impending disk full condition**: Health monitor tracks daily disk growth rate. Current usage: 72%, daily growth: 1.2GB, remaining: 280GB. Projected full date: 233 days. Alert triggers at 80% with recommendation to archive old data or add storage. A second alert at 90% escalates to PagerDuty.

**Identifying autovacuum falling behind**: Health check shows the `events` table with 15M dead tuples (45% dead ratio) and last autovacuum 3 days ago. Root cause: `autovacuum_vacuum_cost_delay` too conservative for a high-write table. Fix: set per-table `autovacuum_vacuum_cost_delay = 2` and `autovacuum_vacuum_scale_factor = 0.01`.

## Resources

- PostgreSQL monitoring statistics: https://www.postgresql.org/docs/current/monitoring-stats.html
- MySQL performance_schema: https://dev.mysql.com/doc/refman/8.0/en/performance-schema.html
- MongoDB serverStatus: https://www.mongodb.com/docs/manual/reference/command/serverStatus/
- PostgreSQL autovacuum tuning: https://www.postgresql.org/docs/current/runtime-config-autovacuum.html
- check_postgres monitoring tool: https://bucardo.org/check_postgres/
