如何在5分钟内使用Prometheus和Grafana监视Redis服务器
本教程将重点监控Linux服务器上的Redis应用程序。 Redis是一种开源的内存中数据结构存储,用作数据库,缓存和消息代理。 Redis提供了具有可选持久性的分布式内存中键值数据库。
Redis支持不同种类的抽象数据结构,例如字符串,集合,地图,列表,排序集合,空间索引和位图。
在5分钟内用Prometheus监视MySQL/MariaDB如何在5分钟内用Prometheus和Grafana监视Apache Web Server
Redis exporter导出了什么?
INFO命令中的大多数项目都已导出,有关详细信息,请参见http://redis.io/commands/info。此外,对于每个数据库,都有数据库中密钥总数,过期密钥和密钥平均TTL的度量。
如果键的值为数字格式,则还可以使用-check-keys标志导出键的值。导出器还将导出密钥的大小(或者取决于数据类型,长度)。可用于导出(排序的)集合,哈希,列表等中的元素数量已安装的Prometheus Server在CentOS 7和Ubuntu 18.04上安装Prometheus Server已安装的Grafana数据可视化和监控在CentOS 7和Ubuntu 18.04上安装Prometheus Server
设置准备工作
该Prometheus Redis导出器指标支持Redis 2.x,3.x和4.x
下载并安装Redis Prometheus导出器
解压缩下载的存档文件
export VER="0.21.2"
wget https://github.com/oliver006/redis_exporter/releases/download/v${VER}/redis_exporter-v${VER}.linux-amd64.tar.gz
redis_exporter应该可以从我们当前的SHELL中执行
tar xvf redis_exporter-v${VER}.linux-amd64.tar.gz
sudo mv redis_exporter /usr/local/bin/
rm -f redis_exporter-v${VER}.linux-amd64.tar.gz
要获得所有支持的选项的列表,请传递--help选项
$redis_exporter -version INFO[0000] Redis Metrics Exporter v0.21.2 build date: 2016-09-20-18:15:12 sha1: 8bb0b841e9a70b0348f69483e58fea01d521c47a Go: go1.10.4
用户prometheus将用于运行该服务。如果不存在,则添加Promethes系统用户
# redis_exporter --help
Usage of redis_exporter:
-check-keys string
Comma separated list of key-patterns to export value and length/size, searched for with SCAN
-check-single-keys string
Comma separated list of single keys to export value and length/size
-debug
Output verbose debug information
-log-format string
Log format, valid options are txt and json (default "txt")
-namespace string
Namespace for metrics (default "redis")
-redis-only-metrics
Whether to export go runtime metrics also
-redis.addr string
Address of one or more redis nodes, separated by separator
-redis.alias string
Redis instance alias for one or more redis nodes, separated by separator
-redis.file string
Path to file containing one or more redis nodes, separated by newline. NOTE: mutually exclusive with redis.addr
-redis.password string
Password for one or more redis nodes, separated by separator
-script string
Path to Lua Redis script for collecting extra metrics
-separator string
separator used to split redis.addr, redis.password and redis.alias into several elements. (default ",")
-use-cf-bindings
Use Cloud Foundry service bindings
-version
Show version information and exit
-web.listen-address string
Address to listen on for web interface and telemetry. (default ":9121")
-web.telemetry-path string
Path under which to expose metrics. (default "/metrics")
创建Prometheus redis exporter systemd服务/初始化脚本
然后继续创建一个systemd服务单元文件。
sudo groupadd --system prometheus sudo useradd -s /sbin/nologin --system -g prometheus prometheus
添加以下内容
sudo vim /etc/systemd/system/redis_exporter.service
安装守护进程(CentOS/Ubuntu)
[Unit] Description=Prometheus Documentation=https://github.com/oliver006/redis_exporter Wants=network-online.target After=network-online.target [Service] Type=simple User=prometheus Group=prometheus ExecReload=/bin/kill -HUP $MAINPID ExecStart=/usr/local/bin/redis_exporter \ --log-format=txt \ --namespace=redis \ --web.listen-address=:9121 \ --web.telemetry-path=/metrics SyslogIdentifier=redis_exporter Restart=always [Install] WantedBy=multi-user.target
对于初始化系统
创建初始化脚本
sudo yum -y install daemonize sudo apt-get install daemonize
加
sudo vim /etc/init.d/redis_exporter
创建参数配置文件
#!/bin/bash
# Author: Josphat Mutai, theitroad@localhost , https://github.com/jmutai
# redis_exporter This shell script takes care of starting and stopping Prometheus redis exporter
## chkconfig: 2345 80 80
# description: Prometheus redis exporter start script
# processname: redis_exporter
# pidfile: /var/run/redis_exporter.pid
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
PROGNAME=redis_exporter
PROG=/usr/local/bin/${PROGNAME}
RUNAS=prometheus
LOCKFILE=/var/lock/subsys/${PROGNAME}
PIDFILE=/var/run/${PROGNAME}.pid
LOGFILE=/var/log/${PROGNAME}.log
DAEMON_SYSCONFIG=/etc/sysconfig/${PROGNAME}
# GO CPU core Limit
#GOMAXPROCS=$(grep -c ^processor /proc/cpuinfo)
GOMAXPROCS=1
# Source config
. ${DAEMON_SYSCONFIG}
start() {
if [[ -f $PIDFILE ]] > /dev/null; then
echo "redis_exporter is already running"
exit 0
fi
echo -n "Starting redis_exporter service…"
daemonize -u ${USER} -p ${PIDFILE} -l ${LOCKFILE} -a -e ${LOGFILE} -o ${LOGFILE} ${PROG} ${ARGS}
RETVAL=$?
echo ""
return $RETVAL
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo "Service not running"
return 1
fi
echo 'Stopping service…'
#kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
killproc -p ${PIDFILE} -d 10 ${PROG}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
return $RETVAL
}
status() {
if [ -f "$PIDFILE" ] || kill -0 $(cat "$PIDFILE"); then
echo "redis exporter service running..."
echo "Service PID: `cat $PIDFILE`"
else
echo "Service not running"
fi
RETVAL=$?
return $RETVAL
}
# Call function
case "" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: sudo vim /etc/sysconfig/redis_exporter
{start|stop|restart}"
exit 2
esac
定义使用的命令参数
ARGS="--log-format=txt \ --namespace=redis \ --web.listen-address=:9121 \ --web.telemetry-path=/metrics"
测试脚本
# /etc/init.d/redis_exporter
Usage: /etc/init.d/redis_exporter {start|stop|restart}
对于一台Systemd服务器,使用systemctl命令
sudo systemctl enable redis_exporter sudo systemctl start redis_exporter
启动Redis Prometheus导出器并启用服务以在启动时启动
ForSysV Init系统,使用
sudo /etc/init.d/redis_exporter start sudo chkconfig redis_exporter on
我们可以使用以下方法验证服务是否正在运行
$sudo /etc/init.d/redis_exporter status
redis exporter service running...
Service PID: 27106
$sudo chkconfig --list | grep redis_exporter
redis_exporter 0:off 1:off 2:on 3:on 4:on 5:on 6:off
$sudo ss -tunelp | grep 9121
tcp LISTEN 0 128 :::9121 :::* users:(("redis_exporter",1970,6)) ino:1823474168 sk:ffff880341cd7800
最后一步是将作业添加到Prometheus服务器以刮取指标。编辑/etc/prometheus/prometheus.yml
# Redis Servers
- job_name: 10.10.10.3-redis
static_configs:
- targets: ['10.10.10.3:9121']
labels:
alias: 10.10.10.3
- job_name: 10.10.10.4-redis
static_configs:
- targets: ['10.10.10.4:9121']
labels:
alias: 10.10.10.4
将导出程序作业添加到Prometheus
重新启动Prometheus服务以开始抓取数据指标
sudo systemctl restart prometheus
测试从Prometheus服务器对端口" 9121"的访问,它应该能够连接。
$telnet 10.1.10.15 9121 Trying 10.1.10.15... Connected to 10.1.10.15. Escape character is '^]'. ^]
如果无法连接,请检查服务端口和防火墙。
wget https://raw.githubusercontent.com/oliver006/redis_exporter/master/contrib/grafana_prometheus_redis_dashboard_alias.json
将Prometheus数据源添加到Grafana并为Redis导入或者创建grafana仪表板。
第5步:将仪表板添加到Grafana
Grafana仪表板可在grafana.net和/或者github.com上找到。我的作业配置使用别名,不适用于带有主机和别名选择器的Grafana仪表板,可在github.com上获得。
下载仪表板的json文件
在Grafana UI上,转到"创建">"导入仪表板">"上载.json文件"。选择下载的json文件,然后单击导入。
##代码##等待数据开始出现在Grafana仪表板上,下面是一个示例视图

