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等数据结构 ...
随机推荐
- unity 渲染第一步
unity 不是将宇宙投影到水晶球里,而是:将整个 view frustum 投影成 一个 cube .------ <unity 渲染箴言> 观察一下,整个 view frustum 以 ...
- select for update和select for update wait和select for update nowait的区别
CREATE TABLE "TEST6" ( "ID" ), "NAME" ), "AGE" ,), "SEX ...
- 【Lua】LWT遍历指定目录并输出到页面中
首先用lua遍历目录: function getDirs(path) local s = {} function attrdir(p) for file in lfs.dir(p) do if fil ...
- HAProxy与Nginx区别
1)HAProxy对于后端服务器一直在做健康检测(就算请求没过来的时候也会做健康检查):后端机器故障发生在请求还没到来的时候,haproxy会将这台故障机切掉,但如果后端机器故障发生在请求到达期间,那 ...
- WPF的布局--DockPanel
1.DockPanel: 以上.下.左.右.中为基本结构的布局方式 类似于Java AWT布局中的BorderLayout. 但与BorderLayout不同的是,每一个区域可以同时放置多个控件,在同 ...
- SpringMVC的参数绑定
一.@RequestMapping注解说明 通过@RequestMapping注解可以定义不同的处理器映射规则. URL路径映射 @RequestMapping(value="/item ...
- AtCoder Regular Contest 059 F Unhappy Hacking
Description 题面 Solution 我们发现如果一个位置需要被退掉,那么是 \(0\) 或 \(1\) 都没有关系 于是我们想到把 \(0,1\) 归为一类 问题转化为每一次可以添加和删除 ...
- [转]Newtonsoft JSON how to dynamically change the date format?
本文转自:http://www.howtobuildsoftware.com/index.php/how-do/cg8K/jsonnet-newtonsoft-json-how-to-dynamica ...
- Change - Why we need coding standards
Change - Why we need coding standards I have the idea of coding standards when I have to review my t ...
- C#中匿名委托以及Lambda表达式的学习笔记
一. C#从1.0到4.0, 随着Linq,泛型的支持,代码越来越简单优雅 , , , , , , , , , }; IEnumerable< select n; newNums = newNu ...