# ---------- open Grafana ----------
# http://localhost:3000     login: admin / admin  (set a new password or skip)

# ---------- A. add Prometheus as data source ----------
# Connections > Data sources > Add data source > Prometheus
#   URL: http://localhost:9090      -> Save & test  (green "Successfully queried")

# ---------- B. create the panel ----------
# Dashboards > New > New dashboard > Add visualization > select Prometheus
#   Query (Code mode):
100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)
#   Title: CPU Utilisation %      Visualization: Time series (or Gauge)
#   Standard options > Unit: Percent (0-100)    Min 0, Max 100

# ---------- C. threshold (colour change) ----------
#   Right side: Thresholds
#     Base = green
#     Add threshold 70 = orange (warning)
#     Add threshold 85 = red (critical)
#   Graph styles > Show thresholds: "As lines (dashed)"
#   Apply, then Save dashboard (name: Server Health)

# ---------- D. alert rule ----------
#   In the panel: Alert tab > Create alert rule   (or Alerting > Alert rules > New)
#     Name        : High CPU
#     Query A     : 100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)
#     Expression  : Reduce (Last)  ->  Threshold: IS ABOVE 85
#     Evaluate    : every 1m, for 2m
#     Folder/group: create "Lab" / "cpu-alerts"
#     Summary     : CPU above 85% for 2 minutes
#   Save rule and exit

# ---------- E. prove it works ----------
stress-ng --cpu $(nproc) --timeout 300s
#   panel turns red above 85; Alerting > Alert rules: state Normal -> Pending -> Firing
pkill stress-ng
#   state returns to Normal

# ---------- optional: ready-made dashboard ----------
# Dashboards > New > Import > ID 1860 (Node Exporter Full) > choose Prometheus > Import