You are currently viewing PHP System Information на Ubuntu Server с ISPConfig контролен панел

PHP System Information на Ubuntu Server с ISPConfig контролен панел

PHP
<?php

// Име на сървъра, uptime, операционна система
$server_name = trim(shell_exec("hostname -f"));
$uptime = trim(shell_exec("uptime -p"));
$os = trim(shell_exec("lsb_release -d | cut -f2"));

// Модел на процесора и натоварване
$cpu_model = trim(shell_exec("lscpu | grep 'Model name' | cut -d ':' -f2"));
$cpu_load = sys_getloadavg();

// RAM памет
$mem_info = shell_exec("free -h | grep Mem");
$mem_parts = preg_split('/\s+/', trim($mem_info));
$mem_total = $mem_parts[1];
$mem_used = $mem_parts[2];

// Дисково пространство
$disk_info = shell_exec("df -h --total | grep 'total'");
$disk_parts = preg_split('/\s+/', trim($disk_info));
$disk_total = $disk_parts[1];
$disk_used = $disk_parts[2];
$disk_percent = $disk_parts[4];

// Мрежови интерфейси и трафик
$interfaces = array_filter(explode("\n", shell_exec("ls /sys/class/net")));
$traffic_info = [];
foreach ($interfaces as $iface) {
    if ($iface != 'lo') {
        $data = shell_exec("cat /proc/net/dev | grep $iface");
        preg_match('/' . $iface . ':\s*(\d+)/', $data, $rx_matches);
        preg_match('/' . $iface . ':\s*\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)/', $data, $tx_matches);
        $rx_bytes = isset($rx_matches[1]) ? $rx_matches[1] : 0;
        $tx_bytes = isset($tx_matches[1]) ? $tx_matches[1] : 0;
        $traffic_info[$iface] = [
            'rx' => formatBytes($rx_bytes),
            'tx' => formatBytes($tx_bytes)
        ];
    }
}

// Функция за форматиране на байтове
function formatBytes($bytes, $precision = 2) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    if ($bytes == 0) return '0 B';
    $power = floor(log($bytes, 1024));
    $bytes /= pow(1024, $power);
    return round($bytes, $precision) . ' ' . $units[$power];
}

// Функция за форматиране на uptime с дни
function formatUptime($uptime_seconds) {
    $days = floor($uptime_seconds / 86400); // 86400 секунди = 1 ден
    $hours = floor(($uptime_seconds % 86400) / 3600);
    $minutes = floor(($uptime_seconds % 3600) / 60);
    $seconds = $uptime_seconds % 60;

    $uptime_str = '';
    if ($days > 0) {
        $uptime_str .= $days . 'd ';
    }
    $uptime_str .= sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
    return $uptime_str;
}

// Състояние на услугите с допълнителна информация
$services = [
    ['service_name' => 'apache2', 'process_name' => 'apache2', 'display_name' => 'Apache2', 'systemctl' => true],
    ['service_name' => 'mariadb', 'process_name' => 'mysqld', 'display_name' => 'MariaDB', 'systemctl' => true],
    ['service_name' => 'postfix', 'process_name' => 'master', 'display_name' => 'Postfix', 'systemctl' => true],
    ['service_name' => 'pure-ftpd', 'process_name' => 'pure-ftpd', 'display_name' => 'Pure-FTPd', 'systemctl' => false],
    ['service_name' => 'bind9', 'process_name' => 'named', 'display_name' => 'BIND9', 'systemctl' => true],
    ['service_name' => 'ntp', 'process_name' => 'ntpd', 'display_name' => 'NTP', 'systemctl' => true],
    ['service_name' => 'clamav-daemon', 'process_name' => 'clamd', 'display_name' => 'ClamAV', 'systemctl' => true],
    ['service_name' => 'smbd', 'process_name' => 'smbd', 'display_name' => 'Samba', 'systemctl' => true],
    ['service_name' => 'fail2ban', 'process_name' => 'fail2ban', 'display_name' => 'Fail2Ban', 'systemctl' => true],
];

$service_status = [];
foreach ($services as $service) {
    $service_name = $service['service_name'];
    $process_name = $service['process_name'];
    $display_name = $service['display_name'];
    $use_systemctl = $service['systemctl'];
    
    if ($use_systemctl) {
        $status = trim(shell_exec("systemctl is-active $service_name"));
    } else {
        // Проверяваме дали процесът работи
        $status = trim(shell_exec("pgrep $process_name")) ? 'active' : 'inactive';
    }

    if ($status === 'active') {
        if ($use_systemctl) {
            // Получаване на MainPID от systemctl
            $main_pid_line = trim(shell_exec("systemctl show -p MainPID $service_name"));
            $main_pid_parts = explode('=', $main_pid_line);
            $main_pid = isset($main_pid_parts[1]) ? trim($main_pid_parts[1]) : 0;
        } else {
            $main_pid = 0;
        }
        
        if ($main_pid > 0) {
            // Получаване на информация за процеса
            $proc_info = shell_exec("ps -p $main_pid -o etimes=,rss=");
            $proc_parts = preg_split('/\s+/', trim($proc_info));
            $uptime_seconds = $proc_parts[0];
            $mem_usage_kb = $proc_parts[1];
            $uptime_service = formatUptime($uptime_seconds);
            $mem_usage = round($mem_usage_kb / 1024, 2) . ' MB';

            $service_status[$display_name] = [
                'status' => 'OK',
                'uptime' => $uptime_service,
                'memory' => $mem_usage,
            ];
        } else {
            // Ако MainPID е 0 или не е наличен, използваме pgrep
            $pids = trim(shell_exec("pgrep $process_name"));
            if ($pids) {
                $pids_array = explode("\n", $pids);
                $total_mem = 0;
                $start_times = [];
                foreach ($pids_array as $pid) {
                    $pid = trim($pid);
                    // Получаване на информация за процеса
                    $proc_info = shell_exec("ps -p $pid -o etimes=,rss=");
                    $proc_parts = preg_split('/\s+/', trim($proc_info));
                    $uptime_seconds = $proc_parts[0];
                    $mem_usage_kb = $proc_parts[1];
                    $start_times[] = $uptime_seconds;
                    $total_mem += $mem_usage_kb;
                }
                // Получаване на най-дългото uptime
                $uptime_service = formatUptime(max($start_times));
                $mem_usage = round($total_mem / 1024, 2) . ' MB';

                $service_status[$display_name] = [
                    'status' => 'OK',
                    'uptime' => $uptime_service,
                    'memory' => $mem_usage,
                ];
            } else {
                // Ако няма намерени PID
                $service_status[$display_name] = [
                    'status' => 'OK',
                    'uptime' => 'N/A',
                    'memory' => 'N/A',
                ];
            }
        }
    } else {
        $service_status[$display_name] = [
            'status' => 'Down',
            'uptime' => '-',
            'memory' => '-',
        ];
    }
}
?>
<!DOCTYPE html>
<html lang="bg">
<head>
    <meta charset="UTF-8">
    <title>Сървър статус</title>
    <style>
        body {
            background-color: #2c2c2c;
            color: #f0f0f0;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            font-size: 14px;
        }
        h1, h2 {
            text-align: center;
            color: #ffffff;
            margin-top: 20px;
        }
        .status-box {
            margin: 20px auto;
            width: 80%;
            background-color: #1a1a1a;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 10px;
            border: 1px solid #333;
            text-align: left;
        }
        th {
            background-color: #444;
        }
        td {
            background-color: #2c2c2c;
        }
        .status-ok {
            color: #00ff00;
        }
        .status-down {
            color: #ff0000;
        }
    </style>
</head>
<body>
    <h1>Сървър статус</h1>
    <div class="status-box">
        <h2>Обща информация</h2>
        <table>
            <tr><th>Сървър</th><td><?php echo $server_name; ?></td></tr>
            <tr><th>Uptime</th><td><?php echo $uptime; ?></td></tr>
            <tr><th>ОС</th><td><?php echo $os; ?></td></tr>
            <tr><th>Процесор</th><td><?php echo $cpu_model; ?></td></tr>
            <tr><th>Натоварване</th><td><?php echo implode(', ', $cpu_load); ?></td></tr>
            <tr><th>RAM</th><td><?php echo $mem_used . ' / ' . $mem_total; ?></td></tr>
            <tr><th>Диск</th><td><?php echo $disk_used . ' / ' . $disk_total . ' (' . $disk_percent . ')'; ?></td></tr>
        </table>

        <h2>Мрежов трафик</h2>
        <table>
            <tr><th>Интерфейс</th><th>Download</th><th>Upload</th></tr>
            <?php foreach ($traffic_info as $iface => $traffic): ?>
            <tr>
                <td><?php echo $iface; ?></td>
                <td><?php echo $traffic['rx']; ?></td>
                <td><?php echo $traffic['tx']; ?></td>
            </tr>
            <?php endforeach; ?>
        </table>

        <h2>Състояние на услугите</h2>
        <table>
            <tr><th>Услуга</th><th>Статус</th><th>Uptime</th><th>Използвана RAM</th></tr>
            <?php foreach ($service_status as $service => $status_info): ?>
            <tr>
                <td><?php echo $service; ?></td>
                <td class="<?php echo $status_info['status'] === 'OK' ? 'status-ok' : 'status-down'; ?>">
                    <?php echo $status_info['status']; ?>
                </td>
                <td><?php echo $status_info['uptime']; ?></td>
                <td><?php echo $status_info['memory']; ?></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </div>
</body>
</html>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.