| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import struct
- formats = {
- 'cpu': 'f',
- 'mem': 'q',
- 'net': 'q',
- 'disk': 'q',
- }
- 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]
- # 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
- return stats
- def dict_insert(d, split, value):
- if len(split) > 1:
- d.setdefault(split[0], {})
- dict_insert(d[split[0]], split[1:], value)
- else:
- 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))
|