¿Cómo obtener el uso de CPU y RAM sin exec?


¿Cómo obtiene VBulletin la información del sistema sin el uso de exec? ¿Hay alguna otra información que pueda obtener sobre el servidor sin exec? Estoy interesado en:

  • ancho de banda utilizado
  • tipo de sistema
  • Velocidad de CPU/uso / recuento
  • Uso de RAM
Author: Arjun Prakash, 2011-01-16

4 answers

Use PHPSysInfo biblioteca

PhpSysInfo es un script PHP de código abierto que muestra información sobre el host al que se accede. Mostrará cosas como:

  • Tiempo de actividad
  • CPU
  • Memoria
  • SCSI, IDE, PCI
  • Ethernet
  • Floppy
  • Información de vídeo

Directamente analiza /proc y no usa exec.


Otra forma es usar Linfo. Es un muy rápido multiplataforma script php que describe el servidor host con extremo detalle, dando información como el uso de ram, espacio en disco, matrices raid, hardware, tarjetas de red, kernel, sistema operativo, estado de samba/cups/truecrypt, temps, discos y mucho más.

 43
Author: shamittomar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-16 14:19:25

Esto es lo que uso en servidores Linux. Todavía usa exec, pero otras preguntas apuntan aquí como duplicadas, y no hay ninguna sugerencia [buena] para ellas. debería funcionar en cada distro, pero si no lo hace, intente jugar con $get_cores + 1 offset.

CPU en porcentaje de núcleos utilizados (5 min avg):

$exec_loads = sys_getloadavg();
$exec_cores = trim(shell_exec("grep -P '^processor' /proc/cpuinfo|wc -l"));
$cpu = round($exec_loads[1]/($exec_cores + 1)*100, 0) . '%';

RAM en porcentaje del total utilizado (tiempo real):

$exec_free = explode("\n", trim(shell_exec('free')));
$get_mem = preg_split("/[\s]+/", $exec_free[1]);
$mem = round($get_mem[2]/$get_mem[1]*100, 0) . '%';

RAM en GB usada (en tiempo real):

$exec_free = explode("\n", trim(shell_exec('free')));
$get_mem = preg_split("/[\s]+/", $exec_free[1]);
$mem = number_format(round($get_mem[2]/1024/1024, 2), 2) . '/' . number_format(round($get_mem[1]/1024/1024, 2), 2);

Aquí está lo que hay en la matriz $get_mem si necesita calc otros facetas:

[0]=>row_title [1]=>mem_total [2]=>mem_used [3]=>mem_free [4]=>mem_shared [5]=>mem_buffers [6]=>mem_cached

Bono, aquí es cómo obtener el tiempo de actividad:

$exec_uptime = preg_split("/[\s]+/", trim(shell_exec('uptime')));
$uptime = $exec_uptime[2] . ' Days';
 4
Author: dhaupin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-29 13:55:21
<?php
function get_server_load() 
{
    $load=array();
    if (stristr(PHP_OS, 'win')) 
    {
        $wmi = new COM("Winmgmts://");
        $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor");  
        $cpu_num = 0;
        $load_total = 0;
        foreach($server as $cpu)
        {
            $cpu_num++;
            $load_total += $cpu->loadpercentage;
        }

        $load[]= round($load_total/$cpu_num);

    } 
    else
    {
        $load = sys_getloadavg();
    }
    return $load;
}
echo implode(' ',get_server_load());
 3
Author: Stergios Zg.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-16 09:02:10

Después de buscar en los foros y probar muchos métodos, la mejor precisión es esta:

$stat1 = file('/proc/stat'); 
sleep(1); 
$stat2 = file('/proc/stat'); 
$info1 = explode(" ", preg_replace("!cpu +!", "", $stat1[0])); 
$info2 = explode(" ", preg_replace("!cpu +!", "", $stat2[0])); 
$dif = array(); 
$dif['user'] = $info2[0] - $info1[0]; 
$dif['nice'] = $info2[1] - $info1[1]; 
$dif['sys'] = $info2[2] - $info1[2]; 
$dif['idle'] = $info2[3] - $info1[3]; 
$total = array_sum($dif); 
$cpu = array(); 
foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 1);
print_r($cpu);
 0
Author: Vishnu T Asok,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-03 08:30:07