-
Notifications
You must be signed in to change notification settings - Fork 1
/
system_monitor.py
executable file
·101 lines (83 loc) · 4.12 KB
/
system_monitor.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import sys
import signal
import time
import psutil
# Global variable to track whether the script should continue running
running = True
# Signal handler to gracefully handle termination
def signal_handler(sig, frame):
global running
running = False
def key_or_val(li, key, value, header):
if header:
li.append(key)
else:
li.append(value)
if __name__ == "__main__":
logfile = sys.argv[1]
print(logfile)
pid = int(sys.argv[2])
print(pid)
p = psutil.Process(pid)
first = True
# Register signal handler for SIGINT (Ctrl+C)
signal.signal(signal.SIGINT, signal_handler)
with open(logfile, "w+") as fout:
print("File open")
while running:
counters = []
key_or_val(counters, "timestamp", f"{time.time() * 1000}", first)
sys_counters = p.as_dict()
for k in sorted(sys_counters):
v = sys_counters[k]
if k in ["environ", "cmdline", "connections", "open_files", "memory_maps", "threads", "cpu_affinity"]:
continue
elif k in ["gids", "memory_info", "uids", "num_ctx_switches", "cpu_times", "io_counters", "ionice",
"memory_full_info"]:
vdict = v._asdict()
for sk in sorted(vdict):
sv = vdict[sk]
key_or_val(counters, f"proc.{k}.{sk}", f"{sv}", first)
else:
key_or_val(counters, f"proc.{k}", f"{v}", first)
net = psutil.net_io_counters(pernic=True)
for device in sorted(net):
net_device = net[device]
key_or_val(counters, f"net.bytes_sent.{device}", f"{net_device.bytes_sent}", first)
key_or_val(counters, f"net.bytes_recv.{device}", f"{net_device.bytes_recv}", first)
key_or_val(counters, f"net.packets_sent.{device}", f"{net_device.packets_sent}", first)
key_or_val(counters, f"net.packets_recv.{device}", f"{net_device.packets_recv}", first)
key_or_val(counters, f"net.errin.{device}", f"{net_device.errin}", first)
key_or_val(counters, f"net.errout.{device}", f"{net_device.errout}", first)
key_or_val(counters, f"net.dropin.{device}", f"{net_device.dropin}", first)
key_or_val(counters, f"net.dropout.{device}", f"{net_device.dropout}", first)
disks = psutil.disk_io_counters(perdisk=True)
for disk in sorted(disks):
disks_disk = disks[disk]
key_or_val(counters, f"disk.read_count.{disk}", f"{disks_disk.read_count}", first)
key_or_val(counters, f"disk.read_bytes.{disk}", f"{disks_disk.read_bytes}", first)
key_or_val(counters, f"disk.write_count.{disk}", f"{disks_disk.write_count}", first)
key_or_val(counters, f"disk.write_bytes.{disk}", f"{disks_disk.write_bytes}", first)
cputimes = psutil.cpu_times(percpu=False)
cpudict = cputimes._asdict()
for sk in sorted(cpudict):
sv = cpudict[sk]
key_or_val(counters, f"cpu.{sk}", f"{sv}", first)
cpupercent = psutil.cpu_percent()
key_or_val(counters, f"cpu.percent", f"{cpupercent}", first)
cpufreq = psutil.cpu_freq()
key_or_val(counters, f"cpu.freq.current", f"{cpufreq.current}", first)
key_or_val(counters, f"cpu.freq.min", f"{cpufreq.min}", first)
key_or_val(counters, f"cpu.freq.max", f"{cpufreq.max}", first)
cpus = psutil.cpu_stats()
key_or_val(counters, f"cpu.ctx_switches", f"{cpus.ctx_switches}", first)
key_or_val(counters, f"cpu.interrupts", f"{cpus.interrupts}", first)
key_or_val(counters, f"cpu.soft_interrupts", f"{cpus.soft_interrupts}", first)
key_or_val(counters, f"cpu.syscalls", f"{cpus.syscalls}", first)
fout.write(";".join(counters))
fout.write(os.linesep)
fout.flush()
first = False
time.sleep(1)
print("Monitoring script terminated.")