Automating Disk Resource Usage Monitoring and Server Health Updates with Python

shahidkhans

Shahid

Posted on October 27, 2024

Automating Disk Resource Usage Monitoring and Server Health Updates with Python

Automating Disk Resource Usage Monitoring and Server Health Updates with Python

Monitoring server disk usage is critical for maintaining optimal performance and preventing downtime. In this blog post, we'll explore how to automate disk resource monitoring using a Python script and update server health via an API. We'll also discuss how to set up a cron job to run the script at regular intervals.

Prerequisites

  • Basic knowledge of Python programming
  • Familiarity with Linux command-line operations
  • Access to a server where you can run Python scripts and set up cron jobs
  • An API endpoint to update server health (replace with your actual API URL and token)

The Python Script Explained

Below is the Python script that performs disk resource monitoring and updates the server health via an API.

Health API creation is not covered in this blog post, comment if you need that as well so i will be publish that api creation steps as well.

import subprocess
import requests
import argparse


class Resource:
    file_system = ''
    disk_size = 0.0
    used = 0.0
    avail = 0.0
    use_percent = 0.0
    mounted_on = 0.0
    disk_free_threshold = 1
    mount_partition = "/"


class ResourcesMonitor(Resource):
    def __init__(self):
        self.__file_system = Resource.file_system
        self.__disk_size = Resource.disk_size
        self.__used = Resource.used
        self.__avail = Resource.avail
        self.__use_percent = Resource.use_percent
        self.__mounted_on = Resource.mounted_on
        self.__disk_free_threshold = Resource.disk_free_threshold
        self.__mount_partition = Resource.mount_partition

    def show_resource_usage(self):
        """
        Print the resource usage of disk.
        """
        print("file_system", "disk_size", "used", "avail", "use_percent", "mounted_on")
        print(self.__file_system, self.__disk_size, self.__used, self.__avail, self.__use_percent, self.__mounted_on)

    def check_resource_usage(self):
        """
        Check the disk usage by running the Unix 'df -h' command.
        """
        response_df = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
        for line in response_df.stdout:
            split_line = line.decode().split()
            if split_line[5] == self.__mount_partition:
                if int(split_line[4][:-1]) > self.__disk_free_threshold:
                    self.__file_system, self.__disk_size, self.__used = split_line[0], split_line[1], split_line[2]
                    self.__avail, self.__use_percent, self.__mounted_on = split_line[3], split_line[4], split_line[5]
                    self.show_resource_usage()
                    self.update_resource_usage_api(self)

    def update_resource_usage_api(self, resource):
        """
        Call the update API using all resource details.
        """
        update_resource_url = url.format(
            resource.__file_system,
            resource.__disk_size,
            resource.__used,
            resource.__avail,
            resource.__use_percent,
            resource_id
        )

        print(update_resource_url)
        payload = {}
        files = {}
        headers = {
            'token': 'Bearer APITOKEN'
        }
        try:
            response = requests.request("GET", update_resource_url, headers=headers, data=payload, files=files)
            if response.ok:
                print(response.json())
        except Exception as ex:
            print("Error while calling update API")
            print(ex)


if __name__ == '__main__':
    url = "http://yourapi.com/update_server_health_by_server_id?path={}&size={}" \
          "&used={}&avail={}&use_percent={}&id={}"
    parser = argparse.ArgumentParser(description='Disk Resource Monitor')
    parser.add_argument('-id', metavar='id', help='ID record of server', default=7, type=int)
    args = parser.parse_args()
    resource_id = args.id
    print(resource_id)
    resource_monitor = ResourcesMonitor()
    resource_monitor.check_resource_usage()
Enter fullscreen mode Exit fullscreen mode

The Resource and ResourcesMonitor Classes

The Resource class defines the attributes related to disk usage, such as file system, disk size, used space, and more. The ResourcesMonitor class inherits from Resource and initializes these attributes.

Checking Disk Usage

The check_resource_usage method executes the Unix df -h command to get disk usage statistics. It parses the output to find the disk usage of the specified mount partition (default is /). If the disk usage exceeds the threshold, it updates the resource details and calls the API update method.

Updating Server Health via API

The update_resource_usage_api method constructs the API request URL with the resource details and sends a GET request to update the server health. Make sure to replace http://yourapi.com/update_server_health_by_server_id with your actual API endpoint and provide the correct API token.

Using the Script

Save the script as resource_monitor.py and run it using Python 3.

Command-Line Arguments

  • -id: The server ID for which the health data is to be updated (default is 7). this will help to run same script in multiple servers just changing the ID.

Example Usage and Output

$ python3 resource_monitor.py -id=7

Output:
file_system disk_size used avail use_percent mounted_on
/dev/root 39G 31G 8.1G 80% /

API GET Request:
http://yourapi.com/update_server_health_by_server_id?path=/dev/root&size=39G&used=31G&avail=8.1G&use_percent=80%&id=7

Response
{'success': 'Servers_health data Updated.', 'data': {'id': 7, 'server_id': 1, 'server_name': 'web-server', 'server_ip': '11.11.11.11', 'size': '39G', 'path': '/dev/root', 'used': '31G', 'avail': '8.1G', 'use_percent': '80%', 'created_at': '2021-08-28T13:45:28.000000Z', 'updated_at': '2024-10-27T08:02:43.000000Z'}}
Enter fullscreen mode Exit fullscreen mode

Automating with Cron

To automate the script execution every 30 minutes, add a cron job as follows:

*/30 * * * * python3 /home/ubuntu/resource_monitor.py -id=7 &
Enter fullscreen mode Exit fullscreen mode

You can edit the cron jobs by running crontab -e and adding the above line. This will ensure the script runs every 30 minutes, keeping your server health data up-to-date.

Conclusion

By automating disk resource monitoring and server health updates, you can proactively manage your server's performance and avoid potential issues due to disk space shortages. This Python script serves as a starting point and can be customized to fit your specific needs.

💖 💪 🙅 🚩
shahidkhans
Shahid

Posted on October 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related