Skip to content

Commit 5b0b9fd

Browse files
committed
Convert values from Kbytes to bytes and milliseconds to seconds. Let RRDtool
scale them
1 parent 9795e4f commit 5b0b9fd

1 file changed

Lines changed: 72 additions & 41 deletions

File tree

diskstat/python_modules/diskstat.py

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@
4343

4444
descriptors = []
4545

46-
logging.basicConfig(level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s", filename='/tmp/gmond.log', filemode='w')
46+
logging.basicConfig(level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s")
4747
logging.debug('starting up')
4848

4949
last_update = 0
5050
cur_time = 0
5151
stats = {}
5252
last_val = {}
5353

54-
MAX_UPDATE_TIME = 30
54+
MAX_UPDATE_TIME = 15
5555
BYTES_PER_SECTOR = 512
5656

5757
# 5 GB
@@ -103,6 +103,32 @@ def get_partitions():
103103
logging.debug('success getting partitions')
104104
return 0
105105

106+
107+
###########################################################################
108+
# This is the order of metrics in /proc/diskstats
109+
# 0 major Major number
110+
# 1 minor Minor number
111+
# 2 blocks Blocks
112+
# 3 name Name
113+
# 4 reads This is the total number of reads completed successfully.
114+
# 5 merge_read Reads and writes which are adjacent to each other may be merged for
115+
# efficiency. Thus two 4K reads may become one 8K read before it is
116+
# ultimately handed to the disk, and so it will be counted (and queued)
117+
# as only one I/O. This field lets you know how often this was done.
118+
# 6 s_read This is the total number of sectors read successfully.
119+
# 7 ms_read This is the total number of milliseconds spent by all reads.
120+
# 8 writes This is the total number of writes completed successfully.
121+
# 9 merge_write Reads and writes which are adjacent to each other may be merged for
122+
# efficiency. Thus two 4K reads may become one 8K read before it is
123+
# ultimately handed to the disk, and so it will be counted (and queued)
124+
# as only one I/O. This field lets you know how often this was done.
125+
# 10 s_write This is the total number of sectors written successfully.
126+
# 11 ms_write This is the total number of milliseconds spent by all writes.
127+
# 12 ios The only field that should go to zero. Incremented as requests are
128+
# given to appropriate request_queue_t and decremented as they finish.
129+
# 13 ms_io This field is increases so long as field 9 is nonzero.
130+
# 14 ms_weighted This field is incremented at each I/O start, I/O completion, I/O
131+
###########################################################################
106132
def update_stats():
107133
logging.debug('updating stats')
108134
global last_update, stats, last_val, cur_time
@@ -148,15 +174,15 @@ def update_stats():
148174
get_diff(dev, 'reads_merged', int(vals[4]))
149175
get_diff(dev, 'writes_merged', int(vals[8]))
150176

151-
get_delta(dev, 'read_kbytes_per_sec', int(vals[5]), float(BYTES_PER_SECTOR) / 1024)
152-
get_delta(dev, 'write_kbytes_per_sec', int(vals[9]), float(BYTES_PER_SECTOR) / 1024)
177+
get_delta(dev, 'read_bytes_per_sec', int(vals[5]), float(BYTES_PER_SECTOR) )
178+
get_delta(dev, 'write_bytes_per_sec', int(vals[9]), float(BYTES_PER_SECTOR) )
153179

154-
get_diff(dev, 'read_time', int(vals[6]))
155-
get_diff(dev, 'write_time', int(vals[10]))
180+
get_delta(dev, 'read_time', float(vals[6]), 0.001 )
181+
get_delta(dev, 'write_time', float(vals[10]), 0.001 )
156182

157-
get_diff(dev, 'io_time', int(vals[12]))
158-
get_percent_time(dev, 'percent_io_time', int(stats[dev]['io_time']))
159-
get_diff(dev, 'weighted_io_time', int(vals[13]))
183+
get_delta(dev, 'io_time', float(vals[12]), 0.001)
184+
get_percent_time(dev, 'percent_io_time', float(stats[dev]['io_time']))
185+
get_delta(dev, 'weighted_io_time', float(vals[13]), 0.001)
160186

161187

162188
logging.debug('success refreshing stats')
@@ -181,7 +207,7 @@ def get_delta(dev, key, val, convert=1):
181207
logging.debug(' fixing int32 wrap')
182208
val += 4294967296
183209

184-
stats[dev][key] = int((val - last_val[dev][key]) * float(convert) / float(interval))
210+
stats[dev][key] = (val - last_val[dev][key]) * float(convert) / float(interval)
185211
else:
186212
stats[dev][key] = 0
187213

@@ -191,10 +217,10 @@ def get_percent_time(dev, key, val):
191217
logging.debug(' get_percent_time for ' + dev + '_' + key)
192218
global stats, last_val
193219

194-
interval = (cur_time - last_update) * 1000
220+
interval = cur_time - last_update
195221

196222
if interval > 0:
197-
stats[dev][key] = round(((val / interval) * 100),2)
223+
stats[dev][key] = (val / interval) * 100
198224
else:
199225
stats[dev][key] = 0
200226

@@ -259,13 +285,13 @@ def metric_init(params):
259285
'units': 'reads',
260286
'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.'},
261287

262-
read_kbytes_per_sec = {
263-
'units': 'Kbytes/sec',
264-
'description': 'The number of Kbytes read per second'},
288+
read_bytes_per_sec = {
289+
'units': 'bytes/sec',
290+
'description': 'The number of bytes read per second'},
265291

266292
read_time = {
267-
'units': 'ms',
268-
'description': 'The time in milliseconds spent reading'},
293+
'units': 's',
294+
'description': 'The time in seconds spent reading'},
269295

270296
writes = {
271297
'units': 'writes',
@@ -275,17 +301,17 @@ def metric_init(params):
275301
'units': 'writes',
276302
'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.'},
277303

278-
write_kbytes_per_sec = {
279-
'units': 'Kbytes/sec',
280-
'description': 'The number of Kbytes written per second'},
304+
write_bytes_per_sec = {
305+
'units': 'bytes/sec',
306+
'description': 'The number of bbytes written per second'},
281307

282308
write_time = {
283-
'units': 'ms',
284-
'description': 'The time in milliseconds spent writing'},
309+
'units': 's',
310+
'description': 'The time in seconds spent writing'},
285311

286312
io_time = {
287-
'units': 'ms',
288-
'description': 'The time in milliseconds spent in I/O operations'},
313+
'units': 's',
314+
'description': 'The time in seconds spent in I/O operations'},
289315

290316
percent_io_time = {
291317
'units': 'percent',
@@ -294,8 +320,8 @@ def metric_init(params):
294320
'description': 'The percent of disk time spent on I/O operations'},
295321

296322
weighted_io_time = {
297-
'units': 'ms',
298-
'description': 'The weighted time in milliseconds 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 milliseconds spent doing I/O.'}
323+
'units': 's',
324+
'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.'}
299325
)
300326

301327
update_stats()
@@ -308,10 +334,10 @@ def metric_init(params):
308334
'name': 'diskstat_' + dev + '_' + label,
309335
'call_back': get_stat,
310336
'time_max': time_max,
311-
'value_type': 'uint',
337+
'value_type': 'float',
312338
'units': '',
313339
'slope': 'both',
314-
'format': '%u',
340+
'format': '%f',
315341
'description': label,
316342
'groups': 'diskstat'
317343
}
@@ -353,18 +379,23 @@ def metric_cleanup():
353379
'devices': options.devices,
354380
})
355381

356-
for d in descriptors:
357-
v = d['call_back'](d['name'])
358-
if not options.quiet:
359-
print ' %s: %s %s [%s]' % (d['name'], v, d['units'], d['description'])
360-
361-
if options.gmetric:
362-
if d['value_type'] == 'uint':
363-
value_type = 'uint32'
364-
else:
365-
value_type = d['value_type']
382+
while True:
383+
for d in descriptors:
384+
v = d['call_back'](d['name'])
385+
if not options.quiet:
386+
print ' %s: %s %s [%s]' % (d['name'], v, d['units'], d['description'])
387+
388+
if options.gmetric:
389+
if d['value_type'] == 'uint':
390+
value_type = 'uint32'
391+
else:
392+
value_type = d['value_type']
393+
394+
cmd = "%s --conf=%s --value='%s' --units='%s' --type='%s' --name='%s' --slope='%s'" % \
395+
(options.gmetric_bin, options.gmond_conf, v, d['units'], value_type, d['name'], d['slope'])
396+
os.system(cmd)
397+
398+
print 'Sleeping 15 seconds'
399+
time.sleep(15)
366400

367-
cmd = "%s --conf=%s --value='%s' --units='%s' --type='%s' --name='%s' --slope='%s'" % \
368-
(options.gmetric_bin, options.gmond_conf, v, d['units'], value_type, d['name'], d['slope'])
369-
os.system(cmd)
370401

0 commit comments

Comments
 (0)