-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcrouter.py
More file actions
103 lines (83 loc) · 2.88 KB
/
mcrouter.py
File metadata and controls
103 lines (83 loc) · 2.88 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import select
import socket
try:
import collectd
except:
import collectd_tools.collectd_mockup as collectd
# default values
config = {
'INSTANCE': 'default',
'HOST': '127.0.0.1',
'PORT': 11211,
'TIMEOUT': 200,
'PLUGIN_NAME': 'mcrouter'
}
RETURNED_VARS = {
'cmd_set_count': 'derive',
'cmd_get_count': 'derive',
'cmd_delete_count': 'derive',
'duration_us': 'gauge',
'proxy_reqs_processing': 'gauge',
'proxy_reqs_waiting': 'gauge',
'asynclog_requests': 'derive'
# 'proxy_reqs_processing': 'derive'
}
def _read_socket(arg_host, arg_port=1121):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((arg_host, int(arg_port)))
client.send("stats\n")
running = True
buffer = ""
while (running):
r, w, e = select.select([client, ], [], [], config["TIMEOUT"])
if not (r or w or e):
raise TimeoutException()
for s in r:
if (s is client):
buffer = buffer + client.recv(16384)
running = (len(buffer) == 0)
client.close()
data = buffer.decode('utf-8').split('\r\n')
return data
def mcrouter_read(arg_host, arg_port=1121, arg_timeout=1):
data_tmp = _read_socket(arg_host, arg_port)
#
# splitting, primary filtering and convert to dictonary
#
data = {}
for i in data_tmp:
for line in RETURNED_VARS.keys():
# if RETURNED_VARS.key matches something like "STAT cmd_delete_count 24972" /.*\scmd_delete_count\s.*/
if re.match(".*\s%s\s.*" % (line,), i):
tmp = i.split(' ')
data[tmp[1]] = (tmp[2], RETURNED_VARS[line])
return data
def config_callback(arg_config):
global config
# if we on the beginning of <Module mcrouter> block
# recursion for getting child values from module block
if arg_config.key == 'Module' and arg_config.values[0] == config["PLUGIN_NAME"] and arg_config.parent is None:
for i in arg_config.children:
config_callback(i)
return
known_keys = ['HOST', 'PORT', 'INSTANCE']
if arg_config.key not in known_keys:
raise Exception('Unknown config option: %s' % arg_config.key)
if config.values:
config[arg_config.key] = arg_config.values[0] # arg_config.values is tuple
collectd.info('config parse: %s => %s;' % (arg_config.key, arg_config.values[0]))
def read_callback():
data = mcrouter_read(config["HOST"], config["PORT"], config["TIMEOUT"])
for x in data:
metric = collectd.Values()
metric.plugin = config["PLUGIN_NAME"]
metric.plugin_instance = config["INSTANCE"]
metric.type = data[x][1]
metric.type_instance = x
metric.values = (data[x][0],)
metric.dispatch()
collectd.register_config(config_callback)
collectd.register_read(read_callback)