Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import re
- import sys
- import time
- import datetime
- from os import listdir
- from os import path
- a_dir = "d:\\"
- def get_active_file():
- files = listdir(a_dir)
- files = filter(lambda x: x.startswith('info'), files)
- files = [path.join(a_dir, file) for file in files]
- files = sorted(files, key=lambda log_file: path.getmtime(log_file))
- if len(files):
- return files.pop()
- else:
- print 'No file for parse found!\n'
- raise SystemExit(1)
- def get_lines(file, lines=10000):
- total_lines_wanted = lines
- BLOCK_SIZE = 1024
- file.seek(0, 2)
- block_end_byte = file.tell()
- lines_to_go = total_lines_wanted
- block_number = -1
- blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting from the end of the file
- while lines_to_go > 0 and block_end_byte > 0:
- if (block_end_byte - BLOCK_SIZE > 0): # read the last block we haven't yet read
- file.seek(block_number * BLOCK_SIZE, 2)
- blocks.append(file.read(BLOCK_SIZE))
- else:
- file.seek(0, 0) # file too small, start from begining
- blocks.append(file.read(block_end_byte)) # only read what was not read
- lines_found = blocks[-1].count('\n')
- lines_to_go -= lines_found
- block_end_byte -= BLOCK_SIZE
- block_number -= 1
- all_read_text = ''.join(reversed(blocks))
- return list(reversed(all_read_text.splitlines()[-total_lines_wanted:]))
- def get_pe_stats(receiver_id, metric_count):
- metrics_hash = {}
- date_exp = re.compile('^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\.\d{3}')
- time_arr = []
- index = 0
- lines_to_parse = get_lines(open(get_active_file()))
- for current_line in lines_to_parse:
- if "statistics for receiver {0}".format(receiver_id) in current_line:
- time_arr.append(
- time.mktime(datetime.datetime.strptime(date_exp.findall(current_line)[0], "%Y-%m-%d %H:%M:%S").timetuple()))
- if len(time_arr) >= 2:
- break
- lines_to_check = lines_to_parse[index - metric_count:index]
- for line_with_metric in lines_to_check:
- metric, value = line_with_metric.split(':')
- metric = metric.replace(' ', '')
- metrics_hash[metric] = float(value)
- else:
- index += 1
- time_step = int(time_arr[0] - time_arr[1])
- return metrics_hash
- # receiver = sys.argv[1]
- # metric = sys.argv[2]
- # type = sys.argv[3]
- receiver = 2
- metric = 'qdrops'
- type = 'dpdk'
- if type == 'pcap':
- metric_count = 12
- elif type == 'dpdk':
- metric_count = 20
- else:
- print 'Wrong type of PE component specified!'
- raise SystemExit(1)
- metrics = get_pe_stats(receiver, metric_count)
- print metrics
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement