使用Grafana和Prometheus對Linux伺服器性能進行監控,主要通過node_exporter進行監控,名額如 CPU、記憶體、磁盤等。Prometheus通過HTTP協定從遠端的機器收集資料并存儲Prometheus本地時序資料庫。
-
安裝啟動node_exporter
需在被監控server上安裝node_exporter。下載下傳node_exporter,下載下傳位址:https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
建立prometheus使用者,用于啟動node_exporter.service服務。wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz tar xzvf /root/node_exporter-0.18.1.linux-amd64.tar.gz mv /root/node_exporter-0.18.1.linux-amd64 /opt/prometheus/ ln -s /opt/prometheus/node_exporter-0.18.1.linux-amd64/ /opt/prometheus/node_exporter #建立軟鍊
建立 /usr/lib/systemd/system/node_exporter.service 檔案。useradd -s /sbin/nologin -M prometheus
啟動:[Unit] Description=node_exporter Documentation=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus ExecStart=/opt/prometheus/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target
systemctl start node_exporter.service systemctl enable node_exporter.service
#!/bin/bash #Author: Joey-Yu cat << EOF +------------------------------------------------------------------+ | ** Welcome to install prometheus node_exporter for CentOS 7 OS| +------------------------------------------------------------------+ EOF function format() { echo -e "\033[32m Success!!! \033[0m\n" echo "#########################################################" } #mkdir dir echo "set prometheus node-exporter dir" dir=/opt/prometheus/ if [ ! -d $dir ]; then mkdir -p /opt/prometheus/ fi format #download node_exporter-0.18.1.linux-amd64.tar.gz echo "download and tar prometheus node_exporter-0.18.1.linux-amd64.tar.gz in root" #cd /root && wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz node_exporter_file=/root/node_exporter-0.18.1.linux-amd64.tar.gz if [ ! -f $node_exporter_file ]; then echo "no node_exporter-0.18.1.linux-amd64.tar.gz in root!" exit else cd /root/ tar xzvf /root/node_exporter-0.18.1.linux-amd64.tar.gz mv /root/node_exporter-0.18.1.linux-amd64 /opt/prometheus/ format fi # useadd prometheus echo "useadd prometheus user" useradd -s /sbin/nologin -M prometheus format # chown prometheus and ln -s echo "chown prometheus and ln -s" cd /opt/prometheus/ chown -R prometheus.prometheus /opt/prometheus/node_exporter-0.18.1.linux-amd64/ ln -s /opt/prometheus/node_exporter-0.18.1.linux-amd64/ /opt/prometheus/node_exporter format # config prometheus /usr/lib/systemd/system/node_exporter.service echo "touch /usr/lib/systemd/system/node_exporter.service" touch /usr/lib/systemd/system/node_exporter.service cat > /usr/lib/systemd/system/node_exporter.service << EOF [Unit] Description=node_exporter Documentation=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus ExecStart=/opt/prometheus/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl start node_exporter.service systemctl enable node_exporter.service format
-
prometheus配置
修改prometheus.yml配置檔案。主要列出scrape_configs下的配置:
- job_name: 'node-db' file_sd_configs: - files: ['/usr/local/prometheus/node-db.yml'] refresh_interval: 5s - job_name: 'node-pre' file_sd_configs: - files: ['/usr/local/prometheus/node-pre.yml'] refresh_interval: 5s - job_name: 'node-application' file_sd_configs: - files: ['/usr/local/prometheus/node-application.yml'] refresh_interval: 5s - job_name: 'node-pro' file_sd_configs: - files: ['/usr/local/prometheus/node-pro.yml'] refresh_interval: 5s
- targets: ["10.20.1.1:9100"] labels: instance: grafana - targets: ["10.20.1.2:9100"] labels: instance: Zabbix-proxy - targets: ["10.20.1.3:9100"] labels: instance: prometheus
systemctl restart prometheus.service
-
Web界面通路
使用Prometheus内置了web界面通路效果。進入Prometheus Web首頁Status => Targets,成功後如下圖:
但Prometheus自帶的圖形并不夠強大,這裡結合Grafana作為Prometheus的Dashboard。 -
配置Grafana
首先在Grafana中添加Prometheus作為資料源。
進入Grafana,點選"Add data source"按鈕,跳轉到添加資料源頁面,配置Data Sources選項中添加資料源。
其中URL填寫Prometheus位址,其他項保持預設。單擊 Save & Test 添加成功後如下:
Granfana 導入node_exporter 監控圖表。這裡選擇官網給的 https://grafana.com/grafana/dashboards/8919 監控模闆,導出Download JSON。該監控模闆有說明支援 Node Exporter v0.16及以上的版本,包含:CPU 記憶體 磁盤 IO 網絡 流量 溫度等監控名額。
Granfana進入Create->Import導入Dashboards。
node_exporter監控圖表最終效果如下:
監控項還是非常詳細,可在左上角選擇不同的node節點下的不同server檢視監控。