fileio.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import copy
  2. import struct
  3. fields = [
  4. ('cpu', 'f', [
  5. 'num_cpus',
  6. 'user', 'iowait', 'system', 'nice',
  7. 'guest', 'guest_nice',
  8. 'irq', 'softirq', 'steal', 'idle',
  9. ]),
  10. ('mem', 'q', ['total', 'used', 'buffers', 'cached']),
  11. ('net', 'q', [
  12. 'bytes_recv', 'bytes_sent',
  13. 'dropin', 'dropout', 'errin', 'errout',
  14. ]),
  15. ('disk', 'q', ['read_bytes', 'write_bytes', 'total', 'used']),
  16. ]
  17. def gen_template(val):
  18. template = {}
  19. for stat_group, _, subfields in fields:
  20. field_data = {}
  21. if stat_group == 'disk':
  22. field_data =
  23. else:
  24. for field in subfields:
  25. field_data[field] = copy.copy(val)
  26. template[stat_group] = field_data
  27. return template
  28. TEMPLATE = gen_template([-1] * 1440)
  29. def read_stats(f):
  30. stats = {}
  31. for stat_group, format_code, subfields in fields:
  32. field_data = {}
  33. fmt = '1440' + format_code
  34. if stat_group != 'disk':
  35. size = struct.calcsize(fmt)
  36. for field in subfields:
  37. field_data[field] = struct.unpack(fmt, f.read(size))
  38. else:
  39. buf = f.read() # disk is last, so we can read everything
  40. index = 0
  41. while index < len(buf):
  42. mountpoint_size = ord(buf[index])
  43. disk_fmt = '%dp %s %s' % (mountpoint_size + 1, fmt, fmt)
  44. size = struct.calcsize(disk_fmt)
  45. data = struct.unpack_from(disk_fmt, buf, index)
  46. mountpoint = data[0]
  47. total = data[1:1441]
  48. used = data[1441:]
  49. field_data[mountpoint] = {'used': used, 'total': total}
  50. index += size
  51. stats[stat_group] = field_data
  52. return stats
  53. def dict_insert(d, split, value):
  54. if len(split) > 1:
  55. d.setdefault(split[0], {})
  56. dict_insert(d[split[0]], split[1:], value)
  57. else:
  58. d[split[0]] = value
  59. def write_datum(f, data):
  60. for stat_group, format_code, subfields in fields:
  61. fmt = '1440' + format_code
  62. if stat_group != 'disk':
  63. for field in subfields:
  64. array = data[stat_group][field]
  65. f.write(struct.pack(fmt, *array))
  66. else:
  67. for mountpoint, disk_data in data['disk'].iteritems():
  68. mountpoint = mountpoint.encode('utf-8')
  69. disk_fmt = '%dp %s %s' % (len(mountpoint) + 1, fmt, fmt)
  70. array = disk_data['total'] + disk_data['used']
  71. f.write(struct.pack(disk_fmt, mountpoint, *array))