-
Notifications
You must be signed in to change notification settings - Fork 29
/
getMemcachedInfo.py
107 lines (92 loc) · 3.05 KB
/
getMemcachedInfo.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
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import getopt, sys
from telnetlib import Telnet
# default memcached server to check
memcachedServer = '127.0.0.1'
memcachedPort = '11211'
ITEMS = (
'bytes',
'cmd_get',
'cmd_set',
'curr_items',
'curr_connections',
'evictions',
'limit_maxbytes',
'uptime',
'get_hits',
'get_misses',
'version',
'bytes_read',
'bytes_written',
)
################################################################################
### This is based in Enrico Tröger sources from:
### http://www.pending.io/yet-another-zabbix-template-to-monitor-memcache/
### but I chose to make it with dictionaries instead of objects.
################################################################################
class MemcachedStatsReader(object):
#----------------------------------------------------------------------
def __init__(self, server, port):
self._server = server
self._port = port
self._stats_raw = None
self._stats = None
#----------------------------------------------------------------------
def read(self):
self._read_stats()
self._parse_stats()
return self._stats
#----------------------------------------------------------------------
def _read_stats(self):
connection = Telnet(self._server, self._port, timeout=30)
connection.write('stats\n')
connection.write('quit\n')
self._stats_raw = connection.read_all()
#----------------------------------------------------------------------
def _parse_stats(self):
self._stats = {}
for line in self._stats_raw.splitlines():
if not line.startswith('STAT'):
continue
parts = line.split()
if not parts[1] in ITEMS:
continue
index = parts[1]
self._stats[index] = parts[2]
try:
ratio = float (self._stats["get_hits"]) * 100 / float (self._stats["cmd_get"])
except ZeroDivisionError:
ratio = 0.0
self._stats["ratio"] = round (ratio, 2)
try:
usage = float (self._stats["bytes"]) * 100 / float (self._stats["limit_maxbytes"])
except ZeroDivisionError:
usage = 0.0
self._stats["usage"] = round (usage, 2)
#----------------------------------------------------------------------
def Usage ():
print "Usage: getMemcachedInfo.py -h 127.0.0.1 -p 11211 -a <item>"
sys.exit(2)
def main(host, port):
getInfo = "ratio"
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "h:p:a:")
for opt,arg in opts:
if opt == '-h':
host = arg
if opt == '-p':
port = arg
if opt == '-a':
getInfo = arg
except:
Usage()
data = MemcachedStatsReader(host, port)
items = data.read()
try:
print items[getInfo]
except:
print "Not valid item."
if __name__ == '__main__':
main(memcachedServer, memcachedPort)