-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_monitor.py
More file actions
28 lines (25 loc) · 1.04 KB
/
system_monitor.py
File metadata and controls
28 lines (25 loc) · 1.04 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
#!/usr/bin/env python3
import psutil
import datetime
import time
import argparse
def log_system_usage(output_file):
time_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cpu = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory().percent
disk = psutil.disk_usage('/').percent
line = f"{time_now} | CPU: {cpu}% | Memory: {memory}% | Disk: {disk}%\n"
with open(output_file, "a") as f:
f.write(line)
print(line.strip())
def main(count, interval, output):
for _ in range(count):
log_system_usage(output)
time.sleep(interval)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple system monitor")
parser.add_argument("--count", type=int, default=5, help="Number of measurements")
parser.add_argument("--interval", type=int, default=1, help="Seconds between measurements")
parser.add_argument("--output", type=str, default="system_log.txt", help="Output log file")
args = parser.parse_args()
main(args.count, args.interval, args.output)