• 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)
    
  • 相关阅读:
    制作基于OK6410的文件系统(2.修改busybox文件系统,使之能启动)
    [转]SlickEdit 2012
    移植TPLINK TLWN721N 驱动到micro2440(by liukun321咕唧咕唧)
    擦,买到了伪劣的SD卡
    Slickedit中文显示与输入
    OMAP3530mini调试笔记(2)
    最近在做linux的移植工作
    嵌入式linux启动时运行的inittab文件(zz)
    OMAP3530mini调试笔记(1)
    Ubuntu中音量调节无效的解决方法
  • 原文地址:https://www.cnblogs.com/liujitao79/p/8945310.html
Copyright © 2020-2023  润新知