Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. Profile system performance
  5. Created by Niall Kavanagh <ntk@restdevices.com> on 2/5/2015
  6. """
  7.  
  8. from __future__ import print_function
  9. import argparse
  10. import time
  11. import datetime
  12. import re
  13.  
  14.  
  15. def profile_memory():
  16. profile = {}
  17.  
  18. pattern = re.compile('^(.+):\s*(.+)$')
  19.  
  20. with open('/proc/meminfo') as f:
  21. for line in f:
  22. match = pattern.match(line)
  23. if match:
  24. profile[match.group(1)] = match.group(2)
  25.  
  26. return profile
  27.  
  28.  
  29. def profile_load():
  30. profile = {}
  31.  
  32. pattern = re.compile('^(\S+)\s+(\S+)\s+(\S+)\s+')
  33.  
  34. with open('/proc/loadavg') as f:
  35. for line in f:
  36. match = pattern.match(line)
  37. if match:
  38. profile['LoadAvg1'] = match.group(1)
  39. profile['LoadAvg5'] = match.group(2)
  40. profile['LoadAvg15'] = match.group(3)
  41.  
  42. return profile
  43.  
  44.  
  45. def profile_processes():
  46. profile = {}
  47.  
  48. # this pattern will not match lot of multi-field lines
  49. pattern = re.compile('^(\S+)\s+(\d+)$')
  50.  
  51. with open('/proc/stat') as f:
  52. for line in f:
  53. match = pattern.match(line)
  54. if match:
  55. profile[match.group(1)] = match.group(2)
  56.  
  57. return profile
  58.  
  59.  
  60. def profile_system(metrics):
  61. memory = profile_memory()
  62. load = profile_load()
  63. processes = profile_processes()
  64.  
  65. profiles = [memory, load, processes]
  66.  
  67. ts = time.time()
  68. profile = {
  69. 'Time': datetime.datetime.fromtimestamp(ts)
  70. .strftime('%Y-%m-%d %H:%M:%S')
  71. }
  72.  
  73. for metric in metrics:
  74. for p in profiles:
  75. if metric in p:
  76. profile[metric] = p[metric]
  77.  
  78. return profile
  79.  
  80.  
  81. def main():
  82. # default profile metrics TODO arg for this
  83. metrics = [
  84. 'Time',
  85. 'MemTotal',
  86. 'MemFree',
  87. 'LoadAvg1',
  88. 'procs_running',
  89. 'procs_blocked'
  90. ]
  91.  
  92. # command line args
  93. parser = argparse.ArgumentParser(description='Profiles system performance \
  94. and outputs CSV.')
  95.  
  96. parser.add_argument('--duration', default=60, type=int,
  97. help='number of seconds to profile for')
  98.  
  99. parser.add_argument('--frequency', default=5, type=int,
  100. help='how often to sample')
  101.  
  102. args = parser.parse_args()
  103.  
  104. print(','.join(metrics))
  105.  
  106. for i in range(0, args.duration/args.frequency):
  107. profile = profile_system(metrics)
  108. values = []
  109.  
  110. for metric in metrics:
  111. values.append(profile[metric])
  112.  
  113. print(','.join(values))
  114.  
  115. time.sleep(args.frequency)
  116.  
  117. if __name__ == '__main__':
  118. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement