-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_monitor_script.py
More file actions
220 lines (182 loc) Β· 8.07 KB
/
system_monitor_script.py
File metadata and controls
220 lines (182 loc) Β· 8.07 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import psutil
import time
import os
import platform
from datetime import datetime
import argparse
from tabulate import tabulate
import signal
class SystemMonitor:
def __init__(self, interval=2, show_processes=False, process_count=5, log_file=None):
self.interval = interval
self.show_processes = show_processes
self.process_count = process_count
self.log_file = log_file
self.running = True
# Register signal handler for clean exit
signal.signal(signal.SIGINT, self.signal_handler)
def signal_handler(self, sig, frame):
print("\n\nGracefully shutting down...")
self.running = False
def get_size(self, bytes):
"""Convert bytes to human readable format"""
for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB']:
if bytes < 1024:
return f"{bytes:.2f} {unit}"
bytes /= 1024
def get_battery_info(self):
"""Get battery information if available"""
try:
battery = psutil.sensors_battery()
if battery:
percent = battery.percent
power_plugged = battery.power_plugged
status = "Charging" if power_plugged else "Discharging"
return f"{percent}% ({status})"
return "N/A"
except:
return "N/A"
def get_top_processes(self):
"""Get top processes by memory usage"""
processes = []
for process in psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent']):
try:
proc_info = process.info
processes.append((
proc_info['pid'],
proc_info['name'],
process.cpu_percent(),
proc_info['memory_percent']
))
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
# Sort by memory usage (can be changed to CPU by changing index to 2)
processes = sorted(processes, key=lambda x: x[3], reverse=True)
return processes[:self.process_count]
def get_network_info(self):
"""Get network information"""
net_io = psutil.net_io_counters()
return {
'bytes_sent': self.get_size(net_io.bytes_sent),
'bytes_recv': self.get_size(net_io.bytes_recv),
'packets_sent': net_io.packets_sent,
'packets_recv': net_io.packets_recv
}
def log_stats(self, stats):
"""Log statistics to file if enabled"""
if not self.log_file:
return
with open(self.log_file, 'a') as f:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
f.write(
f"{timestamp},{stats['cpu']},{stats['memory_percent']},{stats['disk_percent']}\n")
def display_system_info(self):
"""Display system information once at startup"""
print("\n" + "="*50)
print(f"SYSTEM INFORMATION:")
print(f"OS: {platform.system()} {platform.version()}")
print(f"Processor: {platform.processor()}")
print(f"Machine: {platform.machine()}")
print(f"Hostname: {platform.node()}")
print(f"Python Version: {platform.python_version()}")
# CPU Information
cpu_freq = psutil.cpu_freq()
if cpu_freq:
freq_current = f"{cpu_freq.current:.2f}MHz"
else:
freq_current = "N/A"
print(
f"CPU Cores: {psutil.cpu_count(logical=False)} Physical, {psutil.cpu_count(logical=True)} Logical")
print(f"CPU Frequency: {freq_current}")
# Memory Information
vm = psutil.virtual_memory()
print(f"Total Memory: {self.get_size(vm.total)}")
# Disk Information
disk = psutil.disk_usage('/')
print(f"Disk Total: {self.get_size(disk.total)}")
print("="*50 + "\n")
def monitor(self):
"""Main monitoring function"""
if self.log_file:
# Create log file with header if it doesn't exist
if not os.path.exists(self.log_file):
with open(self.log_file, 'w') as f:
f.write("timestamp,cpu_percent,memory_percent,disk_percent\n")
print(f"Logging data to: {self.log_file}")
self.display_system_info()
print("Monitoring system resources... Press Ctrl+C to stop.\n")
try:
while self.running:
# Get timestamps
timestamp = datetime.now().strftime("%H:%M:%S")
# Get CPU stats
cpu = psutil.cpu_percent(interval=1)
# Get memory stats
mem = psutil.virtual_memory()
mem_percent = mem.percent
mem_used = self.get_size(mem.used)
mem_total = self.get_size(mem.total)
# Get disk stats
disk = psutil.disk_usage('/')
disk_percent = disk.percent
disk_used = self.get_size(disk.used)
disk_total = self.get_size(disk.total)
# Get battery info
battery = self.get_battery_info()
# Get network info
net = self.get_network_info()
# Get swap memory
swap = psutil.swap_memory()
swap_percent = swap.percent
swap_used = self.get_size(swap.used)
swap_total = self.get_size(swap.total)
# Create stats dict for logging
stats = {
'cpu': cpu,
'memory_percent': mem_percent,
'disk_percent': disk_percent
}
# Log stats if enabled
self.log_stats(stats)
# Display basic stats
print(f"[{timestamp}] CPU: {cpu:>5.1f}% | Memory: {mem_percent:>5.1f}% ({mem_used}/{mem_total}) | "
f"Disk: {disk_percent:>5.1f}% ({disk_used}/{disk_total}) | Battery: {battery}")
print(f" Swap: {swap_percent:>5.1f}% ({swap_used}/{swap_total}) | "
f"Network: β {net['bytes_sent']} β {net['bytes_recv']}")
# Display top processes if enabled
if self.show_processes:
top_processes = self.get_top_processes()
if top_processes:
process_data = [[pid, name[:20], f"{cpu:.1f}%", f"{mem:.1f}%"]
for pid, name, cpu, mem in top_processes]
print("\nTop Processes:")
print(tabulate(process_data,
headers=["PID", "Name",
"CPU %", "Memory %"],
tablefmt="simple"))
print("") # Empty line for better readability
time.sleep(self.interval)
except Exception as e:
print(f"Error: {e}")
finally:
print("\nMonitoring stopped.")
def main():
parser = argparse.ArgumentParser(description="System Resource Monitor")
parser.add_argument("-i", "--interval", type=float, default=2.0,
help="Update interval in seconds (default: 2.0)")
parser.add_argument("-p", "--processes", action="store_true",
help="Show top processes by memory usage")
parser.add_argument("-n", "--num-processes", type=int, default=5,
help="Number of top processes to show (default: 5)")
parser.add_argument("-l", "--log", type=str,
help="Log data to specified CSV file")
args = parser.parse_args()
monitor = SystemMonitor(
interval=args.interval,
show_processes=args.processes,
process_count=args.num_processes,
log_file=args.log
)
monitor.monitor()
if __name__ == "__main__":
main()