CSc 445 Asst 5

Load Average

Assigned
Due

Nov 1
30 pts
Nov 7

This is a short PHP assignment so you can at least get your fingers dirty. This is a simple page to show the load information on the system. Mine is here. This is a simple script that reads information provided by the OS and presents it as an HTML page.

The name of the server comes from the the $_SERVER variable. (There's a list of the contents here.) The rest of the data comes from a special Linux file called /proc/loadavg. Your script should read this file, extract the values it contains, and present them in a web page in some readable format. The file contains a single line, looking like this:

0.55 0.12 0.04 2/565 283014
The first three values are load averages. Each is an average number of jobs needing CPU time. The averages are over the last one, five and fifteen minutes. (The CPU capacity is fully utilized when the load average is at least the number of cores. A larger average implies some job waiting.) Sandbox is not all that busy, so zeros are not that uncommon. The fourth number (before the /) is the current number of running processes. The documentation says this cannot exceed the number of cores, though I have seen Sandbox report three, even though it has but two cores. The fifth number is current total number of jobs in the system, and the last one is the id of the most-recently-created process.

You can use your shell account to examine the file directly like this:
[bennet@sandbox load]$ cat /proc/loadavg 0.55 0.12 0.04 2/565 283014 [bennet@sandbox load]$ cat /proc/loadavg 0.04 0.02 0.00 1/558 284313 [bennet@sandbox load]$ cat /proc/loadavg 0.03 0.02 0.00 1/558 284316

To get these values into your script, open the file, and read and parse the values. See the documentation for fscanf. It contains an example that shows pretty much all you need to to get the values. Open the file with fopen, read it with fscanf, then close the file. The example shows a loop reading lines from the file, but the assignment requires only one read for the one-line file. You can find more about any of those other methods by using the search box on the documentation page.

Scan using a string describing the format of the file. Show the literal characters (the spaces and slash) and use % conversions for the data, %f for float numbers, and %i for integers, much as the scanf of plain C. Use the form given in the example: send the handle and the format, and receive the data values as an array.

Your existing Sandbox account allows posting PHP files anywhere you can put HTML files, producing URLs in a similar way, but with a php extension. Copy one of our smaller examples to get the basic layout. If you want to run on your local machine, you'll need to install supporting software. Some IDEs have PHP plugins, or you can install a server with PHP on your machine. That may or may not be worth the trouble; we won't have another PHP assignment. Also, unless you're running Linux, you won't have a /proc/loadavg.

To edit files on Sandbox:

  1. You can edit locally in a ssh window using the simple nano editor, the traditional vi, or emacs.
  2. You can always edit on your own computer and send to Sandbox by whatever scp tool you use.
  3. On Windows, WinSCP allows local editing of remote files.
If you attempt to run a PHP script that has a syntax error, the server will report a 500 error, and not much else. To get a bit more help here, use php from the command line on Sandbox to report errors.
[bennet@sandbox load]$ php -l load.php No syntax errors detected in load.php [bennet@sandbox load]$ php -l bogus.php PHP Parse error: syntax error, unexpected token "else", expecting "," or ";" in bogus.php on line 15 Errors parsing bogus.php [bennet@sandbox load]$
The -l option doesn't run the script, but just asks for the error summary.
If you are getting a 500 or some other error loading your web page, you can see the latest logged PHP errors, like this:
[bennet@sandbox load]$ tail /var/log/php-fpm/www-error.log [31-Oct-2024 22:44:49 America/Chicago] PHP Warning: Undefined array key "url_1" in /var/www/homes/bennet/urlcoll.php on line 153 [31-Oct-2024 22:53:25 America/Chicago] PHP Warning: include(): open_basedir restriction in effect. File(/home/bennet/local_html/inc/highest.inc) is not within the allowed path(s): (/var/www/html:/var/www/homes:/var/wwwwrite:/usr/share/pear:/usr/share/php:/usr/local/share/php:/tmp:/proc) in /var/www/homes/bennet/python/asst/index.php on line 2 [31-Oct-2024 22:53:25 America/Chicago] PHP Warning: include(/home/bennet/local_html/inc/highest.inc): Failed to open stream: Operation not permitted in /var/www/homes/bennet/python/asst/index.php on line 2 [31-Oct-2024 22:53:25 America/Chicago] PHP Warning: include(): Failed opening '/home/bennet/local_html/inc/highest.inc' for inclusion (include_path='.:/usr/share/pear:/usr/share/php:/usr/local/share/php') in /var/www/homes/bennet/python/asst/index.php on line 2 [31-Oct-2024 22:53:25 America/Chicago] PHP Fatal error: Uncaught Error: Call to undefined function go_there() in /var/www/homes/bennet/python/asst/index.php:3 Stack trace: #0 {main} thrown in /var/www/homes/bennet/python/asst/index.php on line 3 [31-Oct-2024 23:02:20 America/Chicago] PHP Warning: Undefined array key "uplink" in /var/www/homes/bennet/php/dump.php on line 105 [31-Oct-2024 23:02:20 America/Chicago] PHP Warning: Undefined array key "uplink" in /var/www/homes/bennet/php/dump.php on line 105
Most of them are mine. Someone's got to keep the logger busy. Many are warnings.

After you post your page on Sandbox, make sure you can view it online, then send me the page URL through this form.

For the curious, the file /proc/loadavg is not actually a disk file. The /proc file system on Linux is actually an interface to kernel data that uses the standard file system calls. For each read, the kernel returns the current data.