Skip to main content

Django package for system monitoring and alerting

Project description

Django Alert & Monitoring

A comprehensive Django package for monitoring system health, API performance, and sending automated alerts via email when issues occur.

Features

  • API Error Monitoring: Automatically detect and alert on API errors (4xx, 5xx status codes)
  • System Health Monitoring: Monitor CPU, memory, disk usage with configurable thresholds
  • Redis Monitoring: Track Redis connection health and performance
  • Log Monitoring: Parse and analyze Django application logs for errors and warnings
  • Email Alerts: Send configurable email notifications to administrators
  • Prometheus Integration: Export metrics for external monitoring systems
  • Celery Integration: Background task monitoring and health checks
  • Web Dashboard: Built-in web interface for viewing alerts and system metrics

Installation

pip install django-alert-monitoring

Or install from source:

git clone https://github.com/yourusername/django-alert-monitoring.git
cd django-alert-monitoring
pip install -e .

Quick Start

  1. Add to your Django INSTALLED_APPS:
INSTALLED_APPS = [
    # ... other apps
    'alert_monitoring',
]
  1. Add the middleware to monitor API requests:
MIDDLEWARE = [
    # ... other middleware
    'alert_monitoring.middleware.APIMonitoringMiddleware',
]
  1. Configure email settings for alerts:
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@gmail.com'
EMAIL_HOST_PASSWORD = 'your-app-password'

# Alert monitoring settings
ALERT_MONITORING = {
    'ADMIN_EMAILS': ['admin@example.com', 'dev@example.com'],
    'EMAIL_SUBJECT_PREFIX': '[ALERT] ',
    'API_ERROR_THRESHOLD': 10,  # Alert if more than 10 errors in 5 minutes
    'SYSTEM_THRESHOLDS': {
        'CPU_WARNING': 80,
        'CPU_CRITICAL': 95,
        'MEMORY_WARNING': 85,
        'MEMORY_CRITICAL': 95,
        'DISK_WARNING': 90,
        'DISK_CRITICAL': 98,
    },
    'REDIS_CHECK_INTERVAL': 60,  # seconds
    'LOG_CHECK_INTERVAL': 300,  # seconds
}
  1. Run migrations:
python manage.py migrate
  1. Start the monitoring tasks:
python manage.py start_monitoring

Usage

API Monitoring

The middleware automatically monitors all API requests and creates alerts for:

  • 4xx client errors
  • 5xx server errors
  • Slow response times (>5 seconds)

System Monitoring

Monitor system resources with automatic alerts:

  • High CPU usage
  • High memory usage
  • Low disk space

Redis Monitoring

Track Redis connection health and performance metrics.

Log Monitoring

Parse Django logs for:

  • ERROR level messages
  • WARNING level messages
  • Custom patterns

Email Alerts

Receive email notifications when:

  • API errors exceed threshold
  • System resources are critical
  • Redis connection fails
  • Log errors are detected

Web Dashboard

Access the monitoring dashboard at /monitoring/ to view:

  • Current system health
  • Recent alerts
  • System metrics history
  • Alert statistics

Management Commands

  • start_monitoring: Start all monitoring tasks
  • check_health: Run manual health check
  • clear_old_alerts: Remove alerts older than specified days

API Endpoints

  • GET /monitoring/health/: Basic health check
  • GET /monitoring/health/detailed/: Detailed system health
  • GET /monitoring/metrics/: Prometheus metrics
  • GET /monitoring/alerts/: View alerts (requires authentication)

Configuration

Full configuration options:

ALERT_MONITORING = {
    'ADMIN_EMAILS': ['admin@example.com'],
    'EMAIL_SUBJECT_PREFIX': '[ALERT] ',
    'API_ERROR_THRESHOLD': 10,
    'API_ERROR_WINDOW': 300,  # 5 minutes
    'SYSTEM_THRESHOLDS': {
        'CPU_WARNING': 80,
        'CPU_CRITICAL': 95,
        'MEMORY_WARNING': 85,
        'MEMORY_CRITICAL': 95,
        'DISK_WARNING': 90,
        'DISK_CRITICAL': 98,
    },
    'REDIS_CHECK_INTERVAL': 60,
    'LOG_CHECK_INTERVAL': 300,
    'LOG_FILE_PATH': '/path/to/django.log',
    'LOG_ERROR_PATTERNS': [
        r'ERROR',
        r'CRITICAL',
        r'Exception',
    ],
    'ENABLE_PROMETHEUS': True,
    'PROMETHEUS_PORT': 8001,
}

Dependencies

  • Django >= 3.2
  • psutil >= 5.8.0
  • redis >= 4.0.0
  • celery >= 5.0.0
  • prometheus-client >= 0.16.0

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

MIT License - see LICENSE file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_alert_monitoring-1.0.0.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_alert_monitoring-1.0.0-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

Details for the file django_alert_monitoring-1.0.0.tar.gz.

File metadata

  • Download URL: django_alert_monitoring-1.0.0.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for django_alert_monitoring-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2d83a11a708450189bfdfbd5bb1094fa475a62d139aed9d3fbd0a4b4202aec81
MD5 0d8fff4b7c12331612c149927b07b737
BLAKE2b-256 a2a24cf0bf89b6a76b17540239a81ac1437fff7fb58506d23e64e1729314c985

See more details on using hashes here.

File details

Details for the file django_alert_monitoring-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_alert_monitoring-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2531449d3da067181876dfe9a9e1cdfcb613c53998ac8439a8ecbdb82ee3d53d
MD5 293a1bf2fcf183d71d121cc40be43f9e
BLAKE2b-256 6c3ba647a50753ab6e851748c04cb398e99c324bdf84d4b8130f78e0e5669bca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page