Initializing
Back to Projects
Year2024
DomainFullstack
AccessOpen Source
Complexity0 / 10
PHPWordPressWordPress PluginMonitoringREST API
FullstackArchived

Uptime Kuma Health Monitor (WordPress Plugin)

A lightweight WordPress plugin that provides a REST API health check endpoint for Uptime Kuma and other monitoring services, returning system status, response time, and diagnostics.

# Uptime Kuma Health Monitor (WordPress Plugin)

A lightweight WordPress plugin that provides a REST API endpoint for health checks, designed for integration with Uptime Kuma and other monitoring services.

Purpose

Monitor WordPress site health remotely:

  • Database connectivity check
  • File system permissions
  • Memory usage tracking
  • Server load monitoring
  • Response time metrics

REST Endpoint

code
GET /wp-json/uptime/v1/health

Implementation

php
add_action('rest_api_init', function () {
    register_rest_route('uptime/v1', '/health', array(
        'methods' => 'GET',
        'callback' => 'uptime_health_check',
        'permission_callback' => '__return_true',
    ));
});

function uptime_health_check() {
    $start_time = microtime(true);
    $status = 'ok';
    $http_code = 200;
    $checks = array();
    
    // Database connection
    global $wpdb;
    $db_start = microtime(true);
    $db_check = $wpdb->check_connection(false);
    $db_time = round((microtime(true) - $db_start) * 1000, 2);
    
    if (!$db_check) {
        $status = 'error';
        $http_code = 503;
        $checks['database'] = 'failed';
    } else {
        $checks['database'] = 'ok';
    }
    $checks['database_response_time_ms'] = $db_time;
    
    // Upload directory writable
    $upload_dir = wp_upload_dir();
    $checks['uploads_writable'] = is_writable($upload_dir['basedir']) ? 'ok' : 'warning';
    
    // Memory usage
    $checks['memory_usage_mb'] = round(memory_get_usage(true) / 1024 / 1024, 2);
    $checks['memory_peak_mb'] = round(memory_get_peak_usage(true) / 1024 / 1024, 2);
    $checks['memory_limit'] = ini_get('memory_limit');
    
    // Server load (Linux)
    if (function_exists('sys_getloadavg')) {
        $load = sys_getloadavg();
        $checks['server_load'] = array(
            '1min' => round($load[0], 2),
            '5min' => round($load[1], 2),
            '15min' => round($load[2], 2)
        );
    }
    
    // CPU cores
    if (is_readable('/proc/cpuinfo')) {
        $cpuinfo = file_get_contents('/proc/cpuinfo');
        preg_match_all('/^processor/m', $cpuinfo, $matches);
        $checks['cpu_cores'] = count($matches[0]);
    }
    
    $response_time = round((microtime(true) - $start_time) * 1000, 2);
    
    return new WP_REST_Response(array(
        'status' => $status,
        'response_time_ms' => $response_time,
        'timestamp' => current_time('mysql'),
        'checks' => $checks
    ), $http_code);
}

Response Example

json
{
  "status": "ok",
  "response_time_ms": 45.2,
  "timestamp": "2025-05-18 10:30:00",
  "checks": {
    "database": "ok",
    "database_response_time_ms": 12.5,
    "uploads_writable": "ok",
    "memory_usage_mb": 32.5,
    "memory_peak_mb": 64.2,
    "memory_limit": "256M",
    "server_load": {
      "1min": 0.5,
      "5min": 0.3,
      "15min": 0.2
    },
    "cpu_cores": 4
  }
}

Uptime Kuma Integration

  1. Add new monitor in Uptime Kuma
  2. Select "HTTP(s)" type
  3. Enter URL: https://yoursite.com/wp-json/uptime/v1/health
  4. Set expected status: 200
  5. Monitor interval: 1-5 minutes

Checks Performed

CheckDescription
DatabaseConnection test
Response TimeEndpoint execution time
Uploads Writablewp-content/uploads permission
Memory UsageCurrent PHP memory
Server LoadSystem load averages (Linux)
CPU CoresAvailable processors

Architecture Feedback

Spotted a potential optimization or antipattern? Let me know.

Submit a Technical Suggestion