技術共有

Prometheus Grafana は Linux ホストを監視します

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1.Prometheusをインストールする

1.1. プロメテウスをダウンロードする

ダウンロードURL

https://github.com/prometheus/prometheus/releases
  • 1

必要なバージョンを選択してください

wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
  • 1

1.2. Prometheus ソフトウェアをインストールする

1.2.1. ユーザーグループの作成

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
  • 1
  • 2

1.2.2. 解凍設定

tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz
mv prometheus-2.53.0.linux-amd64 /usr/local/prometheus
sed -i 's/localhost/ 172.25.0.166/g' /usr/local/prometheus/prometheus.yml
  • 1
  • 2
  • 3

1.2.3. Prometheus をシステム環境変数に追加する

vim /etc/profile
  • 1
export PROMETHEUS_HOME=/usr/local/prometheus
PATH=$PATH:$PROMETHEUS_HOME
export PATH
  • 1
  • 2
  • 3

システム環境変数ファイルをリロードする

source /etc/profile

  • 1
  • 2

1.2.4. Prometheusのバージョン情報を確認する

prometheus --version	
  • 1

1.2.5. データディレクトリの作成

mkdir /usr/local/prometheus/data/
  • 1

1.2.6. 権限の変更

chown -R prometheus.prometheus /usr/local/prometheus
  • 1

1.2.7. 設定ファイルの確認とロード

cd /usr/local/prometheus/
./promtool check config prometheus.yml
  • 1
  • 2

1.2.8. systemd サービスの作成

cat <<EOF > /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=30d
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

パラメータのメモ:

  • config.file: 設定ファイルを指定します
  • web.enable-lifecycle: http リクエストによる設定の再読み込みをサポートします
  • storage.tsdb.path: データ格納ディレクトリを指定します(デフォルトはカレントディレクトリのデータディレクトリです。存在しない場合は新規作成します)
  • storage.tsdb.retention.time: データ保持期間を指定します (デフォルトは 15 日)

1.2.9. サービスを開始する

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus && systemctl enable prometheus
  • 1
  • 2
  • 3

1.2.10. ポートが監視されていることを確認します。

ss -lnput | grep 9090
  • 1
tcp     LISTEN   0        512                    *:9090                *:*       users:(("prometheus",pid=20863,fd=7))
  • 1

1.2.11. 設定ファイルの確認とロード

http://172.25.0.166:9090/metrics
  • 1

ここに画像の説明を挿入します


2. Linuxホストを監視する

2.1. Linux ホストにエクスポータ プラグインをインストールする

2.1.1. ノードエクスポーターをダウンロードする

ダウンロードURL

https://github.com/prometheus/node_exporter/releases
  • 1

必要なバージョンをダウンロードする

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
  • 1

2.1.2. ユーザーグループの作成

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
  • 1
  • 2

2.1.3. インストールと解凍

tar -zxvf node_exporter-1.8.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-1.8.1.linux-amd64  node_exporter
  • 1
  • 2
  • 3

2.1.4. 権限の変更

chown -R prometheus.prometheus /usr/local/node_exporter
  • 1

2.1.5. 環境変数にnode_exporter実行ファイルディレクトリを追加します。

vim /etc/profile
  • 1
export NODE_EXPORTER=/usr/local/node_exporter
export PATH=$PATH:$NODE_EXPORTER
export PATH
  • 1
  • 2
  • 3

システム環境変数ファイルをリロードする

source /etc/profile
  • 1

2.1.6. systemd サービスの作成

cat > /usr/lib/systemd/system/node_exporter.service <<EOF

[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.1.7. サービスを開始する

systemctl daemon-reload
systemctl enable node_exporter && systemctl start node_exporter
systemctl status node_exporter
  • 1
  • 2
  • 3

2.1.8. ポートが監視されていることを確認します。

ss -lnput | grep 9100
  • 1
tcp     LISTEN   0        512                    *:9100                *:*       users:(("node_exporter",pid=21904,fd=3))
  • 1

2.2. Linux ホストのエクスポータ プラグインを Prometheus ソフトウェア サービスに追加する

Prometheus設定ファイルを変更する

vim /usr/local/prometheus/prometheus.yml
  • 1
scrape_configs:
... 
  - job_name: 'Linux Node'					# 添加监控项的名字
    static_configs:							
      - targets: ['172.25.0.166:9100']		# 监控主机的ip地址和端口号
  • 1
  • 2
  • 3
  • 4
  • 5

プロメテウスサービスを再起動します

systemctl restart prometheus
  • 1

2.3. Prometheus をテストして Linux ホストのステータスを監視する

http://172.25.0.166:9090/targets?search=&scrapePool=Linux+Node
  • 1

ここに画像の説明を挿入します

http://172.25.0.166:9100/metrics
  • 1

ここに画像の説明を挿入します


3. Grafana を使用してデータを表示する

3.1. グラファナをダウンロードする

ダウンロードURL

https://grafana.com/grafana/download
  • 1

必要なバージョンをダウンロードする

wget  https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.0-1.x86_64.rpm
  • 1

3.2. グラファナのインストール

rpm -ivh grafana-enterprise-11.1.0-1.x86_64.rpm
  • 1

3.3. グラファナを開始する

systemctl daemon-reload 
systemctl enable grafana-server && systemctl start grafana-server
  • 1
  • 2

3.4、グラファナ中国文化

vim /etc/grafana/grafana.ini
  • 1
[users]
# 设置默认语言为中文 
default_language = zh-Hans
  • 1
  • 2
  • 3

サービスを再起動する

systemctl restart grafana-server
  • 1

3.5. grafana テンプレートをダウンロードしてインポートする

https://grafana.com/grafana/dashboards/15172-node-exporter-for-prometheus-dashboard-based-on-11074/
  • 1

ここに画像の説明を挿入します

3.6. grafana-piechart-panel プラグインをロードする

grafana-cli plugins install grafana-piechart-panel
  • 1

または

tar -xvf grafana-piechart-panel.tar.gz
mv grafana-piechart-panel /var/lib/grafana/plugins/grafana-piechart-panel
  • 1
  • 2

3.7. ブラウザアクセス

ここに画像の説明を挿入します


4. 参考文献

https://prometheus.io/
https://blog.csdn.net/heian_99/article/details/126989356
https://www.cnblogs.com/saneri/p/14667301.html
https://blog.csdn.net/qq_31725371/article/details/114697770