我水也

われみずなり

Prometheus + Grafana on AzureでVM監視

2017年も残すところ1日となり、家族がインフルエンザのため外出できず、家事に追われています。

夜時間があったので気になっていた Prometheus を試してみました。

Grafana で可視化するといい感じらしいので、Azureに立ててるVMをモニタリングしてみます。

環境は、Azure上に構築したCentOS7.4です。
CentOS-based 7.4 - Rogue Wave Software (formerly OpenLogic)

PrometheusはOSSのため、githubでソースが取得できますが、今回はビルド済のバイナリを動かします。

github.com

ダウンロードページから最新版のURLを確認し、ダウンロード後 /usr/local/ 配下に移動します。(場所は好みで)

Download | Prometheus

# cd /usr/local/src/
# wget https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
# tar zxfv prometheus-2.0.0.linux-amd64.tar.gz
# mv prometheus-2.0.0.linux-amd64 /usr/local/
# cd /usr/local
# chown root:root prometheus-2.0.0.linux-amd64/
# ln -s /usr/local/prometheus-2.0.0.linux-amd64/ prometheus

次に node_exporter というものを同じようにインストールします。 この人が情報を取得し、prometheusさんとやり取りをするようです。

ダウンロード後、設置したらバックグラウンドで動かします。 ちゃんと動かすときにはサービス化する感じですかね。

# cd /usr/local/src/
# wget https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz
# tar zxfv node_exporter-0.15.2.linux-amd64.tar.gz
# mv node_exporter-0.15.2.linux-amd64 /usr/local/
# cd /usr/local
# chown -R root:root node_exporter-0.15.2.linux-amd64
# ln -s /usr/local/node_exporter-0.15.2.linux-amd64/ node_exporter
cd node_exporter
# ./node_exporter &

この node_exporter がポート9100で受け付けるみたいなので、prometheusさんに情報取得先を設定してあげます。

# vi /usr/local/prometheus/prometheus.yml

scrape_configs 配下の static_configs にある targets を追加します。 もともとあったものはコメントアウトしました。

# 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']
      - targets: ['localhost:9100']

これで起動してみます。

# ./prometheus --config.file=prometheus.yml &

http://<prometheus server>:9090 に接続してみます。

試しに接続してみているので、ネットワークセキュリティグループの9090を自宅IPのみ許可しました。 実際はnginxなどをフロントにおくか、AzureLB配下に入れると良いと思います。

わーい!こんな感じです。

f:id:matcu:20171231103725p:plain

グラフはこんな感じであまりいけてません。

f:id:matcu:20171231103851p:plain

そこで、Grafanaの登場です!
こいつがイケてるグラフを見せてくれます。

grafana.com

ダウンロードページ にインストール方法があるので、それに従います。
RPMがあるので、お手軽です。

# cd /usr/local/src/
# wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.6.3-1.x86_64.rpm 
# yum localinstall grafana-4.6.3-1.x86_64.rpm
# systemctl daemon-reload
# systemctl start grafana-server
# systemctl status grafana-server
# systemctl enable grafana-server.service

http://<grafana server>:3000 に接続してみます。
これもお試しで接続したので、ネットワークセキュリティグループの3000を自宅IPのみ許可しました。

おぉ、かっこいい。なんか期待できます。

f:id:matcu:20171231104403p:plain

初期ユーザー(admin/admin)でログインします。

左上メニューから「Data Sources」を選択し、「+ Add data source」をクリックします。

f:id:matcu:20171231104656p:plain

  • Name 適当に
  • Type Prometheus
  • URL Prometheusサーバー(今回は同一サーバーなので、localhost:9090)
  • Access proxy
    • directの場合は、実際にブラウザがアクセス出来るURLにする
    • proxy の場合は、Grafanaサーバーからアクセス出来るURLにする あとは必要であれば設定をし、「Add」!!

続いて左上メニューの「Dashboards - Import」を選択します。

Grafana Dashboards - discover and share dashboards for Grafana. | Grafana Labs にいろんなダッシュボードのテンプレートがあるので、好みのものを選びますが、今回はこれにします。

grafana.com

ダッシュボードIDかURLを入力し、「Load」します

f:id:matcu:20171231105623p:plain

なんということでしょう。モニタリングっぽい!

データソースもいろいろ選べますし、カスタマイズは無限大みたいです。

ちなみに、PrometheusにAzure用の設定があり試してみたのですが、うまく動かず。。
これがあればおそらくエージェント的に node_exporter を入れる必要はないと思っています。

Configuration | Prometheus

次はこれの設定を頑張ってみます。

それでは良いお年を!!