シェルスクリプトマガジン

香川大学SLPからお届け!(Vol.64掲載)

著者:清水赳

前回に引き続き、OSSのシステム監視ツール「Prometheus」を「Itamae」というプロビジョニングツールを使って、サーバー監視システムを構築する方法を紹介します。今回は、Prometheusでノード情報を取得・計算する方法や、外形監視、データの可視化方法について解説します。

シェルスクリプトマガジン Vol.64は以下のリンク先でご購入できます。

図5 「./cookbooks/blackbox_exporter/default.yml」ファイルに記述する内容

url_head = "https://github.com/prometheus/blackbox_exporter/releases/download"
url_ver  = "v0.16.0"
origin_dir = "blackbox_exporter-0.16.0.linux-amd64"
install_dir = "/usr/local/bin"
#==== Blackbox Exporterのインストール
execute "download blackbox_exporter" do
  cwd "/tmp"
  command "wget #{File.join(url_head, url_ver, origin_dir)}.tar.gz"
end
execute "extract blackbox_exporter" do
  cwd "/tmp"
  command "tar xvfz #{origin_dir}.tar.gz"
end
execute "install blackbox_exporter" do
  cwd "/tmp"
  command "mv #{File.join(origin_dir, "blackbox_exporter")} #{install_dir}"
end
#==== Blackbox Exporterをサービスとして登録する
remote_file "/etc/systemd/system/blackbox_exporter.service" do
  owner "root"
  group "root"
  source "files/etc/systemd/system/blackbox_exporter.service"
end
remote_directory "/etc/blackbox_exporter" do
  owner "root"
  group "root"
  source "files/etc/blackbox_exporter"
end
service "blackbox_exporter" do
  action :restart
end

図6 「./cookbooks/blackbox_exporter/files/etc/systemd/system/blackbox_exporter.service」ファイルに記述する内容

[Unit]
Description=BlackboxExporter

[Service]
ExecStart=/usr/local/bin/blackbox_exporter --config.file /etc/blackbox_exporter/blackbox.yml

[Install]
WantedBy=multi-user.target

図7 「./roles/client.rb」ファイルの編集内容

include_recipe "../cookbooks/node_exporter" # 前回追加
include_recipe "../cookbooks/blackbox_exporter" # 今回追加

図8 「./cookbooks/blackbox_exporter/files/etc/blackbox_exporter/blackbox.yml」ファイルに記述する内容

modules:
  http_2xx:
    prober: http
    http:
  http_post_2xx:
    prober: http
    http:
      method: POST
  icmp:
    prober: icmp

図9 「./cookbooks/prometheus/files/etc/prometheus/prometheus.yml」ファイルに追加する内容

  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
        - localhost
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115