-
Notifications
You must be signed in to change notification settings - Fork 3
/
redis_plugin.py
392 lines (336 loc) · 14.8 KB
/
redis_plugin.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# -*- coding: utf-8 -*-
# redis collectd plugin - redis_plugin.py
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; only version 2 of the License is applicable.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Author:
# Lior Goikhburg <goikhburg at gmail.com >
#
# Description:
# This is a collectd plugin which runs under the Python plugin to
# collect metrics from redis.
#
# collectd:
# http://collectd.org
#
# Redis:
# http://redis.io
#
# collectd-python:
# https://collectd.org/wiki/index.php/Plugin:Python
#
# Inspired by:
#
# Michael Leinartas' Haproxy plugin:
# https://github.com/mleinart/collectd-haproxy
#
# Garret Heaton's Redis plugin:
# https://github.com/powdahound/redis-collectd-plugin
#
# Redis API handling and functions code parts were taken from:
# Abdelkader ALLAM's Desir:
# https://github.com/aallamaa/desir
import collectd
import socket
NAME = 'redis'
# map of collectd data sets to lists of data sources as defined in redis_types.db
INFO_STATS_MAP = {
'clients': ['connected_clients', 'blocked_clients'],
'connected_slaves': ['connected_slaves'],
'redis_connections': ['rejected_connections', 'total_connections_received'],
'redis_keys': ['expired_keys', 'evicted_keys'],
'keyspace': ['keyspace_misses', 'keyspace_hits'],
'last_save': ['rdb_last_bgsave_time_sec', 'aof_last_rewrite_time_sec', 'rdb_changes_since_last_save'],
'redis_memory': ['used_memory', 'used_memory_lua', 'used_memory_peak',
'used_memory_rss', 'mem_fragmentation_ratio'],
'commands_per_sec': ['instantaneous_ops_per_sec'],
'pubsub': ['pubsub_channels', 'pubsub_patterns'],
'uptime_in_seconds': ['uptime_in_seconds'],
'cpu_used': ['used_cpu_user_children', 'used_cpu_sys', 'used_cpu_sys_children', 'used_cpu_user'],
'expires': ['expires'],
'total': ['keys'],
'avg_ttl': ['avg_ttl'],
'calls': ['calls'],
'usec_per_call': ['usec_per_call']}
METRIC_DELIM = '.'
def logger(level, message):
if level == 'err':
collectd.error("%s: %s" % (NAME, message))
elif level == 'warn':
collectd.warning("%s: %s" % (NAME, message))
elif level == 'verb':
if CONFIG_INSTANCES['root_config']['VERBOSE_LOGGING']:
collectd.info("%s: %s" % (NAME, message))
else:
collectd.notice("%s: %s" % (NAME, message))
class RedisError(Exception):
pass
class ServerError(Exception):
pass
class RedisSocket(object):
def __init__(self, socket_file=None, ip=None, port=None, auth=None):
self.socket_file = socket_file
self.ip = ip
self.port = port
self.auth = auth
self._socket = None
self._handler = None
self.endpoint = ''
def connect(self):
""" Connects to redis on UNIX Domain socket or on TCP port """
if self._handler is not None:
return
try:
if self.ip:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.connect((self.ip, self.port))
self.endpoint = "%s:%s" % (self.ip, self.port)
elif self.socket_file:
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._socket.connect(self.socket_file)
self.endpoint = self.socket_file
self._handler = self._socket.makefile()
except socket.error as error:
self.disconnect()
if len(error.args) == 1:
raise ServerError("Error connecting to redis on %s: %s." % (self.endpoint,
error.args[0]))
else:
raise ServerError("Error %s connecting to redis on %s: %s." % (error.args[0],
self.endpoint,
error.args[1]))
finally:
if self._handler is not None:
if self.auth is not None:
if not self.send_command("auth %s" % (self.auth)):
self.disconnect()
raise RedisError('Redis plugin: AUTH command failed')
# issue an "empty" command to validate connection
self.send_command('select 0')
def disconnect(self):
if self._socket:
try:
self._socket.close()
except socket.error:
pass
finally:
self._socket = None
self._handler = None
def write_line(self, message):
""" Write a single line to the socket """
self.connect()
try:
self._handler.write(message + '\r\n')
self._handler.flush()
except socket.error as error:
self.disconnect()
if len(error.args) == 1:
raise ServerError("Error sending data to redis on %s: %s" % (self.endpoint,
error.args[0]))
else:
raise ServerError("Error %s sending data to redis on %s: %s." % (error.args[0],
self.endpoint,
error.args[1]))
def read_line(self):
""" Read a line from redis and handle it according to message type """
try:
response = self._handler.readline()
except socket.error as error:
self.disconnect()
if len(error.args) == 1:
raise ServerError("Error reading data from redis on %s: %s" % (self.endpoint,
error.args[0]))
else:
raise ServerError("Error %s reading data from redis on %s: %s." % (error.args[0],
self.endpoint,
error.args[1]))
if response[:-2] in ["$-1", "*-1"]:
return None
msg_type, response = response[0], response[1:]
if msg_type == "+":
return response[:-2]
elif msg_type == "-":
raise RedisError(response.strip())
elif msg_type == "$":
try:
response = self._handler.read(int(response))
self._handler.read(2)
return response.strip()
except socket.error as error:
self.disconnect()
if len(error.args) == 1:
raise ServerError("Error reading data from redis on %s: %s" % (self.endpoint,
error.args[0]))
else:
raise ServerError("Error %s reading data from redis on %s: %s." % (error.args[0],
self.endpoint,
error.args[1]))
else:
raise RedisError("Unknown redis message type %s" % (msg_type))
def send_command(self, command):
self.write_line(command)
return self.read_line()
def info2dict(info_lines):
""" Parse response from Redis into a Dict """
metric_dict = {}
for line in info_lines.splitlines():
if not line:
continue
# sections in INFO command stat with #
if line.startswith('#'):
metric_section = line.split(' ')[1]
continue
if ':' not in line:
logger('warn', "Bad format for info line: %s" % (line))
continue
metric_name, metric_value = line.split(':')
# Handle multi-value keys (like dbs and cmdstats).
# db lines look like "db0:keys=10,expire=0"
# cmdstats lines look like "cmdstat_ping:calls=1,usec=2,usec_per_call=2.00"
if ',' in metric_value:
new_section = METRIC_DELIM.join([metric_section, metric_name])
if new_section.startswith('Commandstats'):
new_section = new_section.replace('cmdstat_', '')
if new_section not in metric_dict:
metric_dict[new_section] = {}
for sub_value in metric_value.split(','):
key, _, value = sub_value.rpartition('=')
metric_dict[new_section][key] = value
else:
if metric_section not in metric_dict:
metric_dict[metric_section] = {}
metric_dict[metric_section][metric_name] = metric_value
return metric_dict
def get_metric(metric_dict, metric_name):
""" find metric in dict, return it's value and section """
found_values = []
for section_name, section_data in metric_dict.iteritems():
if metric_name in section_data:
found_values.append((section_data[metric_name], section_name))
return found_values
def get_stats(config_data):
""" sends commands to redis and returns a dict of results to callback read function """
redis = RedisSocket(socket_file=config_data['REDIS_SOCKET'],
ip=config_data['REDIS_IP'],
port=config_data['REDIS_PORT'],
auth=config_data['REDIS_AUTH'])
data = ''
collectd_stats = {}
try:
redis.connect()
except Exception as error:
logger('err', "Could not connect to redis: %s" % (error.message))
return collectd_stats
logger('verb', "Connected to redis on %s" % (redis.endpoint))
logger('verb', "Sending 'INFO' command")
try:
data += redis.send_command('info')
except Exception as error:
logger('err', "Error while sending 'INFO' command: %s" % (error.message))
if config_data['COMMANDSTATS']:
logger('verb', "Sending 'INFO COMMANDSTATS' command")
try:
data += "\n" + redis.send_command('info commandstats')
except Exception as error:
logger('err', "Error while sending 'INFO COMMANDSTATS' command: %s" % (error.message))
metric_dict = info2dict(data)
for data_set, data_sources in INFO_STATS_MAP.iteritems():
for data_source in data_sources:
metric_data = get_metric(metric_dict, data_source)
if metric_data is not None:
for metric_value, section_name in metric_data:
if section_name not in collectd_stats:
collectd_stats[section_name] = {}
if data_set not in collectd_stats[section_name]:
collectd_stats[section_name][data_set] = []
collectd_stats[section_name][data_set].append(float(metric_value))
return collectd_stats
def get_instance_config(config_child):
""" builds per-instance config dict from collectd config data """
instance_config = {
'REDIS_SOCKET': None,
'REDIS_IP': '127.0.0.1',
'REDIS_PORT': 6379,
'REDIS_AUTH': None,
'COMMANDSTATS': False,
'VERBOSE_LOGGING': False}
for node in config_child.children:
if node.key == "Socket":
instance_config['REDIS_SOCKET'] = node.values[0]
instance_config['REDIS_IP'] = None
instance_config['REDIS_PORT'] = None
elif node.key == "IP":
instance_config['REDIS_IP'] = node.values[0]
instance_config['REDIS_SOCKET'] = None
elif node.key == "Port":
instance_config['REDIS_PORT'] = int(node.values[0])
instance_config['REDIS_SOCKET'] = None
elif node.key == "Auth":
instance_config['REDIS_AUTH'] = node.values[0]
elif node.key == "Commandstats":
instance_config['COMMANDSTATS'] = bool(node.values[0])
elif node.key == "Verbose":
instance_config['VERBOSE_LOGGING'] = bool(node.values[0])
elif node.key == "Instance":
continue
else:
logger('warn', "Unknown config key: %s" % (node.key))
return instance_config
def configure_callback(conf):
"""
process config provided by collectd config callback, handles instances
and root configs
"""
global CONFIG_INSTANCES
CONFIG_INSTANCES = {}
for node in conf.children:
if node.children:
# instance config
if node.key == 'Instance':
instance_name = node.values[0]
else:
instance_name = node.key
CONFIG_INSTANCES[instance_name] = get_instance_config(node)
else:
# root config
CONFIG_INSTANCES['root_config'] = get_instance_config(conf)
def read_callback():
""" read callback gathers data and dispatches values to collectd """
logger('verb', "beginning read_callback")
collectd_stats = {}
for instance_name, instance_config in CONFIG_INSTANCES.iteritems():
if instance_name == 'root_config':
if len(CONFIG_INSTANCES) == 1:
collectd_stats['root'] = get_stats(instance_config)
if not collectd_stats['instance']:
logger('err', 'No data received from Redis')
else:
collectd_stats[instance_name] = get_stats(instance_config)
if not collectd_stats[instance_name]:
logger('warn', "No data received from redis instance %s" % (instance_name))
for instance_name, instance_data in collectd_stats.iteritems():
for section_name, data_sets in instance_data.iteritems():
for data_set, values in data_sets.iteritems():
metric_prefix = NAME
if instance_name != 'root':
metric_prefix = METRIC_DELIM.join([metric_prefix, instance_name])
metric_prefix = METRIC_DELIM.join([metric_prefix, section_name])
logger('verb', "dispatching values - plugin_name: %s, type: %s, values: %s" % (
metric_prefix, data_set, ", ".join([str(i) for i in values])))
val = collectd.Values(plugin=metric_prefix.lower(), type=data_set)
val.values = values
val.dispatch()
# register collectd plugins
collectd.register_config(configure_callback)
collectd.register_read(read_callback)