forked from ganglia/gmond_python_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskstat.py
More file actions
518 lines (409 loc) · 14.8 KB
/
diskstat.py
File metadata and controls
518 lines (409 loc) · 14.8 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
### This script reports disk stat metrics to ganglia.
###
### Notes:
### This script exposes values in /proc/diskstats and calculates
### various statistics based on the Linux kernel 2.6. To find
### more information on these values, look in the Linux kernel
### documentation for "I/O statistics fields".
###
### This script has the option of explicitly setting which devices
### to check using the "devices" option in your configuration. If
### you set this value, it will invalidate the MIN_DISK_SIZE and
### IGNORE_DEV options described below. This enables you to
### monitor specific partitions instead of the entire device.
### Example value: "sda1 sda2".
### Example value: "sda sdb sdc".
###
### This script also checks for a minimum disk size in order to
### only measure interesting devices by default.
### [Can be overriden if "devices" is set]
###
### This script looks for disks to check in /proc/partitions while
### ignoring any devices present in IGNORE_DEV by default.
### [Can be overriden if "devices" is set]
###
### Changelog:
### v1.0.1 - 2010-07-22
### * Initial version
###
### v1.0.2 - 2010-08-03
### * Modified reads_per_sec to not calculate per second delta.
### This enables us to generate a better graph by stacking
### reads/writes with reads/writes merged.
###
### Copyright Jamie Isaacs. 2010
### License to use, modify, and distribute under the GPL
### http://www.gnu.org/licenses/gpl.txt
import time
import subprocess
import traceback
import logging
import os
import stat
descriptors = []
logging.basicConfig(level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s")
logging.debug('starting up')
last_update = 0
cur_time = 0
stats = {}
last_val = {}
MAX_UPDATE_TIME = 15
BYTES_PER_SECTOR = 512
# 5 GB
MIN_DISK_SIZE = 5242880
DEVICES = ''
IGNORE_DEV = 'dm-|loop|drbd'
PARTITIONS_FILE = '/proc/partitions'
DISKSTATS_FILE = '/proc/diskstats'
DMDIR = '/dev/mapper'
device_mapper = ''
PARTITIONS = []
dmnames = []
dm2pair = {}
pair2dm = {}
devnames = []
dev2pair = {}
pair2dev = {}
def build_dmblock_major_minor_tables():
"""Returns
1) a table of filenames that are all device mapper block special files
2) a dict mapping each device mapper name to (major,minor)
3) a dict mapping each (major,minor) pair to a table of devce mapper names"""
names = []
name2pair = {}
pair2name = {}
mapper_entries = []
mapper_entries = os.listdir(DMDIR)
for n in mapper_entries:
s = os.lstat(DMDIR + '/' + n)
if stat.S_ISBLK(s[stat.ST_MODE]):
names.append(n)
maj = str(os.major(s.st_rdev))
min = str(os.minor(s.st_rdev))
name2pair[n] = (maj, min)
pair2name[(maj, min)] = n
logging.debug('grabbed dmsetup device info')
logging.debug('dmsetup devices: ' + str(name2pair))
return (names, name2pair, pair2name)
def build_block_major_minor_tables():
"""Returns
1) a table of filenames that are all block special files
2) a dict mapping each dev name to (major,minor)
3) a dict mapping each (major,minor) pair to a table of dev names"""
dnames = []
d2p = {}
p2d = {}
# Get values from diskstats file
p = subprocess.Popen("awk '{print $1,$2, $3}' " + DISKSTATS_FILE, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
logging.debug('grabbed diskstat device info')
logging.debug('diskstat devices: ' + str(out))
for n in out.split('\n'):
if n:
[maj, min, name] = n.split()
dnames.append(name)
d2p[name] = (maj, min)
p2d[(maj, min)] = name
return (dnames, d2p, p2d)
def get_devname(dev):
"""Returns
device mapper name converted to dev name"""
(maj,min) = dm2pair[dev]
name = pair2dev[(maj,min)]
return name
def list_dmnames():
"""Returns
string of device names associated with device mapper names"""
global dmnames
global dm2pair
global pair2dm
global devnames
global dev2pair
global pair2dev
devlist = ''
dmnames, dm2pair, pair2dm = build_dmblock_major_minor_tables()
logging.debug('dmnames: ' + str(dmnames))
devnames, dev2pair, pair2dev = build_block_major_minor_tables()
logging.debug('devnames: ' + str(dmnames))
for d in dmnames:
devlist = devlist + ' ' + str(d)
logging.debug('devlist: ' + str(devlist))
return devlist
def get_partitions():
logging.debug('getting partitions')
global PARTITIONS
if DEVICES != '':
# Explicit device list has been set
logging.debug(' DEVICES has already been set')
out = DEVICES
else:
# Load partitions
awk_cmd = "awk 'NR > 1 && $0 !~ /" + IGNORE_DEV + "/ && $4 !~ /[0-9]$/ {ORS=\" \"; print $4}' "
p = subprocess.Popen(awk_cmd + PARTITIONS_FILE, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
logging.debug(' result: ' + out)
if p.returncode:
logging.warning('failed getting partitions')
return p.returncode
for dev in out.split():
if DEVICES != '':
# Explicit device list has been set
PARTITIONS.append(dev)
else:
# Load disk block size
f = open('/sys/block/' + dev + '/size', 'r')
c = f.read()
f.close()
# Make sure device is large enough to collect stats
if (int(c) * BYTES_PER_SECTOR / 1024) > MIN_DISK_SIZE:
PARTITIONS.append(dev)
else:
logging.debug(' ignoring ' + dev + ' due to size constraints')
logging.debug('success getting partitions')
return 0
###########################################################################
# This is the order of metrics in /proc/diskstats
# 0 major Major number
# 1 minor Minor number
# 2 blocks Blocks
# 3 name Name
# 4 reads This is the total number of reads completed successfully.
# 5 merge_read Reads and writes which are adjacent to each other may be merged for
# efficiency. Thus two 4K reads may become one 8K read before it is
# ultimately handed to the disk, and so it will be counted (and queued)
# as only one I/O. This field lets you know how often this was done.
# 6 s_read This is the total number of sectors read successfully.
# 7 ms_read This is the total number of milliseconds spent by all reads.
# 8 writes This is the total number of writes completed successfully.
# 9 merge_write Reads and writes which are adjacent to each other may be merged for
# efficiency. Thus two 4K reads may become one 8K read before it is
# ultimately handed to the disk, and so it will be counted (and queued)
# as only one I/O. This field lets you know how often this was done.
# 10 s_write This is the total number of sectors written successfully.
# 11 ms_write This is the total number of milliseconds spent by all writes.
# 12 ios The only field that should go to zero. Incremented as requests are
# given to appropriate request_queue_t and decremented as they finish.
# 13 ms_io This field is increases so long as field 9 is nonzero.
# 14 ms_weighted This field is incremented at each I/O start, I/O completion, I/O
###########################################################################
def update_stats():
logging.debug('updating stats')
global last_update, stats, last_val, cur_time
global MAX_UPDATE_TIME
cur_time = time.time()
if cur_time - last_update < MAX_UPDATE_TIME:
logging.debug(' wait ' + str(int(MAX_UPDATE_TIME - (cur_time - last_update))) + ' seconds')
return True
#####
# Update diskstats
stats = {}
if not PARTITIONS:
part = get_partitions()
if part:
# Fail if return is non-zero
logging.warning('error getting partitions')
return False
# Get values for each disk device
for dev in PARTITIONS:
logging.debug(" dev: " + dev)
# Setup storage lists
if not dev in stats:
stats[dev] = {}
if not dev in last_val:
last_val[dev] = {}
# Convert from dmname to devname for use by awk
if device_mapper == 'true':
olddev = dev
dev = get_devname(dev)
# Get values from diskstats file
p = subprocess.Popen("awk -v dev=" + dev + " '$3 == dev' " + DISKSTATS_FILE, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
vals = out.split()
logging.debug(' vals: ' + str(vals))
# Reset back to orignal dev name
if device_mapper == 'true':
dev = olddev
get_diff(dev, 'reads', int(vals[3]))
get_diff(dev, 'writes', int(vals[7]))
get_diff(dev, 'reads_merged', int(vals[4]))
get_diff(dev, 'writes_merged', int(vals[8]))
get_delta(dev, 'read_bytes_per_sec', int(vals[5]), float(BYTES_PER_SECTOR) )
get_delta(dev, 'write_bytes_per_sec', int(vals[9]), float(BYTES_PER_SECTOR) )
get_delta(dev, 'read_time', float(vals[6]), 0.001 )
get_delta(dev, 'write_time', float(vals[10]), 0.001 )
get_diff(dev, 'io_time', float(vals[12]), 0.001)
get_percent_time(dev, 'percent_io_time', float(stats[dev]['io_time']))
get_delta(dev, 'weighted_io_time', float(vals[13]), 0.001)
logging.debug('success refreshing stats')
logging.debug('stats: ' + str(stats))
logging.debug('last_val: ' + str(last_val))
last_update = cur_time
return True
def get_delta(dev, key, val, convert=1):
logging.debug(' get_delta for ' + dev + '_' + key)
global stats, last_val
if convert == 0:
logging.warning(' convert is zero!')
interval = cur_time - last_update
if key in last_val[dev] and interval > 0:
if val < last_val[dev][key]:
logging.debug(' fixing int32 wrap')
val += 4294967296
stats[dev][key] = (val - last_val[dev][key]) * float(convert) / float(interval)
else:
stats[dev][key] = 0
last_val[dev][key] = int(val)
def get_percent_time(dev, key, val):
logging.debug(' get_percent_time for ' + dev + '_' + key)
global stats, last_val
interval = cur_time - last_update
if interval > 0:
stats[dev][key] = (val / interval) * 100
else:
stats[dev][key] = 0
def get_diff(dev, key, val, convert=1):
logging.debug(' get_diff for ' + dev + '_' + key)
global stats, last_val
if key in last_val[dev]:
stats[dev][key] = (val - last_val[dev][key]) * float(convert)
else:
stats[dev][key] = 0
last_val[dev][key] = val
def get_stat(name):
logging.debug(' getting stat: ' + name)
global stats
ret = update_stats()
if ret:
if name.startswith('diskstat_'):
fir = name.find('_')
sec = name.find('_', fir + 1)
dev = name[fir+1:sec]
label = name[sec+1:]
try:
return stats[dev][label]
except:
logging.warning('failed to fetch [' + dev + '] ' + name)
return 0
else:
label = name
try:
return stats[label]
except:
logging.warning('failed to fetch ' + name)
return 0
else:
return 0
def metric_init(params):
global descriptors, device_mapper
global MIN_DISK_SIZE, DEVICES, IGNORE_DEV
# Use params.get here to assure function via gmond
if params.get('device-mapper') == 'true':
devices = list_dmnames()
DEVICES = devices
IGNORE_DEV = 'loop|drbd'
device_mapper = 'true'
logging.debug('dm block devices: ' + str(devices))
else:
DEVICES = params.get('devices')
logging.debug('init: ' + str(params))
time_max = 60
descriptions = dict(
reads = {
'units': 'reads',
'description': 'The number of reads completed'},
reads_merged = {
'units': 'reads',
'description': 'The number of reads merged. Reads which are adjacent to each other may be merged for efficiency. Multiple reads may become one before it is handed to the disk, and it will be counted (and queued) as only one I/O.'},
read_bytes_per_sec = {
'units': 'bytes/sec',
'description': 'The number of bytes read per second'},
read_time = {
'units': 's',
'description': 'The time in seconds spent reading'},
writes = {
'units': 'writes',
'description': 'The number of writes completed'},
writes_merged = {
'units': 'writes',
'description': 'The number of writes merged. Writes which are adjacent to each other may be merged for efficiency. Multiple writes may become one before it is handed to the disk, and it will be counted (and queued) as only one I/O.'},
write_bytes_per_sec = {
'units': 'bytes/sec',
'description': 'The number of bbytes written per second'},
write_time = {
'units': 's',
'description': 'The time in seconds spent writing'},
io_time = {
'units': 's',
'description': 'The time in seconds spent in I/O operations'},
percent_io_time = {
'units': 'percent',
'value_type': 'float',
'format': '%f',
'description': 'The percent of disk time spent on I/O operations'},
weighted_io_time = {
'units': 's',
'description': 'The weighted time in seconds spend in I/O operations. This measures each I/O start, I/O completion, I/O merge, or read of these stats by the number of I/O operations in progress times the number of seconds spent doing I/O.'}
)
update_stats()
for label in descriptions:
for dev in PARTITIONS:
if stats[dev].has_key(label):
d = {
'name': 'diskstat_' + dev + '_' + label,
'call_back': get_stat,
'time_max': time_max,
'value_type': 'float',
'units': '',
'slope': 'both',
'format': '%f',
'description': label,
'groups': 'diskstat'
}
# Apply metric customizations from descriptions
d.update(descriptions[label])
descriptors.append(d)
else:
logging.error("skipped " + label)
#logging.debug('descriptors: ' + str(descriptors))
# For command line testing
#time.sleep(MAX_UPDATE_TIME)
#update_stats()
return descriptors
def metric_cleanup():
logging.shutdown()
# pass
if __name__ == '__main__':
from optparse import OptionParser
import os
logging.debug('running from cmd line')
parser = OptionParser()
parser.add_option('-d', '--devices', dest='devices', default='', help='devices to explicitly check')
parser.add_option('-b', '--gmetric-bin', dest='gmetric_bin', default='/usr/bin/gmetric', help='path to gmetric binary')
parser.add_option('-c', '--gmond-conf', dest='gmond_conf', default='/etc/ganglia/gmond.conf', help='path to gmond.conf')
parser.add_option('-g', '--gmetric', dest='gmetric', action='store_true', default=False, help='submit via gmetric')
parser.add_option('-m', '--device-mapper', dest='device_mapper', action='store_true', default=False, help='utilize all device mapper devices if set')
parser.add_option('-q', '--quiet', dest='quiet', action='store_true', default=False)
(options, args) = parser.parse_args()
if options.device_mapper:
metric_init({
'device-mapper': 'true',
})
else:
metric_init({
'devices': options.devices,
})
while True:
for d in descriptors:
v = d['call_back'](d['name'])
if not options.quiet:
print ' %s: %s %s [%s]' % (d['name'], v, d['units'], d['description'])
if options.gmetric:
if d['value_type'] == 'uint':
value_type = 'uint32'
else:
value_type = d['value_type']
cmd = "%s --conf=%s --value='%s' --units='%s' --type='%s' --name='%s' --slope='%s'" % \
(options.gmetric_bin, options.gmond_conf, v, d['units'], value_type, d['name'], d['slope'])
os.system(cmd)
print 'Sleeping 15 seconds'
time.sleep(15)