search
HomeBackend DevelopmentPython TutorialAutomating Disk Resource Usage Monitoring and Server Health Updates with Python

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()

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'}}

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 &

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.

The above is the detailed content of Automating Disk Resource Usage Monitoring and Server Health Updates with Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Python: Is it Truly Interpreted? Debunking the MythsPython: Is it Truly Interpreted? Debunking the MythsMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

Python concatenate lists with same elementPython concatenate lists with same elementMay 11, 2025 am 12:08 AM

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Interpreted vs Compiled Languages: Python's PlaceInterpreted vs Compiled Languages: Python's PlaceMay 11, 2025 am 12:07 AM

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

For and While loops: when do you use each in python?For and While loops: when do you use each in python?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools