14
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Grafana + Prometheus を稼働させてみる手順

Posted at

概要

Grafana + Prometheus を動かして、サーバのリソースなどがグラフ化されるのを体験してみたい人向け。

環境

  • CentOS 6.9 (まっさらな状態を想定)
  • Prometheus 2.0.0
  • node_exporter 0.15.1
  • Grafana 4.6.3-1

Prometheus 本体のインストール

# cd /tmp
# curl -LO https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
# tar xvzf prometheus-2.0.0.linux-amd64.tar.gz
# mv -i prometheus-2.0.0.linux-amd64 /etc/prometheus

Prometheus の起動スクリプト設定

# vim /etc/rc.d/init.d/prometheus
#!/bin/bash
#
# /etc/rc.d/init.d/prometheus
#
# Prometheus monitoring server
#
#  chkconfig: 2345 20 80 Read
#  description: Prometheus monitoring server
#  processname: prometheus

# Source function library.
. /etc/rc.d/init.d/functions

PROGNAME=prometheus
PROG=/etc/prometheus/$PROGNAME
USER=root
LOGFILE=/var/log/prometheus/prometheus.log
DATADIR=/etc/prometheus/data
LOCKFILE=/var/run/$PROGNAME.pid
CONFIG_FILE=/etc/prometheus/prometheus.yml

start() {
    echo -n "Starting $PROGNAME: "
    daemon --user $USER --pidfile="$LOCKFILE" "$PROG --config.file=$CONFIG_FILE --storage.tsdb.path $DATADIR &>>$LOGFILE &"
    echo $(pidofproc $PROGNAME) >$LOCKFILE
    echo
}

stop() {
    echo -n "Shutting down $PROGNAME: "
    killproc $PROGNAME
    rm -f $LOCKFILE
    echo
}


case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status $PROGNAME
    ;;
    restart)
    stop
    start
    ;;
    reload)
    echo "Sending SIGHUP to $PROGNAME"
    kill -SIGHUP $(pidofproc $PROGNAME)
    ;;
    *)
        echo "Usage: <servicename> {start|stop|status|reload|restart}"
        exit 1
    ;;
esac
# chmod +x /etc/init.d/prometheus
# mkdir /var/log/prometheus
# /etc/init.d/prometheus start
# chkconfig prometheus on

ログローテート設定(prometheus)

# vim /etc/logrotate.d/prometheus
/var/log/prometheus/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /etc/init.d/prometheus reload > /dev/null 2>/dev/null || true
    endscript
}

node_exporter のインストール

# curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.15.1/node_exporter-0.15.1.linux-amd64.tar.gz
# tar xvzf node_exporter-0.15.1.linux-amd64.tar.gz
# mv -i node_exporter-0.15.1.linux-amd64 /etc/node_exporter

node_exporter の起動スクリプトの設定

# vim /etc/rc.d/init.d/node_exporter
#!/bin/bash
#
# /etc/rc.d/init.d/node_exporter
#
#  chkconfig: 2345 20 80 Read
#  description: Prometheus node_exporter
#  processname: node_exporter

# Source function library.
. /etc/rc.d/init.d/functions

PROGNAME=node_exporter
PROG=/etc/node_exporter/$PROGNAME
USER=root
LOGFILE=/var/log/node_exporter/node_exporter.log
LOGLEVEL=info
LOCKFILE=/var/run/$PROGNAME.pid

start() {
    echo -n "Starting $PROGNAME: "
    daemon --user $USER --pidfile="$LOCKFILE" "$PROG --log.level="$LOGLEVEL" &>>$LOGFILE &"
    echo $(pidofproc $PROGNAME) >$LOCKFILE
    echo
}

stop() {
    echo -n "Shutting down $PROGNAME: "
    killproc $PROGNAME
    rm -f $LOCKFILE
    echo
}


case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status $PROGNAME
    ;;
    restart)
    stop
    start
    ;;
    *)
        echo "Usage: <servicename> {start|stop|status|restart}"
        exit 1
    ;;
esac
# chmod +x /etc/rc.d/init.d/node_exporter
# mkdir /var/log/node_exporter
# /etc/init.d/node_exporter start
# chkconfig node_exporter on

ログローテート設定(node_exporter)

# vim /etc/logrotate.d/node_exporter
/var/log/node_exporter/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /etc/init.d/node_exporter restart > /dev/null 2>/dev/null || true
    endscript
}

監視されるサーバを Prometheus の設定に追加

# vim /etc/prometheus/prometheus.yml

以下を最下部に記載

  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']
# /etc/prometheus/promtool check config /etc/prometheus/prometheus.yml
※SUCCESSが表示されてればたぶんOK
# /etc/init.d/prometheus reload

http://[サーバのIP]:9090/ → node_memory_Cached_bytes など入力して Excuteボタン → その下にある Graphタブ に切り替えてグラフが描画されていれば動作確認OK

Grafana インストール & Prometheus と連携

# yum install https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.6.3-1.x86_64.rpm
# /etc/init.d/grafana-server start
# chkconfig grafana-server on

http://[サーバのIP]:3000/ → admin/admin
左上のロゴマーク → DataSources → +Add data source

Name: Prometheus
Type: Prometheus

HTTP Settings
URL: http://localhost:9090/
Access: Server(Default)
Access: proxy

Auth
(何もチェックしない)

Skip TLS Verification(Insecure): (チェックを入れる)

Scrape interval: (何も入れない)    ← 重要

上記設定にて Add 。問題無ければ Data Source is working と出る。

Dashboard の追加

https://grafana.com/dashboards?dataSource=prometheus&panelType=graph&category=hostmetrics&collector=nodeExporter
らへんから好きなのを選ぶ。

自分が選んだのは以下。
https://grafana.com/dashboards/405
このページの右側にある Download JSON から JSON をローカルにDLして Grafana に戻る。

左上のロゴマーク → Dashboard にカーソルを併せて → Import
Upload .json file → ローカルにDLした .json を選択 → Name はそのままで Prometheus は Prometheus に → Import

グラフが表示されればOK。右上のところから表示期間が変更できるので Last 3 hours など弄って見やすいものに設定してください。

参考にさせて頂いたサイト

14
15
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
14
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?