Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. import re
  6. import sys
  7. import time
  8. import datetime
  9. from os import listdir
  10. from os import path
  11.  
  12. a_dir = "d:\\"
  13.  
  14.  
  15. def get_active_file():
  16. files = listdir(a_dir)
  17. files = filter(lambda x: x.startswith('info'), files)
  18. files = [path.join(a_dir, file) for file in files]
  19. files = sorted(files, key=lambda log_file: path.getmtime(log_file))
  20. if len(files):
  21. return files.pop()
  22. else:
  23. print 'No file for parse found!\n'
  24. raise SystemExit(1)
  25.  
  26.  
  27. def get_lines(file, lines=10000):
  28. total_lines_wanted = lines
  29. BLOCK_SIZE = 1024
  30. file.seek(0, 2)
  31. block_end_byte = file.tell()
  32. lines_to_go = total_lines_wanted
  33. block_number = -1
  34. blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting from the end of the file
  35. while lines_to_go > 0 and block_end_byte > 0:
  36. if (block_end_byte - BLOCK_SIZE > 0): # read the last block we haven't yet read
  37. file.seek(block_number * BLOCK_SIZE, 2)
  38. blocks.append(file.read(BLOCK_SIZE))
  39. else:
  40. file.seek(0, 0) # file too small, start from begining
  41. blocks.append(file.read(block_end_byte)) # only read what was not read
  42. lines_found = blocks[-1].count('\n')
  43. lines_to_go -= lines_found
  44. block_end_byte -= BLOCK_SIZE
  45. block_number -= 1
  46. all_read_text = ''.join(reversed(blocks))
  47. return list(reversed(all_read_text.splitlines()[-total_lines_wanted:]))
  48.  
  49.  
  50. def get_pe_stats(receiver_id, metric_count):
  51. metrics_hash = {}
  52. date_exp = re.compile('^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\.\d{3}')
  53. time_arr = []
  54. index = 0
  55. lines_to_parse = get_lines(open(get_active_file()))
  56. for current_line in lines_to_parse:
  57. if "statistics for receiver {0}".format(receiver_id) in current_line:
  58. time_arr.append(
  59. time.mktime(datetime.datetime.strptime(date_exp.findall(current_line)[0], "%Y-%m-%d %H:%M:%S").timetuple()))
  60. if len(time_arr) >= 2:
  61. break
  62. lines_to_check = lines_to_parse[index - metric_count:index]
  63. for line_with_metric in lines_to_check:
  64. metric, value = line_with_metric.split(':')
  65. metric = metric.replace(' ', '')
  66. metrics_hash[metric] = float(value)
  67. else:
  68. index += 1
  69. time_step = int(time_arr[0] - time_arr[1])
  70.  
  71. return metrics_hash
  72.  
  73. # receiver = sys.argv[1]
  74. # metric = sys.argv[2]
  75. # type = sys.argv[3]
  76.  
  77. receiver = 2
  78. metric = 'qdrops'
  79. type = 'dpdk'
  80.  
  81. if type == 'pcap':
  82. metric_count = 12
  83. elif type == 'dpdk':
  84. metric_count = 20
  85. else:
  86. print 'Wrong type of PE component specified!'
  87. raise SystemExit(1)
  88.  
  89. metrics = get_pe_stats(receiver, metric_count)
  90. print metrics
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement