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

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

著者:清水 赳

 こんにちは、香川大学修士1年の清水です。2年ぶりの登板です。
 今回から2回にわたり、OSSのシステム監視ツール「Prometheus」と「Itamae」というプロビジョニングツールを使って、サーバー監視システムを構築する方法を紹介します。今回は、Prometheusの配備とサーバー稼働状況の簡単な可視化について解説します。

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

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

url_head = "https://github.com/prometheus/prometheus/releases/download"
url_ver  = "v2.13.1"
origin_dir = "Prometheus-2.13.1.linux-amd64"
install_dir = "/usr/local/bin"
config_dir = "/etc/prometheus"
## Prometheusをダウンロードする
execute "download prometheus" do
  cwd "/tmp"
  command "wget #{File.join(url_head, url_ver, origin_dir)}.tar.gz"
end
## Prometheusを展開する
execute "extract prometheus" do
  cwd "/tmp"
  command "tar xvfz #{origin_dir}.tar.gz"
end
## Prometheusを所定のディレクトリに配置
execute "install prometheus" do
  cwd "/tmp"
  command "mv #{File.join(origin_dir, "prometheus")} #{install_dir}"
end
## Systemd設定ファイルの転送
remote_file "/etc/systemd/system/prometheus.service" do
  owner "root"
  group "root"
  source "files/etc/systemd/system/prometheus.service"
end
## Prometheusの設定ファイルを配置する
remote_directory "/etc/prometheus" do
  owner "root"
  group "root"
  source "files/etc/prometheus"
end
## Prometheusサービスの開始
service "prometheus" do
  action :restart
end

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

[Unit]
Description=Prometheus

[Service]
ExecStart=/usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles/ \
    --web.console.libraries=/etc/prometheus/console_libraries/

[Install]
WantedBy=multi-user.target

図8 「./cookbooks/node_exporter/default.rb」ファイルに記述する内容

url_head = "https://github.com/prometheus/node_exporter/releases/download"
url_ver  = "v0.18.1"
origin_dir = "node_exporter-0.18.1.linux-amd64"
install_dir = "/usr/local/bin"
## node_exporterをダウンロードする
execute "download node_exporter" do
  cwd "/tmp"
  command "wget #{File.join(url_head, url_ver, origin_dir)}.tar.gz"
end
## node_exporterを展開する
execute "extract node_exporter" do
  cwd "/tmp"
  command "tar xvfz #{origin_dir}.tar.gz"
end
## node_exporterを所定のディレクトリに配置
execute "install node_exporter" do
  cwd "/tmp"
  command "mv #{File.join(origin_dir, "node_exporter")} #{install_dir}"
end
## Systemd設定ファイルの転送
remote_file "/etc/systemd/system/node_exporter.service" do
  owner "root"
  group "root"
  source "files/etc/systemd/system/node_exporter.service"
end
## node_exporterサービスの開始
service "node_exporter" do
  action :restart
end

図9 「./cookbooks/node_exporter/etc/systemd/system/node
_exporter.service」ファイルに記述する内容

[Unit]
Description=NodeExporter

[Service]
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

図10 「./cookbooks/prometheus/files/etc/prometheus/prome
theus.yml」ファイルの編集内容

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

# ここから下を追加
  - job_name: 'node'
    static_configs:
    - targets: ['192.168.100.12:9100']