LoginSignup
1
1

Linux: システム監視ソフト「Prometheus」を無料インストールしてみた

Posted at
実施環境:
Linux
[testuser@testhost ~]$ uname -a
Linux testhost 4.18.0-448.el8.x86_64 #1 SMP Wed Jan 18 15:02:46 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
[testuser@testhost ~]$ cat /etc/redhat-release
CentOS Stream release 8

prometheus:2.46.0.linux-amd64
node_exporter:1.6.1.linux-amd64

0. 概要

クラウドネイティブという単語をご存じでしょうか。
クラウドネイティブとは「設計レベルでクラウド上での動作を前提とする考え方」のことであり、「クラウド上でも動作できるようにする」ことのさらに先をゆく概念です。
クラウドネイティブを推進する団体に CNCF ( Cloud Native Computing Foundation ) という団体があり、その団体が管理するソフトウェアの1つが Prometheus です。

Prometheus (プロメテウス)はオープンソースのシステム監視ソフトウェアです。
監視対象となるサーバを動的に管理できるなどの特徴があり、クラウドネイティブな環境によく適合するとして注目されています。
また、セットアップも簡単であり、サーバ上に配置してコマンドを実行すればすぐに起動することが可能です。

Prometheus

今回はお試しとして、クラウド環境ではなく単一の Linux サーバにインストールしてみました。
最低限の構成として、本体である「 Prometheus 」と情報取得用のコンポーネントである「 exporter 」を導入し、 CPU の使用状況を画面に表示するところまでを目指します。

1. ソフトウェアのダウンロード

まずは以下のサイトからソフトウェアをダウンロードします。

Download

Prometheus 本体はバージョン 2.46.0 を使用しました。

WS000348.JPG

exporter は収集する情報によっていろいろなものがありますが、今回はマシンリソース収集向けの node_exporter を使用します。
バージョンは 1.6.1 です。

WS000349.JPG

2. Prometheus インストール

では、インストールを始めていきます。
まずは Prometheus 本体の .tar.gz ファイルをサーバ上に配置し、解凍します。

Linux
[testuser@testhost tmp]$ pwd
/tmp
[testuser@testhost tmp]$ ls -l prometheus-*.tar.gz
-rw-rw-r--. 1 testuser testuser 94876162  8月 15 12:42 prometheus-2.46.0.linux-amd64.tar.gz
[testuser@testhost tmp]$ tar xzf prometheus-2.46.0.linux-amd64.tar.gz
[testuser@testhost tmp]$ ls -ld prometheus-*
drwxr-xr-x. 4 testuser testuser      132  7月 25 22:11 prometheus-2.46.0.linux-amd64
-rw-rw-r--. 1 testuser testuser 94876162  8月 15 12:42 prometheus-2.46.0.linux-amd64.tar.gz
[testuser@testhost tmp]$

解凍したファイルの中身は以下のようになっています。

Linux
[testuser@testhost tmp]$ cd prometheus-2.46.0.linux-amd64/
[testuser@testhost prometheus-2.46.0.linux-amd64]$ pwd
/tmp/prometheus-2.46.0.linux-amd64
[testuser@testhost prometheus-2.46.0.linux-amd64]$ ls -l
合計 236276
-rw-r--r--. 1 testuser testuser     11357  7月 25 22:06 LICENSE
-rw-r--r--. 1 testuser testuser      3773  7月 25 22:06 NOTICE
drwxr-xr-x. 2 testuser testuser        38  7月 25 22:06 console_libraries
drwxr-xr-x. 2 testuser testuser       173  7月 25 22:06 consoles
-rwxr-xr-x. 1 testuser testuser 123611355  7月 25 21:34 prometheus
-rw-r--r--. 1 testuser testuser       934  7月 25 22:06 prometheus.yml
-rwxr-xr-x. 1 testuser testuser 118310964  7月 25 21:36 promtool

本来であればこれらをきちんとしたディレクトリに配置したり設定ファイルを修正したりしますが、今回はお試しなので手っ取り早く tmp ディレクトリ上で動作させてしまいます。

実行コマンドは以下の通りです。
Ctrl + C 等でコマンドプロセスを KILL するまで Prometheus は起動状態を保ちます。

Linux
[testuser@testhost prometheus-2.46.0.linux-amd64]$ ./prometheus --config.file=./prometheus.yml
ts=2023-08-15T04:10:42.048Z caller=main.go:541 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2023-08-15T04:10:42.049Z caller=main.go:585 level=info msg="Starting Prometheus Server" mode=server version="(version=2.46.0, branch=HEAD, revision=cbb69e51423565ec40f46e74f4ff2dbb3b7fb4f0)"
(省略)
ts=2023-08-15T04:10:42.190Z caller=main.go:1011 level=info msg="Server is ready to receive web requests."
ts=2023-08-15T04:10:42.190Z caller=manager.go:1009 level=info component="rule manager" msg="Starting rule manager..."

この状態で、さっそく Prometheus にアクセスしてみましょう。
ブラウザを立ち上げ、 Prometheus をインストールしたサーバに http でアクセスします。
ポート番号は 9090 です。

http://(サーバのIPアドレス):9090/

以下のような画面が出れば成功です。

WS000350.JPG

3. exporter インストール

さて、これだけだとまだ情報の収集ができていません。
情報収集のためには exporter というコンポーネントが必要となります。
先ほどダウンロードした node_exporter をサーバ上に転送し、解凍しましょう。

Linux
[testuser@testhost tmp]$ pwd
/tmp
[testuser@testhost tmp]$ ls -l node_exporter-*.tar.gz
-rw-rw-r--. 1 testuser testuser 10368103  8月 15 12:44 node_exporter-1.6.1.linux-amd64.tar.gz
[testuser@testhost tmp]$ tar xzf node_exporter-1.6.1.linux-amd64.tar.gz
[testuser@testhost tmp]$ ls -ld node_exporter-*
drwxr-xr-x. 2 testuser testuser       56  7月 17 21:16 node_exporter-1.6.1.linux-amd64
-rw-rw-r--. 1 testuser testuser 10368103  8月 15 12:44 node_exporter-1.6.1.linux-amd64.tar.gz

解凍した中身は以下の通りです。

Linux
[testuser@testhost ~]$ cd /tmp/node_exporter-1.6.1.linux-amd64/
[testuser@testhost node_exporter-1.6.1.linux-amd64]$ pwd
/tmp/node_exporter-1.6.1.linux-amd64
[testuser@testhost node_exporter-1.6.1.linux-amd64]$ ls -l
合計 19572
-rw-r--r--. 1 testuser testuser    11357  7月 17 21:15 LICENSE
-rw-r--r--. 1 testuser testuser      463  7月 17 21:15 NOTICE
-rwxr-xr-x. 1 testuser testuser 20025119  7月 17 21:11 node_exporter

さっそく node_exporter も起動しましょう。

Linux
[testuser@testhost node_exporter-1.6.1.linux-amd64]$ ./node_exporter
ts=2023-08-15T04:18:45.379Z caller=node_exporter.go:180 level=info msg="Starting node_exporter" version="(version=1.6.1, branch=HEAD, revision=4a1b77600c1873a8233f3ffb55afcedbb63b8d84)"
ts=2023-08-15T04:18:45.379Z caller=node_exporter.go:181 level=info msg="Build context" build_context="(go=go1.20.6, platform=linux/amd64, user=root@586879db11e5, date=20230717-12:10:52, tags=netgo osusergo static_build)"
(省略)
ts=2023-08-15T04:18:45.389Z caller=tls_config.go:274 level=info msg="Listening on" address=[::]:9100
ts=2023-08-15T04:18:45.389Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=[::]:9100

ただ、これだけだとまだ Prometheus と exporter は連携できていません。

4. Prometheus と exporter の連携

さて、先ほどは説明しませんでしたが、 Prometheus 起動時のコマンドライン

Linux
$ ./prometheus --config.file=./prometheus.yml

について、 ./prometheus.yml は Prometheus の基本的な設定ファイルです。
設定ファイルは YAML という形式で記載されており、実際の中身は以下のようになっています。

prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

この設定ファイルの末尾

prometheus.yml
    static_configs:
      - targets: ["localhost:9090"]

の部分を適切なホスト名とポート番号に書き換えることで exporter と Prometheus を連携させることができます。
今回 exporter と Prometheus は同じサーバ上にインストールするので、 localhost はそのままにしておきます。
node_exporter のポート番号は「 9100 」です。
すなわち、以下のように書き換えることになります。

prometheus.yml
    static_configs:
      - targets: ["localhost:9100"]

ではさっそく、 Ctrl+C でいったん Prometheus 本体のコマンドプロセスを停止して、設定ファイルの書き換えを行っていきます。

Linux
[testuser@testhost prometheus-2.46.0.linux-amd64]$ cp -p prometheus.yml prometheus.yml.bk
[testuser@testhost prometheus-2.46.0.linux-amd64]$ ls -l prometheus.yml*
-rw-r--r--. 1 testuser testuser 934  7月 25 22:06 prometheus.yml
-rw-r--r--. 1 testuser testuser 934  7月 25 22:06 prometheus.yml.bk
[testuser@testhost prometheus-2.46.0.linux-amd64]$ vi prometheus.yml
[testuser@testhost prometheus-2.46.0.linux-amd64]$ diff prometheus.yml prometheus.yml.bk
29c29
<       - targets: ["localhost:9100"]
---
>       - targets: ["localhost:9090"]

書き換えが完了したら再び Prometheus 本体を起動します。

Linux
[testuser@testhost prometheus-2.46.0.linux-amd64]$ ./prometheus --config.file=./prometheus.yml
(省略)

先ほどと同じようにしてブラウザで Prometheus 本体に接続し、 exporter がきちんと連携できていることを確認します。
「 Status 」⇒「 Targets 」を選択してください。

WS000351.JPG

「 Targets 」の画面に「 localhost:9100 」が表示されているので、先ほど起動した exporter が検知できていることがわかります。

WS000352.JPG

では CPU の使用状況を表示してみましょう。
「 Graph 」を選択して最初の画面に戻ります。

WS000354.JPG

検索フォームの右、丸いマークの書かれたボタンを押します。

WS000357.JPG

すると現在表示できるメトリクスの一覧が出てくるので、今回は「 node_cpu_seconds_total 」を選択します。

WS000358.JPG

検索フォームに「 node_cpu_seconds_total 」が入力されたことを確認して、「 Execute 」を押します。

WS000359.JPG

すると CPU の使用状況に関する情報がずらっと出てきます。
デフォルトの表示形式はテーブルとなっているので、「 Graph 」を選択してグラフ表記にしましょう。

WS000360.JPG

以下のようにグラフが表示できました。

WS000361.JPG

5. クエリ文

最後にちょっとクエリ文に手を加えて表示する情報を絞ってみましょう。
Prometheus のクエリ文の文法は以下のページに記載されています。

query

これを参考にして、以下のクエリ文を作成してみました。
表示するインスタンスに制限がかけられています。

Prometheus
node_cpu_seconds_total{instance="localhost:9100",cpu="0"}

このクエリ文を実行してみた結果が以下です。
表示されるグラフが絞られていることがわかります。

WS000362.JPG

6. 備考

今回はごく最低限の構築まででとどめましたが、 Prometheus は他にも様々なことができます。
例えば exporter は今回使用した node_exporter 以外にも様々なものがあり、それらを使い分けることで多種多様な情報を収集することが可能です。
また、今回は紹介しませんでしたが alertmanager というコンポーネントを使用すればメールやチャットにアラートを発行することも可能です。

さらに、 Prometheus は外部のソフトウェアとの連携も可能です。
特に Grafana は公式サイトでも連携方法が紹介されており、ダッシュボード等でより高度な可視化を行うことが可能です。

UsingGrafana

公式サイトのドキュメントには他にも様々な情報が記載されているので、 Prometheus を利用する際は見ておいて損はないかと思います。

Docs

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1