← Back to LLM prompts

Python CPU Utilization Monitor & Analyzer

A production-ready Python script for real-time CPU utilization monitoring, logging, and analysis with configurable thresholds, alerting, and data export capabilities.

data a general-purpose LLM AnalysisWriting
<role>You are a senior Python developer specializing in system monitoring and performance analysis tools.</role>

<task>Create a robust, production-ready Python script that monitors CPU utilization in real-time, logs metrics, triggers alerts on threshold breaches, and exports data for analysis.</task>

<context>The script will be deployed on [target_os: e.g., Linux servers, Windows machines, cross-platform] to monitor [monitoring_scope: e.g., overall system CPU, per-core usage, specific process CPU] for [use_case: e.g., capacity planning, anomaly detection, performance benchmarking, SLA compliance]. It must run as a [deployment_mode: e.g., daemon/service, scheduled cron job, interactive CLI tool] with minimal resource overhead.</context>

<constraints>
- Use only standard library + [allowed_dependencies: e.g., psutil, pandas, prometheus-client, none]
- Support configurable sampling interval [default_interval_seconds: e.g., 5]
- Implement [alert_mechanism: e.g., logging, email, webhook, Prometheus metrics, syslog]
- Handle graceful shutdown on SIGTERM/SIGINT
- Rotate logs at [log_rotation_size_mb: e.g., 100] with [retention_days: e.g., 30] retention
- Export data in [export_formats: e.g., CSV, JSON, Parquet, Prometheus]
- Include [authentication_method: e.g., none, API key, mTLS] for remote endpoints
- Achieve < [max_cpu_overhead_percent: e.g., 1%] monitoring overhead
- Support [python_version: e.g., 3.8+] with type hints
</constraints>

<format>
Provide a single Python file with:
1. Shebang, module docstring, and version
2. Configuration via [config_method: e.g., YAML file, environment variables, argparse]
3. Main monitoring class with start()/stop()/get_metrics() methods
4. Alert evaluation engine with [alert_rules: e.g., threshold, rate-of-change, anomaly]
5. Structured logging (JSON format) with correlation IDs
6. Unit tests for core logic (pytest fixtures)
7. Example systemd service file / Windows service wrapper
8. README with usage examples
</format>

<tone>Technical, precise, and production-focused. Prioritize reliability, observability, and maintainability.</tone>

<placeholders>
- [target_os]
- [monitoring_scope]
- [use_case]
- [deployment_mode]
- [allowed_dependencies]
- [default_interval_seconds]
- [alert_mechanism]
- [log_rotation_size_mb]
- [retention_days]
- [export_formats]
- [authentication_method]
- [max_cpu_overhead_percent]
- [python_version]
- [config_method]
- [alert_rules]
</placeholders>

<final_instruction>Generate the complete Python script now, replacing all placeholders with sensible defaults for a Linux production environment monitoring overall system CPU with psutil, 5-second intervals, log+webhook alerts, CSV/JSON export, 30-day retention, and systemd deployment.</final_instruction>
Website Source
#text