How to Auto‑Discover and Monitor Redis Ports with Zabbix
This guide explains how to use Zabbix's auto‑discovery feature to automatically find Redis instances on a server, create shell or Python scripts for port detection, configure Zabbix agent keys, set up server‑side templates, discovery rules, item prototypes, graphs, and triggers, and finally apply the template to monitored hosts.
Zabbix Auto-Discovery of Redis Ports and Monitoring
When a server runs many Redis instances, manually adding each to Zabbix is time‑consuming; Zabbix's auto‑discovery can automatically find Redis ports and collect metrics.
Monitoring Items
Redis metrics are obtained via the
infocommand. Example items include:
<code>uptime_in_days ## days Redis has been up
connected_clients ## number of client connections
blocked_clients ## clients waiting for blocking commands
used_memory_peak_human ## peak memory usage
used_memory ## current memory usage
expired_keys ## number of expired keys
evicted_keys ## number of evicted keys
keyspace_misses ## cache misses
keyspace_hits ## cache hits
connected_slaves ## number of slave servers
rejected_connections ## connections rejected due to limit</code>Additional items can be retrieved with the
infocommand as needed.
Develop Auto‑Discovery Scripts
Both shell and Python scripts can be used; two examples are provided.
Shell Script
<code>#!/bin/bash
# Auto‑discover Redis ports
port=(`sudo netstat -tpln | awk -F "[ :]+" '/redis/ && /0.0.0.0/ {print $5}'`)
printf '{
"data":[
'
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]]; then
socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1`
printf ' {
' "{#REDISPORT}":"${port[${key}]}"},
else
socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F '=' '{print $10}'|cut -d ' ' -f 1`
printf ' {
' "{#REDISPORT}":"${port[${key}]}"}
fi
done
printf ' ]
}
'</code>Python Script
<code>#!/usr/bin/env python
import os, json
t=os.popen("""sudo netstat -tlpn |grep redis-server|grep 0.0.0.0|awk '{print $4}'|awk -F: '{print $2}' """)
ports = []
for port in t.readlines():
r = os.path.basename(port.strip())
ports += [{'{#REDISPORT}': r}]
print(json.dumps({'data': ports}, sort_keys=True, indent=4, separators=(',', ':')))
</code>These scripts discover Redis ports; they can be adapted to discover other services by passing the service name as a parameter.
Configure the Agent on the Monitored Host
(1) Copy one of the scripts to the host, e.g.,
/etc/zabbix/scripts/discovery_redis.py, and make it executable.
<code># mkdir /etc/zabbix/scripts
# chmod +x discovery_redis.py</code>(2) Create a Zabbix user‑parameter file
userparameter_discovery_redis.conf:
<code>UserParameter=redis.discovery,/etc/zabbix/scripts/discovery_redis.py
UserParameter=redis.stats[*],/usr/local/redis-3.2.9/src/redis-cli -h 127.0.0.1 -p $1 info | grep -w $2 | cut -d ':' -f2</code>(3) Edit
zabbix_agentd.confto include the custom directory and allow unsafe parameters:
<code>Include=/etc/zabbix/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1</code>(4) Grant the Zabbix user sudo rights for
netstat(or set the set‑uid bit):
<code># visudo
Defaults:zabbix !requiretty
zabbix ALL=(root) NOPASSWD:/bin/netstat</code>(5) Restart the agent and test with
zabbix_get:
<code># service zabbix-agent restart
zabbix_get -s 10.2.42.16 -p 10050 -k redis.discovery
zabbix_get -s 10.2.42.16 -p 10050 -k redis.stats[6383,expired_keys]
</code>If zabbix_get is missing, install it via yum install zabbix_get -y .
Server‑Side Configuration (Web UI)
Create a template, an application set, and a discovery rule. Configure optional filters and regular expressions to limit discovered hosts.
Create Item Prototypes
Within the discovery rule, add item prototypes such as:
<code>redis_stats[{#REDISPORT},evicted_keys]</code>Explanation:
redis_stats – the key defined on the agent side.
{#REDISPORT} – the macro supplied by the discovery script.
evicted_keys – the specific Redis metric to collect.
Create Graph Prototypes
Define graph prototypes under the same discovery rule to visualize metrics.
Create Triggers
Add trigger prototypes to the discovery rule, for example monitoring evicted keys.
Add the Template to the Host
Assign the created template to the target host, ensuring the discovery script and agent configuration are present on that host.
After linking the template, you can view the latest data and graphs for each discovered Redis instance.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.