-
Notifications
You must be signed in to change notification settings - Fork 0
/
twemproxy.py
executable file
·191 lines (161 loc) · 7.52 KB
/
twemproxy.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#! /usr/bin/env python
import collectd
import socket
import json
from datetime import timedelta
class NutcrackerServer(object):
def __init__(self):
self.server = '127.0.0.1'
self.port = '22222'
def submit(self, type, instance, value, server=None):
if server:
plugin_instance = '%s-%s' % (self.port, server)
else:
plugin_instance = str(self.port)
v = collectd.Values()
v.plugin = self.plugin_name
v.plugin_instance = plugin_instance
v.type = type
v.type_instance = instance
v.values = [value, ]
v.dispatch()
def do_twemproxy_status(self):
conn = socket.create_connection((self.server, self.port))
buf = True
content = ''
while buf:
buf = conn.recv(1024)
content += buf
conn.close()
# print "content : %s" % (content)
self.data = json.loads(content)
for k in sorted(self.data.keys()):
try:
v = self.data[k]
# just to prove we are looking at a key for a backend server
v['server_ejects']
# dispatch([type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = 'client_connections'
# metric.plugin_instance = 'client_connections'
metric.type = 'tcp_connections'
metric.values = [str(v['client_connections'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = 'client_eof'
metric.type = 'derive'
metric.values = [str(v['client_eof'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = 'forward_error'
metric.type = 'derive'
metric.values = [str(v['forward_error'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = 'client_err'
metric.type = 'derive'
metric.values = [str(v['client_err'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = 'fragments'
metric.type = 'derive'
metric.values = [str(v['fragments'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = 'server_ejects'
metric.type = 'derive'
metric.values = [str(v['server_ejects'])]
metric.dispatch()
for bk in v.keys():
if type(v[bk]) is dict:
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-server_eof' % bk
metric.type = 'derive'
metric.values = [str(v[bk]['server_eof'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-server_err' % bk
metric.type = 'derive'
metric.values = [str(v[bk]['server_err'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-server_connections' % bk
metric.type = 'gauge'
metric.values = [str(v[bk]['server_connections'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-server_timedout' % bk
metric.type = 'derive'
metric.values = [str(v[bk]['server_timedout'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-responses' % bk
metric.type = 'counter'
metric.values = [str(v[bk]['responses'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-response_bytes' % bk
metric.type = 'total_bytes'
metric.values = [str(v[bk]['response_bytes'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-in_queue_bytes' % bk
metric.type = 'gauge'
metric.values = [str(v[bk]['in_queue_bytes'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-out_queue_bytes' % bk
metric.type = 'gauge'
metric.values = [str(v[bk]['out_queue_bytes'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-request_bytes' % bk
metric.type = 'derive'
metric.values = [str(v[bk]['request_bytes'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-requests' % bk
metric.type = 'derive'
metric.values = [str(v[bk]['requests'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-in_queue' % bk
metric.type = 'gauge'
metric.values = [str(v[bk]['in_queue'])]
metric.dispatch()
metric = collectd.Values()
metric.plugin = 'twemproxy-%s' % k
metric.type_instance = '%s-out_queue' % bk
metric.type = 'gauge'
metric.values = [str(v[bk]['out_queue'])]
metric.dispatch()
except:
pass
def config(self, obj):
for node in obj.children:
if node.key == 'Port':
self.port = int(node.values[0])
elif node.key == 'Host':
self.server = node.values[0]
else:
collectd.warning("twemproxy plugin: Unkown configuration key %s" % node.key)
twemproxy = NutcrackerServer()
collectd.register_config(twemproxy.config)
collectd.register_read(twemproxy.do_twemproxy_status)