-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached_stats.py
executable file
·83 lines (63 loc) · 2.35 KB
/
memcached_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
#!/usr/bin/env python
"""
Rackspace Cloud Monitoring plugin to provide memcached statistics.
Copyright 2013 Steve Katen <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Minimal Example criteria:
if (metric['legacy_state'] != 'ok') {
return new AlarmStatus(CRITICAL, 'memcache is NOT running.');
}
return new AlarmStatus(OK, 'memcache is running.');
"""
import sys
import telnetlib
import re
import socket
def memcached_stats(host, port):
regex = re.compile(ur"STAT (.*) (.*)\r")
try:
c = telnetlib.Telnet(host, port)
except socket.error:
return
else:
c.write("stats\n")
return dict(regex.findall(c.read_until('END')))
def hit_percent(hits, misses):
total = hits + misses
if total > 0:
return 100 * float(hits) / float(total)
else:
return 0.0
def fill_percent(used, total):
return 100 * float(used) / float(total)
def main():
if len(sys.argv) != 3:
print "Usage: %s <host> <port>" % sys.argv[0]
sys.exit(0)
host = sys.argv[1]
port = sys.argv[2]
s = memcached_stats(host, port)
if not s:
print "status err unable to generate statistics"
sys.exit(0)
print "status ok memcached statistics generated"
print "metric uptime int", s['uptime']
print "metric curr_connections int", s['curr_connections']
print "metric listen_disabled_num int", s['listen_disabled_num']
print "metric curr_items int", s['curr_items']
print "metric total_items int", s['total_items']
print "metric evictions int", s['evictions']
print "metric hit_percent float", hit_percent(int(s['get_hits']),
int(s['get_misses']))
print "metric fill_percent float", fill_percent(int(s['bytes']),
int(s['limit_maxbytes']))
if __name__ == '__main__':
main()