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

Mihir SavlaMihir Savla
2 min read

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

0
Subscribe to my newsletter

Read articles from Mihir Savla directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mihir Savla
Mihir Savla