collectd的python插件(redis)
https://blog.dbrgn.ch/2017/3/10/write-a-collectd-python-plugin/

redis_info.conf
<LoadPlugin python>
Globals true
</LoadPlugin>
<Plugin python>
ModulePath "/opt/redis-collectd-plugin"
Import "redis_info"'
<Module redis_info>
Host "10.105.225.8"
Port 6379
Password "@sentinel"
sentinel_port 26379
sentinel_name mymaster
Redis_redis_alive "gauge"
Redis_sentinel_alive "gauge"
Redis_connected_clients "gauge"
Redis_blocked_clients "gauge"
Redis_rejected_connections "counter"
Redis_expired_keys "counter"
Redis_evicted_keys "counter"
Redis_used_memory "gauge"
Redis_used_memory_rss "gauge"
Redis_maxmemory "gauge"
Redis_mem_used_ratio "gauge"
Redis_mem_fragmentation_ratio "gauge"
Redis_instantaneous_ops_per_sec "gauge"
Redis_total_connections_received "counter"
Redis_total_commands_processed "counter"
Redis_keyspace_hits "derive"
Redis_keyspace_misses "derive"
Redis_cmdstat_get_calls "counter"
Redis_cmdstat_set_calls "counter"
Redis_db0_keys "gauge"
Redis_db0_expires "gauge"
</Module>
<Module redis_info>
Host "10.105.223.86"
Port 6379
Password "@sentinel"
sentinel_port 26379
sentinel_name mymaster
Redis_redis_alive "gauge"
Redis_sentinel_alive "gauge"
Redis_connected_clients "gauge"
Redis_blocked_clients "gauge"
Redis_rejected_connections "counter"
Redis_expired_keys "counter"
Redis_evicted_keys "counter"
Redis_used_memory "gauge"
Redis_used_memory_rss "gauge"
Redis_maxmemory "gauge"
Redis_mem_used_ratio "gauge"
Redis_mem_fragmentation_ratio "gauge"
Redis_instantaneous_ops_per_sec "gauge"
Redis_total_connections_received "counter"
Redis_total_commands_processed "counter"
Redis_keyspace_hits "derive"
Redis_keyspace_misses "derive"
Redis_cmdstat_get_calls "counter"
Redis_cmdstat_set_calls "counter"
Redis_db0_keys "gauge"
Redis_db0_expires "gauge"
</Module>
</Plugin>
redis_info.py
# -*- coding: utf-8 -*-
import collectd
import redis
from redis.sentinel import Sentinel
import re
import json
CONFIG = []
def configure_callback(config):
host = '127.0.0.1'
port = 6379
password = '@sentinel'
sentinel_port = 26379
sentinel_name = 'mymaster'
redis_info = {}
for node in config.children:
k, v = node.key, node.values[0]
match = re.search(r'Redis_(.*)$', k, re.M|re.I)
if k == 'Host':
host = v
elif k == 'Port':
port = int(v)
elif k == 'Password':
password = v
elif k == 'Sentinel_port':
sentinel_port = int(v)
elif k == 'Sentinel_name':
sentinel_name = v
elif match:
redis_info[match.group(1)] = v
else:
collectd.warning('unknown config key: %s' % (k))
CONFIG.append({'host': host, 'port': port, 'password': password, 'sentinel_port': sentinel_port, 'sentinel_name': sentinel_name, 'redis_info': redis_info})
def fetch_redis_info(conf):
info = {}
# 获取redis状态信息(0 dead, 1 master, -1 slave)
try:
r = redis.Redis(host=conf['host'], port=conf['port'], password=conf['password'], socket_connect_timeout=5)
for k, v in r.info().items():
if k in conf['redis_info'].keys():
info[k] = v
elif k.startswith('db'):
for i in ['keys','expires']:
info[k+'_'+i] = v[i]
elif k == 'role':
if v == 'master':
info['redis_alive'] = 1
else:
info['redis_alive'] = -1
if info['maxmemory'] > 0:
info['mem_used_ratio'] = round(float(info['used_memory'])/float(info['maxmemory'])*100, 2)
else:
info['mem_used_ratio'] = 0
for k, v in r.info('commandstats').items():
if k+'_calls' in conf['redis_info'].keys():
info[k+'_calls'] = v['calls']
except redis.RedisError as e:
collectd.error('redis %s:%s connection error!' % (conf['host'], conf['port']))
info['redis_alive'] = 0
# 获取sentinel状态信息 (0 dead, 1 leader, -1 leaf)
try:
s = Sentinel([(conf['host'], conf['sentinel_port'])], socket_timeout=0.1)
if conf['host'] == s.discover_master(conf['sentinel_name'])[0]:
info['sentinel_alive'] = 1
else:
info['sentinel_alive'] = -1
except redis.RedisError as e:
collectd.error('sentinel %s:%s connection error!' % (conf['host'], conf['sentinel_port']))
info['sentinel_alive'] = 0
return info
def read_callback():
for conf in CONFIG:
info = fetch_redis_info(conf)
#collectd.info('[%s] %s' % (conf['host'], json.dumps(info)))
plugin_instance = '%s:%d' % (conf['host'], conf['port'])
for k, v in info.items():
if k in conf['redis_info'].keys():
dispatch_value(k, v, conf['redis_info'][k], plugin_instance)
def dispatch_value(key, value, type, plugin_instance):
val = collectd.Values(plugin='redis_info')
val.type = type
val.type_instance = key
val.plugin_instance = plugin_instance
val.values = [value]
val.dispatch()
#注册回调函数
collectd.register_config(configure_callback)
collectd.register_read(read_callback)
collectd的python插件(redis)的更多相关文章
- Python操作Redis(一)
redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- python中redis
一.简介 二.redis的安装和使用 三.python操作readis之安装和支持存储类型 四.python操作redis值普通链接 五.python操作redis值连接池 六.操作之String操作 ...
- python之redis和memcache操作
Redis 教程 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据 ...
- Python—操作redis
Python操作redis 连接方式:点击 1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redis中设置值,默认不存在则创建, ...
- python——操作Redis
在使用django的websocket的时候,发现web请求和其他当前的django进程的内存是不共享的,猜测django的机制可能是每来一个web请求,就开启一个进程去与web进行交互,一次来达到利 ...
- 【python】Redis介绍及简单使用
一.redis redis是一个key-value存储系统.和 Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合).zset(s ...
- python之 Redis
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- Python操作Redis、Memcache、RabbitMQ、SQLAlchemy
Python操作 Redis.Memcache.RabbitMQ.SQLAlchemy redis介绍:redis是一个开源的,先进的KEY-VALUE存储,它通常被称为数据结构服务器,因为键可以包含 ...
- python之redis
Redis简单介绍 如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构 ...
随机推荐
- ImportError: libSM.so.6: cannot open shared object file: No such file or directory
Solution sudo apt-get install libsm6 Similarly ImportError: libXrender.so.1: cannot open shared obje ...
- 参数化登录QQ空间实例
通过参数化的方式,登录QQ空间 实例源码: # coding:utf-8 from selenium import webdriver import unittest import time clas ...
- Log4j 2.0读取配置文件的方法
log4j中配置日志文件存放的位置不一定在src下面,即根目录下.这个时候我们需要解决如何加载配置文件的问题.在log4j1.x中解决的方法就比较多了.如:PropertyConfigurator.c ...
- jQuery 自定义方法(扩展方法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Redis学习笔记--常用命令
以下为本人学习Redis的备忘录,记录了大部分常用命令 1.客户端连接redis服务端: ===启动Redis服务端 redis-server /yourpath/redis.conf ===启动Re ...
- SpringBoot | 第三十六章:集成多CacheManager
前言 今天有网友咨询了一个问题:如何在一个工程中使用多种缓存进行差异化缓存,即实现多个cacheManager灵活切换.原来没有遇见这种场景,今天下班抽空试了下,以下就把如何实现的简单记录下. 一点知 ...
- Firebird execute block 批处理
火鸟的批处理,效率好高,使用简单. execute block as declare variable i ; begin ) do begin :i = :i + ; insert into m_u ...
- bitbucket 源代码托管
5个人以下可以免费使用,不限制仓库的数量; 国外的注册需要开启蓝灯FQ; 1.注册账号 maanshancss w1-g1@qq.com;创建仓库; 然后拷贝现有项目 然后提交 然后push; 2.写 ...
- WebAPI搭建(一)如何在Webforms 下 搭建WebAPI
公司的很多项目前期一直是用的WebForms.但是因为业务的发展,公司要在原有的项目上接入移动端,webservice有点老旧了,现在比较流行RESTFul,于是乎就想到了WebAPI. 一.如果是新 ...
- [shell]管理 Sphinx 启动|停止|重新生成索引的脚本
对于启动sphinx的服务,可以直接输入如下命令 /usr/bin/searchd -c /etc/sphinx/sphinx.conf <!-- /usr/local/bin/searchd ...