diff --git a/scripts/check_opcache.php b/scripts/check_opcache.php index ebed37f..766298a 100644 --- a/scripts/check_opcache.php +++ b/scripts/check_opcache.php @@ -19,7 +19,7 @@ if ($mode !== '--simple') { echo "PHP SAPI: " . php_sapi_name() . "\n"; } -// Check if OPCache is enabled +// Check if OPCache is installed if (!function_exists('opcache_get_status')) { if ($mode === '--simple') { echo "OPcache status: Disabled - OPcache is not installed"; @@ -29,18 +29,7 @@ if (!function_exists('opcache_get_status')) { exit(1); } -// Get OPCache status -$status = opcache_get_status(false); -if ($status === false) { - if ($mode === '--simple') { - echo "OPcache status: Error - Failed to get OPcache status"; - } else { - echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']); - } - exit(1); -} - -// Get OPCache configuration +// Get OPCache configuration first $config = opcache_get_configuration(); if ($config === false) { if ($mode === '--simple') { @@ -51,11 +40,27 @@ if ($config === false) { exit(1); } +// Check if OPcache is enabled in configuration +$opcache_enabled = $config['directives']['opcache.enable']; + +// Get OPCache status only if it's enabled +$status = null; +if ($opcache_enabled) { + $status = opcache_get_status(false); + if ($status === false) { + if ($mode === '--simple') { + echo "OPcache status: Error - Failed to get OPcache status"; + } else { + echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']); + } + exit(1); + } +} + // Handle different output modes switch ($mode) { case '--simple': - $enabled = $config['directives']['opcache.enable']; - if ($enabled) { + if ($opcache_enabled) { echo "OPcache status: Enabled"; } else { echo "OPcache status: Disabled"; @@ -64,64 +69,85 @@ switch ($mode) { case '--summary': // Summary: Basic info + memory usage + key stats - $response = [ - 'status' => 'success', - 'summary' => [ - 'enabled' => $config['directives']['opcache.enable'], - 'memory_usage' => [ - 'used_mb' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2), - 'free_mb' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2), - 'wasted_mb' => round($status['memory_usage']['wasted_memory'] / 1024 / 1024, 2), - 'total_mb' => round(($status['memory_usage']['used_memory'] + $status['memory_usage']['free_memory']) / 1024 / 1024, 2), - 'wasted_percentage' => $status['memory_usage']['current_wasted_percentage'] - ], - 'cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], - 'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2) . '%', - 'interned_strings' => [ - 'count' => $status['interned_strings_usage']['number_of_strings'], - 'used_mb' => round($status['interned_strings_usage']['used_memory'] / 1024 / 1024, 2), - 'buffer_mb' => round($status['interned_strings_usage']['buffer_size'] / 1024 / 1024, 2) + if (!$opcache_enabled) { + $response = [ + 'status' => 'success', + 'summary' => [ + 'enabled' => false, + 'message' => 'OPcache is disabled. No runtime data available.' ] - ] - ]; + ]; + } else { + $response = [ + 'status' => 'success', + 'summary' => [ + 'enabled' => true, + 'memory_usage' => [ + 'used_mb' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2), + 'free_mb' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2), + 'wasted_mb' => round($status['memory_usage']['wasted_memory'] / 1024 / 1024, 2), + 'total_mb' => round(($status['memory_usage']['used_memory'] + $status['memory_usage']['free_memory']) / 1024 / 1024, 2), + 'wasted_percentage' => $status['memory_usage']['current_wasted_percentage'] + ], + 'cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], + 'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2) . '%', + 'interned_strings' => [ + 'count' => $status['interned_strings_usage']['number_of_strings'], + 'used_mb' => round($status['interned_strings_usage']['used_memory'] / 1024 / 1024, 2), + 'buffer_mb' => round($status['interned_strings_usage']['buffer_size'] / 1024 / 1024, 2) + ] + ] + ]; + } echo json_encode($response, JSON_PRETTY_PRINT); break; case '--statistics': // Statistics: Detailed performance metrics - $response = [ - 'status' => 'success', - 'statistics' => [ - 'cache_performance' => [ - 'hits' => $status['opcache_statistics']['hits'], - 'misses' => $status['opcache_statistics']['misses'], - 'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2) . '%', - 'blacklist_misses' => $status['opcache_statistics']['blacklist_misses'], - 'blacklist_miss_ratio' => $status['opcache_statistics']['blacklist_miss_ratio'] - ], - 'cache_usage' => [ - 'cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], - 'cached_keys' => $status['opcache_statistics']['num_cached_keys'], - 'max_cached_keys' => $status['opcache_statistics']['max_cached_keys'], - 'key_usage_percentage' => round(($status['opcache_statistics']['num_cached_keys'] / $status['opcache_statistics']['max_cached_keys']) * 100, 2) . '%' - ], - 'memory_details' => [ - 'used_memory' => $status['memory_usage']['used_memory'], - 'free_memory' => $status['memory_usage']['free_memory'], - 'wasted_memory' => $status['memory_usage']['wasted_memory'], - 'current_wasted_percentage' => $status['memory_usage']['current_wasted_percentage'] - ], - 'restart_info' => [ - 'restart_pending' => $status['restart_pending'] ?? false, - 'restart_in_progress' => $status['restart_in_progress'] ?? false + if (!$opcache_enabled) { + $response = [ + 'status' => 'success', + 'statistics' => [ + 'enabled' => false, + 'message' => 'OPcache is disabled. No statistics available.' ] - ] - ]; + ]; + } else { + $response = [ + 'status' => 'success', + 'statistics' => [ + 'enabled' => true, + 'cache_performance' => [ + 'hits' => $status['opcache_statistics']['hits'], + 'misses' => $status['opcache_statistics']['misses'], + 'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2) . '%', + 'blacklist_misses' => $status['opcache_statistics']['blacklist_misses'], + 'blacklist_miss_ratio' => $status['opcache_statistics']['blacklist_miss_ratio'] + ], + 'cache_usage' => [ + 'cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], + 'cached_keys' => $status['opcache_statistics']['num_cached_keys'], + 'max_cached_keys' => $status['opcache_statistics']['max_cached_keys'], + 'key_usage_percentage' => round(($status['opcache_statistics']['num_cached_keys'] / $status['opcache_statistics']['max_cached_keys']) * 100, 2) . '%' + ], + 'memory_details' => [ + 'used_memory' => $status['memory_usage']['used_memory'], + 'free_memory' => $status['memory_usage']['free_memory'], + 'wasted_memory' => $status['memory_usage']['wasted_memory'], + 'current_wasted_percentage' => $status['memory_usage']['current_wasted_percentage'] + ], + 'restart_info' => [ + 'restart_pending' => $status['restart_pending'] ?? false, + 'restart_in_progress' => $status['restart_in_progress'] ?? false + ] + ] + ]; + } echo json_encode($response, JSON_PRETTY_PRINT); break; case '--settings': - // Settings: Configuration directives only + // Settings: Configuration directives only (always available) $response = [ 'status' => 'success', 'settings' => [ @@ -157,37 +183,50 @@ switch ($mode) { default: // Full response (original functionality) - $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'] + if (!$opcache_enabled) { + $response = [ + 'status' => 'success', + 'data' => [ + 'enabled' => false, + 'message' => 'OPcache is disabled. Only configuration data is available.', + 'configuration' => [ + 'directives' => $config['directives'] + ] ] - ] - ]; + ]; + } else { + $response = [ + 'status' => 'success', + 'data' => [ + 'enabled' => true, + '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'] + ] + ] + ]; + } echo json_encode($response, JSON_PRETTY_PRINT); break; } \ No newline at end of file