This guide will focus on monitoring of Redis application on a Linux server. Redis is an open source in-memory data structure store, used as a database, cache and message broker. Redis provides a distributed, in-memory key-value database with optional durability.
Redis supports different kinds of abstract data structures, such as strings, sets, maps, lists, sorted sets, spatial indexes, and bitmaps.
So far we have covered the following monitoring with Prometheus:
- Monitoring Ceph Cluster with Prometheus and Grafana
- Monitor Linux Server Performance with Prometheus and Grafana in 5 minutes
- Monitor BIND DNS server with Prometheus and Grafana
- Monitoring MySQL / MariaDB with Prometheus in five minutes
- Monitor Apache Web Server with Prometheus and Grafana in 5 minutes
What’s exported by Redis exporter?
Most items from the INFO command are exported, see http://redis.io/commands/info for details. In addition, for every database there are metrics for total keys, expiring keys and the average TTL for keys in the database.
You can also export values of keys if they’re in numeric format by using the -check-keys flag. The exporter will also export the size (or, depending on the data type, the length) of the key. This can be used to export the number of elements in (sorted) sets, hashes, lists, etc.
Setup Pre-requisite
- Installed Prometheus Server – Install Prometheus Server on CentOS 7 and Ubuntu
- Installed Grafana Data Visualization & Monitoring – Install Prometheus Server on CentOS 7 and Ubuntu
Step 1: Install Prometheus Redis exporter
This Prometheus exporter for Redis metrics supports Redis:
curl -s https://api.github.com/repos/oliver006/redis_exporter/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
Extract the downloaded archive file
tar xvf redis_exporter-*.linux-amd64.tar.gz
sudo mv redis_exporter-*.linux-amd64/redis_exporter /usr/local/bin/
rm -rf redis_exporter-*.linux-amd64*
redis_exporter
should be executable from your current SHELL
$ redis_exporter --version
INFO[0000] Redis Metrics Exporter v1.44.0 build date: 2022-08-25-05:21:38 sha1: 19f7b036bb46869858eec74d8d3fc2186d641399 Go: go1.19 GOOS: linux GOARCH: amd64
To get a list of all options supported, pass --help
option
$ 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")
...
Step 2: Create redis exporter systemd service / Init script
The user prometheus
will be used to run the service. Add Promethes
system user if it doesn’t exist
sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus
Then proceed to create a systemd service unit file.
sudo vim /etc/systemd/system/redis_exporter.service
Add below content
[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
For Init system
Install daemonize ( CentOS / Ubuntu )
sudo yum -y install daemonize
sudo apt-get install daemonize
Create init script
sudo vim /etc/init.d/redis_exporter
Add
#!/bin/bash
# Author: Josphat Mutai, [email protected] , 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 "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 2
esac
Create Arguments configuration file
sudo vim /etc/sysconfig/redis_exporter
Define used command Arguments
ARGS="--log-format=txt \
--namespace=redis \
--web.listen-address=:9121 \
--web.telemetry-path=/metrics"
Test the script
$ sudo /etc/init.d/redis_exporter
Usage: /etc/init.d/redis_exporter {start|stop|restart}
Step 3: Start Redis Prometheus exporter
For a Systemd server, use systemctl
command
sudo systemctl daemon-reload
sudo systemctl enable redis_exporter
sudo systemctl start redis_exporter
Confirm service status:
$ systemctl status redis_exporter
● redis_exporter.service - Prometheus
Loaded: loaded (/etc/systemd/system/redis_exporter.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-10-14 13:07:23 UTC; 6s ago
Docs: https://github.com/oliver006/redis_exporter
Main PID: 1861 (redis_exporter)
CGroup: /system.slice/redis_exporter.service
└─1861 /usr/local/bin/redis_exporter --log-format=txt --namespace=redis --web.listen-address=:9121 --web.telemetry-path=/metrics
Oct 14 13:07:23 cent7.mylab.io systemd[1]: Started Prometheus.
Oct 14 13:07:23 cent7.mylab.io redis_exporter[1861]: time="2022-10-14T13:07:23Z" level=info msg="Redis Metrics Exporter v1.44.0 build date: 2022-08-25-05:21:38 sha1: 19f7b036bb4...ARCH: amd64"
Oct 14 13:07:23 cent7.mylab.io redis_exporter[1861]: time="2022-10-14T13:07:23Z" level=info msg="Providing metrics at :9121/metrics"
For SysV Init system, use
sudo /etc/init.d/redis_exporter start
sudo chkconfig redis_exporter on
You can verify that the service is running using
$ 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
Step 4: Add exporter job to Prometheus
The last step is to add a job to the Prometheus server for scraping metrics. Edit /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
Restart prometheus
service for scraping of data metrics to begin
sudo systemctl restart prometheus
Test access to port 9121
from Prometheus server, it should be able to connect.
$ telnet 10.1.10.15 9121
Trying 10.1.10.15...
Connected to 10.1.10.15.
Escape character is '^]'.
^]
If it can’t connect, check your Service port and firewall.
Step 5: Add Dashboard to Grafana
Add Prometheus data source to Grafana and import or create a grafana dashboard for Redis.
Grafana dashboard is available on grafana.net and/or github.com. My Job configuration uses an alias, I’ll use Grafana dashboard with host & alias selector is available on github.com.
Download the dashboard json
file
wget https://raw.githubusercontent.com/oliver006/redis_exporter/master/contrib/grafana_prometheus_redis_dashboard.json
On Grafana UI, go to Create > Import Dashboard > Upload .json File. Select downloaded json file and click “Import“.
Wait for data to start appearing on your Grafana Dashboard, below is a sample view
Enjoy using Grafana to monitor your Redis server(s).