Posted on:|Last Updated:|Category: Experiment
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.
Python
Built-in Python libraries: os
, shutil
, sys
, socket
, psutil
.
Check for Pending Reboot
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
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
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
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
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.
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.
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.
Latest commit: Format code for better readability and maintainability This commit improves the formatting of the provided Python script for enhanced code readabilit...View
Experiment
Production App