A Python tool to quickly run queries on Prometheus, export results as CSV files, and generate PDF reports with visualized plots.
- π Query Prometheus metrics with configurable time ranges
- π Export data to CSV format for analysis
- π Generate PDF reports with automated plots and visualizations
- π³ Docker support for easy deployment
- βοΈ Flexible configuration with custom metrics files
- π Multiple date formats support (RFC 3339 and Unix timestamps)
- Python 3.7+
- Prometheus instance (can be started using the provided Docker Compose)
- Required Python packages (see Installation)
Install the required Python dependencies:
pip3 install jproperties requests pandas pillow plotly kaleidopip3 install -r requirements.txtBuild the Docker image:
docker build -t prometheus-csvplot .A configuration file is required with a list of Prometheus metrics to be collected.
Default metrics file: config/metrics.txt
Custom metrics file: Place your custom metrics file in the config/ directory
Example: config/metrics_haproxy.txt
Create or edit config/metrics.txt:
rate(go_gc_duration_seconds[5m]) scrape_duration_seconds prometheus_http_request_duration_seconds_bucket{handler="/api/v1/query_range", instance="localhost:9090", job="prometheus", le="0.1"} sum(rate(http_server_requests_seconds_count{instance="application:8080", status!~"5.*"}[5m])) Use the provided Docker Compose file to start a Prometheus instance with Node Exporter:
docker-compose up -dThis will start:
- Prometheus on port
9090 - Node Exporter for system metrics
Generate CSV files from Prometheus metrics:
python3 export_csv.py <prometheus_url> <start_date> <end_date> [custom_metrics_file]Parameters:
prometheus_url: URL of your Prometheus instancestart_date: Start date in RFC 3339 format or Unix timestampend_date: End date in RFC 3339 format or Unix timestampcustom_metrics_file: (Optional) Custom metrics file name
Output: A new directory csv/metrics_YYYY-MM-DD-HH:MM:SS containing CSV files
Create a PDF report with plots from CSV files:
python3 plot.py <csv_directory>Parameters:
csv_directory: Path to the directory containing CSV files
Output: A report.pdf file in the specified CSV directory
# Export metrics for the last hour python3 export_csv.py http://localhost:9090 2023-12-14T10:00:00Z 2023-12-14T11:00:00Z # Generate PDF report from exported data python3 plot.py csv/metrics_2023-12-14-11:20:20# Export with custom metrics file python3 export_csv.py http://localhost:9090 2023-12-14T10:00:00Z 2023-12-14T11:00:00Z metrics_haproxy.txt# Using Unix timestamps instead of RFC 3339 python3 export_csv.py http://localhost:9090 1702554000 1702557600 metrics.txt# Build the image docker build -t prometheus-csvplot . # Run with Docker Compose docker-compose up -d # Run the export script docker run --rm -v $(pwd)/csv:/app/csv prometheus-csvplot python3 export_csv.py http://prometheus:9090 2023-12-14T10:00:00Z 2023-12-14T11:00:00ZStop and remove all containers and volumes:
docker-compose down --volumesThis project is licensed under the Apache License 2.0. See the LICENSE file for details.