gitscope
https://dev.kronoz.win/r/marzneshin.git
app/utils/system.py · v0.3.0-alpha6
Source
1 a47eb4c Dawsh 2y ago import math
2 a47eb4c Dawsh 2y ago import secrets
3 a47eb4c Dawsh 2y ago import socket
4 a47eb4c Dawsh 2y ago from dataclasses import dataclass
5 a47eb4c Dawsh 2y ago
6 a47eb4c Dawsh 2y ago import psutil
7 a47eb4c Dawsh 2y ago
8 a47eb4c Dawsh 2y ago
9 a47eb4c Dawsh 2y ago @dataclass
10 f7af2b3 Dawsh 2y ago class MemoryStat:
11 a47eb4c Dawsh 2y ago total: int
12 a47eb4c Dawsh 2y ago used: int
13 a47eb4c Dawsh 2y ago free: int
14 a47eb4c Dawsh 2y ago
15 a47eb4c Dawsh 2y ago
16 a47eb4c Dawsh 2y ago @dataclass
17 f7af2b3 Dawsh 2y ago class CPUStat:
18 a47eb4c Dawsh 2y ago cores: int
19 a47eb4c Dawsh 2y ago percent: float
20 a47eb4c Dawsh 2y ago
21 a47eb4c Dawsh 2y ago
22 a47eb4c Dawsh 2y ago def cpu_usage() -> CPUStat:
23 a47eb4c Dawsh 2y ago return CPUStat(cores=psutil.cpu_count(), percent=psutil.cpu_percent())
24 a47eb4c Dawsh 2y ago
25 a47eb4c Dawsh 2y ago
26 a47eb4c Dawsh 2y ago def memory_usage() -> MemoryStat:
27 a47eb4c Dawsh 2y ago mem = psutil.virtual_memory()
28 a47eb4c Dawsh 2y ago return MemoryStat(total=mem.total, used=mem.used, free=mem.available)
29 a47eb4c Dawsh 2y ago
30 a47eb4c Dawsh 2y ago
31 a47eb4c Dawsh 2y ago @dataclass
32 a47eb4c Dawsh 2y ago class RealtimeBandwidth:
33 a47eb4c Dawsh 2y ago def __post_init__(self):
34 a47eb4c Dawsh 2y ago io = psutil.net_io_counters()
35 a47eb4c Dawsh 2y ago self.bytes_recv = io.bytes_recv
36 a47eb4c Dawsh 2y ago self.bytes_sent = io.bytes_sent
37 a47eb4c Dawsh 2y ago self.packets_recv = io.packets_recv
38 a47eb4c Dawsh 2y ago self.packet_sent = io.packets_sent
39 a47eb4c Dawsh 2y ago
40 a47eb4c Dawsh 2y ago incoming_bytes: int
41 a47eb4c Dawsh 2y ago outgoing_bytes: int
42 a47eb4c Dawsh 2y ago incoming_packets: int
43 a47eb4c Dawsh 2y ago outgoing_packets: int
44 a47eb4c Dawsh 2y ago
45 a47eb4c Dawsh 2y ago bytes_recv: int = None
46 a47eb4c Dawsh 2y ago bytes_sent: int = None
47 a47eb4c Dawsh 2y ago packets_recv: int = None
48 a47eb4c Dawsh 2y ago packet_sent: int = None
49 a47eb4c Dawsh 2y ago
50 a47eb4c Dawsh 2y ago
51 a47eb4c Dawsh 2y ago @dataclass
52 a47eb4c Dawsh 2y ago class RealtimeBandwidthStat:
53 a47eb4c Dawsh 2y ago incoming_bytes: int
54 a47eb4c Dawsh 2y ago outgoing_bytes: int
55 a47eb4c Dawsh 2y ago incoming_packets: int
56 a47eb4c Dawsh 2y ago outgoing_packets: int
57 a47eb4c Dawsh 2y ago
58 a47eb4c Dawsh 2y ago
59 a47eb4c Dawsh 2y ago rt_bw = RealtimeBandwidth(
60 01cdf2e Dawsh 2y ago incoming_bytes=0, outgoing_bytes=0, incoming_packets=0, outgoing_packets=0
61 01cdf2e Dawsh 2y ago )
62 a47eb4c Dawsh 2y ago
63 a47eb4c Dawsh 2y ago
64 a47eb4c Dawsh 2y ago def realtime_bandwidth() -> RealtimeBandwidthStat:
65 a47eb4c Dawsh 2y ago return RealtimeBandwidthStat(
66 01cdf2e Dawsh 2y ago incoming_bytes=rt_bw.incoming_bytes,
67 01cdf2e Dawsh 2y ago outgoing_bytes=rt_bw.outgoing_bytes,
68 01cdf2e Dawsh 2y ago incoming_packets=rt_bw.incoming_packets,
69 01cdf2e Dawsh 2y ago outgoing_packets=rt_bw.outgoing_packets,
70 01cdf2e Dawsh 2y ago )
71 a47eb4c Dawsh 2y ago
72 a47eb4c Dawsh 2y ago
73 a47eb4c Dawsh 2y ago def random_password() -> str:
74 a47eb4c Dawsh 2y ago return secrets.token_urlsafe(16)
75 a47eb4c Dawsh 2y ago
76 a47eb4c Dawsh 2y ago
77 a47eb4c Dawsh 2y ago def check_port(port: int) -> bool:
78 a47eb4c Dawsh 2y ago s = socket.socket()
79 a47eb4c Dawsh 2y ago try:
80 01cdf2e Dawsh 2y ago s.connect(("127.0.0.1", port))
81 a47eb4c Dawsh 2y ago return True
82 a47eb4c Dawsh 2y ago except socket.error:
83 a47eb4c Dawsh 2y ago return False
84 a47eb4c Dawsh 2y ago finally:
85 a47eb4c Dawsh 2y ago s.close()
86 a47eb4c Dawsh 2y ago
87 a47eb4c Dawsh 2y ago
88 a47eb4c Dawsh 2y ago def get_public_ip():
89 a47eb4c Dawsh 2y ago try:
90 a47eb4c Dawsh 2y ago sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
91 01cdf2e Dawsh 2y ago sock.connect(("8.8.8.8", 80))
92 a47eb4c Dawsh 2y ago return sock.getsockname()[0]
93 a47eb4c Dawsh 2y ago except (socket.error, IndexError):
94 a47eb4c Dawsh 2y ago pass
95 a47eb4c Dawsh 2y ago finally:
96 a47eb4c Dawsh 2y ago sock.close()
97 a47eb4c Dawsh 2y ago
98 01cdf2e Dawsh 2y ago return "127.0.0.1"
99 a47eb4c Dawsh 2y ago
100 a47eb4c Dawsh 2y ago
101 a47eb4c Dawsh 2y ago def readable_size(size_bytes):
102 a47eb4c Dawsh 2y ago if size_bytes == 0:
103 a47eb4c Dawsh 2y ago return "0 B"
104 a47eb4c Dawsh 2y ago size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
105 a47eb4c Dawsh 2y ago i = int(math.floor(math.log(size_bytes, 1024)))
106 a47eb4c Dawsh 2y ago p = math.pow(1024, i)
107 a47eb4c Dawsh 2y ago s = round(size_bytes / p, 2)
108 01cdf2e Dawsh 2y ago return f"{s} {size_name[i]}"