Basic PC Health Check Script

Posted on:|Last Updated:|Category: Experiment

Featured image for Basic PC Health Check Script

Introduction:

The "Basic PC Health Check Script" is a Python program developed as part of my training for the 'Google IT Automation with Python: Professional Certificate.' This script performs a series of checks to ensure the health and performance of a computer.

Tech Stack:

  • Python

  • Built-in Python libraries: os, shutil, sys, socket, psutil.

Code Snippets and Explanations

Check for Pending Reboot

python
def check_reboot():
    """Returns True if the computer has a pending reboot."""
    return os.path.exists("/run/reboot-required")

This function checks if the computer requires a reboot by looking for the existence of a specific file. A pending reboot can indicate that important system updates need to be applied.

Check for Disk Space

python
def check_disk_full(disk, min_gb, min_percent):
    """Returns true if there isn't enough disk space, False otherwise."""
    du = shutil.disk_usage(disk)
    # Calculate the percentage of free space
    percent_free = 100 * du.free / du.total
    # Calculate how many free gigabytes
    gigabytes_free = du.free / 2**30
    if percent_free < min_percent or gigabytes_free < min_gb:
        return True
    return False

This function checks the available disk space on a specified drive and compares it to minimum thresholds. It ensures that there's enough space for the system to operate smoothly.

Check for Root Partition

python
def check_root_full():
    """Returns True if the root partition is full, False otherwise."""
    return check_disk_full(disk="/", min_gb=2, min_percent=10)

This function specifically checks if the root partition of the system is running out of space, which is crucial for the stability of the operating system.

Check CPU Usage

python
def check_cpu_constrained():
    """Returns True if the CPU is having too much usage, False otherwise."""
    return psutil.cpu_percent(1) > 75

This function monitors the CPU usage and returns True if it exceeds a 75% threshold. High CPU usage may indicate performance issues or resource contention.

Check Network Connectivity

python
def check_no_network():
    """Returns True if it fails to resolve Google's URL, False otherwise"""
    try:
        socket.gethostbyname("www.google.com")
        return False
    except:
        return True

This function checks for network connectivity by attempting to resolve Google's URL. If it fails to do so, it indicates a potential network issue.

Testing and Usage

To run this script, execute it using Python:

python health_check.py

The script will perform the checks and display messages indicating the status of the system health.

Conclusion

The "Basic PC Health Check Script" is a valuable tool for monitoring the health of a computer system. By automating these checks, it helps ensure that the system is running smoothly and efficiently. This project highlights my Python coding skills and my ability to create practical solutions for real-world problems.

Github Repository:health-checks

Latest commit: Format code for better readability and maintainability This commit improves the formatting of the provided Python script for enhanced code readabilit...View

2
2
0

Tech Stack:

Related Projects

Image for Dialogue Creator

A tool for designing non-linear game dialogues with a visual node-based interface. Features include adding characters, messages, branches, and randomness.

Image for Full Stack Workout Tracker with Next 14, Prisma, & Clerk

Full stack workout tracking app. Browse over 800 exercises, create your own routines, record your workouts and view your data with beautiful charts. Built with Next.js 14, Clerk and OpenAI