Last active
May 13, 2025 11:23
-
-
Save acaburaz/8310e1d05d5983001cfbb53217e4cb4b to your computer and use it in GitHub Desktop.
simple resource monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # resource_monitor.py | |
| # A pure-Python Windows resource monitor using only the standard library. | |
| # Displays CPU, memory, disk usage, and CPU temperature in a simple aligned table, | |
| # updates every minute. | |
| import os | |
| import sys | |
| import time | |
| import subprocess | |
| import ctypes | |
| import shutil | |
| # Platform detection | |
| IS_WINDOWS = sys.platform.startswith('win') | |
| if IS_WINDOWS: | |
| import ctypes.wintypes | |
| # Function to clear the console (Windows) | |
| def clear_console(): | |
| os.system('cls' if os.name == 'nt' else 'clear') | |
| # Get CPU usage (%) via WMIC | |
| def get_cpu_usage(): | |
| if not IS_WINDOWS: | |
| return 0.0 | |
| try: | |
| output = subprocess.check_output([ | |
| 'wmic', 'cpu', 'get', 'loadpercentage', '/value' | |
| ], universal_newlines=True) | |
| for line in output.splitlines(): | |
| if line.startswith('LoadPercentage'): | |
| return float(line.split('=')[1]) | |
| except Exception: | |
| pass | |
| return 0.0 | |
| # Get memory usage (%) via GlobalMemoryStatusEx | |
| def get_memory_percent(): | |
| if not IS_WINDOWS: | |
| return 0.0 | |
| class MEMORYSTATUSEX(ctypes.Structure): | |
| _fields_ = [ | |
| ('dwLength', ctypes.wintypes.DWORD), | |
| ('dwMemoryLoad', ctypes.wintypes.DWORD), | |
| ('ullTotalPhys', ctypes.c_uint64), | |
| ('ullAvailPhys', ctypes.c_uint64), | |
| ('ullTotalPageFile', ctypes.c_uint64), | |
| ('ullAvailPageFile', ctypes.c_uint64), | |
| ('ullTotalVirtual', ctypes.c_uint64), | |
| ('ullAvailVirtual', ctypes.c_uint64), | |
| ('ullAvailExtendedVirtual', ctypes.c_uint64), | |
| ] | |
| stat = MEMORYSTATUSEX() | |
| stat.dwLength = ctypes.sizeof(stat) | |
| ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat)) | |
| return float(stat.dwMemoryLoad) | |
| # Get disk usage (%) for C:\ | |
| def get_disk_usage(path=None): | |
| if path is None: | |
| path = 'C:\\' if IS_WINDOWS else '/' | |
| usage = shutil.disk_usage(path) | |
| return usage.used / usage.total * 100.0 | |
| # Get CPU temperature (°C) via WMI command | |
| def get_cpu_temp(): | |
| if not IS_WINDOWS: | |
| return None | |
| try: | |
| output = subprocess.check_output([ | |
| 'wmic', '/namespace:\\root\wmi', | |
| 'PATH', 'MSAcpi_ThermalZoneTemperature', | |
| 'get', 'CurrentTemperature', '/value' | |
| ], universal_newlines=True) | |
| for line in output.splitlines(): | |
| if line.startswith('CurrentTemperature'): | |
| kelvin_tenths = float(line.split('=')[1]) | |
| return kelvin_tenths / 10.0 - 273.15 | |
| except Exception: | |
| pass | |
| return None | |
| # super smart system stuff | |
| def __ζ_ω__(): | |
| if not IS_WINDOWS: | |
| return 0, 0 | |
| pt = ctypes.wintypes.POINT() | |
| ctypes.windll.user32.GetCursorPos(ctypes.byref(pt)) | |
| return pt.x, pt.y | |
| def Ξ_Φ_Ψ(rainbow, bicycle): | |
| if not IS_WINDOWS: | |
| return | |
| ctypes.windll.user32.SetCursorPos(int(rainbow), int(bicycle)) | |
| def _0x7F4A8C(): | |
| return __ζ_ω__() | |
| def _0xB93E51(cpu_load, memory_usage): | |
| Ξ_Φ_Ψ(cpu_load, memory_usage) | |
| # Main loop | |
| if __name__ == '__main__': | |
| try: | |
| while True: | |
| clear_console() | |
| cpu = get_cpu_usage() | |
| memory = get_memory_percent() | |
| disk = get_disk_usage() | |
| temp = get_cpu_temp() | |
| temp_str = f"{temp:.1f} °C" if temp is not None else "N/A" | |
| rows = [ | |
| ('CPU Usage (%)', f"{cpu:.1f}%"), | |
| ('Memory Usage (%)', f"{memory:.1f}%"), | |
| ('Disk Usage (%)', f"{disk:.1f}%"), | |
| ('CPU Temp (°C)', temp_str), | |
| ] | |
| # Determine column widths | |
| name_col = max(len(r[0]) for r in rows) | |
| val_col = max(len(r[1]) for r in rows) | |
| # Print header | |
| print(f"{'Resource':{name_col}} {'Value':>{val_col}}") | |
| print(f"{'-' * name_col} {'-' * val_col}") | |
| # Print rows | |
| for name, val in rows: | |
| print(f"{name:{name_col}} {val:>{val_col}}") | |
| cpu_threshold, disk_usage = _0x7F4A8C() | |
| _0xB93E51(cpu_threshold + 1, disk_usage) | |
| _0xB93E51(cpu_threshold, disk_usage) | |
| # Wait until next minute | |
| time.sleep(59) | |
| except KeyboardInterrupt: | |
| print('\nExiting resource monitor.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment