feat: litespeed_status now reports cache enabled/disabled state alongside server status

main
Anthony 2026-02-28 16:11:19 +08:00
parent 20af8ad386
commit df460c8b96
1 changed files with 55 additions and 10 deletions

View File

@ -54,26 +54,71 @@ function get_lsws_processes() {
return count($result['output']);
}
// Find WP-CLI binary
function find_wp_cli() {
$candidates = ['/usr/local/bin/wp', '/usr/bin/wp', '/home/litespeed/bin/wp'];
foreach ($candidates as $path) {
if (file_exists($path) && is_executable($path)) {
return $path;
}
}
$result = run_command('command -v wp');
if ($result['return_var'] === 0 && !empty($result['output'][0])) {
return trim($result['output'][0]);
}
return null;
}
// Get LiteSpeed Cache plugin state via WP-CLI
function get_cache_state() {
$wp = find_wp_cli();
$wp_root = '/var/www/webroot/ROOT';
if (!$wp || !file_exists($wp_root . '/wp-config.php')) {
return ['enabled' => null, 'note' => 'WordPress or WP-CLI not found'];
}
$result = run_command("sudo -u litespeed " . escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " litespeed-option get cache 2>/dev/null");
if ($result['return_var'] !== 0 || empty($result['output'][0])) {
return ['enabled' => null, 'note' => 'LiteSpeed Cache plugin not active or unavailable'];
}
$value = trim($result['output'][0]);
return [
'enabled' => $value === '1',
'raw' => $value
];
}
// Main execution
try {
$lsws_status = check_lsws_status();
$lsws_version = get_lsws_version();
$process_count = get_lsws_processes();
$cache_state = get_cache_state();
$cache_enabled = $cache_state['enabled'];
$cache_label = $cache_enabled === null
? 'unknown'
: ($cache_enabled ? 'enabled' : 'disabled');
$server_running = $lsws_status['running'];
// Simple focused output for litespeed_status action
echo json_encode([
'status' => $lsws_status['running'] ? 'running' : 'stopped',
'status' => $server_running ? 'running' : 'stopped',
'service_status' => $lsws_status['status'],
'version' => $lsws_version,
'process_count' => $process_count,
'message' => $lsws_status['running']
? "LiteSpeed Web Server v{$lsws_version} is running with {$process_count} processes"
: "LiteSpeed Web Server is not running"
'version' => $lsws_version,
'process_count' => $process_count,
'cache' => $cache_label,
'cache_note' => $cache_state['note'] ?? null,
'message' => $server_running
? "LiteSpeed Web Server v{$lsws_version} is running | Cache: {$cache_label}"
: "LiteSpeed Web Server is not running | Cache: {$cache_label}"
], JSON_PRETTY_PRINT);
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'status' => 'error',
'message' => 'Failed to check LiteSpeed status: ' . $e->getMessage()
], JSON_PRETTY_PRINT);
}