diff --git a/scripts/check_opcache.php b/scripts/check_opcache.php index 09c745f..61f4d28 100644 --- a/scripts/check_opcache.php +++ b/scripts/check_opcache.php @@ -1,31 +1,55 @@ 'not_installed']); - exit(1); -} - -if (!ini_get('opcache.enable')) { - echo json_encode(['status' => 'disabled']); +// Check if OPCache is enabled +if (!function_exists('opcache_get_status')) { + echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']); exit(1); } +// Get OPCache status $status = opcache_get_status(false); if ($status === false) { - echo json_encode(['status' => 'error', 'message' => 'Cannot retrieve OPCache status']); + echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']); exit(1); } -echo json_encode([ - 'status' => 'enabled', - 'enabled' => $status['opcache_enabled'], - 'memory_usage' => [ - 'used' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2), - 'free' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2), - 'wasted' => round($status['memory_usage']['wasted_memory'] / 1024 / 1024, 2) - ], - 'statistics' => [ - 'hits' => $status['opcache_statistics']['hits'], - 'misses' => $status['opcache_statistics']['misses'], - 'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2) +// Get OPCache configuration +$config = opcache_get_configuration(); +if ($config === false) { + echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache configuration']); + exit(1); +} + +// Format the response +$response = [ + 'status' => 'success', + 'data' => [ + 'enabled' => $config['directives']['opcache.enable'], + 'memory_usage' => [ + 'used' => $status['memory_usage']['used_memory'], + 'free' => $status['memory_usage']['free_memory'], + 'wasted' => $status['memory_usage']['wasted_memory'], + 'current_wasted_percentage' => $status['memory_usage']['current_wasted_percentage'] + ], + 'interned_strings_usage' => [ + 'buffer_size' => $status['interned_strings_usage']['buffer_size'], + 'used_memory' => $status['interned_strings_usage']['used_memory'], + 'free_memory' => $status['interned_strings_usage']['free_memory'], + 'number_of_strings' => $status['interned_strings_usage']['number_of_strings'] + ], + 'opcache_statistics' => [ + 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], + 'num_cached_keys' => $status['opcache_statistics']['num_cached_keys'], + 'max_cached_keys' => $status['opcache_statistics']['max_cached_keys'], + 'hits' => $status['opcache_statistics']['hits'], + 'misses' => $status['opcache_statistics']['misses'], + 'blacklist_misses' => $status['opcache_statistics']['blacklist_misses'], + 'blacklist_miss_ratio' => $status['opcache_statistics']['blacklist_miss_ratio'], + 'opcache_hit_rate' => $status['opcache_statistics']['opcache_hit_rate'] + ], + 'configuration' => [ + 'directives' => $config['directives'] + ] ] -]); \ No newline at end of file +]; + +echo json_encode($response, JSON_PRETTY_PRINT); \ No newline at end of file diff --git a/scripts/clear_opcache.php b/scripts/clear_opcache.php index ac71c00..b838aa9 100644 --- a/scripts/clear_opcache.php +++ b/scripts/clear_opcache.php @@ -1,18 +1,37 @@ 'error', 'message' => 'OPCache is not installed']); exit(1); } -if (!ini_get('opcache.enable')) { - echo json_encode(['status' => 'error', 'message' => 'OPCache is disabled']); - exit(1); -} - +// Reset OPCache $result = opcache_reset(); if ($result === false) { echo json_encode(['status' => 'error', 'message' => 'Failed to reset OPCache']); exit(1); } -echo json_encode(['status' => 'success', 'message' => 'OPCache cleared successfully']); \ No newline at end of file +// Get status after reset +$status = opcache_get_status(false); +if ($status === false) { + echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status after reset']); + exit(1); +} + +echo json_encode([ + 'status' => 'success', + 'message' => 'OPCache cleared successfully', + 'data' => [ + 'memory_usage' => [ + 'used' => $status['memory_usage']['used_memory'], + 'free' => $status['memory_usage']['free_memory'], + 'wasted' => $status['memory_usage']['wasted_memory'] + ], + 'statistics' => [ + 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], + 'hits' => $status['opcache_statistics']['hits'], + 'misses' => $status['opcache_statistics']['misses'] + ] + ] +], JSON_PRETTY_PRINT); \ No newline at end of file diff --git a/scripts/toggle_opcache.php b/scripts/toggle_opcache.php index a3f8754..3f0c0a2 100644 --- a/scripts/toggle_opcache.php +++ b/scripts/toggle_opcache.php @@ -1,40 +1,104 @@ 'error', 'message' => 'OPCache configuration file not found']); +// Check if OPCache is installed +if (!function_exists('opcache_get_status')) { + echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']); exit(1); } -$ini_content = file_get_contents($ini_file); -if ($ini_content === false) { - echo json_encode(['status' => 'error', 'message' => 'Cannot read OPCache configuration file']); - exit(1); +// Function to get current OPCache status +function get_opcache_status() { + $status = opcache_get_status(false); + if ($status === false) { + return false; + } + return $status; } +// Function to get OPCache configuration +function get_opcache_config() { + $config = opcache_get_configuration(); + if ($config === false) { + return false; + } + return $config; +} + +// Handle different actions switch ($action) { case 'enable': - $new_content = preg_replace('/opcache\.enable\s*=\s*0/', 'opcache.enable = 1', $ini_content); + // Enable OPCache + if (!ini_set('opcache.enable', '1')) { + echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache']); + exit(1); + } + $message = 'OPCache enabled successfully'; break; + case 'disable': - $new_content = preg_replace('/opcache\.enable\s*=\s*1/', 'opcache.enable = 0', $ini_content); + // Disable OPCache + if (!ini_set('opcache.enable', '0')) { + echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache']); + exit(1); + } + $message = 'OPCache disabled successfully'; break; + + case 'status': default: - echo json_encode(['status' => 'error', 'message' => 'Invalid action']); - exit(1); + // Get current status + $status = get_opcache_status(); + $config = get_opcache_config(); + + if ($status === false || $config === false) { + echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']); + exit(1); + } + + echo json_encode([ + 'status' => 'success', + 'data' => [ + 'enabled' => $config['directives']['opcache.enable'], + 'memory_usage' => [ + 'used' => $status['memory_usage']['used_memory'], + 'free' => $status['memory_usage']['free_memory'], + 'wasted' => $status['memory_usage']['wasted_memory'] + ], + 'statistics' => [ + 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], + 'hits' => $status['opcache_statistics']['hits'], + 'misses' => $status['opcache_statistics']['misses'] + ] + ] + ], JSON_PRETTY_PRINT); + exit(0); } -if (file_put_contents($ini_file, $new_content) === false) { - echo json_encode(['status' => 'error', 'message' => 'Cannot write to OPCache configuration file']); +// Get status after action +$status = get_opcache_status(); +$config = get_opcache_config(); + +if ($status === false || $config === false) { + echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status after action']); exit(1); } -// Restart LiteSpeed to apply changes -exec('service lsws restart', $output, $return_var); -if ($return_var !== 0) { - echo json_encode(['status' => 'warning', 'message' => 'OPCache ' . $action . 'd but LiteSpeed restart failed']); - exit(1); -} - -echo json_encode(['status' => 'success', 'message' => 'OPCache ' . $action . 'd successfully']); \ No newline at end of file +echo json_encode([ + 'status' => 'success', + 'message' => $message, + 'data' => [ + 'enabled' => $config['directives']['opcache.enable'], + 'memory_usage' => [ + 'used' => $status['memory_usage']['used_memory'], + 'free' => $status['memory_usage']['free_memory'], + 'wasted' => $status['memory_usage']['wasted_memory'] + ], + 'statistics' => [ + 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], + 'hits' => $status['opcache_statistics']['hits'], + 'misses' => $status['opcache_statistics']['misses'] + ] + ] +], JSON_PRETTY_PRINT); \ No newline at end of file