32

How does VBulletin get the system information without the use of exec? Is there any other information I can get about the server without exec? I am interested in:

  • bandwidth used
  • system type
  • CPU speed/usage/count
  • RAM usage
Arjun Prakash
  • 669
  • 9
  • 23
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
  • 1
    I have some experince to get cpu load on windows and i post it at : [http://stackoverflow.com/questions/7538251/how-can-i-get-the-cpu-and-memory-useage/13666951#13666951](http://stackoverflow.com/questions/7538251/how-can-i-get-the-cpu-and-memory-useage/13666951#13666951) Hope it help . –  Dec 02 '12 at 06:16

5 Answers5

51

Use PHPSysInfo library

phpSysInfo is a open source PHP script that displays information about the host being accessed. It will displays things like:

  • Uptime
  • CPU
  • Memory
  • SCSI, IDE, PCI
  • Ethernet
  • Floppy
  • Video Information

It directly parsed parses /proc and does not use exec.


Another way is to use Linfo. It is a very fast cross-platform php script that describes the host server in extreme detail, giving information such as ram usage, disk space, raid arrays, hardware, network cards, kernel, os, samba/cups/truecrypt status, temps, disks, and much more.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • @Ronan - it reads directly from /proc, provided open_basedir() is not in effect. So, you should fall back to exec() if that restriction is in place. – Tim Post Jan 16 '11 at 14:13
  • i mean will it work on a shared hosting or does it need some functions enabled (like exec) , my script will be installed by many customers on many hosting plans/dedicated servers and stuff , i need something to work on everything , or with functions that could be enabled ... – Rami Dabain Jan 16 '11 at 14:14
  • @Ronan: Yes, it does not use `exec`. Try installing that script on your web-server to quickly check. It works on my shared server. – shamittomar Jan 16 '11 at 14:14
  • What if PHPSysInfo tells me that file_exists(/proc/net/dev) does not exist on my machine? –  Apr 05 '13 at 09:24
  • As a note to an old post by someone from the future; these are both licenced under GPL, not MIT or Apache. So you must disclose the source of your project; I'd be careful if you're planning to use this for business applications where you're not allowed to disclose your source. – RMSD May 25 '17 at 19:48
  • Using this library is super useful. I noticed one can also get the info in XML or JSON format when adding this to your string in the URL for example: **/phpsysinfo/xml.php?plugin=complete&json** like it shows on their homepage. – JamLizzy101 Sep 03 '20 at 12:21
11

This is what I use on Linux servers. It still uses exec, but other questions point here as duplicate, and there is no [good] suggestion for those. It should work on every distro, but if it doesn't, try messing with $get_cores + 1 offset.

CPU in percent of cores used (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 in percent of total used (realtime):

$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 in GB used (realtime):

$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);

Here is what's in the $get_mem array if you need to calc other facets:

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

Bonus, here is how to get the uptime:

$exec_uptime = preg_split("/[\s]+/", trim(shell_exec('uptime')));
$uptime = $exec_uptime[2] . ' Days';
dhaupin
  • 1,613
  • 2
  • 21
  • 24
3
<?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());
Arjun Prakash
  • 669
  • 9
  • 23
Stergios Zg.
  • 652
  • 6
  • 9
  • COM extension for Windows isn't enabled by default in PHP, and certainly not in production mode. – Tim Oct 19 '17 at 08:22
3

This is what I use for instant CPU usage without 1 second delay

$cpu = shell_exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'');
Saud Iqbal
  • 375
  • 1
  • 4
  • 15
  • Upvoted the effort but this either does not work or only works on one core or something, keeps outputting 1000 when my usage is 9% – Lynob Apr 16 '20 at 22:35
0

after searching on forums and trying many methods, best accurate is this:

$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);
Vishnu T Asok
  • 244
  • 4
  • 15
  • 1
    This does not show the total cpu usage on the server, like this answer https://stackoverflow.com/a/38085813/1920003 – Lynob Apr 16 '20 at 22:38