This project involves creating a Python script to monitor the health of a server by checking key metrics such as CPU usage, memory usage, and disk usage. Additionally, the script includes a feature to back up log files every 30 days.
health_check_project/ │ ├── health_check.py ├── requirements.txt ├── README.md └── logs/ └── health_check.log └── backup/
- health_check.py: The main Python script for health checks.
- requirements.txt: A file containing the project's dependencies.
- README.md: Documentation for the project.
- logs/: Directory to store log files and backups.
Ensure you have Python 3.x installed on your system. You can download it from python.org.
git clone https://github.com/aycaoktay/health-check-script-with-python
cd healthCheck
Create a virtual environment to manage dependencies:
python -m venv venv
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOs/Linux:
source venv/bin/activate
Install the required Python packages using pip
:
pip install -r requirements.txt
Run the script to perform a server health check:
python health_check.py
The script will log CPU usage, memory usage, and disk usage in logs/health_check.log. If the log file is older than 30 days, it will be backed up to the logs/backup/ directory.
- CPU Usage Monitoring: Logs the CPU usage percentage. If usage exceeds 80%, a warning is logged.
- Memory Usage Monitoring: Logs the memory usage percentage. If usage exceeds 80%, a warning is logged.
- Disk Usage Monitoring: Logs the disk usage percentage. If usage exceeds 80%, a warning is logged.
- Log Backup: Backs up the log file every 30 days to the logs/backup/ directory.
- To regularly back up log files, prevent data loss, and preserve historical records, the Python standard libraries shutil and datetime were used. The os module was utilized to interact with the operating system.