-
Notifications
You must be signed in to change notification settings - Fork 10
/
cw-redis-stats.py
97 lines (84 loc) · 4.92 KB
/
cw-redis-stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
'''
Send Redis usage metrics to Amazon CloudWatch
This is intended to run on an Amazon EC2 instance and requires a boto config
(~/.boto) allowing to write CloudWatch metrics.
'''
import redis
from boto.ec2 import cloudwatch
from boto.utils import get_instance_metadata
command_groups = {
'GetTypeCmds': ['get','getbit','getrange','getset','mget','hget','hgetall','hmget'],
'SetTypeCmds': ['set','setbit','setex','setnx','setrange','mset','msetnx','psetnx',
'hmset','hset','hsetnx','lset'],
'KeyBasedCmds': ['zdel','dump','exists','expire','expireat','keys','move','persist',
'pexpire','pexpireat','pttl','rename','renamenx','restore','ttl',
'type','append','bitcount','bitop','bitpos','decr','decrby','get',
'getbit','getrange','getset','incr','incrby','incrbyfloat','mget',
'mset','msetnx','psetnx','set','setbit','setex','setnx','setrange',
'strlen','hdel','hexists','hget','hgetall','hincrby','hincrbyfloat',
'hkeys','hlen','hmget','hmset','hset','hsetnx','hvals','blpop',
'brpop','lindex','linsert','llen','lpop','lpush','lpushx','lrange',
'lrem','lset','ltrim','rpop','rpush','rpushx','sadd','scard','sdiff',
'sdiffstore','sinter','sinterstore','sismember','smembers','spop',
'srandmember','srem','sunion','sunionstore', 'sscan','zadd','zcard',
'zcount','zincrby','zinterstore','zlexcount','zrange','zrangebylex',
'zrangebyscore','zrank','zrem','zremrangebylex','zremrangebyrank',
'zremrangebyscore','zrevrange','zrevrangebyscore','zrevrank','zscore',
'zunionstore','zscan','pfadd','pfcount','pfmerge','watch','eval',
'evalsha'],
'StringBasedCmds': ['append','bitcount','bitop','bitpos','decr','decrby','get','getbit',
'getrange','getset','incr','incrby','incrbyfloat','mget','mset',
'msetnx','psetnx','set','setbit','setex','setnx','setrange','strlen'],
'HashBasedCmds': ['hdel','hexists','hget','hgetall','hincrby','hincrbyfloat','hkeys',
'hlen','hmget','hmset','hset','hsetnx','hvals','hscan'],
'ListBasedCmds': ['blpop','brpop','brpoplpush','lindex','linsert','llen','lpop','lpush',
'lpushx','lrange','lrem','lset','ltrim','rpop','rpoplpush','rpush',
'rpushx'],
'SetBasedCmds': ['sadd','scard','sdiff','sdiffstore','sinter','sinterstore','sismember',
'smembers','smove','spop','srandmember','srem','sunion','sunionstore',
'sscan'],
'SortedSetBasedCmds': ['zadd','zcard','zcount','zincrby','zinterstore','zlexcount',
'zrange','zrangebylex','zrangebyscore','zrank','zrem',
'zremrangebylex','zremrangebyrank','zremrangebyscore','zrevrange',
'zrevrangebyscore','zrevrank','zscore','zunionstore','zscan'],
'HyperLogLogBasedCmds': ['pfadd','pfcount','pfmerge'],
'ScriptBasedCmds': ['eval','evalsha']
}
def collect_redis_info():
r = redis.StrictRedis('localhost', port=6379, db=0)
info = r.info()
cmd_info = r.info('commandstats')
return dict(info.items() + cmd_info.items())
def send_multi_metrics(instance_id, region, metrics, unit='Count', namespace='EC2/Redis'):
cw = cloudwatch.connect_to_region(region)
cw.put_metric_data(namespace, metrics.keys(), metrics.values(),
unit=unit, dimensions={"InstanceId": instance_id})
if __name__ == '__main__':
metadata = get_instance_metadata()
instance_id = metadata['instance-id']
region = metadata['placement']['availability-zone'][0:-1]
redis_data = collect_redis_info()
count_metrics = {
'CurrConnections': redis_data['connected_clients'],
'Evictions': redis_data['evicted_keys'],
'Reclaimed': redis_data['expired_keys'],
'CacheHits': redis_data['keyspace_hits'],
'CacheMisses': redis_data['keyspace_misses'],
'UsedMemory': redis_data['used_memory'],
'IOPS': redis_data['instantaneous_ops_per_sec'],
'InputKbps': redis_data['instantaneous_input_kbps'],
'OutputKbps': redis_data['instantaneous_output_kbps'],
}
count_metrics['CurrItems'] = sum([value['keys'] for key, value in redis_data.items() if key.startswith('db')])
for command_group, commands in command_groups.items():
count_metrics[command_group] = 0
for command in commands:
key = 'cmdstat_' + command
if key in redis_data:
count_metrics[command_group] += redis_data[key]['calls']
byte_metrics = {
'BytesUsedForCache': redis_data['used_memory'],
}
send_multi_metrics(instance_id, region, count_metrics)
send_multi_metrics(instance_id, region, byte_metrics, 'Bytes')