API Reference and Cron Jobs #

Complete reference for Server Monitor API endpoints and cron job configuration.

API Authentication #

All API routes require your configured API key passed as a URL parameter:

https://yoursite.com/api/servermonitor/endpoint/YOUR_API_KEY

The API key is configured in your .env file:

SERVER_MONITOR_API_KEY=your-secure-api-key-here

Required Cron Jobs #

1. Premium Server Checks (Every Minute) #

Critical for 1-minute SLA compliance

* * * * * curl -s https://yoursite.com/api/servermonitor/check/premium/YOUR_API_KEY

This dispatches jobs to check all servers on premium plans (Basic, Pro, Enterprise).

2. Free Server Checks (Every 5 Minutes) #

Required for free plan monitoring

*/5 * * * * curl -s https://yoursite.com/api/servermonitor/check/free/YOUR_API_KEY

This dispatches jobs to check all servers on free plans.

3. Heartbeat Checks (Every Minute) #

Required for heartbeat monitoring

* * * * * curl -s https://yoursite.com/api/servermonitor/check/heartbeats/YOUR_API_KEY

This checks for missing heartbeat pings and triggers alerts.

4. SSL Certificate Checks (Daily at 6 AM) #

Required for SSL certificate monitoring

0 6 * * * curl -s https://yoursite.com/api/servermonitor/check/ssl/YOUR_API_KEY

This checks SSL certificate expiration for all monitored certificates.

5. Uptime Calculations (Hourly) #

Recalculates uptime percentages

0 * * * * curl -s https://yoursite.com/api/servermonitor/calculate/YOUR_API_KEY

This processes servers marked for uptime recalculation.

6. Log Purging (Daily at 2 AM) #

Maintains database performance

0 2 * * * curl -s https://yoursite.com/api/servermonitor/purge-logs/YOUR_API_KEY

This removes old logs based on plan retention limits.

API Endpoints #

Server Monitoring #

Dispatch All Server Checks #

GET /api/servermonitor/check/all/{api_key}

Dispatches jobs for both premium and free servers.

Response:

{
    "success": true,
    "message": "All server check initiated successfully",
    "output": "Dispatched 150 server checks (5 new servers)"
}

Dispatch Premium Server Checks #

GET /api/servermonitor/check/premium/{api_key}

Dispatches jobs only for premium plan servers (1-minute SLA).

Response:

{
    "success": true,
    "message": "Premium server check initiated successfully",
    "output": "Dispatched 75 premium server checks (2 new servers)"
}

Dispatch Free Server Checks #

GET /api/servermonitor/check/free/{api_key}

Dispatches jobs only for free plan servers (5-minute SLA).

Response:

{
    "success": true,
    "message": "Free server check initiated successfully",
    "output": "Dispatched 75 free server checks (3 new servers)"
}

Heartbeat Monitoring #

Check Heartbeats #

GET /api/servermonitor/check/heartbeats/{api_key}

Checks for missing heartbeat pings and triggers alerts.

Response:

{
    "success": true,
    "message": "Heartbeat check initiated successfully",
    "missing": 2,
    "healthy": 48
}

SSL Certificate Monitoring #

Check SSL Certificates #

GET /api/servermonitor/check/ssl/{api_key}

Checks SSL certificate expiration dates.

Response:

{
    "success": true,
    "message": "SSL certificate check completed",
    "checked": 25,
    "expiring_soon": 2,
    "expired": 0
}

Uptime Calculations #

Recalculate All Uptimes #

GET /api/servermonitor/calculate/{api_key}

Dispatches jobs to recalculate uptime for servers marked as needing calculation.

Response:

{
    "success": true,
    "message": "Uptime calculation job dispatched successfully"
}

Recalculate Specific Server #

GET /api/servermonitor/calculate/server/{server_id}/{api_key}

Forces recalculation for a specific server.

Parameters:

  • server_id - The server's database ID

Response:

{
    "success": true,
    "message": "Server Example Server (ID: 123) marked for recalculation and job dispatched"
}

Force Recalculate All Servers #

GET /api/servermonitor/calculate/all/{api_key}

Forces recalculation for ALL servers (use with caution on large deployments).

Response:

{
    "success": true,
    "message": "150 servers marked for recalculation and job dispatched"
}

Log Management #

Purge Old Logs (All Organizations) #

GET /api/servermonitor/purge-logs/{api_key}

Purges old logs for all organizations based on their plan limits.

Response:

{
    "success": true,
    "message": "Log purge command executed successfully",
    "output": "Purged 1,234 logs across 25 organizations"
}

Purge Logs for Specific Organization #

GET /api/servermonitor/purge-logs/organization/{organization_id}/{api_key}

Purges logs only for the specified organization.

Parameters:

  • organization_id - The organization's database ID

Response:

{
    "success": true,
    "message": "Log purge command executed successfully for organization ID: 123",
    "output": "Purged 45 logs for organization 123"
}

Worker Monitoring #

Worker Status Check #

GET /api/servermonitor/worker-status/{api_key}

Returns current worker performance and SLA compliance status.

Response:

{
    "success": true,
    "status": "healthy",
    "timestamp": "2025-01-01T12:00:00Z",
    "sla_compliance": {
        "premium": {
            "percentage": 98.5,
            "checked": 74,
            "total": 75
        },
        "free": {
            "percentage": 100.0,
            "checked": 60,
            "total": 60
        }
    },
    "queue_status": {
        "premium": 5,
        "free": 12
    },
    "recent_breaches": 2,
    "dashboard_url": "https://yoursite.com/albrightlabs/servermonitor/workerperformance"
}

Status Values:

  • healthy - All systems operating normally
  • warning - Some performance issues detected
  • critical - Immediate attention required

Complete Cron Configuration #

Add all required cron jobs to your server's crontab:

# Edit crontab
crontab -e

# Add these lines (replace YOUR_API_KEY and yoursite.com)
# Premium server checks (every minute)
* * * * * curl -s https://yoursite.com/api/servermonitor/check/premium/YOUR_API_KEY

# Free server checks (every 5 minutes)
*/5 * * * * curl -s https://yoursite.com/api/servermonitor/check/free/YOUR_API_KEY

# Heartbeat checks (every minute)
* * * * * curl -s https://yoursite.com/api/servermonitor/check/heartbeats/YOUR_API_KEY

# SSL certificate checks (daily at 6 AM)
0 6 * * * curl -s https://yoursite.com/api/servermonitor/check/ssl/YOUR_API_KEY

# Uptime calculations (hourly)
0 * * * * curl -s https://yoursite.com/api/servermonitor/calculate/YOUR_API_KEY

# Log purging (daily at 2 AM)
0 2 * * * curl -s https://yoursite.com/api/servermonitor/purge-logs/YOUR_API_KEY

# Optional: Worker capacity monitoring (every 5 minutes)
*/5 * * * * curl -s https://yoursite.com/api/servermonitor/worker-status/YOUR_API_KEY > /dev/null

Error Responses #

Authentication Errors #

Invalid API Key (403):

{
    "success": false,
    "message": "Invalid API key"
}

Missing API Key (403):

{
    "success": false,
    "message": "Invalid API key"
}

Subscription Errors #

No Active Subscriptions (402):

{
    "success": false,
    "message": "No active subscriptions found"
}

No Servers Found (404):

{
    "success": false,
    "message": "No servers found for active subscriptions"
}

Validation Errors #

Invalid Check Type (400):

{
    "success": false,
    "message": "Invalid check type. Use \"all\", \"premium\", or \"free\"."
}

Server Not Found (404):

{
    "success": false,
    "message": "Server with ID 999 not found"
}

System Errors #

Command Execution Failed (500):

{
    "success": false,
    "message": "Failed to initiate server check",
    "error": "Detailed error message"
}

Console Commands #

You can also run these operations via console commands:

# Server status checks
php artisan servermonitor:checkserverstatus
php artisan servermonitor:checkserverstatus --premium
php artisan servermonitor:checkserverstatus --free

# Heartbeat checks
php artisan servermonitor:checkheartbeats

# SSL certificate checks
php artisan servermonitor:checksslcertificates

# Log purging
php artisan servermonitor:purge-logs
php artisan servermonitor:purge-logs --organization=123

# Worker capacity monitoring
php artisan servermonitor:check-capacity

Monitoring API Performance #

Health Check Script #

Create a simple health check script:

#!/bin/bash
# health-check.sh

API_KEY="your-api-key-here"
BASE_URL="https://yoursite.com"

# Check worker status
STATUS=$(curl -s "${BASE_URL}/api/servermonitor/worker-status/${API_KEY}" | jq -r '.status')

if [ "$STATUS" = "critical" ]; then
    echo "CRITICAL: Server monitoring requires immediate attention"
    exit 2
elif [ "$STATUS" = "warning" ]; then
    echo "WARNING: Server monitoring performance issues detected"
    exit 1
else
    echo "OK: Server monitoring is healthy"
    exit 0
fi

Integration with Monitoring Services #

Nagios/Icinga:

# Add to nagios commands
define command{
    command_name    check_servermonitor
    command_line    /path/to/health-check.sh
}

Uptime Robot:

  • Monitor the worker status endpoint
  • Alert on non-200 responses or status != "healthy"

Security Considerations #

API Key Protection #

  • Never commit API keys to version control
  • Rotate regularly (monthly recommended)
  • Use environment variables for storage
  • Monitor access logs for unauthorized usage

Network Security #

# Restrict API access by IP (nginx example)
location /api/servermonitor/ {
    allow 192.168.1.0/24;    # Your server network
    allow 10.0.0.0/8;        # Private networks
    deny all;

    # Pass to PHP
    try_files $uri $uri/ /index.php?$query_string;
}

Rate Limiting #

The API has built-in protection against:

  • Duplicate simultaneous requests
  • Overlapping cron executions
  • Resource exhaustion

Next Steps #

After API setup:

  1. Configure Notifications - Set up email and SMS alerts
  2. Add Your First Server - Start monitoring
  3. Monitor Performance - Watch worker health

Previous: Queue Setup | Next: Notification System