-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnetstats.py
More file actions
66 lines (48 loc) · 1.79 KB
/
netstats.py
File metadata and controls
66 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""
formatter for netstats plugin
"""
from util.util import convert_bytes
from plugins import netstats
netstats_class = netstats.Netstats()
def main():
"""
returns the data, but formatted.
"""
data = netstats_class.get_data()
device_name = data["interface"]
if device_name and data["local_ip"] != "!?!?":
# the above local_ip check is a workaround so netstats updates the output
# when youre using sysmon, and your internet connection goes down or up
local_ip = data["local_ip"]
raw_received = data["statistics"]["received"]
raw_transferred = data["statistics"]["transferred"]
human_received = convert_bytes(raw_received)
human_transferred = convert_bytes(raw_transferred)
human_received_speed = convert_bytes(data["statistics"]["speeds"]["received"])
human_transferred_speed = convert_bytes(
data["statistics"]["speeds"]["transferred"]
)
return (
f" --- /sys/class/net {'-' * 46}\n"
f" Local IP: {local_ip}{' ' * max(15 - len(local_ip), 0)} | Interface: {device_name}\n"
f" Received: {human_received}"
+ " "
* (14 - len(human_received))
+ f"({raw_received} bytes)\n"
f" Transferred: {human_transferred}"
+ " "
* (14 - len(human_transferred))
+ f"({raw_transferred} bytes)\n"
f" Speed: Down {human_received_speed}"
+ " " * (14 - len(human_received_speed))
+ f"| Up {human_transferred_speed}\n"
)
return f" --- /sys/class/net/!?!? {'-' * 41}\n"
def end():
"""
clean up when the program exits
"""
netstats_class.close_files()
if __name__ == "__main__":
main()