mb-admin/scripts/check_litespeed.php

134 lines
4.5 KiB
PHP

<?php
// LiteSpeed Web Server Status Check for Virtuozzo LLSMP
// Focused on checking if LiteSpeed is running or not
// Function to execute system commands safely
function run_command($command) {
$output = [];
$return_var = 0;
exec($command . ' 2>&1', $output, $return_var);
return ['output' => $output, 'return_var' => $return_var];
}
// Function to check if LiteSpeed is running
function check_lsws_status() {
$result = run_command('systemctl is-active lsws');
if ($result['return_var'] !== 0) {
// Try alternative service names
$alt_services = ['lshttpd', 'litespeed'];
foreach ($alt_services as $service) {
$alt_result = run_command("systemctl is-active $service");
if ($alt_result['return_var'] === 0) {
return [
'running' => true,
'status' => $alt_result['output'][0] ?? 'active',
'service_name' => $service
];
}
}
}
return [
'running' => $result['return_var'] === 0,
'status' => $result['output'][0] ?? 'unknown',
'service_name' => 'lsws'
];
}
// Function to get LiteSpeed version (quick check)
function get_lsws_version() {
$result = run_command('/var/www/bin/lshttpd -v');
if ($result['return_var'] === 0 && !empty($result['output'])) {
foreach ($result['output'] as $line) {
if (preg_match('/LiteSpeed.*?(\d+\.\d+(?:\.\d+)?)/i', $line, $matches)) {
return $matches[1];
}
}
}
return 'unknown';
}
// Function to check LiteSpeed processes
function get_lsws_processes() {
$result = run_command('ps aux | grep -E "(lshttpd|litespeed)" | grep -v grep');
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'];
}
// Try as litespeed user first (same pattern as manage_litespeed.php)
$cmd = "sudo -u litespeed " . escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " litespeed-option get cache 2>&1";
$result = run_command($cmd);
// Fallback: run as root with --allow-root (read-only operation, safe)
if ($result['return_var'] !== 0 || empty($result['output'])) {
$cmd = escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " litespeed-option get cache --allow-root 2>&1";
$result = run_command($cmd);
}
if ($result['return_var'] !== 0 || empty($result['output'])) {
$detail = !empty($result['output']) ? implode(' ', $result['output']) : 'no output';
return ['enabled' => null, 'note' => 'LiteSpeed Cache plugin not active or unavailable: ' . $detail];
}
$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'];
echo json_encode([
'status' => $server_running ? 'running' : 'stopped',
'service_status' => $lsws_status['status'],
'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',
'message' => 'Failed to check LiteSpeed status: ' . $e->getMessage()
], JSON_PRETTY_PRINT);
}