Created
May 20, 2019 12:35
-
-
Save guoxingx/d86ebba3fb3f25d5a8744b8417f4830c to your computer and use it in GitHub Desktop.
analyse script for "nethogs -t > filename"
This file contains 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
#! /usr/bin/env python | |
# analyse script for "nethogs -t > filename" | |
import sys | |
def process_line(line, sent, received): | |
""" | |
@params: line: string: line to process | |
@params: sent: int: threshold for sent | |
@params: received: int: threshold for received | |
@returns: matched line or None | |
""" | |
parts = line.split("\t") | |
try: | |
if len(parts) < 3: | |
return None | |
s = float(parts[-2]) | |
r = float(parts[-1].strip("\n")) | |
if s > sent or r > received: | |
return line | |
return None | |
except Exception as e: | |
print("unrecognized line: {}, error: {}".format(line, e)) | |
return None | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
exit() | |
sent, received = 500, 500 | |
if len(sys.argv) > 2: | |
sent = float(sys.argv[2]) | |
if len(sys.argv) > 3: | |
received = float(sys.argv[3]) | |
path = sys.argv[1] | |
with open(path, "r") as f: | |
for line in f: | |
if process_line(line, sent, received): | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment