Skip to content

Instantly share code, notes, and snippets.

@guoxingx
Created May 20, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guoxingx/d86ebba3fb3f25d5a8744b8417f4830c to your computer and use it in GitHub Desktop.
Save guoxingx/d86ebba3fb3f25d5a8744b8417f4830c to your computer and use it in GitHub Desktop.
analyse script for "nethogs -t > filename"
#! /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