There are several ways you can store the data using these commands, but two basic approaches suggest themselves:
One is to put the command(s) in a shell script and have a cron job execute that on a regular basis. Use the cron job or the shell script itself to write the data to a file. Then your PHP script can read the data from that file.
Another is to use one of the PHP functions that allows you to capture the output of a shell command to a string - I think shell_exec() does this - and either use the data there and then or write it to your data file using PHP.
As an example, the following shell command will output a single row of CSV data containing the total, used and free RAM (in MB):
The cells in the row are date and time (YYYY-MM-DD HH:MM), total ram, used ram and free ram, e.g.
So you can either redirect the output of this command to a file to build up a CSV file of your data (note the double “>>”, that appends the output of the command to the file rather than overwriting it):
Or you can capture the data directly in your PHP script and do what you want with it from there (present it, email it, store it in a datafile, whatever):
[code]$memCsv = trim(shell_exec(‘free -m | grep Mem | sed “s/ */,/g” | sed "s/Mem:/$(date +’"%Y-%m-%d %H:%M"’)/" | cut -d"," -f1-4’));
$memStats = str_getcsv($memCsv);
$date = $memStats;
$totalMb = $memStats;
$usedMb = $memStats;
$availMb = $memStats;
You can use a similar approach with the df -hm command to get the disk space usage stats.