python编写Prometheus exporter获取redis数据

近期有使用Prometheus监控我们的redis,使用了第三方的redis_exportor,但是它无法获取redis实例的maxmemory,我就通过自己编写python脚本自己来获取这个数据了

#!/usr/bin/python
# -*- coding: utf-8 -*-
from prometheus_client import start_http_server
from prometheus_client import Gauge
import redis
import random
import time

fp=open("/export/scripts/sa/redisList.txt", "r")

g = Gauge('redis_maxmemory', 'Maxmemory of redis instance', ['addr', 'alias'])

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        for line in fp:
            RdHost,RdPort,RdWarn,RdName,RdContact = line.split()
            #print RdHost
            r=redis.Redis(host=RdHost, port=RdPort, db=0, password='YOUR REDIS PASSWORD')
            maxmemory=r.config_get("maxmemory")
            #print maxmemory
            g.labels(addr="%s:%s" % (RdHost,RdPort),alias=u"%s" % RdName.decode('utf-8')).set(float(maxmemory['maxmemory']))

需要监控的redis的相关信息都存放在/export/scripts/sa/redisList.txt文件中了:

r-XXXXX.redis.rds.aliyuncs.com 6379 95 我的REDIS实例-010 contact_user:XXX,YYY||150123456,138123456
r-XXXXX.redis.rds.aliyuncs.com 6379 95 我的测试实例002 contact_user:XXX,YYY||150123456,138123456

运行我的python exportor:

python readRedis.py

然后修改Prometheus.yml,添加上述IP:port到target里面,通过Grafana模板,可以查看到相关信息了

new

监控redis的grafana模版可供 下载