PC Health Check Using Python

Boost your system’s performance with our Python script which regularly monitor PC health check, using essential metrics like CPU usage, memory usage, disk space, and network traffic. Optionally, track system uptime and load averages to optimize efficiency with Prompt AI Tools. Here’s a step-by-step guide to create such a script:

Step 1: Install Required Libraries

We have to ensure we have the psutil library installed, which provides an easy interface to retrieve system utilization and performance metrics.

pip install psutil

Step 2: Create a New Python Script

Open your preferred code editor and create a new Python file, e.g., monitor_system_health.py.

Step 3: Import Necessary Libraries

Import psutil for system monitoring and any other libraries needed for optional functionalities like logging or email alerts.

Styled Text Area
import psutil
import schedule
import time
import datetime

Step 4: Define Monitoring Functions

Now we have to create functions to monitor different aspects of system health:

Styled Text Area
def monitor_cpu_usage(): cpu_usage = psutil.cpu_percent() print(f"CPU Usage: {cpu_usage}%") def monitor_memory_usage():
memory_info = psutil.virtual_memory()
print(f"Memory Usage: {memory_info.percent}%")

def monitor_disk_usage():
disk_usage = psutil.disk_usage('/')
print(f"Disk Usage: {disk_usage.percent}%")

def monitor_network_traffic():
net_io = psutil.net_io_counters()
print(f"Network - Sent: {net_io.bytes_sent / (1024 * 1024):.2f} MB, Received: {net_io.bytes_recv / (1024 * 1024):.2f} MB")

def monitor_system_health():
monitor_cpu_usage()
monitor_memory_usage()
monitor_disk_usage()
monitor_network_traffic()
# Add more monitoring functions as needed

Step 5: Schedule Monitoring Tasks

Use the schedule library to schedule monitoring tasks at desired intervals:

Styled Text Area
schedule.every(5).minutes.do(monitor_system_health)
# Adjust interval and add more monitoring tasks if needed

Step 6: Run the PC Health Check Script

Add a loop to continuously run the scheduled tasks:

Styled Text Area
if __name__ == "__main__":
print("System health monitoring started...")
while True:
schedule.run_pending()
time.sleep(1)

Complete Script :

Styled Text Area
import os
import shutil

def cleanup_temp_files(temp_folder):
import psutil
import schedule
import time
import datetime

def monitor_cpu_usage():
cpu_usage = psutil.cpu_percent()
print(f"CPU Usage: {cpu_usage}%")

def monitor_memory_usage():
memory_info = psutil.virtual_memory()
print(f"Memory Usage: {memory_info.percent}%")

def monitor_disk_usage():
disk_usage = psutil.disk_usage('/')
print(f"Disk Usage: {disk_usage.percent}%")

def monitor_network_traffic():
net_io = psutil.net_io_counters()
print(f"Network - Sent: {net_io.bytes_sent / (1024 * 1024):.2f} MB, Received: {net_io.bytes_recv / (1024 * 1024):.2f} MB")

def monitor_system_health():
monitor_cpu_usage()
monitor_memory_usage()
monitor_disk_usage()
monitor_network_traffic()
# Add more monitoring functions as needed

schedule.every(5).minutes.do(monitor_system_health)
# Adjust interval and add more monitoring tasks if needed

if __name__ == "__main__":
print("System health monitoring started...")
while True:
schedule.run_pending()
time.sleep(1)

Benefits of Using PC Health Check Script

By automating the cleanup of temporary files using Python, you:

  • Efficiency: Enhance system efficiency by identifying and addressing performance bottlenecks promptly.
  • Resource Management: Optimize resource allocation with real-time insights into CPU, memory, disk, and network usage.
  • Proactive Maintenance: Prevent potential issues by monitoring system health continuously.
  • Automation: Automate monitoring tasks to save time and ensure consistent performance.
  • User Experience: Improve overall user experience by ensuring smoother and faster system operation.

Use Case Examples:

  • IT Operations:
    • IT administrators can use the script to monitor server health, ensuring optimal performance and uptime.
  • Software Development:
    • Developers can utilize the script to debug and optimize application performance during testing phases.
  • Cloud Management:
    • Cloud service providers can monitor virtual machines and containers to maintain service-level agreements (SLAs).
  • Educational Institutions:
    • Schools and colleges can use the script to manage computer labs and ensure smooth operation of shared resources.
  • Office Environments:
    • Office IT teams can monitor workstations to prevent slowdowns and interruptions in daily operations.

Explore more

Clean Up Temporary Files
Capture Screen Using Python
Linkedin Web Scraping
Send Email Using Python

Some Useful Links:

Scroll to Top