• check_mk插件 redis


    /usr/lib/check_mk_agent/plugins/mk_redis

    #!/bin/bash
    
    echo '<<<redis>>>'
    hosts=$(netstat -nlp -t |grep redis-server|awk '{print $4}')
    
    for host in $hosts;
    do
      ip=$(echo $host|cut -d ':' -f1)
      port=$(echo $host|cut -d ':' -f2)
      echo "start $ip:$port"
      redis-cli -p $port info |egrep '^redis_version|^redis_mode|^process_id|^uptime_in_days|^rdb_last_save_time|^rdb_changes_since_last_save|^master_link_down_since|^role|^connected_slaves|^master_last_io_seconds_ago|^blocked_clients|^connected_clients|^rejected_connections|^blocked_clients|^connected_clients|^rejected_connections|^instantaneous_ops_per_sec|^keyspace_hits|^keyspace_misses|^evicted_keys|^used_memory:|^mem_fragmentation_ratio'|awk -F ':' '{print $1,$2}'
      echo "end $ip:$port"
    done
    

    /opt/omd/sites/test/share/check_mk/checks/redis

    '''
    start 127.0.0.1:6381
    redis_version 2.8.19
    redis_mode standalone
    process_id 13675
    uptime_in_days 5
    rdb_changes_since_last_save 0
    rdb_last_save_time 1490268431
    role master
    connected_slaves 0
    
    connected_clients 1
    blocked_clients 0
    rejected_connections 0
    evicted_keys 0
    instantaneous_ops_per_sec 0
    
    
    keyspace_hits 0
    keyspace_misses 0
    
    used_memory 502624
    
    mem_fragmentation_ratio 11.67
    end 127.0.0.1:6381
    '''
    
    import time
    
    def inventory_redis_info(info):
        inventory = []
        for line in info:
            if line[0] == 'start':
                inventory.append((line[1], None))
        return inventory
     
    def inventory_redis_stat(info):
        inventory = []
        for line in info:
            if line[0] == 'start':
                inventory.append((line[1], None))
        return inventory
    
    def check_redis_info(item, params, info):
        message = []
        flag = False
        for line in info:
            if line[0] == 'end' and line[1] == item:
                break
    
            elif line[0] == 'start' and line[1] == item:
                flag = True
            
            elif flag:
                if line[0] in ['redis_version', 'redis_mode', 'process_id', 'uptime_in_days', 'role', 'connected_slaves', 'rdb_changes_since_last_save']:
                    message.append(line[0]+': '+line[1])
                elif line[0] in ['rdb_last_save_time']:
                    message.append(line[0]+': '+ time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(line[1]))))
    
        return 0, ', '.join(message)
    
    def check_redis_stat(item, params, info):
        message = []
        perfdata = []
        flag = False
    
        for line in info:
            if line[0] == 'end' and line[1] == item:
                break
    
            elif line[0] == 'start' and line[1] == item:
                flag = True
            
            elif flag:
                if line[0] in ['connected_clients', 'blocked_clients', 'rejected_connections', 'evicted_keys', 'instantaneous_ops_per_sec', 'used_memory']: 
                    message.append(line[0]+': '+line[1])
                    perfdata.append((line[0], int(line[1]), None, None, 0, None))
    
                elif line[0] in ['mem_fragmentation_ratio']: 
                    message.append(line[0]+': '+line[1])
                    perfdata.append((line[0], float(line[1]), None, None, 0, 100))
    
                elif line[0] in ['keyspace_hits']: 
                    message.append(line[0]+': '+line[1])
                    perfdata.append((line[0], int(line[1]), None, None, 0, None))
                    hits = int(line[1])
    
                elif line[0] in ['keyspace_misses']: 
                    message.append(line[0]+': '+line[1])
                    perfdata.append((line[0], int(line[1]), None, None, 0, None))
                    misses = int(line[1])
        if (hits+misses) == 0:
            hit_rate = 0
        else:          
            hit_rate = round(hits/(hits+misses)*1.0, 2)
    
        message.append('hit_rate: %.2f' % hit_rate)
        perfdata.append(('hit_rate', hit_rate*100, None, None, 0, 100))   
    
        return 0, ', '.join(message), perfdata
    
    check_info['redis.info'] = {
        'check_function':            check_redis_info,
        'inventory_function':        inventory_redis_info,
        'service_description':       'redis_info %s',
    }
    
    check_info['redis.stat'] = {
        'check_function':            check_redis_stat,
        'inventory_function':        inventory_redis_stat,
        'service_description':       'redis_stat %s',
        'has_perfdata':              True,
    }
    

    /opt/omd/sites/test/share/check_mk/pnp-templates/check_mk-redis.stat.php

    <?php
    
    # clients
    $opt[1] = "-t "Clients" -v "Number" -l 0 ";
    $def[1] = "";
    
    $def[1] .= "DEF:v1=$RRDFILE[1]:$DS[1]:MAX ";
    $def[1] .= "LINE2:v1#ff4000:"connected_clients" ";
    $def[1] .= "GPRINT:v1:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v1:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v1:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v1:MIN:"min :%5.0lf" ";
    
    $def[1] .= "DEF:v2=$RRDFILE[2]:$DS[2]:MAX ";
    $def[1] .= "LINE2:v2#a00000:"blocked_clients" ";
    $def[1] .= "GPRINT:v2:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v2:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v2:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v2:MIN:"min :%5.0lf" ";
    
    $def[1] .= "DEF:v6=$RRDFILE[6]:$DS[6]:MAX ";
    $def[1] .= "LINE2:v6#00f040:"rejected_connections" ";
    $def[1] .= "GPRINT:v6:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v6:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v6:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v6:MIN:"min :%5.0lf" ";
    
    $def[1] .= "DEF:v7=$RRDFILE[7]:$DS[7]:MAX ";
    $def[1] .= "LINE2:v7#00b0b0:"evicted_keys" ";
    $def[1] .= "GPRINT:v7:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v7:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v7:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v7:MIN:"min :%5.0lf" ";
    
    $def[1] .= "DEF:v8=$RRDFILE[8]:$DS[8]:MAX ";
    $def[1] .= "LINE2:v8#c060ff:"keyspace_hits" ";
    $def[1] .= "GPRINT:v8:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v8:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v8:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v8:MIN:"min :%5.0lf" ";
    
    $def[1] .= "DEF:v9=$RRDFILE[9]:$DS[9]:MAX ";
    $def[1] .= "LINE2:v9#f000f0:"keyspace_misses" ";
    $def[1] .= "GPRINT:v9:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v9:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v9:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v9:MIN:"min :%5.0lf" ";
    
    $def[1] .= "DEF:v5=$RRDFILE[5]:$DS[5]:MAX ";
    $def[1] .= "LINE2:v5#ff4000:"instantaneous_ops_per_sec" ";
    $def[1] .= "GPRINT:v5:LAST:"last :%5.0lf" ";
    $def[1] .= "GPRINT:v5:AVERAGE:"avg :%5.0lf" ";
    $def[1] .= "GPRINT:v5:MAX:"max :%5.0lf" ";
    $def[1] .= "GPRINT:v5:MIN:"min :%5.0lf" ";
    
    # performance
    $opt[2] = "-t "Performance" -v "(%)" -l 0 ";
    $def[2] = "";
    
    $def[2] .= "DEF:v4=$RRDFILE[4]:$DS[4]:MAX ";
    $def[2] .= "LINE2:v4#a00000:"mem_fragmentation_ratio" ";
    $def[2] .= "GPRINT:v4:LAST:"last :%5.2lf" ";
    $def[2] .= "GPRINT:v4:AVERAGE:"avg :%5.2lf" ";
    $def[2] .= "GPRINT:v4:MAX:"max :%5.2lf" ";
    $def[2] .= "GPRINT:v4:MIN:"min :%5.2lf" ";
    
    $def[2] .= "DEF:v10=$RRDFILE[10]:$DS[10]:MAX ";
    $def[2] .= "LINE2:v10#00f040:"hit_rate" ";
    $def[2] .= "GPRINT:v10:LAST:"last :%5.2lf" ";
    $def[2] .= "GPRINT:v10:AVERAGE:"avg :%5.2lf" ";
    $def[2] .= "GPRINT:v10:MAX:"max :%5.2lf" ";
    $def[2] .= "GPRINT:v10:MIN:"min :%5.2lf" ";
    
    # memory
    $opt[3] = "-t "Use Memory" -v "Bytes" -l 0 ";
    $def[3] = "";
    
    $def[3] .= "DEF:v3=$RRDFILE[3]:$DS[3]:MAX ";
    $def[3] .= "AREA:v3#FF6666:"use_memory" ";
    $def[3] .= "GPRINT:v3:LAST:"last :%5.0lf" ";
    $def[3] .= "GPRINT:v3:AVERAGE:"avg :%5.0lf" ";
    $def[3] .= "GPRINT:v3:MAX:"max :%5.0lf" ";
    $def[3] .= "GPRINT:v3:MIN:"min :%5.0lf" ";
    ?>
    



  • 相关阅读:
    hdoj-1013-Digital Roots(九余数定理)
    hdu-1012-u Calculate e(水题)
    hdoj-1005-Number Sequence
    JavaScript--收藏栏添加按钮,放大hdu题目字体
    string和double之间的相互转换(C++)
    轻谈 return i++
    Java中equals和==之间的区别
    进程的内核栈、用户栈
    关于Linux中cd的一些快捷用法
    Mac 下如何使用sed -i命令
  • 原文地址:https://www.cnblogs.com/liujitao79/p/6646802.html
Copyright © 2020-2023  润新知