π Real-Time WHM Monitoring with Grafana, Prometheus & a Custom Python Exporter


1. Introduction
π Why monitor WHM in real-time?
For system admins and hosting providers, tracking CPU, memory, disk, and bandwidth usage is critical. Whether youβre troubleshooting spikes, identifying resource-hungry accounts, or preventing downtime β live dashboards save time and stress.
Today, Iβm sharing how I built a WHM β Python Exporter β Prometheus β Grafana pipeline for complete visibility.
2. π Prerequisites
Before starting, make sure you have:
β WHM/cPanel root access
β A VPS/Dedicated server
β Python 3.x installed
β Prometheus installed
β Grafana installed
β WHM API access token
3. π Step 1 β Enable WHM API
1οΈβ£ Login to WHM β Development β Manage API Tokens
2οΈβ£ Create a token with read permissions for server stats.
3οΈβ£ Copy and save it securely (will be used in the Python script).
4. π Step 2 β Create Python Exporter
This script queries WHM API for:
π» CPU & Memory usage
πΎ Disk usage per cPanel user
π‘ Bandwidth usage per cPanel user
π« Suspended accounts
from flask import Flask, Response
import requests
app = Flask(__name__)
WHM_API_TOKEN = "YOUR_API_TOKEN"
WHM_HOST = "https://YOUR_SERVER:2087"
HEADERS = {"Authorization": f"whm root:{WHM_API_TOKEN}"}
@app.route("/metrics")
def metrics():
load = requests.get(f"{WHM_HOST}/json-api/loadavg?api.version=1", headers=HEADERS, verify=False).json()
cpu = load['data']['one']
return Response(f"server_cpu_usage {cpu}\n", mimetype="text/plain")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=9100)
5. β Step 3 β Run as a System Service
To make sure it starts on boot:
[Unit]
Description=WHM Prometheus Exporter
After=network.target
[Service]
ExecStart=/usr/bin/python3 /path/to/whm_exporter.py
Restart=always
User=root
[Install]
WantedBy=multi-user.target
systemctl enable whm_exporter
systemctl start whm_exporter
6. π‘ Step 4 β Configure Prometheus
Edit /etc/prometheus/prometheus.yml
:
scrape_configs:
- job_name: 'whm_exporter'
static_configs:
- targets: ['YOUR_SERVER_IP:9100']
Restart Prometheus:
systemctl restart prometheus
7. π Step 5 β Create Grafana Dashboard
1οΈβ£ Add Prometheus as a datasource.
2οΈβ£ Create panels for:
CPU & Memory usage gauges
Disk usage per user (table)
Bandwidth usage per user (time series)
Suspended accounts list
3οΈβ£ Use thresholds to highlight high usage in red.
8. π Step 6 β Share Your Dashboard
Use Grafana Snapshot to share safely without exposing live data.
Post screenshots on LinkedIn, GitHub README, or your website.
9. β Conclusion
This setup gave me real-time WHM monitoring with full visibility into server health and per-user activity.
Next steps: add alerts for overuse, automate resource suspension, and integrate with Slack/Telegram.
For more insights and help support me Join me on my journey at Linkedin
Subscribe to my newsletter
Read articles from Mihir Savla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
