Automated Action 1501f02451 Add automatic background scheduler for uptime monitoring
- Implemented APScheduler for automatic uptime checks
- Each monitor runs on its own configured interval (default 5 minutes)
- Scheduler starts automatically on application startup
- Real-time job management when monitors are created/updated/deleted
- Added scheduler status and control endpoints
- Background logging of all check results
- Updated documentation with scheduling features
2025-06-20 11:33:47 +00:00

136 lines
4.2 KiB
Markdown

# Uptime Monitoring API
A FastAPI-based uptime monitoring service that allows you to monitor website/endpoint availability and performance.
## Features
- **Monitor Management**: Create, update, delete, and list website monitors
- **Automatic Scheduling**: Background scheduler runs uptime checks at configured intervals
- **Uptime Checking**: Automated and manual uptime checks with response time tracking
- **Statistics**: Get uptime percentage, average response times, and check history
- **RESTful API**: Full REST API with OpenAPI documentation
- **SQLite Database**: Lightweight database for storing monitors and check results
## API Endpoints
### Base Endpoints
- `GET /` - API information and navigation
- `GET /health` - Health check endpoint
- `GET /docs` - Interactive API documentation (Swagger UI)
- `GET /redoc` - Alternative API documentation
### Monitor Endpoints
- `POST /api/v1/monitors` - Create a new monitor
- `GET /api/v1/monitors` - List all monitors
- `GET /api/v1/monitors/{monitor_id}` - Get specific monitor
- `PUT /api/v1/monitors/{monitor_id}` - Update monitor
- `DELETE /api/v1/monitors/{monitor_id}` - Delete monitor
- `GET /api/v1/monitors/{monitor_id}/checks` - Get monitor check history
- `GET /api/v1/monitors/{monitor_id}/stats` - Get monitor statistics
### Check Endpoints
- `POST /api/v1/checks/run/{monitor_id}` - Run check for specific monitor
- `POST /api/v1/checks/run-all` - Run checks for all active monitors
### Scheduler Endpoints
- `GET /api/v1/scheduler/status` - Get scheduler status and running jobs
- `POST /api/v1/scheduler/start` - Start the background scheduler
- `POST /api/v1/scheduler/stop` - Stop the background scheduler
- `POST /api/v1/scheduler/restart` - Restart scheduler and reschedule all monitors
## Installation & Setup
1. Install dependencies:
```bash
pip install -r requirements.txt
```
2. Run database migrations (optional - tables are created automatically):
```bash
alembic upgrade head
```
3. Start the application:
```bash
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
```
The API will be available at `http://localhost:8000`
## Usage Examples
### Create a Monitor
```bash
curl -X POST "http://localhost:8000/api/v1/monitors" \
-H "Content-Type: application/json" \
-d '{
"name": "My Website",
"url": "https://example.com",
"method": "GET",
"timeout": 30,
"interval": 300,
"is_active": true
}'
```
### Run a Check
```bash
curl -X POST "http://localhost:8000/api/v1/checks/run/1"
```
### Get Monitor Statistics
```bash
curl "http://localhost:8000/api/v1/monitors/1/stats"
```
### Check Scheduler Status
```bash
curl "http://localhost:8000/api/v1/scheduler/status"
```
## Monitor Configuration
When creating a monitor, you can configure:
- **name**: Human-readable name for the monitor
- **url**: The URL to monitor
- **method**: HTTP method (GET, POST, etc.) - defaults to GET
- **timeout**: Request timeout in seconds - defaults to 30
- **interval**: Check interval in seconds - defaults to 300 (5 minutes)
- **is_active**: Whether the monitor is active - defaults to true
## Automatic Scheduling
The API includes a background scheduler that automatically runs uptime checks for all active monitors:
- **Automatic Start**: The scheduler starts automatically when the application launches
- **Individual Intervals**: Each monitor runs on its own configured interval (default 5 minutes)
- **Real-time Updates**: Creating, updating, or deleting monitors automatically adjusts the scheduler
- **Logging**: All check results are logged with timestamps and status information
- **Scheduler Management**: Use the scheduler endpoints to check status, start, stop, or restart the scheduler
## Database
The application uses SQLite database located at `/app/storage/db/db.sqlite`. The database contains:
- **monitors**: Store monitor configurations
- **uptime_checks**: Store check results and history
## Environment Variables
No environment variables are required for basic operation. The application uses SQLite with default settings.
## Development
### Linting
```bash
ruff check .
ruff format .
```
### Database Migrations
```bash
alembic revision --autogenerate -m "Description"
alembic upgrade head
```