Skip to content

pgElephant/pgSentinel

Repository files navigation

pgSentinel

pgSentinel Logo

Professional Web-Based Monitoring & Management for pgbalancer

License: MIT Docker Prometheus Grafana


πŸš€ Overview

pgSentinel is a comprehensive, enterprise-grade monitoring and management platform for pgbalancer - the PostgreSQL connection pooler. Built with modern technologies, it provides real-time metrics, interactive dashboards, and intelligent alerting for your PostgreSQL infrastructure.

Key Features

  • ⚑ Real-time Monitoring - WebSocket-based live updates with sub-second latency
  • πŸ“Š Beautiful Dashboards - Interactive visualizations built with React and Recharts
  • πŸ”” Smart Alerting - Prometheus-based alerts with configurable thresholds
  • πŸ“ˆ Historical Analysis - Long-term metrics storage and trend analysis
  • 🎯 Node Management - Attach/detach backend nodes with one click
  • πŸ”’ Secure & Scalable - Production-ready architecture with Docker support
  • 🎨 Modern UI/UX - Responsive design with Tailwind CSS
  • πŸ”§ Configuration Management - Edit and reload pgbalancer config via UI

πŸ“‹ Table of Contents


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ pgSentinel Platform β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Frontend β”‚ β”‚ Backend β”‚ β”‚ Prometheus β”‚ β”‚ β”‚ β”‚ (Next.js) │◄───│ (FastAPI) │◄───│ (Metrics) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Real-time WebSocket Connection β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ pgbalancer Cluster β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Node β”‚ β”‚ Node β”‚ β”‚ Node β”‚ β”‚ β”‚ β”‚ 1 β”‚ β”‚ 2 β”‚ β”‚ 3 β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 

Component Overview

Component Technology Purpose
Frontend Next.js 14, React 18, TypeScript Interactive web dashboard
Backend API FastAPI, Python 3.12, WebSocket RESTful API & real-time updates
Metrics Prometheus, Node Exporter Metrics collection & storage
Visualization Grafana, Recharts Data visualization & dashboards
Database PostgreSQL 17 Metadata & configuration storage
Cache Redis 7 Session management & caching
Monitoring Alertmanager Alert routing & notification

πŸ› οΈ Technology Stack

Frontend

  • Framework: Next.js 14 (React 18)
  • Language: TypeScript
  • Styling: Tailwind CSS 3
  • Charts: Recharts 2
  • State Management: React Hooks
  • HTTP Client: Axios
  • WebSocket: socket.io-client
  • Icons: Lucide React

Backend

  • Framework: FastAPI 0.115
  • Language: Python 3.12
  • Async Runtime: uvicorn, asyncio
  • Database ORM: SQLAlchemy 2
  • WebSocket: Native WebSocket support
  • Metrics: Prometheus Client
  • Validation: Pydantic 2

Monitoring Stack

  • Metrics: Prometheus 2.x
  • Visualization: Grafana 10.x
  • Alerts: Alertmanager 0.26
  • System Metrics: Node Exporter

Infrastructure

  • Containerization: Docker 24+
  • Orchestration: Docker Compose 3.9
  • Database: PostgreSQL 17
  • Cache: Redis 7

⚑ Quick Start

Prerequisites

  • Docker 24+ and Docker Compose
  • Git
  • 4GB+ RAM
  • 10GB+ disk space

One-Command Start

# Clone the repository git clone https://github.com/your-org/pgsentinel.git cd pgSentinel # Start all services docker-compose up -d # View logs docker-compose logs -f # Access the dashboard open http://localhost:3000

Access Points

Service URL Credentials
pgSentinel Dashboard http://localhost:3000 -
Backend API http://localhost:8000 -
API Documentation http://localhost:8000/docs -
Prometheus http://localhost:9090 -
Grafana http://localhost:3001 admin / admin
Alertmanager http://localhost:9093 -

πŸ“₯ Installation

Option 1: Docker (Recommended)

# Production deployment docker-compose -f docker-compose.yml up -d # Development with hot reload docker-compose -f docker-compose.dev.yml up

Option 2: Manual Installation

Backend Setup

cd backend # Create virtual environment python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install dependencies pip install -r requirements.txt # Set environment variables export DATABASE_URL="postgresql://user:pass@localhost:5432/pgsentinel" export REDIS_URL="redis://localhost:6379/0" # Run backend uvicorn main:app --reload --host 0.0.0.0 --port 8000

Frontend Setup

cd frontend # Install dependencies npm install # Set environment variables echo "NEXT_PUBLIC_API_URL=http://localhost:8000" > .env.local echo "NEXT_PUBLIC_WS_URL=ws://localhost:8000/ws/live" >> .env.local # Run development server npm run dev # Or build for production npm run build npm start

βš™οΈ Configuration

Environment Variables

Backend (backend/.env)

# Database DATABASE_URL=postgresql://user:pass@localhost:5432/pgsentinel # Redis REDIS_URL=redis://localhost:6379/0 # pgbalancer PGBALANCER_CONFIG=/path/to/pgbalancer.conf # Logging LOG_LEVEL=INFO # Security SECRET_KEY=your-secret-key-here ALLOWED_HOSTS=*

Frontend (frontend/.env.local)

NEXT_PUBLIC_API_URL=http://localhost:8000 NEXT_PUBLIC_WS_URL=ws://localhost:8000/ws/live NEXT_TELEMETRY_DISABLED=1

pgbalancer Configuration

Update pgbalancer.conf to enable metrics export:

# Enable metrics endpoint enable_metrics = on metrics_port = 9187 # Connection pool settings num_init_children = 32 max_pool = 4

πŸ“– Usage

Dashboard Overview

The main dashboard provides:

  1. System Metrics

    • CPU usage
    • Memory utilization
    • Network I/O
    • Disk I/O
  2. Connection Pool Stats

    • Total connections
    • Active connections
    • Idle connections
    • Pool utilization %
  3. Backend Nodes

    • Node status (up/down)
    • Connection count per node
    • Replication lag
    • Primary/Replica roles
  4. Real-time Charts

    • System resource trends
    • Connection trends
    • Query performance
    • Replication lag graphs

Managing Backend Nodes

Detach a Node

# Via UI: Click "Detach" button on node card # Via API: curl -X POST http://localhost:8000/api/v1/backends/1/detach

Attach a Node

# Via UI: Click "Attach" button on node card # Via API: curl -X POST http://localhost:8000/api/v1/backends/1/attach

Configuration Management

# Get current configuration curl http://localhost:8000/api/v1/config # Update configuration curl -X PUT http://localhost:8000/api/v1/config \ -H "Content-Type: application/json" \ -d '{"num_init_children": 64}' # Reload configuration curl -X POST http://localhost:8000/api/v1/reload

πŸ“Š Monitoring & Alerts

Predefined Alerts

Alert Threshold Severity Description
HighPoolUtilization >80% Warning Pool is heavily utilized
PoolExhausted Waiting clients > 0 Critical No available connections
BackendNodeDown Status = 0 Critical Backend node is unreachable
HighReplicationLag >10MB Warning Replica is lagging
CriticalReplicationLag >100MB Critical Severe replication delay
HighCPUUsage >80% Warning System CPU is high
HighMemoryUsage >85% Warning System memory is high

Custom Dashboards

Import pre-built Grafana dashboards:

# Import dashboard curl -X POST http://localhost:3001/api/dashboards/import \ -u admin:admin \ -H "Content-Type: application/json" \ -d @monitoring/grafana/dashboards/pgbalancer-overview.json

πŸ”§ Development

Project Structure

pgSentinel/ β”œβ”€β”€ backend/ # FastAPI backend β”‚ β”œβ”€β”€ main.py # Main application β”‚ β”œβ”€β”€ requirements.txt # Python dependencies β”‚ └── Dockerfile # Backend container β”œβ”€β”€ frontend/ # Next.js frontend β”‚ β”œβ”€β”€ app/ # Next.js 14 app directory β”‚ β”œβ”€β”€ components/ # React components β”‚ β”‚ β”œβ”€β”€ common/ # Reusable components β”‚ β”‚ β”œβ”€β”€ dashboard/ # Dashboard-specific β”‚ β”‚ β”œβ”€β”€ charts/ # Chart components β”‚ β”‚ └── nodes/ # Node management β”‚ β”œβ”€β”€ hooks/ # Custom React hooks β”‚ β”œβ”€β”€ services/ # API service layer β”‚ β”œβ”€β”€ types/ # TypeScript types β”‚ β”œβ”€β”€ utils/ # Utility functions β”‚ └── Dockerfile # Frontend container β”œβ”€β”€ monitoring/ # Monitoring stack β”‚ β”œβ”€β”€ prometheus/ # Prometheus config β”‚ β”‚ β”œβ”€β”€ prometheus.yml # Main config β”‚ β”‚ └── rules/ # Alert rules β”‚ β”œβ”€β”€ grafana/ # Grafana dashboards β”‚ └── alertmanager/ # Alert routing β”œβ”€β”€ docker/ # Docker configs β”œβ”€β”€ docs/ # Documentation └── docker-compose.yml # Orchestration 

Running Tests

# Backend tests cd backend pytest # Frontend tests cd frontend npm test # E2E tests npm run test:e2e

Code Quality

# Backend linting cd backend ruff check . black --check . # Frontend linting cd frontend npm run lint npm run type-check

πŸš€ Deployment

Production Deployment

# Build and deploy docker-compose -f docker-compose.prod.yml up -d # Scale services docker-compose up -d --scale backend=4 # Update services docker-compose pull docker-compose up -d

Kubernetes Deployment

# Apply Kubernetes manifests kubectl apply -f k8s/ # Check status kubectl get pods -n pgsentinel

🀝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Workflow

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments

  • pgbalancer team for the excellent connection pooler
  • PostgreSQL community
  • FastAPI and Next.js teams
  • All contributors and users

πŸ“ž Support


Built with ❀️ by pgElephant, Inc.

Website β€’ Documentation β€’ Blog

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published