├── README.md ├── memcached ├── README.md ├── memcached-monitor.py └── memcached_monitor.php ├── phpfpm ├── README.md └── phpfpm-monitor.py ├── rabbitmq ├── README.md └── rabbitmq-monitor.py └── redis ├── README.md ├── php_redis.php └── redis-monitor.py /README.md: -------------------------------------------------------------------------------- 1 | # falcon-monit-scripts 2 | 用于小米开源监控系统open-falcon的监控采集脚本集合 3 | -------------------------------------------------------------------------------- /memcached/README.md: -------------------------------------------------------------------------------- 1 | open-falcon memcached监控脚本 2 | ================================ 3 | 4 | 系统需求 5 | -------------------------------- 6 | 操作系统:Linux 7 | 8 | Python >= 2.6 9 | 10 | python-simplejson 11 | 12 | request 13 | 14 | 主要逻辑 15 | -------------------------------- 16 | 通过ps过滤获得本机启动的memcached端口列表,然后使用telnet stat获取并解析相关数据,然后推送到falco-agent 17 | 18 | 汇报字段 19 | -------------------------------- 20 | 所有memcached stats字段(pid,time除外)均汇报,除此之外,增加了以下原值: 21 | 22 | | key | tag | type | note | 23 | |-----|------|------|------| 24 | memcached.get_hit_ratio|port(实例端口号)|GAUGE|get命令总体命中率| 25 | memcached.incr_hit_ratio|port(实例端口号)|GAUGE|incr命令总体命中率| 26 | memcached.decr_hit_ratio|port(实例端口号)|GAUGE|decr命令总体命中率| 27 | memcached.delete_hit_ratio|port(实例端口号)|GAUGE|delete命令总体命中率| 28 | memcached.usage|port(实例端口号)|GAUGE|分配内存使用率,等于byte/limitmaxbyte| 29 | 30 | 31 | 使用方法 32 | -------------------------------- 33 | 1. 根据实际情况,决定是否修改83行的ip参数 34 | 2. 将脚本加入crontab即可 35 | 36 | 37 | 授权类型:MIT 38 | 39 | 40 | -------------------------------------------------------------------------------- /memcached/memcached-monitor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding:utf-8 -*- 3 | 4 | __author__ = 'iambocai' 5 | 6 | import requests 7 | import json 8 | import time 9 | import socket 10 | import os 11 | import re 12 | import telnetlib 13 | import sys 14 | import commands 15 | 16 | class MemcachedStats: 17 | 18 | _client = None 19 | _key_regex = re.compile(ur'ITEM (.*) \[(.*); (.*)\]') 20 | _slab_regex = re.compile(ur'STAT items:(.*):number') 21 | _stat_regex = re.compile(ur"STAT (.*) ([0-9]+\.?[0-9]*)\r") 22 | 23 | def __init__(self, host='localhost', port='11211'): 24 | self._host = host 25 | self._port = port 26 | 27 | @property 28 | def client(self): 29 | if self._client is None: 30 | self._client = telnetlib.Telnet(self._host, self._port) 31 | return self._client 32 | 33 | def command(self, cmd): 34 | ' Write a command to telnet and return the response ' 35 | self.client.write("%s\n" % cmd) 36 | return self.client.read_until('END') 37 | 38 | def close(self): 39 | 'close telnet connection' 40 | return self.client.write("quit\n") 41 | 42 | def key_details(self, sort=True, limit=100): 43 | ' Return a list of tuples containing keys and details ' 44 | cmd = 'stats cachedump %s %s' 45 | keys = [key for id in self.slab_ids() 46 | for key in self._key_regex.findall(self.command(cmd % (id, limit)))] 47 | if sort: 48 | return sorted(keys) 49 | else: 50 | return keys 51 | 52 | def keys(self, sort=True, limit=100): 53 | ' Return a list of keys in use ' 54 | return [key[0] for key in self.key_details(sort=sort, limit=limit)] 55 | 56 | def slab_ids(self): 57 | ' Return a list of slab ids in use ' 58 | return self._slab_regex.findall(self.command('stats items')) 59 | 60 | def stats(self): 61 | ' Return a dict containing memcached stats ' 62 | return dict(self._stat_regex.findall(self.command('stats'))) 63 | 64 | 65 | def main(): 66 | ip = socket.gethostname() 67 | timestamp = int(time.time()) 68 | step = 60 69 | insts_list = [ os.path.basename(i) for i in commands.getoutput(''' ps -ef |grep memcached|grep -v grep |sed -n 's/.* *-p *\([0-9]\{1,5\}\).*/\1/p' ''' ).split('\n') ] 70 | data = [] 71 | 72 | gauges = [ 'get_hit_ratio', 'incr_hit_ratio', 'decr_hit_ratio', 'delete_hit_ratio', 'usage', 'curr_connections', 'total_connections', 'bytes', 'pointer_size', 'uptime', 'limit_maxbytes', 'threads', 'curr_items', 'total_items', 'connection_structures' ] 73 | 74 | for inst in insts_list: 75 | 76 | port = inst 77 | metric = "memcached" 78 | endpoint = ip 79 | tags = 'port=%s' % port 80 | 81 | try: 82 | # ATT: if your instance listened on 127.0.0.1, change "ip" to "127.0.0.1" in follow line 83 | conn = MemcachedStats(ip, port) 84 | stats = conn.stats() 85 | conn.close() 86 | except: 87 | continue 88 | 89 | del stats['pid'] 90 | del stats['time'] 91 | 92 | stats['usage'] = str(100 * float(stats['bytes']) / float(stats['limit_maxbytes'])) 93 | try: 94 | stats['get_hit_ratio'] = str(100 * float(stats['get_hits']) / (float(stats['get_hits']) + float(stats['get_misses']))) 95 | except ZeroDivisionError: 96 | stats['get_hit_ratio'] = '0.0' 97 | try: 98 | stats['incr_hit_ratio'] = str(100 * float(stats['incr_hits']) / (float(stats['incr_hits']) + float(stats['incr_misses']))) 99 | except ZeroDivisionError: 100 | stats['incr_hit_ratio'] = '0.0' 101 | try: 102 | stats['decr_hit_ratio'] = str(100 * float(stats['decr_hits']) / (float(stats['decr_hits']) + float(stats['decr_misses']))) 103 | except ZeroDivisionError: 104 | stats['decr_hit_ratio'] = '0.0' 105 | try: 106 | stats['delete_hit_ratio'] = str(100 * float(stats['delete_hits']) / (float(stats['delete_hits']) + float(stats['delete_misses']))) 107 | except ZeroDivisionError: 108 | stats['delete_hit_ratio'] = '0.0' 109 | 110 | 111 | for key in stats: 112 | value = float(stats[key]) 113 | if key in gauges: 114 | suffix = '' 115 | vtype = 'GAUGE' 116 | else: 117 | suffix = '_cps' 118 | vtype = 'COUNTER' 119 | 120 | i = { 121 | 'metric': '%s.%s%s' % (metric, key, suffix), 122 | 'endpoint': endpoint, 123 | 'timestamp': timestamp, 124 | 'step': step, 125 | 'value': value, 126 | 'counterType': vtype, 127 | 'tags': tags 128 | } 129 | data.append(i) 130 | 131 | return data 132 | 133 | 134 | if __name__ == '__main__': 135 | proc = commands.getoutput(''' ps -ef|grep 'memcached-monitor.py'|grep -v grep|wc -l ''') 136 | if int(proc) < 3: 137 | r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(main())) 138 | print r.text 139 | -------------------------------------------------------------------------------- /memcached/memcached_monitor.php: -------------------------------------------------------------------------------- 1 | array(//服务器组 9 | array('127.0.0.1', 11211), 10 | ), 11 | 'policy' => array(//缓存策略 12 | 'compressed' => false, //是否压缩,开启后,默认大于100b时压缩 13 | 'life_time' => 3600, //缓存时间 秒 14 | 'pool' => 'mc', //连接池,同一连接池建立长连接 15 | ) 16 | ); 17 | $memcached = new \Memcached($config['policy']['pool']); 18 | $memcached->setOption(\Memcached::OPT_COMPRESSION, $config['policy']['compressed']); 19 | $memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $conTimeout); 20 | if ($memcached->addServers($config['servers'])) { 21 | $data = $memcached->getStats(); 22 | } 23 | } 24 | //要监控的key列表 25 | $monitKeys = array( 26 | 'uptime' => 'GAUGE', 27 | 'threads' => 'GAUGE', 28 | 'pointer_size' => 'GAUGE', 29 | 'rusage_user_seconds' => 'GAUGE', 30 | 'rusage_user_microseconds' => 'GAUGE', 31 | 'rusage_system_seconds' => 'GAUGE', 32 | 'rusage_system_microseconds' => 'GAUGE', 33 | 'curr_items' => 'GAUGE', 34 | 'total_items' => 'GAUGE', 35 | 'limit_maxbytes' => 'GAUGE', 36 | 'curr_connections' => 'GAUGE', 37 | 'total_connections' => 'GAUGE', 38 | 'connection_structures' => 'GAUGE', 39 | 'bytes' => 'GAUGE', 40 | 'cmd_get' => 'GAUGE', 41 | 'cmd_set' => 'GAUGE', 42 | 'get_hits' => 'GAUGE', 43 | 'get_misses' => 'GAUGE', 44 | 'evictions' => 'GAUGE', 45 | 'bytes_read' => 'GAUGE', 46 | 'bytes_written' => 'GAUGE', 47 | ); 48 | //关联监控指标数值 49 | foreach ($monitKeys as $k => $v) { 50 | $falArr[] = array( 51 | "endpoint" => gethostname(), 52 | "metric" => "memcached_" . $k, 53 | "timestamp" => time(), 54 | "step" => 60, 55 | "value" => (isset($data[$k]) ? $data[$k] : 0), 56 | "counterType" => $v, 57 | "tags" => "project=atido,group=server,name=memcached,method=monitor", 58 | ); 59 | } 60 | //stdOut 61 | echo json_encode($falArr); 62 | -------------------------------------------------------------------------------- /phpfpm/README.md: -------------------------------------------------------------------------------- 1 | open-falcon php-fpm监控脚本 2 | =========================== 3 | 4 | 安装使用(Ubuntu) 5 | -------- 6 | 7 | `sudo apt-get install fcgiwrap` 8 | 9 | php-fpm设置`pm.status_path`,与脚本中设置相应,并填上php-fpm监控地址或sock文件。 10 | 11 | 加入crontab中定时执行。 12 | 13 | 汇报字段 14 | -------- 15 | | key | tag | type | note | 16 | |-----|-----|------|------| 17 | |php.active_process|pool|GAUGE|活跃进程数| 18 | |php.accepted_conn|pool|GAUGE|接受请求数| 19 | |php.listen_queue|pool|GAUGE|| 20 | |php.idle_processes|pool|GAUGE|空闲进程数| 21 | |php.slow_requests|pool|GAUGE|慢请求数| 22 | |php.max_active_processes|pool|GAUGE|达到的最大活跃进程数| 23 | |php.max_children_reqched|pool|GAUGE|达到的最大子进程数| 24 | |php.max_listen_queue|pool|GAUGE|达到的最大请求队列数| 25 | |php.total_processes|pool|GAUGE|总进程数| 26 | |php.listen_queue_len|pool|GAUGE|| 27 | 28 | 29 | 授权类型:MIT 30 | -------------------------------------------------------------------------------- /phpfpm/phpfpm-monitor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | __author__ = 'serika00' 5 | 6 | import commands, socket, time, json, urllib2 7 | #import sys 8 | 9 | STATUS_PATH = "/fpm/status" 10 | LISTEN_ADDR = "127.0.0.1:9000" 11 | 12 | def go(): 13 | monit_keys = [ 14 | # 'pool 15 | # 'start_time' 16 | # 'process_manager' 17 | # 'start_since' 18 | ('active_processes', 'GAUGE'), 19 | ('accepted_conn', 'COUNTER'), 20 | ('listen_queue', 'GAUGE'), 21 | ('idle_processes', 'GAUGE'), 22 | ('slow_requests', 'GAUGE'), 23 | ('max_active_processes', 'GAUGE'), 24 | ('max_children_reached', 'GAUGE'), 25 | ('max_listen_queue', 'GAUGE'), 26 | ('total_processes', 'GAUGE'), 27 | ('listen_queue_len', 'GAUGE'), 28 | ] 29 | 30 | status = commands.getoutput("SCRIPT_NAME=%s SCRIPT_FILENAME=%s QUERY_STRING='json' REQUEST_METHOD=GET cgi-fcgi -bind -connect %s | tail -n 1" % (STATUS_PATH, STATUS_PATH, LISTEN_ADDR)) 31 | status = json.loads(status) 32 | 33 | ip = socket.gethostname() 34 | timestamp = int(time.time()) 35 | step = 60 36 | metric = 'php' 37 | endpoint = ip 38 | tags = 'pool=%s' % status['pool'] 39 | p = [] 40 | 41 | for key, vtype in monit_keys: 42 | value = int(status[key.replace('_', ' ')]) 43 | i = { 44 | 'Metric': '%s.%s' % (metric, key), 45 | 'Endpoint': endpoint, 46 | 'Timestamp': timestamp, 47 | 'Step': step, 48 | 'Value': value, 49 | 'CounterType': vtype, 50 | 'TAGS': tags 51 | } 52 | p.append(i) 53 | 54 | #print json.dumps(p) 55 | #sys.exit(0) 56 | method = "POST" 57 | handler = urllib2.HTTPHandler() 58 | opener = urllib2.build_opener(handler) 59 | url = 'http://127.0.0.1:1988/v1/push' 60 | request = urllib2.Request(url, data=json.dumps(p) ) 61 | request.add_header("Content-Type",'application/json') 62 | request.get_method = lambda: method 63 | try: 64 | connection = opener.open(request) 65 | except urllib2.HTTPError,e: 66 | connection = e 67 | 68 | if connection.code == 200: 69 | pass 70 | else: 71 | print '{"err":1,"msg":"%s"}' % connection 72 | 73 | if __name__ == '__main__': 74 | go() 75 | -------------------------------------------------------------------------------- /rabbitmq/README.md: -------------------------------------------------------------------------------- 1 | open-falcon rabbitmq监控脚本 2 | ================================ 3 | 4 | 系统需求 5 | -------------------------------- 6 | 操作系统:Linux 7 | Python >= 2.6 8 | python-simplejson 9 | 10 | 主要逻辑 11 | -------------------------------- 12 | 从rabbitmq-server的api接口读取相关数据,然后推送到falco-agent 13 | 14 | 汇报字段 15 | -------------------------------- 16 | | key | tag | type | note | 17 | |-----|------|------|------| 18 | |rabbitmq.messages_ready|name(Queue名字)|GAUGE|队列中处于等待被消费状态消息数| 19 | |rabbitmq.messages_unacknowledged|name(Queue名字)|GAUGE|队列中处于消费中状态的消息数| 20 | rabbitmq.messages_total|name(Queue名字)|GAUGE|队列中所有未完成消费的消息数,等于messages_ready+messages_unacknowledged| 21 | rabbitmq.ack_rate|name(Queue名字)|GAUGE|消费者ack的速率| 22 | rabbitmq.deliver_rate|name(Queue名字)|GAUGE|deliver的速率| 23 | rabbitmq.deliver_get_rate|name(Queue名字)|GAUGE|deliver_get的速率| 24 | rabbitmq.publish_rate|name(Queue名字)|GAUGE|publish的速率| 25 | 26 | 27 | 使用方法 28 | -------------------------------- 29 | 1. 根据实际部署情况,修改15,16行的rabbitmq-server管理端口和登录用户名密码 30 | 2. 确认1中配置的rabbitmq用户有你想监控的queue/vhosts的权限 31 | 3. 将脚本加入crontab即可 32 | 33 | 34 | 授权类型:MIT 35 | -------------------------------------------------------------------------------- /rabbitmq/rabbitmq-monitor.py: -------------------------------------------------------------------------------- 1 | #!/bin/env python 2 | #-*- coding:utf-8 -*- 3 | 4 | __author__ = 'iambocai' 5 | 6 | import sys, urllib2, base64, json, time,socket 7 | 8 | 9 | step = 60 10 | ip = socket.gethostname() 11 | ts = int(time.time()) 12 | keys = ('messages_ready', 'messages_unacknowledged') 13 | rates = ('ack', 'deliver', 'deliver_get', 'publish') 14 | 15 | request = urllib2.Request("http://%s:15672/api/queues" %ip) 16 | # see #issue4 17 | base64string = base64.b64encode('guest:guest') 18 | request.add_header("Authorization", "Basic %s" % base64string) 19 | result = urllib2.urlopen(request) 20 | data = json.loads(result.read()) 21 | tag = '' 22 | #tag = sys.argv[1].replace('_',',').replace('.','=') 23 | 24 | p = [] 25 | for queue in data: 26 | # ready and unack 27 | msg_total = 0 28 | for key in keys: 29 | q = {} 30 | q["endpoint"] = ip 31 | q['timestamp'] = ts 32 | q['step'] = step 33 | q['counterType'] = "GAUGE" 34 | q['metric'] = 'rabbitmq.%s' % key 35 | q['tags'] = 'name=%s,%s' % (queue['name'],tag) 36 | q['value'] = int(queue[key]) 37 | msg_total += q['value'] 38 | p.append(q) 39 | 40 | # total 41 | q = {} 42 | q["endpoint"] = ip 43 | q['timestamp'] = ts 44 | q['step'] = step 45 | q['counterType'] = "GAUGE" 46 | q['metric'] = 'rabbitmq.messages_total' 47 | q['tags'] = 'name=%s,%s' % (queue['name'],tag) 48 | q['value'] = msg_total 49 | p.append(q) 50 | 51 | # rates 52 | for rate in rates: 53 | q = {} 54 | q["endpoint"] = ip 55 | q['timestamp'] = ts 56 | q['step'] = step 57 | q['counterType'] = "GAUGE" 58 | q['metric'] = 'rabbitmq.%s_rate' % rate 59 | q['tags'] = 'name=%s,%s' % (queue['name'],tag) 60 | try: 61 | q['value'] = int(queue['message_stats']["%s_details" % rate]['rate']) 62 | except: 63 | q['value'] = 0 64 | p.append(q) 65 | 66 | print json.dumps(p, indent=4) 67 | 68 | 69 | method = "POST" 70 | handler = urllib2.HTTPHandler() 71 | opener = urllib2.build_opener(handler) 72 | url = 'http://127.0.0.1:1988/v1/push' 73 | request = urllib2.Request(url, data=json.dumps(p) ) 74 | request.add_header("Content-Type",'application/json') 75 | request.get_method = lambda: method 76 | try: 77 | connection = opener.open(request) 78 | except urllib2.HTTPError,e: 79 | connection = e 80 | 81 | # check. Substitute with appropriate HTTP code. 82 | if connection.code == 200: 83 | print connection.read() 84 | else: 85 | print '{"err":1,"msg":"%s"}' % connection 86 | -------------------------------------------------------------------------------- /redis/README.md: -------------------------------------------------------------------------------- 1 | open-falcon redis监控脚本 2 | ================================ 3 | 4 | 系统需求 5 | -------------------------------- 6 | 操作系统:Linux 7 | 8 | Python >= 2.6 9 | 10 | python-simplejson 11 | 12 | 主要逻辑 13 | -------------------------------- 14 | 根据搜索到的redis配置文件,依次从文件中取出redis实例的端口号号和密码,然后使用telnetlib telnet相应端口 15 | 发送auth和info命令,解析返回结果,最后将关心的key组装成json后push到falcon-agent 16 | 17 | 汇报字段 18 | -------------------------------- 19 | | key | tag | type | note | 20 | |-----|------|------|------| 21 | |redis.connected_clients|port|GAUGE|已连接客户端的数量| 22 | |redis.blocked_clients|port|GAUGE|正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客户端的数量| 23 | |redis.used_memory|port|GAUGE|由 Redis 分配器分配的内存总量,以字节(byte)为单位| 24 | |redis.used_memory_rss|port|GAUGE| 从操作系统的角度,返回 Redis 已分配的内存总量(俗称常驻集大小)| 25 | |redis.mem_fragmentation_ratio|port|GAUGE|used_memory_rss 和 used_memory 之间的比率| 26 | |redis.total_commands_processed|port|COUNTER|采集周期内执行命令总数| 27 | |redis.rejected_connections|port|COUNTER|采集周期内拒绝连接总数| 28 | |redis.expired_keys|port|COUNTER|采集周期内过期key总数| 29 | |redis.evicted_keys|port|COUNTER|采集周期内踢出key总数| 30 | |redis.keyspace_hits|port|COUNTER|采集周期内key命中总数| 31 | |redis.keyspace_misses|port|COUNTER|采集周期内key拒绝总数| 32 | |redis.keyspace_hit_ratio|port|GAUGE|访问命中率| 33 | 34 | 如需增减字段,请修改monit_keys变量 35 | 36 | 使用方法 37 | -------------------------------- 38 | 1. 根据实际部署情况,修改有注释位置附近的配置 39 | 2. 测试: python redis-monitor.py 40 | 3. 将脚本加入crontab执行即可 41 | 42 | 43 | 授权类型:MIT 44 | -------------------------------------------------------------------------------- /redis/php_redis.php: -------------------------------------------------------------------------------- 1 | 8 | * @date 16/7/29 9 | */ 10 | 11 | $falArr = $data = array(); 12 | $conTimeout = 1; 13 | //采集数据 14 | if (extension_loaded('memcached')) { 15 | $redis = new \Redis(); 16 | if ($result = $redis->connect("192.168.0.115", 6379, $conTimeout)) { 17 | $data = $redis->info(); 18 | $redis->close(); 19 | } 20 | } 21 | //要监控的key列表 22 | $monitKeys = array( 23 | 'connected_clients' => 'GAUGE', 24 | 'blocked_clients' => 'GAUGE', 25 | 'used_memory' => 'GAUGE', 26 | 'used_memory_rss' => 'GAUGE', 27 | 'mem_fragmentation_ratio' => 'GAUGE', 28 | 'total_commands_processed' => 'COUNTER', 29 | 'rejected_connections' => 'COUNTER', 30 | 'expired_keys' => 'COUNTER', 31 | 'evicted_keys' => 'COUNTER', 32 | 'keyspace_hits' => 'COUNTER', 33 | 'keyspace_misses' => 'COUNTER', 34 | 'keyspace_hit_ratio' => 'GAUGE', 35 | ); 36 | //关联监控指标数值 37 | foreach ($monitKeys as $k => $v) { 38 | $falArr[] = array( 39 | "endpoint" => gethostname(), 40 | "metric" => "redis_" . $k, 41 | "timestamp" => time(), 42 | "step" => 60, 43 | "value" => (isset($data[$k]) ? $data[$k] : 0), 44 | "counterType" => $v, 45 | "tags" => "project=atido,group=server,name=redis,method=monitor", 46 | ); 47 | } 48 | //stdOut 49 | echo json_encode($falArr); 50 | -------------------------------------------------------------------------------- /redis/redis-monitor.py: -------------------------------------------------------------------------------- 1 | #!/bin/env python 2 | #-*- coding:utf-8 -*- 3 | 4 | __author__ = 'iambocai' 5 | 6 | import json 7 | import time 8 | import socket 9 | import os 10 | import re 11 | import sys 12 | import commands 13 | import urllib2, base64 14 | 15 | class RedisStats: 16 | # 如果你是自己编译部署到redis,请将下面的值替换为你到redis-cli路径 17 | _redis_cli = '/usr/bin/redis-cli' 18 | _stat_regex = re.compile(ur'(\w+):([0-9]+\.?[0-9]*)\r') 19 | 20 | def __init__(self, port='6379', passwd=None, host='127.0.0.1'): 21 | self._cmd = '%s -h %s -p %s info' % (self._redis_cli, host, port) 22 | if passwd not in ['', None]: 23 | self._cmd = '%s -h %s -p %s -a %s info' % (self._redis_cli, host, port, passwd) 24 | 25 | def stats(self): 26 | ' Return a dict containing redis stats ' 27 | info = commands.getoutput(self._cmd) 28 | return dict(self._stat_regex.findall(info)) 29 | 30 | 31 | def main(): 32 | ip = socket.gethostname() 33 | timestamp = int(time.time()) 34 | step = 60 35 | # inst_list中保存了redis配置文件列表,程序将从这些配置中读取port和password,建议使用动态发现的方法获得,如: 36 | # inst_list = [ i for i in commands.getoutput("find /etc/ -name 'redis*.conf'" ).split('\n') ] 37 | insts_list = [ '/etc/redis/redis.conf' ] 38 | p = [] 39 | 40 | monit_keys = [ 41 | ('connected_clients','GAUGE'), 42 | ('blocked_clients','GAUGE'), 43 | ('used_memory','GAUGE'), 44 | ('used_memory_rss','GAUGE'), 45 | ('mem_fragmentation_ratio','GAUGE'), 46 | ('total_commands_processed','COUNTER'), 47 | ('rejected_connections','COUNTER'), 48 | ('expired_keys','COUNTER'), 49 | ('evicted_keys','COUNTER'), 50 | ('keyspace_hits','COUNTER'), 51 | ('keyspace_misses','COUNTER'), 52 | ('keyspace_hit_ratio','GAUGE'), 53 | ] 54 | 55 | for inst in insts_list: 56 | port = commands.getoutput("sed -n 's/^port *\([0-9]\{4,5\}\)/\\1/p' %s" % inst) 57 | passwd = commands.getoutput("sed -n 's/^requirepass *\([^ ]*\)/\\1/p' %s" % inst) 58 | metric = "redis" 59 | endpoint = ip 60 | tags = 'port=%s' % port 61 | 62 | try: 63 | conn = RedisStats(port, passwd) 64 | stats = conn.stats() 65 | except Exception,e: 66 | continue 67 | 68 | for key,vtype in monit_keys: 69 | #一些老版本的redis中info输出的信息很少,如果缺少一些我们需要采集的key就跳过 70 | if key not in stats.keys(): 71 | continue 72 | #计算命中率 73 | if key == 'keyspace_hit_ratio': 74 | try: 75 | value = float(stats['keyspace_hits'])/(int(stats['keyspace_hits']) + int(stats['keyspace_misses'])) 76 | except ZeroDivisionError: 77 | value = 0 78 | #碎片率是浮点数 79 | elif key == 'mem_fragmentation_ratio': 80 | value = float(stats[key]) 81 | else: 82 | #其他的都采集成counter,int 83 | try: 84 | value = int(stats[key]) 85 | except: 86 | continue 87 | 88 | i = { 89 | 'Metric': '%s.%s' % (metric, key), 90 | 'Endpoint': endpoint, 91 | 'Timestamp': timestamp, 92 | 'Step': step, 93 | 'Value': value, 94 | 'CounterType': vtype, 95 | 'TAGS': tags 96 | } 97 | p.append(i) 98 | 99 | 100 | print json.dumps(p, sort_keys=True,indent=4) 101 | method = "POST" 102 | handler = urllib2.HTTPHandler() 103 | opener = urllib2.build_opener(handler) 104 | url = 'http://127.0.0.1:1988/v1/push' 105 | request = urllib2.Request(url, data=json.dumps(p) ) 106 | request.add_header("Content-Type",'application/json') 107 | request.get_method = lambda: method 108 | try: 109 | connection = opener.open(request) 110 | except urllib2.HTTPError,e: 111 | connection = e 112 | 113 | # check. Substitute with appropriate HTTP code. 114 | if connection.code == 200: 115 | print connection.read() 116 | else: 117 | print '{"err":1,"msg":"%s"}' % connection 118 | if __name__ == '__main__': 119 | proc = commands.getoutput(' ps -ef|grep %s|grep -v grep|wc -l ' % os.path.basename(sys.argv[0])) 120 | sys.stdout.flush() 121 | if int(proc) < 5: 122 | main() 123 | --------------------------------------------------------------------------------