


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
