# 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/healthImplementation
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
- Add new monitor in Uptime Kuma
- Select "HTTP(s)" type
- Enter URL:
https://yoursite.com/wp-json/uptime/v1/health - Set expected status: 200
- Monitor interval: 1-5 minutes
Checks Performed
| Check | Description |
|---|---|
| Database | Connection test |
| Response Time | Endpoint execution time |
| Uploads Writable | wp-content/uploads permission |
| Memory Usage | Current PHP memory |
| Server Load | System load averages (Linux) |
| CPU Cores | Available processors |
Architecture Feedback
Spotted a potential optimization or antipattern? Let me know.