Get Server Ram with PHP

get server ram with php

If you know this code will only be running under Linux, you can use the special /proc/meminfo file to get information about the system's virtual memory subsystem. The file has a form like this:

MemTotal:       255908 kB
MemFree: 69936 kB
Buffers: 15812 kB
Cached: 115124 kB
SwapCached: 0 kB
Active: 92700 kB
Inactive: 63792 kB
...

That first line, MemTotal: ..., contains the amount of physical RAM in the machine, minus the space reserved by the kernel for its own use. It's the best way I know of to get a simple report of the usable memory on a Linux system. You should be able to extract it via something like the following code:

<?php
$fh = fopen('/proc/meminfo','r');
$mem = 0;
while ($line = fgets($fh)) {
$pieces = array();
if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
$mem = $pieces[1];
break;
}
}
fclose($fh);

echo "$mem kB RAM found"; ?>

(Please note: this code may require some tweaking for your environment.)

How to get CPU/RAM usage from differenet servers?

  1. Create a php script that stores the data into a common data storage
    like mysql, redis etc.
  2. Add the php script to cron job to do it in an interval, for that on each server run crontab -e, it will bring you an editor
    window where; put the script interval logic.
  3. modify monitor.php file to get contents from common database.

example:

cron script

  // do.php it runs in every server you have  
$cpu_usage = get_server_cpu_usage();
$memory_usage = get_server_memory_usage();
// you can use hostname, ip name logic
$server_id = get_server_uniqid();
//logic to insert data
insert_monitor_data($server_id, $cpu_usage, $server_id);

crontab file

*/10 * * * * /path/to/do.php

the cron will run every 10 minutes in individual servers & store the data to a common data store.

How to get CPU usage and RAM usage without exec?

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.

Get total available system memory with PHP on Windows

You can do this via exec:

exec('wmic memorychip get capacity', $totalMemory);
print_r($totalMemory);

This will print (on my machine having 2x2 and 2x4 bricks of RAM):

Array
(
[0] => Capacity
[1] => 4294967296
[2] => 2147483648
[3] => 4294967296
[4] => 2147483648
[5] =>
)

You can easily sum this by using

echo array_sum($totalMemory);

which will then give 12884901888. To turn this into Kilo-, Mega- or Gigabytes, divide by 1024 each, e.g.

echo array_sum($totalMemory) / 1024 / 1024 / 1024; // GB

Additional command line ways of querying total RAM can be found in

  • https://superuser.com/questions/315195/is-there-a-command-to-find-out-the-available-memory-in-windows

Another programmatic way would be through COM:

// connect to WMI
$wmi = new COM('WinMgmts:root/cimv2');

// Query this Computer for Total Physical RAM
$res = $wmi->ExecQuery('Select TotalPhysicalMemory from Win32_ComputerSystem');

// Fetch the first item from the results
$system = $res->ItemIndex(0);

// print the Total Physical RAM
printf(
'Physical Memory: %d MB',
$system->TotalPhysicalMemory / 1024 /1024
);

For details on this COM example, please see:

  • http://php.net/manual/en/book.com.php
  • MSDN: Constructing a Moniker String
  • MSDN: Win32_ComputerSystem class

You can likely get this information from other Windows APIs, like the .NET API., as well.


There is also PECL extension to do this on Windows:

  • win32_ps_stat_mem — Retrieves statistics about the global memory utilization.

According to the documentation, it should return an array which contains (among others) a key named total_phys which corresponds to "The amount of total physical memory."

But since it's a PECL extension, you'd first have to install it on your machine.

php get system informations ram usage and storage usage

If you know this code will only be running under Linux, you can use the special /proc/meminfo file to get information about the system's virtual memory subsystem. The file has a form like this:

MemTotal:       255908 kB
MemFree: 69936 kB
Buffers: 15812 kB
Cached: 115124 kB
SwapCached: 0 kB
Active: 92700 kB
Inactive: 63792 kB
...

That first line, MemTotal: ..., contains the amount of physical RAM in the machine, minus the space reserved by the kernel for its own use. It's the best way I know of to get a simple report of the usable memory on a Linux system. You should be able to extract it via something like the following code:

<?php
$fh = fopen('/proc/meminfo');
$mem = 0;
while ($line = fgets($fh)) {
$pieces = array();
if (preg_match('^MemTotal:\s+(\d+)\skB$', $line, $pieces)) {
$mem = $pieces[1];
break;
}
}
fclose($fh);

echo "$mem kB RAM found"; ?>

(Please note: this code may require some tweaking for your environment.)

How to get the memory available to PHP

You can use the php.ini setting memory_limit:

ini_get('memory_limit');

Technically, this is not the total amount available to PHP, it's the amount available to a PHP script.



Related Topics



Leave a reply



Submit