Skip to main content

System metrics collector for CPU, memory, disk, and network

Project description

philiprehberger-server-monitor

Tests PyPI version Last updated

System metrics collector for CPU, memory, disk, and network.

Installation

pip install philiprehberger-server-monitor

Usage

Single Snapshot

from philiprehberger_server_monitor import Monitor

monitor = Monitor()
snap = monitor.snapshot()
print(f"CPU: {snap.cpu.percent}%")
print(f"Memory: {snap.memory.used_gb:.1f}/{snap.memory.total_gb:.1f} GB")
print(f"Disk: {snap.disk['/'].percent}%")

# Export snapshot
data = snap.to_dict()

Continuous Monitoring with Alerts

from philiprehberger_server_monitor import Monitor, Alert

monitor = Monitor()
monitor.watch(
    interval=5.0,
    on_snapshot=lambda s: print(f"CPU: {s.cpu.percent}%"),
    alerts=[
        Alert(metric="cpu.percent", threshold=90, callback=lambda m, v, t: print(f"HIGH CPU: {v}%")),
        Alert(metric="memory.percent", threshold=85, callback=lambda m, v, t: print(f"HIGH MEM: {v}%")),
    ],
)

Trend Tracking

from philiprehberger_server_monitor import Monitor

monitor = Monitor()

# Start recording snapshots every 5 seconds (keeps last 720)
monitor.start_recording(interval=5.0, max_snapshots=720)

# Later, analyze trends over the last 5 minutes
trend = monitor.get_trend("cpu.percent", window_seconds=300)
print(f"CPU slope: {trend.slope:.4f}%/s")
print(f"CPU went from {trend.start_value}% to {trend.end_value}%")

# Stop recording
monitor.stop_recording()

Persisting Recorded Snapshots

monitor = Monitor()
monitor.start_recording(interval=5.0)
# ... time passes ...
monitor.export_json("metrics.json")

# Or grab a defensive copy of the buffer for in-process analysis
recent = monitor.snapshots()
print(f"Have {len(recent)} snapshots")

API

Function / Class Description
Monitor System metrics monitor with snapshot(), watch(), stop(), and trend tracking methods
Snapshot A point-in-time system metrics snapshot with cpu, memory, disk, network fields
CpuInfo CPU metrics (percent, count, count_logical, per_cpu, freq_mhz)
MemoryInfo Memory metrics (total, available, used, percent) with GB properties
DiskInfo Disk metrics for a single mount point (total, used, free, percent)
NetworkInfo Network metrics (bytes_sent, bytes_recv, packets_sent, packets_recv)
Alert(metric, threshold, callback) Threshold-based alert configuration for continuous monitoring
Trend Trend analysis result with metric, start_value, end_value, slope, duration_seconds
monitor.start_recording(interval, max_snapshots) Start background snapshot recording into a ring buffer
monitor.stop_recording() Stop the recording thread
monitor.get_trend(metric, window_seconds) Compute linear trend for a metric over recent snapshots
monitor.snapshots() Return a copy of the recorded snapshot buffer
monitor.export_json(path) Write recorded snapshots to a JSON file

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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

philiprehberger_server_monitor-0.3.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file philiprehberger_server_monitor-0.3.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_server_monitor-0.3.0.tar.gz
Algorithm Hash digest
SHA256 713065554bc76f3ba5a76551d3b25f5eae01cbdc19560e5c3ca2cca3d300c951
MD5 d90b67feb7f91474e298d5bdfc4fc9f7
BLAKE2b-256 e144c0e813df709887a5c63bd6afc2ac7d1e73cd492077b85fbf46074ca45416

See more details on using hashes here.

File details

Details for the file philiprehberger_server_monitor-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_server_monitor-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f10ab31929f5fa9c62597e99dd89d204ed343284d38a9003e068c073848db363
MD5 d84e28b6ca2dcaf85857133ad09f0352
BLAKE2b-256 d11d123944daab2bf133a79f6f7089108c48d4f9a012facdc530e82f01550eaf

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