Created
December 23, 2020 08:38
-
-
Save Groverkss/e0d4c24c1364314aeccd07fe6fed2710 to your computer and use it in GitHub Desktop.
Parser for network data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import re | |
from pprint import pprint | |
# Parameters | |
in_file = "nw-metrics" | |
out_file = "nw-parsed" | |
delimiters = '["\n ]' | |
# Dictionaries to store sums | |
source_data = {} | |
dest_data = {} | |
# Open file and calculate sums for destination and source | |
with open(in_file, "r") as rfile: | |
for line in rfile: | |
# Abuse the fact that the data is structured and split according to | |
# the delimiters defined above | |
parsed = re.split(delimiters, line) | |
# The values in parsed after splitting are like: | |
# ['pcap_bytes_transfered{DestinationAddress=', '103.174.252.216', | |
# ',SourceAddress=', '158.146.78.104', '}', '179028', ''] | |
# index 1 --> Destination IP | |
# index 3 --> Source IP | |
# index 5 --> Data | |
dest_ip = parsed[1] | |
source_ip = parsed[3] | |
data = int(parsed[5]) | |
# Calculate sum of destination ip's data | |
if dest_ip in dest_data: | |
dest_data[dest_ip] += data | |
else: | |
dest_data[dest_ip] = data | |
# Calculate sum of source ip's data | |
if source_ip in source_data: | |
source_data[source_ip] += data | |
else: | |
source_data[source_ip] = data | |
# Open file and print parsed data to it | |
with open(out_file, "w") as ofile: | |
for ip, data in dest_data.items(): | |
out_data = f'pcap_bytes_transfered{{DestinationAddress="{ip}"}} {data}\n' | |
ofile.write(out_data) | |
for ip, data in source_data.items(): | |
out_data = f'pcap_bytes_transfered{{SourceAddress="{ip}"}} {data}\n' | |
ofile.write(out_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment