📊 Grafana
多資料來源的美觀監控儀表板
Grafana 能連接 Prometheus、InfluxDB、MySQL、Loki 等多種資料來源,建立精美的監控儀表板,支援告警通知至 Slack、Email、PagerDuty 等渠道。
安裝
$ sudo apt install -y apt-transport-https software-properties-common wget
wget -q -O - https://apt.grafana.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/grafana.gpg
echo "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://apt.grafana.com stable main" | \
sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update && sudo apt install -y grafana
sudo systemctl enable --now grafana-server安裝後訪問 http://伺服器IP:3000,預設帳號密碼為 admin / admin(首次登入強制更改)。
$ # 若要透過 Nginx 反向代理(建議)
sudo ufw allow 3000/tcp # 或只允許特定 IP 存取新增 Prometheus 資料來源
- 登入後點選左側選單 Connections → Data sources
- 點選 Add data source → 選擇 Prometheus
- 在 URL 填入
http://localhost:9090 - 點選 Save & test,出現綠色成功訊息即可
透過 API 設定資料來源(Provisioning)
$ sudo vim /etc/grafana/provisioning/datasources/prometheus.ymlapiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://localhost:9090
isDefault: true
editable: false$ sudo systemctl restart grafana-server匯入現成儀表板
Grafana 社群提供大量現成儀表板,直接匯入即可使用:
| 儀表板 | ID | 說明 |
|---|---|---|
| Node Exporter Full | 1860 | 全面的主機系統指標 |
| Node Exporter Quickstart | 13978 | 精簡版主機監控 |
| MySQL Overview | 7362 | MySQL 效能監控 |
| Nginx Exporter | 12708 | Nginx 請求與連線統計 |
| Redis Overview | 763 | Redis 記憶體與命中率 |
匯入方式:左側 Dashboards → New → Import,輸入 ID 後點選 Load。
告警設定
- 建立告警規則:在 Panel → Edit → Alert tab 設定條件
- 設定通知渠道:Alerting → Contact points 新增 Email、Slack、Telegram 等
- 設定通知政策:Alerting → Notification policies 指定哪些告警走哪個渠道
進階設定(/etc/grafana/grafana.ini)
[server]
http_port = 3000
domain = grafana.example.com
root_url = https://grafana.example.com
[security]
admin_user = admin
admin_password = 更強的密碼
[smtp]
enabled = true
host = smtp.sendgrid.net:587
user = apikey
password = 你的API金鑰
from_address = grafana@example.com
[auth.anonymous]
enabled = false # 禁止匿名訪問$ sudo systemctl restart grafana-server