#!/bin/bash # PostgreSQL Monitoring Script for Proxmox LXC Container # Load environment variables if [ -f ../.env ]; then source ../.env else echo "Error: .env file not found!" exit 1 fi # Set default values if not provided in .env POSTGRES_USER=${POSTGRES_USER:-"postgres"} POSTGRES_DB=${POSTGRES_DB:-"postgres"} echo "PostgreSQL Service Monitoring for Proxmox LXC" echo "==========================================" echo # Check container status echo "Container Status:" docker ps --filter "name=postgres" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" echo # Check PostgreSQL service health echo "PostgreSQL Health Check:" docker exec postgres pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB} if [ $? -eq 0 ]; then echo "PostgreSQL is accepting connections" else echo "PostgreSQL is not accepting connections" fi echo # Database size information echo "Database Size Information:" docker exec postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c "SELECT datname, pg_size_pretty(pg_database_size(datname)) as size FROM pg_database ORDER BY pg_database_size(datname) DESC;" echo # Connection information echo "Current Connections:" docker exec postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c "SELECT datname, count(*) as connections FROM pg_stat_activity GROUP BY datname;" echo # Active queries echo "Active Queries (running for more than 5 seconds):" docker exec postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c "SELECT pid, datname, usename, application_name, client_addr, state, now() - query_start as duration, query FROM pg_stat_activity WHERE state = 'active' AND now() - query_start > interval '5 seconds';" echo # Resource usage echo "Container Resource Usage:" docker stats postgres --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" echo # Disk usage echo "Disk Usage:" df -h $(docker volume inspect -f '{{ .Mountpoint }}' postgres_data) echo # System resource usage echo "System Resource Usage:" echo "CPU:" top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}' echo "Memory:" free -m | awk 'NR==2{printf "Used: %s MB (%.2f%%)", $3, $3*100/$2 }' echo exit 0