|
|
@@ -1,34 +1,43 @@
|
|
|
import struct
|
|
|
|
|
|
-formats = {
|
|
|
- 'cpu': 'f',
|
|
|
- 'mem': 'q',
|
|
|
- 'net': 'q',
|
|
|
- 'disk': 'q',
|
|
|
-}
|
|
|
+fields = [
|
|
|
+ ('cpu', 'f', [
|
|
|
+ 'user', 'iowait', 'system', 'nice',
|
|
|
+ 'guest', 'guest_nice',
|
|
|
+ 'irq', 'softirq', 'steal', 'idle',
|
|
|
+ ]),
|
|
|
+ ('mem', 'q', ['total', 'used', 'buffers', 'cached']),
|
|
|
+ ('net', 'q', [
|
|
|
+ 'bytes_recv', 'bytes_sent',
|
|
|
+ 'dropin', 'dropout', 'errin', 'errout',
|
|
|
+ ]),
|
|
|
+ ('disk', 'q', ['total', 'used']),
|
|
|
+]
|
|
|
|
|
|
def read_stats(f):
|
|
|
- buf = f.read()
|
|
|
stats = {}
|
|
|
- index = 0
|
|
|
- while index < len(buf):
|
|
|
- # read the key
|
|
|
- key_size = ord(buf[index])
|
|
|
- fmt = '%dp' % (key_size + 1)
|
|
|
- data = struct.unpack(fmt, buf[index:index+key_size+1])
|
|
|
- key = data[0]
|
|
|
+ for stat_group, format_code, subfields in fields:
|
|
|
+ field_data = {}
|
|
|
+ fmt = '1440' + format_code
|
|
|
+ if stat_group != 'disk':
|
|
|
+ size = struct.calcsize(fmt)
|
|
|
+ for field in subfields:
|
|
|
+ field_data[field] = struct.unpack(fmt, f.read(size))
|
|
|
+ else:
|
|
|
+ buf = f.read() # disk is last, so we can read everything
|
|
|
+ index = 0
|
|
|
+ while index < len(buf):
|
|
|
+ mountpoint_size = ord(buf[index])
|
|
|
+ disk_fmt = '%dp %s %s' % (mountpoint_size + 1, fmt, fmt)
|
|
|
+ size = struct.calcsize(disk_fmt)
|
|
|
+ data = struct.unpack(disk_fmt, buf[index:index+size])
|
|
|
|
|
|
- # get the format_code
|
|
|
- split = key.split('.')
|
|
|
- stat_group = split[0]
|
|
|
- format_code = formats[stat_group]
|
|
|
-
|
|
|
- # read the data
|
|
|
- fmt = '%dp 1440%s' % (key_size + 1, format_code)
|
|
|
- size = struct.calcsize(fmt)
|
|
|
- data = struct.unpack(fmt, buf[index:index+size])
|
|
|
- dict_insert(stats, split, data[1:])
|
|
|
- index += size
|
|
|
+ mountpoint = data[0]
|
|
|
+ total = data[1:1441]
|
|
|
+ used = data[1441:]
|
|
|
+ field_data[mountpoint] = {'used': used, 'total': total}
|
|
|
+ index += size
|
|
|
+ stats[stat_group] = field_data
|
|
|
return stats
|
|
|
|
|
|
def dict_insert(d, split, value):
|
|
|
@@ -39,13 +48,19 @@ def dict_insert(d, split, value):
|
|
|
d[split[0]] = value
|
|
|
|
|
|
def write_datum(f, data):
|
|
|
- for stat_group, stats in data.items():
|
|
|
- if stat_group == 'client_id':
|
|
|
- continue
|
|
|
- format_code = formats[stat_group]
|
|
|
- for stat, datum in stats.items():
|
|
|
- key = '%s.%s' % (stat_group, stat)
|
|
|
- fmt = '%dp 1440%s' % (len(key) + 1, format_code)
|
|
|
- array = [-1] * 1440
|
|
|
- array[0] = datum
|
|
|
- f.write(struct.pack(fmt, key.encode('utf-8'), *array))
|
|
|
+ for stat_group, format_code, subfields in fields:
|
|
|
+ fmt = '1440' + format_code
|
|
|
+ if stat_group == 'disk':
|
|
|
+ for mountpoint, disk_data in data['disk'].items():
|
|
|
+ mountpoint = mountpoint.encode('utf-8')
|
|
|
+ disk_fmt = '%dp %s %s' % (len(mountpoint) + 1, fmt, fmt)
|
|
|
+ total = [-1] * 1440
|
|
|
+ total[0] = disk_data['total']
|
|
|
+ used = [-1] * 1440
|
|
|
+ used[0] = disk_data['used']
|
|
|
+ f.write(struct.pack(disk_fmt, mountpoint, *(total + used)))
|
|
|
+ else:
|
|
|
+ for field in subfields:
|
|
|
+ array = [-1] * 1440
|
|
|
+ array[0] = data[stat_group][field]
|
|
|
+ f.write(struct.pack(fmt, *array))
|