From bc68405581bbe6ded8ca8b78b29f31bc8bddd70f Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 18 Jun 2025 02:09:33 +0800 Subject: [PATCH] Added redis scripts --- mbadmin.jps | 15 +++ scripts/check_redis.php | 202 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 scripts/check_redis.php diff --git a/mbadmin.jps b/mbadmin.jps index cf10291..e6744a2 100644 --- a/mbadmin.jps +++ b/mbadmin.jps @@ -31,6 +31,8 @@ onInstall: - if [ ! -f check_litespeed.php ]; then echo "Failed to download check_litespeed.php"; exit 1; fi - curl -OL https://deploy.mightybox.io/tony/mb-admin/raw/branch/main/scripts/manage_litespeed.php - if [ ! -f manage_litespeed.php ]; then echo "Failed to download manage_litespeed.php"; exit 1; fi + - curl -OL https://deploy.mightybox.io/tony/mb-admin/raw/branch/main/scripts/check_redis.php + - if [ ! -f check_redis.php ]; then echo "Failed to download check_redis.php"; exit 1; fi - chmod +x *.php *.sh # Download WordPress Installer with path verification - cd /home/litespeed/mbmanager @@ -151,6 +153,11 @@ menu: caption: Flush Keys action: redis_clear_keys successText: "${response.out}" + - confirmText: Get Redis metrics? + loadingText: Getting Redis metrics... + caption: Get Redis Metrics + action: redis_metrics + successText: "${response.out}" - confirmText: Check OPcache status? loadingText: Checking OPcache status... caption: Check OPcache Status @@ -729,6 +736,14 @@ actions: - return: type: info message: "${response.out}" + redis_metrics: + - cmd[cp]: + user: root + commands: + - php /home/litespeed/mbmanager/scripts/check_redis.php + - return: + type: info + message: "${response.out}" db_import_preparation: - cmd[cp]: user: root diff --git a/scripts/check_redis.php b/scripts/check_redis.php new file mode 100644 index 0000000..07a2ea6 --- /dev/null +++ b/scripts/check_redis.php @@ -0,0 +1,202 @@ +/dev/null"); + return trim($output); +} + +function parseRedisInfo($info_output) { + $data = []; + $lines = explode("\n", $info_output); + + foreach ($lines as $line) { + $line = trim($line); + if (empty($line) || strpos($line, '#') === 0) { + continue; + } + + if (strpos($line, ':') !== false) { + list($key, $value) = explode(':', $line, 2); + $data[trim($key)] = trim($value); + } + } + + return $data; +} + +function calculateHitRate($hits, $misses) { + $total = $hits + $misses; + if ($total == 0) { + return 0; + } + return round(($hits / $total) * 100, 2); +} + +function convertBytesToMB($bytes) { + return round($bytes / 1024 / 1024, 2); +} + +function convertBytesToHuman($bytes) { + $units = ['B', 'KB', 'MB', 'GB']; + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + + $bytes /= (1 << (10 * $pow)); + + return round($bytes, 2) . ' ' . $units[$pow]; +} + +function measureLatency($socket) { + $start_time = microtime(true); + $result = executeRedisCommand($socket, 'PING'); + $end_time = microtime(true); + + if ($result === 'PONG') { + return round(($end_time - $start_time) * 1000, 2); // Convert to milliseconds + } + + return null; +} + +try { + // Check if Redis is running + $ping_result = executeRedisCommand($redis_socket, 'PING'); + + if ($ping_result !== 'PONG') { + echo json_encode([ + 'status' => 'error', + 'message' => 'Redis is not responding', + 'data' => [ + 'redis_status' => 'disconnected', + 'memory_utilization' => [ + 'used_mb' => 0, + 'max_mb' => 0, + 'used_human' => '0 B', + 'max_human' => '0 B', + 'percentage' => 0, + 'fragmentation_ratio' => 0 + ], + 'hit_rate' => [ + 'hits' => 0, + 'misses' => 0, + 'percentage' => 0 + ], + 'latency' => [ + 'avg_ms' => 0, + 'status' => 'unavailable' + ], + 'server_info' => [ + 'version' => 'unknown', + 'uptime_seconds' => 0, + 'uptime_human' => '0 seconds' + ] + ] + ], JSON_PRETTY_PRINT); + exit(1); + } + + // Get Redis memory information + $memory_info = parseRedisInfo(executeRedisCommand($redis_socket, 'INFO memory')); + + // Get Redis statistics + $stats_info = parseRedisInfo(executeRedisCommand($redis_socket, 'INFO stats')); + + // Get Redis server information + $server_info = parseRedisInfo(executeRedisCommand($redis_socket, 'INFO server')); + + // Measure latency + $latency_ms = measureLatency($redis_socket); + + // Calculate memory utilization + $used_memory = isset($memory_info['used_memory']) ? (int)$memory_info['used_memory'] : 0; + $max_memory = isset($memory_info['maxmemory']) ? (int)$memory_info['maxmemory'] : 0; + $used_memory_human = isset($memory_info['used_memory_human']) ? $memory_info['used_memory_human'] : convertBytesToHuman($used_memory); + $max_memory_human = isset($memory_info['maxmemory_human']) ? $memory_info['maxmemory_human'] : convertBytesToHuman($max_memory); + + $memory_percentage = 0; + if ($max_memory > 0) { + $memory_percentage = round(($used_memory / $max_memory) * 100, 2); + } + + // Calculate hit rate + $hits = isset($stats_info['keyspace_hits']) ? (int)$stats_info['keyspace_hits'] : 0; + $misses = isset($stats_info['keyspace_misses']) ? (int)$stats_info['keyspace_misses'] : 0; + $hit_rate_percentage = calculateHitRate($hits, $misses); + + // Format uptime + $uptime_seconds = isset($server_info['uptime_in_seconds']) ? (int)$server_info['uptime_in_seconds'] : 0; + $uptime_human = ''; + if ($uptime_seconds > 0) { + $days = floor($uptime_seconds / 86400); + $hours = floor(($uptime_seconds % 86400) / 3600); + $minutes = floor(($uptime_seconds % 3600) / 60); + $seconds = $uptime_seconds % 60; + + if ($days > 0) { + $uptime_human = "{$days}d {$hours}h {$minutes}m"; + } elseif ($hours > 0) { + $uptime_human = "{$hours}h {$minutes}m"; + } elseif ($minutes > 0) { + $uptime_human = "{$minutes}m {$seconds}s"; + } else { + $uptime_human = "{$seconds}s"; + } + } + + // Prepare response + $response = [ + 'status' => 'success', + 'data' => [ + 'redis_status' => 'connected', + 'memory_utilization' => [ + 'used_mb' => convertBytesToMB($used_memory), + 'max_mb' => convertBytesToMB($max_memory), + 'used_human' => $used_memory_human, + 'max_human' => $max_memory_human, + 'percentage' => $memory_percentage, + 'fragmentation_ratio' => isset($memory_info['mem_fragmentation_ratio']) ? (float)$memory_info['mem_fragmentation_ratio'] : 0 + ], + 'hit_rate' => [ + 'hits' => $hits, + 'misses' => $misses, + 'percentage' => $hit_rate_percentage + ], + 'latency' => [ + 'avg_ms' => $latency_ms, + 'status' => $latency_ms !== null ? 'available' : 'unavailable' + ], + 'server_info' => [ + 'version' => isset($server_info['redis_version']) ? $server_info['redis_version'] : 'unknown', + 'uptime_seconds' => $uptime_seconds, + 'uptime_human' => $uptime_human, + 'total_connections' => isset($stats_info['total_connections_received']) ? (int)$stats_info['total_connections_received'] : 0, + 'total_commands' => isset($stats_info['total_commands_processed']) ? (int)$stats_info['total_commands_processed'] : 0, + 'ops_per_sec' => isset($stats_info['instantaneous_ops_per_sec']) ? (int)$stats_info['instantaneous_ops_per_sec'] : 0 + ], + 'additional_metrics' => [ + 'evicted_keys' => isset($stats_info['evicted_keys']) ? (int)$stats_info['evicted_keys'] : 0, + 'expired_keys' => isset($stats_info['expired_keys']) ? (int)$stats_info['expired_keys'] : 0, + 'keyspace_misses_ratio' => $hits + $misses > 0 ? round(($misses / ($hits + $misses)) * 100, 2) : 0 + ] + ] + ]; + + echo json_encode($response, JSON_PRETTY_PRINT); + +} catch (Exception $e) { + echo json_encode([ + 'status' => 'error', + 'message' => 'Failed to get Redis information: ' . $e->getMessage(), + 'data' => null + ], JSON_PRETTY_PRINT); + exit(1); +} +?> \ No newline at end of file