A simple Flask add-on that provides a status page to monitor the health of your web application, including system metrics and database connectivity.
- System Health Checks: Provides real-time metrics such as CPU usage and available memory.
- Database Connection: Monitors MongoDB connectivity.
- HTTP Status Codes: Returns
200if everything is fine, and500if there is an issue with the system or the database connection.
You can install the package directly from GitHub using pip:
pip install git+https://github.com/botsarefuture/Flask-status.gitThis will download and install the package and its dependencies automatically.
-
Clone the repository:
git clone https://github.com/botsarefuture/Flask-status.git
-
Navigate to the package directory:
cd status-package -
Install the package in editable mode:
pip install -e . -
Alternatively, you can add the following line to your
requirements.txtfor future installations:git+https://github.com/botsarefuture/Flask-status.git -
If your project has a
requirements.txtfile, just run:pip install -r requirements.txt
To integrate the Status class into your Flask application, follow these steps:
-
Initialize the Status Package:
from flask import Flask from status_package import Status app = Flask(__name__) # Initialize the status checker status = Status(app) if __name__ == '__main__': app.run(debug=True)
-
Custom MongoDB URI (optional):
You can also pass a custom MongoDB URI to the
Statusclass during initialization:status = Status(app, mongo_uri="mongodb://your-custom-uri")
-
Access the Status Page:
Once your Flask app is running, visit the
/statusendpoint to see real-time system and database information:http://localhost:5000/status
The /status endpoint returns a JSON object with the following structure:
{
"app_status": "running",
"database_connected": true,
"system_metrics": {
"cpu_usage": 15.3,
"memory_available": 2147483648,
"memory_total": 8589934592
}
}If an issue occurs (e.g., high CPU usage or a database connection failure), the endpoint returns a 500 HTTP status code, along with relevant information in the response body.
Here’s the project tree for the status_package:
status_package/
├── status_package/
│ ├── __init__.py
│ ├── status.py
│ └── checks.py
├── tests/
│ └── test_status.py
├── setup.py
├── README.md
└── requirements.txt
status.py: Main class for handling/statusroute and logic.checks.py: Contains helper functions for system and database checks.test_status.py: Unit tests for status checks usingpytest.
To run the tests:
-
Install
pytest:pip install pytest
-
Run the tests:
pytest
- Python 3.7+
- Flask
- psutil (for system metrics)
- pymongo (for MongoDB connectivity)
This project is licensed under the MIT License. See the LICENSE file for more details.