py編寫exporter
python編寫Prometheus exporter獲取redis數據
第三方`redis_exporter 無法獲取實例的maxmemory
<code> from
prometheus_clientimport
start_http_serverfrom
prometheus_clientimport
Gaugeimport
redis FilePath =open ("/root/pyredis/redisList.txt"
,"r"
) """ 鏈接地址 端口 告警百分比 實例使用說明 聯繫人信息 118.190.217.164 6379 80 我的REDIS實例002 contact_user:JackFeng,A2Data||185123456,138123456 127.0.0.1 6379 80 我的REDIS實例002 contact_user:DataScience,Fen||185123456,138123456 """
g =Gauge('redis_maxMemory'
,'maxMemory of redis instance'
, ['addr'
,'alias'
]) if
__name__ =="__main__"
:""" 啟動服務 到 metrics """
start_http_server(8666
)""" 生成請求 """
while
True
:for
linein
FilePath: RdHost,RdPort,RdWarn,RdName,RdContact = line.split() print(RdHost,RdPort) r=redis.Redis(host=RdHost, port=RdPort, db=0
, password='123456'
) maxMemory=r.config_get("maxMemory"
) g.labels(addr="%s:%s"
% (RdHost,RdPort),alias=u"%s"
% RdName.decode('utf-8'
)).set(float(maxMemory['maxMemory'
])) /<code>
Prometheus配置
<code>
-
job_name:
'PyRedis'
static_configs:
-
targets:
['118.190.217.164:8666']
/<code>
1、啟動
2、查看Prometheus
可以發現 任務已經在 targets了
搜索你的這個id 就可以 ,通過Grafana模板看到相關信息
3、配置Grafana
如下圖配置
4、導入自定義dashboard
監控的redis模板
請加小編獲取 小編整理一些常用的。 dashboard
更多精彩,之後探索踩坑會做記錄。