Monitoring Disk usage and CPU utilization

·

3 min read

There are many IT tasks that can be automated to improve efficiency and reduce errors. However, not all tasks are suitable for automation, as some require human expertise and judgment. Here are some examples of tasks that can be automated:

Tasks that can be automated:

  1. Software deployment: Automating software deployment can save time and reduce errors. For example, using a configuration management tool like Ansible or Puppet can automate the installation and configuration of software across multiple servers.

  2. Backups and recovery: Automating backups and recovery can ensure that data is backed up regularly and is easily recoverable in case of a disaster. Backup tools like Veeam or Bacula can automate the backup process and ensure data integrity.

  3. Monitoring: Automating monitoring can help IT teams identify and fix issues before they become major problems. Tools like Nagios or Zabbix can monitor servers, applications, and networks and alert IT teams if there are any issues.

As we rely more and more on technology in our daily lives, it's important to ensure that our computers are functioning properly. One way to do this is to monitor the health of our systems by checking their disk usage and CPU utilization. In this blog post, we'll explore a Python script that can help us do just that.

The script uses two Python modules: shutil and psutil. The shutil module provides a way to access many file and directory related functions, while the psutil module provides an interface to many system-level functions in Python.

Let's take a closer look at the code:

import shutil
import psutil


def check_disk_usage(disk):
    du = shutil.disk_usage(disk)
    free = du.free / du.total * 100
    return free > 20


def check_cpu_util():
    usage = psutil.cpu_percent(1)
    return usage < 75


if not check_disk_usage("/") or not check_cpu_util:
    print("error")
else:
    print("Everything is fine")

The code starts by importing the necessary modules, shutil and psutil. The check_disk_usage() function takes a parameter disk and returns True if the free space on the specified disk is greater than 20% of the total disk space. Otherwise, it returns False. The function uses the shutil.disk_usage() method to get the disk usage statistics of the specified disk, and then calculates the percentage of free space on the disk.

The check_cpu_util() function returns True if the CPU utilization is less than 75%. Otherwise, it returns False. The psutil.cpu_percent() method is used to get the current CPU utilization percentage.

The if statement checks the return values of the check_disk_usage() and check_cpu_util() functions. If either of them returns False (i.e., the disk usage is too high or the CPU utilization is too high), the script prints "error". If both functions return True (i.e., the disk usage and CPU utilization are within acceptable limits), the script prints "Everything is fine".

So, what are the practical uses of this script? One possible use case is to incorporate it into a larger monitoring system that checks the health of a computer and sends alerts if any issues are detected. For example, if the disk usage is too high, it could indicate that the computer is running low on storage space and may need to have files deleted or moved to an external drive. Similarly, if the CPU utilization is too high, it could indicate that the computer is running too many resource-intensive programs simultaneously, and it may be necessary to close some programs or upgrade the computer's hardware.

Another possible use case is to simply run the script periodically to ensure that the computer is functioning properly. This could be especially useful for servers or other mission-critical systems that need to be operational at all times.

In conclusion, this Python script provides a simple yet effective way to monitor the health of a computer by checking its disk usage and CPU utilization. By incorporating it into a larger monitoring system or running it periodically, users can ensure that their computers are functioning properly and take action if any issues are detected.

Do check out my other blogs at https://indianstudent.hashnode.dev/ - THARUN K

Did you find this article valuable?

Support THARUN by becoming a sponsor. Any amount is appreciated!