PHP code example of snipershady / redis-information-analyzer

1. Go to this page and download the library: Download snipershady/redis-information-analyzer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snipershady / redis-information-analyzer example snippets




edisAnalizer\Service\RedisInformationRetriever;

// Create retriever instance
$retriever = new RedisInformationRetriever();

// Get all Redis information at once
$allInfo = $retriever->getAllInfo();

// Access specific sections
echo "Redis Version: " . $allInfo['server']->getRedisVersion() . "\n";
echo "Used Memory: " . $allInfo['memory']->getUsedMemoryHuman() . "\n";
echo "Connected Clients: " . $allInfo['clients']->getConnectedClients() . "\n";

$serverInfo = $retriever->getServerInfo();

echo "Redis Version: " . $serverInfo->getRedisVersion() . "\n";
echo "Redis Mode: " . $serverInfo->getRedisMode() . "\n";
echo "OS: " . $serverInfo->getOs() . "\n";
echo "Uptime: " . $serverInfo->getUptimeInDays() . " days\n";
echo "TCP Port: " . $serverInfo->getTcpPort() . "\n";

$memoryInfo = $retriever->getMemoryInfo();

echo "Used Memory: " . $memoryInfo->getUsedMemoryHuman() . "\n";
echo "Peak Memory: " . $memoryInfo->getUsedMemoryPeakHuman() . "\n";
echo "Fragmentation Ratio: " . $memoryInfo->getMemFragmentationRatio() . "\n";

if ($memoryInfo->isFragmented()) {
    echo "Warning: High memory fragmentation detected!\n";
}

$statsInfo = $retriever->getStatsInfo();

echo "Total Commands: " . number_format($statsInfo->getTotalCommandsProcessed()) . "\n";
echo "Ops/Second: " . $statsInfo->getInstantaneousOpsPerSec() . "\n";
echo "Hit Rate: " . $statsInfo->getHitRate() . "%\n";
echo "Keyspace Hits: " . number_format($statsInfo->getKeyspaceHits()) . "\n";
echo "Keyspace Misses: " . number_format($statsInfo->getKeyspaceMisses()) . "\n";

// Get connected clients count
$numberOfClients = $retriever->getNumberOfConnection();
echo "Connected clients: {$numberOfClients}\n";

// Get detailed client list
$clientList = $retriever->getClientList();

foreach ($clientList as $client) {
    echo "Client #{$client->getId()}\n";
    echo "  Address: {$client->getAddr()}\n";
    echo "  Name: " . ($client->getName() ?? 'unnamed') . "\n";
    echo "  Age: {$client->getAge()}s\n";
    echo "  Idle: {$client->getIdle()}s\n";
    echo "  Database: {$client->getDb()}\n";
    echo "  Last Command: " . ($client->getCmd() ?? 'N/A') . "\n";
    echo "  Memory: {$client->getTotalMemoryKB()} KB\n";
    echo "\n";
}

$clientsInfo = $retriever->getClientsInfo();

echo "Connected Clients: " . $clientsInfo->getConnectedClients() . "\n";
echo "Max Clients: " . $clientsInfo->getMaxclients() . "\n";
echo "Blocked Clients: " . $clientsInfo->getBlockedClients() . "\n";
echo "Client Usage: " . $clientsInfo->getClientUsagePercentage() . "%\n";

$cpuInfo = $retriever->getCpuInfo();

echo "System CPU: " . $cpuInfo->getUsedCpuSys() . "s\n";
echo "User CPU: " . $cpuInfo->getUsedCpuUser() . "s\n";
echo "Total CPU: " . $cpuInfo->getTotalCpuUsed() . "s\n";

$keyspaceInfo = $retriever->getKeyspaceInfo();

echo "Total Databases: " . $keyspaceInfo->getDatabaseCount() . "\n";
echo "Total Keys: " . number_format($keyspaceInfo->getTotalKeys()) . "\n";
echo "Total Expires: " . number_format($keyspaceInfo->getTotalExpires()) . "\n";

foreach ($keyspaceInfo->getDatabases() as $dbNumber => $dbInfo) {
    echo "\nDatabase {$dbNumber}:\n";
    echo "  Keys: {$dbInfo['keys']}\n";
    echo "  Expires: {$dbInfo['expires']}\n";
    echo "  Avg TTL: {$dbInfo['avg_ttl']} ms\n";
}

$replicationInfo = $retriever->getReplicationInfo();

echo "Role: " . $replicationInfo->getRole() . "\n";

if ($replicationInfo->isMaster()) {
    echo "Connected Slaves: " . $replicationInfo->getConnectedSlaves() . "\n";
    echo "Replication Offset: " . $replicationInfo->getMasterReplOffset() . "\n";
} else {
    echo "Master Host: " . $replicationInfo->getMasterHost() . "\n";
    echo "Master Port: " . $replicationInfo->getMasterPort() . "\n";
    echo "Link Status: " . $replicationInfo->getMasterLinkStatus() . "\n";
    echo "Connected: " . ($replicationInfo->isReplicaConnected() ? 'Yes' : 'No') . "\n";
}

$persistenceInfo = $retriever->getPersistenceInfo();

// RDB (Snapshot) information
echo "RDB Last Save: " . $persistenceInfo->getRdbLastSaveTimeFormatted() . "\n";
echo "Changes Since Last Save: " . $persistenceInfo->getRdbChangesSinceLastSave() . "\n";
echo "Last Save Status: " . $persistenceInfo->getRdbLastBgsaveStatus() . "\n";
echo "RDB Save in Progress: " . ($persistenceInfo->isRdbSaveInProgress() ? 'Yes' : 'No') . "\n";

// AOF information
if ($persistenceInfo->isAofEnabled()) {
    echo "\nAOF Enabled: Yes\n";
    echo "AOF Current Size: " . number_format($persistenceInfo->getAofCurrentSize()) . " bytes\n";
    echo "AOF Rewrite in Progress: " . ($persistenceInfo->isAofRewriteInProgress() ? 'Yes' : 'No') . "\n";
} else {
    echo "\nAOF Enabled: No\n";
}



edisAnalizer\Service\RedisInformationRetriever;

$retriever = new RedisInformationRetriever();
$allInfo = $retriever->getAllInfo();

// Check memory fragmentation
if ($allInfo['memory']->isFragmented()) {
    echo "[WARNING] High memory fragmentation detected: " .
         $allInfo['memory']->getMemFragmentationRatio() . "\n";
}

// Check hit rate
$hitRate = $allInfo['stats']->getHitRate();
if ($hitRate < 80) {
    echo "[WARNING] Low hit rate: {$hitRate}%\n";
    echo "Consider reviewing your caching strategy.\n";
}

// Check client usage
$clientUsage = $allInfo['clients']->getClientUsagePercentage();
if ($clientUsage > 80) {
    echo "[WARNING] High client usage: {$clientUsage}%\n";
    echo "Current: {$allInfo['clients']->getConnectedClients()} / " .
         "{$allInfo['clients']->getMaxclients()}\n";
}

// Check persistence status
if ($allInfo['persistence']->getRdbLastBgsaveStatus() !== 'ok') {
    echo "[ERROR] Last RDB save failed!\n";
}



iever = new RedisAnalizer\Service\RedisInformationRetriever();
$allInfo = $retriever->getAllInfo();
$clientList = $retriever->getClientList();

// Use the data in your application
// $allInfo contains all Redis information
// $clientList contains detailed client information



namespace RedisAnalizer\Service;

use Predis\Client;

class RedisConnection
{
    private static ?RedisConnection $redisConnection = null;

    private function __construct(
        string $server = 'localhost',  // Change default host
        int $port = 6379,              // Change default port
        string $connectionIdentifier = 'redis-analyzer-predis01',
        bool $isPersistent = true
    ) {
        // Connection configuration
    }
}