Skip to main content

A python package for threshold based alerting using simple configuration.

Project description

PySentinel

pysentinel logo

PyPI version Python versions Codecov Snyk License: MIT

PySentinel is a Python package for threshold-based alerting using simple configuration. It scans data sources and raises alerts when specified thresholds are crossed.

Features

  • Scan data sources for threshold violations
  • Simple configuration for thresholds and alerts
  • Easy integration into existing Python projects

Installation

You can install pysentinel using Poetry or pip.

Using Poetry

poetry add pysentinel

Using pip

pip install pysentinel

Usage Examples

from pysentinel.core.scanner import Scanner

# Example configuration
config = {
    "global": {
        "alert_cooldown_minutes": 5
    },
    "datasources": {
        "my_postgres": {
            "type": "postgresql",
            "enabled": True,
            "host": "localhost",
            "port": 5432,
            "database": "mydb",
            "user": "user",
            "password": "pass"
        }
    },
    "alert_channels": {
        "email_alerts": {
            "type": "email",
            "enabled": True,
            "recipients": ["admin@example.com"],
            "smtp_server": "smtp.example.com"
        }
    },
    "alert_groups": {
        "critical_metrics": {
            "enabled": True,
            "alerts": [
                {
                    "name": "High CPU Usage",
                    "metrics": "cpu_usage",
                    "query": "SELECT cpu_usage FROM metrics WHERE time > now() - interval '1 minute'",
                    "datasource": "my_postgres",
                    "threshold": 90,
                    "severity": "CRITICAL",
                    "interval": 300,  # seconds between checks
                    "alert_channels": ["email_alerts"],
                    "description": "CPU usage is above 90% for the last minute"
                }
            ]
        }
    }
}

scanner = Scanner(config)
scanner.start_background()

# The scanner will now run in the background, checking alerts at configured intervals.
# Alert evaluation intervals and last run times are persisted in a local SQLite database (alerts.db).

This example shows how to configure and start the scanner, with alert intervals and persistent runtime tracking.

Blocking usage with start():

from pysentinel.core.scanner import Scanner

config = { ... }  # your configuration dictionary

scanner = Scanner(config)
scanner.start()  # This will block and run the scanner loop

Async usage with start_async():

import asyncio
from pysentinel.core.scanner import Scanner

config = { ... }  # your configuration dictionary

async def main():
    scanner = Scanner(config)
    await scanner.start_async()
    # Optionally, do other async tasks here

asyncio.run(main())

Replace { ... } with your actual configuration.
start() runs the scanner in the main thread and blocks, while start_async() allows integration with other async code.

FastAPI Integration Example

from fastapi import FastAPI
from pysentinel.core.scanner import Scanner

app = FastAPI()

config = {
    # ... your pysentinel configuration ...
}

scanner = Scanner(config)

@app.on_event("startup")
async def start_scanner():
    # Start the scanner in the background when FastAPI starts
    import asyncio
    await asyncio.create_task(scanner.start_async())

@app.get("/")
async def root():
    return {"message": "pysentinel FastAPI integration running"}

This example shows how to integrate pysentinel with FastAPI, starting the scanner in the background when the application starts.

Configuration

Here’s how to use the load_config() function from pysentinel.config.loader to load your YAML config and start the scanner.
This approach works for both YAML and JSON config files.

import pysentinel.config.loader as loader
from pysentinel.core.scanner import Scanner

config = loader.load_config("config.yml")
scanner = Scanner(config)
scanner.start_background()

This will load your configuration from config.yml and start the scanner in the background.

Example Configuration File in yml format

Here is an example config.yml:

See the full example config at tests/pysentinel/fixtures/config.yml.

This can be used as a reference for creating your own configuration in YAML format for pysentinel:

global:
  alert_cooldown_minutes: 5

datasources:
  my_postgres:
    type: postgresql
    enabled: true
    host: localhost
    port: 5432
    database: mydb
    user: user
    password: pass

alert_channels:
  email_alerts:
    type: email
    enabled: true
    recipients:
      - admin@example.com
    smtp_server: smtp.example.com

alert_groups:
  critical_metrics:
    enabled: true
    alerts:
      - name: High CPU Usage
        metrics: cpu_usage
        query: SELECT cpu_usage FROM metrics WHERE time > now() - interval '1 minute'
        datasource: my_postgres
        threshold: 90
        severity: CRITICAL
        interval: 300  # seconds between checks
        alert_channels:
          - email_alerts
        description: CPU usage is above 90% for the last minute

This YAML config can be loaded using load_config("config.yml") and passed to the Scanner.

Requirements

  • Python >= 3.9, < 4.0

Development

To set up the development environment:

poetry install

To run tests:

poetry run pytest

License

MIT

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

pysentinel-0.1.3.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

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

pysentinel-0.1.3-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file pysentinel-0.1.3.tar.gz.

File metadata

  • Download URL: pysentinel-0.1.3.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for pysentinel-0.1.3.tar.gz
Algorithm Hash digest
SHA256 eb25c105764bf3d083d0cf891fa8317d07e74d26b50157f4592883373c089fb4
MD5 804b342bfa148c1a6f22304fcb867623
BLAKE2b-256 6a574e991c3333fecb8773120b6d6535df532525a5073d87df069f848930ceea

See more details on using hashes here.

File details

Details for the file pysentinel-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pysentinel-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for pysentinel-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a58856b368c8e5da195ba2eed9ae8c9bf71804b51516605a9ef2ee09f5733149
MD5 2b1975c184bef9da2cffbf5911eab01a
BLAKE2b-256 a29c82c481dc486d829f4e267fe23a8c28aff19e9fb471db496448e006480b0b

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