Sitemap
Holyposts

Holyposts

Holyposts is a technology blog that offers its readers a wide range of articles, reviews and tutorials.

Tracking Server Stats with PHP

1 min readJan 25, 2023

--

There are several ways to get system usage information in PHP, depending on the information you want to retrieve. Below are a few examples:

Retrieving CPU usage:

You can use the sys_getloadavg() function to retrieve the system's load average. The load average is the average number of processes that are waiting for CPU time over a period of time.

<?php
$load = sys_getloadavg();
print_r($load);
?>

Retrieving Memory usage:

You can use the memory_get_usage() function to retrieve the amount of memory used by the script. This function returns the used memory in bytes.

Get Holyposts’s stories in your inbox

Join Medium for free to get updates from this writer.

<?php
$memory_used = memory_get_usage();
echo "Used memory: " . $memory_used . " bytes";
?>

Retrieving Disk usage:

You can use the disk_free_space() and disk_total_space() functions to retrieve the amount of free and total disk space on a specified partition.

<?php
$free = disk_free_space("/");
$total = disk_total_space("/");

echo "Free disk space: " . $free;
echo "Total disk space: " . $total;
?>

Retrieving other system information like Network usage, CPU usage, and Memory usage:

You can use the shell_exec() function and run command line commands like 'top', 'free', 'df', 'netstat'.

<?php
$output = shell_exec('top -b -n 1');
echo "<pre>$output</pre>";
?>

Keep in mind that some of these function may require certain permissions, thus you need to make sure that your PHP script has the necessary permissions to run these commands, also it’s best to be careful when running shell commands, as it can be a security risk if not done properly.

--

--

Holyposts
Holyposts

Written by Holyposts

0 followers

Holyposts is a technology blog that offers its readers a wide range of articles, reviews and tutorials.