202 lines
7.0 KiB
PHP
202 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Redis Monitoring Script
|
|
* Returns comprehensive Redis metrics for Laravel panel
|
|
*/
|
|
|
|
// Redis socket path (from existing scripts)
|
|
$redis_socket = '/var/run/redis/redis.sock';
|
|
|
|
function executeRedisCommand($socket, $command) {
|
|
$output = shell_exec("redis-cli -s $socket $command 2>/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);
|
|
}
|
|
?>
|