尝试运行Grafana + Prometheus的步骤
总结
适合想要体验将Grafana与Prometheus结合使用来绘制服务器资源图形化的人。
环境
-
- CentOS 6.9 (まっさらな状態を想定)
-
- Prometheus 2.0.0
-
- node_exporter 0.15.1
- Grafana 4.6.3-1
普罗米修斯的本体安装
# 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
如果在 [服务器的IP地址]: 9090/ 上输入 node_memory_Cached_bytes 等内容,然后点击“执行”按钮,如果切换到下面的“图表”选项卡并绘制了图形,则表示运行正常。
安装 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
左上方的徽标 → 数据源 → +添加数据源
Name: Prometheus
Type: Prometheus
HTTP Settings
URL: http://localhost:9090/
Access: Server(Default)
Access: proxy
Auth
(何もチェックしない)
Skip TLS Verification(Insecure): (チェックを入れる)
Scrape interval: (何も入れない) ← 重要
如果以上配置没有问题,接下来会显示“Data Source is working”。
添加仪表盘
从https://grafana.com/dashboards?dataSource=prometheus&panelType=graph&category=hostmetrics&collector=nodeExporter这里选择你喜欢的一个。
我选择的是以下内容。
从此链接中的 https://grafana.com/dashboards/405
下载JSON并将其保存到本地,然后返回Grafana页面。
将光标悬停在左上角的标志 → 导航到仪表盘 → 上传 .json 文件 → 选择已下载到本地的 .json 文件 → 名称保持不变,将 Prometheus 保留为 Prometheus → 导入
只要图表显示出来就可以了。你可以从右上角更改显示时间段,例如“过去3小时”,请将其调整为方便查看的设置。
参考了的网站
-
- https://prometheus.io/
-
- https://grafana.com/
-
- http://tyru.hatenablog.com/entry/2017/07/27/235416
- https://gist.github.com/JoergM/489b21d903134db5d3c2