forked from deniszh/collectd-iostat-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectd_iostat_python.py
329 lines (278 loc) · 10.6 KB
/
collectd_iostat_python.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
#!/usr/bin/env python
# coding=utf-8
#
# collectd-iostat-python
# ======================
#
# Collectd-iostat-python is an iostat plugin for collectd that allows you to
# graph Linux iostat metrics in Graphite or other output formats that are
# supported by collectd.
#
# https://github.com/powdahound/redis-collectd-plugin
# - was used as template
# https://github.com/keirans/collectd-iostat/
# - was used as inspiration and contains some code from
# https://bitbucket.org/jakamkon/python-iostat
# - by Kuba Kończyk <jakamkon at users.sourceforge.net>
#
import signal
import string
import subprocess
import sys
__version__ = '0.0.3'
__author__ = '[email protected]'
class IOStatError(Exception):
pass
class CmdError(IOStatError):
pass
class ParseError(IOStatError):
pass
class IOStat(object):
def __init__(self, path='/usr/bin/iostat', interval=2, count=2, disks=[]):
self.path = path
self.interval = interval
self.count = count
self.disks = disks
def parse_diskstats(self, input):
"""
Parse iostat -d and -dx output.If there are more
than one series of statistics, get the last one.
By default parse statistics for all avaliable block devices.
@type input: C{string}
@param input: iostat output
@type disks: list of C{string}s
@param input: lists of block devices that
statistics are taken for.
@return: C{dictionary} contains per block device statistics.
Statistics are in form of C{dictonary}.
Main statistics:
tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
Extended staistics (available with post 2.5 kernels):
rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz \
avgqu-sz await svctm %util
See I{man iostat} for more details.
"""
dstats = {}
dsi = input.rfind('Device:')
if dsi == -1:
raise ParseError('Unknown input format: %r' % input)
ds = input[dsi:].splitlines()
hdr = ds.pop(0).split()[1:]
for d in ds:
if d:
d = d.split()
dev = d.pop(0)
if (dev in self.disks) or not self.disks:
dstats[dev] = dict([(k, float(v)) for k, v in zip(hdr, d)])
return dstats
def sum_dstats(self, stats, smetrics):
"""
Compute the summary statistics for chosen metrics.
"""
avg = {}
for disk, metrics in stats.iteritems():
for mname, metric in metrics.iteritems():
if mname not in smetrics:
continue
if mname in avg:
avg[mname] += metric
else:
avg[mname] = metric
return avg
def _run(self, options=None):
"""
Run iostat command.
"""
close_fds = 'posix' in sys.builtin_module_names
args = '%s %s %s %s %s' % (
self.path,
''.join(options),
self.interval,
self.count,
' '.join(self.disks))
return subprocess.Popen(
args,
bufsize=1,
shell=True,
stdout=subprocess.PIPE,
close_fds=close_fds)
@staticmethod
def _get_childs_data(child):
"""
Return child's data when avaliable.
"""
(stdout, stderr) = child.communicate()
ecode = child.poll()
if ecode != 0:
raise CmdError('Command %r returned %d' % (child.cmd, ecode))
return stdout
def get_diskstats(self):
"""
Get all avaliable disks statistics that we can get.
"""
dstats = self._run(options=['-kNd'])
extdstats = self._run(options=['-kNdx'])
dsd = self._get_childs_data(dstats)
edd = self._get_childs_data(extdstats)
ds = self.parse_diskstats(dsd)
eds = self.parse_diskstats(edd)
for dk, dv in ds.iteritems():
if dk in eds:
ds[dk].update(eds[dk])
return ds
class IOMon(object):
def __init__(self):
self.plugin_name = 'collectd-iostat-python'
self.iostat_path = '/usr/bin/iostat'
self.iostat_interval = 2
self.iostat_count = 2
self.iostat_disks = []
self.iostat_nice_names = False
self.iostat_disks_regex = ''
self.verbose_logging = False
self.names = {
'tps': {'t': 'transfers_per_second'},
'Blk_read/s': {'t': 'blocks_per_second', 'ti': 'read'},
'kB_read/s': {'t': 'bytes_per_second', 'ti': 'read', 'm': 10e3},
'MB_read/s': {'t': 'bytes_per_second', 'ti': 'read', 'm': 10e6},
'Blk_wrtn/s': {'t': 'blocks_per_second', 'ti': 'write'},
'kB_wrtn/s': {'t': 'bytes_per_second', 'ti': 'write', 'm': 10e3},
'MB_wrtn/s': {'t': 'bytes_per_second', 'ti': 'write', 'm': 10e6},
'Blk_read': {'t': 'blocks', 'ti': 'read'},
'kB_read': {'t': 'bytes', 'ti': 'read', 'm': 10e3},
'MB_read': {'t': 'bytes', 'ti': 'read', 'm': 10e6},
'Blk_wrtn': {'t': 'blocks', 'ti': 'write'},
'kB_wrtn': {'t': 'bytes', 'ti': 'write', 'm': 10e3},
'MB_wrtn': {'t': 'bytes', 'ti': 'write', 'm': 10e6},
'rrqm/s': {'t': 'requests_merged_per_second', 'ti': 'read'},
'wrqm/s': {'t': 'requests_merged_per_second', 'ti': 'write'},
'r/s': {'t': 'per_second', 'ti': 'read'},
'w/s': {'t': 'per_second', 'ti': 'write'},
'rsec/s': {'t': 'sectors_per_second', 'ti': 'read'},
'rkB/s': {'t': 'bytes_per_second', 'ti': 'read', 'm': 10e3},
'rMB/s': {'t': 'bytes_per_second', 'ti': 'read', 'm': 10e6},
'wsec/s': {'t': 'sectors_per_second', 'ti': 'write'},
'wkB/s': {'t': 'bytes_per_second', 'ti': 'write', 'm': 10e3},
'wMB/s': {'t': 'bytes_per_second', 'ti': 'write', 'm': 10e6},
'avgrq-sz': {'t': 'avg_request_size'},
'avgqu-sz': {'t': 'avg_request_queue'},
'await': {'t': 'avg_wait_time'},
'r_await': {'t': 'avg_wait_time', 'ti': 'read'},
'w_await': {'t': 'avg_wait_time', 'ti': 'write'},
'svctm': {'t': 'avg_service_time'},
'%util': {'t': 'percent', 'ti': 'util'}
}
def log_verbose(self, msg):
if not self.verbose_logging:
return
collectd.info('%s plugin [verbose]: %s' % (self.plugin_name, msg))
def configure_callback(self, conf):
"""
Receive configuration block
"""
for node in conf.children:
val = str(node.values[0])
if node.key == 'Path':
self.iostat_path = val
elif node.key == 'Interval':
self.iostat_interval = int(float(val))
elif node.key == 'Count':
self.iostat_count = int(float(val))
elif node.key == 'Disks':
self.iostat_disks = val.split(',')
elif node.key == 'NiceNames':
self.iostat_nice_names = val in ['True', 'true']
elif node.key == 'DisksRegex':
self.iostat_disks_regex = val
elif node.key == 'PluginName':
self.plugin_name = val
elif node.key == 'Verbose':
self.verbose_logging = val in ['True', 'true']
else:
collectd.warning(
'%s plugin: Unknown config key: %s.' % (
self.plugin_name,
node.key))
self.log_verbose(
'Configured with iostat=%s, interval=%s, count=%s, disks=%s, '
'disks_regex=%s' % (
self.iostat_path,
self.iostat_interval,
self.iostat_count,
self.iostat_disks,
self.iostat_disks_regex))
def dispatch_value(self, plugin_instance, val_type, type_instance, value):
"""
Dispatch a value to collectd
"""
self.log_verbose(
'Sending value: %s-%s.%s=%s' % (
self.plugin_name,
plugin_instance,
'-'.join([val_type, type_instance]),
value))
val = collectd.Values()
val.plugin = self.plugin_name
val.plugin_instance = plugin_instance
val.type = val_type
if len(type_instance):
val.type_instance = type_instance
val.values = [value, ]
val.meta={'0': True}
val.dispatch()
def read_callback(self):
"""
Collectd read callback
"""
self.log_verbose('Read callback called')
iostat = IOStat(
path=self.iostat_path,
interval=self.iostat_interval,
count=self.iostat_count,
disks=self.iostat_disks)
ds = iostat.get_diskstats()
if not ds:
self.log_verbose('%s plugin: No info received.' % self.plugin_name)
return
for disk in ds:
for name in ds[disk]:
if self.iostat_nice_names and name in self.names:
val_type = self.names[name]['t']
if 'ti' in self.names[name]:
type_instance = self.names[name]['ti']
else:
type_instance = ''
value = ds[disk][name]
if 'm' in self.names[name]:
value *= self.names[name]['m']
else:
val_type = 'gauge'
tbl = string.maketrans('/-%', '___')
type_instance = name.translate(tbl)
value = ds[disk][name]
self.dispatch_value(
disk, val_type, type_instance, value)
def restore_sigchld():
"""
Restore SIGCHLD handler for python <= v2.6
It will BREAK exec plugin!!!
See https://github.com/deniszh/collectd-iostat-python/issues/2 for details
"""
if sys.version_info[0] == 2 and sys.version_info[1] <= 6:
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
if __name__ == '__main__':
iostat = IOStat()
ds = iostat.get_diskstats()
for disk in ds:
for metric in ds[disk]:
tbl = string.maketrans('/-%', '___')
metric_name = metric.translate(tbl)
print("%s.%s:%s" % (disk, metric_name, ds[disk][metric]))
sys.exit(0)
else:
import collectd
iomon = IOMon()
# Register callbacks
collectd.register_init(restore_sigchld)
collectd.register_config(iomon.configure_callback)
collectd.register_read(iomon.read_callback)