Humanity

Edit the world by your favorite way

CentOS 7 に Prometheus 入れる

VM 作成

※Prometheus に興味のある人はここは飛ばして構いません。自分用の記録みたいなものです。

まず検証用に KVMVM を作る。 最初 virt-manager を RLogin + Xming で立ち上げようと思ったらうまく Xming に接続してくれなくて開けなかった。 ここでしばらく迷ってたが、後々 virsh で VM を clone すれば一から作成する必要がないことに気付く。 というかいつもそうやって VM 作ってた。 というわけで以下のコマンドで clone できるらしい。

$ sudo virt-clone --original <コピー元仮想マシン名> --name <コピー先仮想マシン名> --file /var/lib/libvirt/images/<コピー先イメージファイル名>

参考記事:KVMのクローンを利用して新規仮想マシンを作成する : アジャイル株式会社

clone は成功。 起動する前に DHCP で割り当てる IP の設定を DNS サーバ(つまり RTX810)に入れる。

[rtx810]# ip host ホスト名 xxx.xxx.xxx.xxx
[rtx810]# dhcp scope bind xx xxx.xxx.xxx.xxx 52:54:00:xx:xx:xx
[rtx810]# save

この MAC アドレスは sudo virsh domiflist <仮想マシン名> で見れる(参考記事:qemu - libvirt: fetch ipv4 address from guest - Stack Overflow)。

で、RTX810 への設定が終わったら sudo virsh start <仮想マシン名> する。 別の端末で ping しておいて 無事 IP が割り当てられたことを確認、ログイン。

Prometheus のインストール

いよいよ Prometheus のインストールに入る。 ぶっちゃけこれ以降は PrometheusをCentOS7.3&Docker上にインストールしてみた - Qiita の「3.Prometheusインストール」以降をなぞっただけです(ただあちらは Docker 用の手順ですが自分は KVM です)。

まずはここから最新のリリースを取得する(現在の最新は v1.7.1)。 バイナリで配布されているので展開するだけ。手軽すぎる…

$ curl -LO https://github.com/prometheus/prometheus/releases/download/v1.7.1/prometheus-1.7.1.linux-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   624    0   624    0     0    765      0 --:--:-- --:--:-- --:--:--   764
100 16.3M  100 16.3M    0     0  2508k      0  0:00:06  0:00:06 --:--:-- 3385k
$ tar xzf prometheus-1.7.1.linux-amd64.tar.gz
$ cd prometheus-1.7.1.linux-amd64/
$ ls
LICENSE  NOTICE  console_libraries  consoles  prometheus  prometheus.yml  promtool
$ file prometheus
prometheus: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not str                                                                                             ipped
$ ./prometheus --help
usage: prometheus [<args>]


   -version false
      Print version information.

   -config.file "prometheus.yml"
      Prometheus configuration file name.

 == ALERTMANAGER ==

最後の help は省略(219行あった)。

起動も簡単。引数なしで実行するだけ。

$ ./prometheus
INFO[0000] Starting prometheus (version=1.7.1, branch=master, revision=3afb3fffa3a29c3de865e1172fb740442e9d0133)  source="main.go:88"
INFO[0000] Build context (go=go1.8.3, user=root@0aa1b7fc430d, date=20170612-11:44:05)  source="main.go:89"
INFO[0000] Host details (Linux 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 ホスト名 (none))  source="main.go:90"
INFO[0000] Loading configuration file prometheus.yml     source="main.go:252"
INFO[0000] Loading series map and head chunks...         source="storage.go:428"
INFO[0000] 0 series loaded.                              source="storage.go:439"
INFO[0000] Starting target manager...                    source="targetmanager.go:63"
INFO[0000] Listening on :9090                            source="web.go:259"

(ここで Ctrl-C を押して終了)

^CWARN[0032] Received SIGTERM, exiting gracefully...       source="main.go:234"
INFO[0032] See you next time!                            source="main.go:241"
INFO[0032] Stopping target manager...                    source="targetmanager.go:77"
INFO[0032] Stopping rule manager...                      source="manager.go:388"
INFO[0032] Rule manager stopped.                         source="manager.go:394"
INFO[0032] Stopping notification handler...              source="notifier.go:418"
INFO[0032] Stopping local storage...                     source="storage.go:457"
INFO[0032] Stopping maintenance loop...                  source="storage.go:459"
INFO[0032] Maintenance loop stopped.                     source="storage.go:1458"
INFO[0032] Stopping series quarantining...               source="storage.go:463"
INFO[0032] Series quarantining stopped.                  source="storage.go:1907"
INFO[0032] Stopping chunk eviction...                    source="storage.go:467"
INFO[0032] Chunk eviction stopped.                       source="storage.go:1153"
INFO[0032] Checkpointing in-memory metrics and chunks...  source="persistence.go:633"
INFO[0032] Done checkpointing in-memory metrics and chunks in 20.712234ms.  source="persistence.go:665"
INFO[0032] Checkpointing fingerprint mappings...         source="persistence.go:1526"
INFO[0032] Done checkpointing fingerprint mappings in 3.875662ms.  source="persistence.go:1549"
INFO[0032] Local storage stopped.                        source="storage.go:484"

起動の仕方を確認したところで所定の位置に配置していきます。 今回は /opt/prometheus に配置します。

$ sudo mv prometheus-1.7.1.linux-amd64/ /opt/prometheus  # さっき解凍したディレクトリを移動
$ sudo chmod 755 /opt/prometheus
$ sudo chown -R root:root /opt/prometheus

$ sudo mkdir /etc/prometheus  # prometheus 用の設定ディレクトリを作る
$ sudo ln -s /opt/prometheus/console_libraries /etc/prometheus/console_libraries  # いろいろ symlink をはっていく
$ sudo ln -s /opt/prometheus/consoles /etc/prometheus/consoles
$ sudo ln -s /opt/prometheus/prometheus.yml /etc/prometheus/prometheus.yml

$ sudo vim /etc/default/prometheus
# /var/lib/prometheus (データ用ディレクトリはなければ勝手に作られます)
OPTIONS="-config.file=/etc/prometheus/prometheus.yml -storage.local.path=/var/lib/prometheus -web.console.libraries=/etc/prometheus/console_libraries -web.console.templates=/etc/prometheus/consoles"

$ sudo vim /etc/systemd/system/prometheus.service  # 参考記事の設定そのままです(感謝)
[Unit]
Description=Prometheus Service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/default/prometheus
ExecStart=/opt/prometheus/prometheus $OPTIONS
PrivateTmp=true
# (追記:2017/08/12 12:36) それなりにメモリ食うので restart させておくといいかもしれません
#Restart=always

[Install]
WantedBy=multi-user.target

$ sudo systemctl daemon-reload  # 設定ファイルを追加したので必要かわからんけど一応
$ sudo systemctl start prometheus  # 起動
$ sudo systemctl status -l prometheus
● prometheus.service - Prometheus Service
   Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2017-07-27 00:29:01 JST; 15min ago
 Main PID: 1323 (prometheus)
   CGroup: /system.slice/prometheus.service
           └─1323 /opt/prometheus-1.7.1/prometheus -config.file=/etc/prometheus/prometheus.yml -storage.local.path=/var/lib/prometheus -web.console.libraries=/etc/prometheus/console_libraries -web.console.templates=/etc/prometheus/consoles

 7月 27 00:29:01 ホスト名 prometheus[1323]: time="2017-07-27T00:29:01+09:00" level=info msg="Loading series map and head chunks..." source="storage.go:428"
 7月 27 00:29:01 ホスト名 prometheus[1323]: time="2017-07-27T00:29:01+09:00" level=info msg="546 series loaded." source="storage.go:439"
 7月 27 00:29:01 ホスト名 prometheus[1323]: time="2017-07-27T00:29:01+09:00" level=info msg="Starting target manager..." source="targetmanager.go:63"
 7月 27 00:29:01 ホスト名 prometheus[1323]: time="2017-07-27T00:29:01+09:00" level=info msg="Listening on :9090" source="web.go:259"
 7月 27 00:34:01 ホスト名 prometheus[1323]: time="2017-07-27T00:34:01+09:00" level=info msg="Checkpointing in-memory metrics and chunks..." source="persistence.go:633"
 7月 27 00:34:01 ホスト名 prometheus[1323]: time="2017-07-27T00:34:01+09:00" level=info msg="Done checkpointing in-memory metrics and chunks in 21.002274ms." source="persistence.go:665"
 7月 27 00:39:01 ホスト名 prometheus[1323]: time="2017-07-27T00:39:01+09:00" level=info msg="Checkpointing in-memory metrics and chunks..." source="persistence.go:633"
 7月 27 00:39:01 ホスト名 prometheus[1323]: time="2017-07-27T00:39:01+09:00" level=info msg="Done checkpointing in-memory metrics and chunks in 20.588386ms." source="persistence.go:665"
 7月 27 00:44:01 ホスト名 prometheus[1323]: time="2017-07-27T00:44:01+09:00" level=info msg="Checkpointing in-memory metrics and chunks..." source="persistence.go:633"
 7月 27 00:44:01 ホスト名 prometheus[1323]: time="2017-07-27T00:44:01+09:00" level=info msg="Done checkpointing in-memory metrics and chunks in 20.26329ms." source="persistence.go:665"

Web UI がついてるので「Listening on :9090」の通り 9090 ポートにアクセスすれば管理画面が見れる。

f:id:tyru:20170727014628p:plain

node_exporter(エージェント)を監視対象のノードにインストールする

参考記事:【入門】PrometheusでサーバやDockerコンテナのリソース監視 | Pocketstudio.jp log3

node_exporter も curl で持ってきて解凍するだけで動きます。 現在の最新リリースは v0.14.0 でした。他のリリースはこちら

今回はテストなので Prometheus server と同じノード上にインストールします。

$ curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz
$ tar xzf node_exporter-0.14.0.linux-amd64.tar.gz
$ cd node_exporter-0.14.0.linux-amd64/
$ ls
LICENSE  NOTICE  node_exporter
$ ./node_exporter
INFO[0000] Starting node_exporter (version=0.14.0, branch=master, revision=840ba5dcc71a084a3bc63cb6063003c1f94435a6)  source="node_exporter.go:140"
INFO[0000] Build context (go=go1.7.5, user=root@bb6d0678e7f3, date=20170321-12:12:54)  source="node_exporter.go:141"
INFO[0000] No directory specified, see --collector.textfile.directory  source="textfile.go:57"
INFO[0000] Enabled collectors:                           source="node_exporter.go:160"
INFO[0000]  - conntrack                                  source="node_exporter.go:162"
INFO[0000]  - stat                                       source="node_exporter.go:162"
INFO[0000]  - uname                                      source="node_exporter.go:162"
INFO[0000]  - vmstat                                     source="node_exporter.go:162"
INFO[0000]  - wifi                                       source="node_exporter.go:162"
INFO[0000]  - entropy                                    source="node_exporter.go:162"
INFO[0000]  - infiniband                                 source="node_exporter.go:162"
INFO[0000]  - netdev                                     source="node_exporter.go:162"
INFO[0000]  - netstat                                    source="node_exporter.go:162"
INFO[0000]  - sockstat                                   source="node_exporter.go:162"
INFO[0000]  - diskstats                                  source="node_exporter.go:162"
INFO[0000]  - meminfo                                    source="node_exporter.go:162"
INFO[0000]  - textfile                                   source="node_exporter.go:162"
INFO[0000]  - zfs                                        source="node_exporter.go:162"
INFO[0000]  - mdadm                                      source="node_exporter.go:162"
INFO[0000]  - time                                       source="node_exporter.go:162"
INFO[0000]  - edac                                       source="node_exporter.go:162"
INFO[0000]  - filefd                                     source="node_exporter.go:162"
INFO[0000]  - filesystem                                 source="node_exporter.go:162"
INFO[0000]  - hwmon                                      source="node_exporter.go:162"
INFO[0000]  - loadavg                                    source="node_exporter.go:162"
INFO[0000] Listening on :9100                            source="node_exporter.go:186"

というわけでこれも systemctl で起動できるようにする。 先ほどの prometheus の設定ファイルをほぼコピペする。

$ sudo vim /etc/systemd/system/node_exporter.service
[Unit]
Description=Node exporter for prometheus
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/default/node_exporter
ExecStart=/opt/node_exporter/node_exporter $OPTIONS
PrivateTmp=true

[Install]
WantedBy=multi-user.target

$ sudo vim /etc/default/node_exporter  # ほぼ空の設定ファイルを作っておく
OPTIONS=

$ sudo systemctl daemon-reload
$ sudo systemctl start node_exporter
$ sudo systemctl status -l node_exporter
● node_exporter.service - Node exporter for prometheus
   Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2017-07-27 01:10:16 JST; 3min 5s ago
 Main PID: 1568 (node_exporter)
   CGroup: /system.slice/node_exporter.service
           └─1568 /opt/node_exporter/node_exporter

 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - zfs" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - diskstats" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - filefd" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - loadavg" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - meminfo" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - netdev" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - textfile" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - edac" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg=" - uname" source="node_exporter.go:162"
 7月 27 01:10:16 ホスト名 node_exporter[1568]: time="2017-07-27T01:10:16+09:00" level=info msg="Listening on :9100" source="node_exporter.go:186"

systemctl で起動できたので、今度は先ほど微塵も変更しなかった 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).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

# 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']

それをこんな風に変更します。

--- /etc/prometheus/prometheus.yml.org  2017-07-27 01:21:56.237724564 +0900
+++ /etc/prometheus/prometheus.yml      2017-07-27 01:21:58.481728041 +0900
@@ -24,4 +24,4 @@
     # scheme defaults to 'http'.

     static_configs:
-      - targets: ['localhost:9090']
+      - targets: ['localhost:9090', 'localhost:9100']

こう書くと Prometheus server が http://localhost:9100/metrics に HTTP GET して情報を取得するようになります。 ちなみに見ての通り既に localhost:9090 というものがあるので、 Prometheus server も http://localhost:9100/metrics という情報を公開してるんですね。 自分自身の情報を公開してるので今回 node_exporter のホストを追加する意味あんの?と思いましたが、 「node_~」で始まる項目はそもそも Prometheus server は公開してなかったり、結構取得できる値が違ってました。 被った項目はまぁ当然こんな風にノードごとに分けて見れます。

f:id:tyru:20170727014417p:plain

ちなみに設定ファイルが正しいかどうかをチェックするには promtool コマンドを使います。

$ sudo /opt/prometheus/promtool check-config /etc/prometheus/prometheus.yml
Checking /etc/prometheus/prometheus.yml
  SUCCESS: 0 rule files found

おわり

今回はインストールがメインだったのでまだ全然中身理解してないですが、とりあえず一旦これで。