There are various system and server related information which server administrators always need to have as soon as possible, infact I must say in real time. There are several open and closed source softwares in the market which can generate almost real time notifications for you. Most famous one being Nagios. In this blog post I will discuss, how to generate real time system notifications using PHP and XMPP. Specifically, I will present sample script using Jaxl (Jabber XMPP Client Library) for generating real time system load notifications, which can be received using any Instant Messengers.
/proc/loadavg
We will be using system /proc/loadavg file to get real time system load information. If you are unaware about this file, here is in brief how this file is helpful to us:
sabhinav:~# cat /proc/loadavg 0.22 0.12 0.09 1/68 12621
where first three columns measure the CPU and IO utilization of last one, five and 10 minute periods. The fourth column shows the number of currently running processes and the total number of processes. The last column displays the last process ID used.
jaxl4serveradmins.class.php
We will be using Jaxl PHP client library for handling the XMPP part. jaxl4serveradmins.class.php is an extension to Jaxl, providing various server administration helper function. Below is the code for server administration extension:
include_once("xmpp.class.php");
define('JAXL_SERVER_ADMIN', 'mailsforabhinav@gmail.com');
define('JAXL_SERVER_LOAD_POLL_INTERVAL', 10);
class JAXL extends XMPP {
function eventMessage($fromJid, $content, $offline = FALSE) {
}
function eventPresence($fromJid, $status, $photo) {
}
function eventNewEMail($total,$thread,$url,$participation,$messages,$date,$senders,$labels,$subject,$snippet) {
// Not used here. See jaxl4gmail.class.php for it's use case
}
function setStatus() {
// Set a custom status or use $this->status
$this->sendStatus($this->status);
print "Setting Status...\n";
print "Done\n";
$this->addJob(JAXL_SERVER_LOAD_POLL_INTERVAL, array($this, 'parseServerLoad'));
}
function parseServerLoad() {
$loadavg = file_get_contents('/proc/loadavg');
$this->sendMessage(JAXL_SERVER_ADMIN, $loadavg);
}
}
I have utilized addJob() method provided by Jaxl library, using which you can specify a callback to be called after every N seconds (in short a periodic cron). Here we add a periodic job to be runned every JAXL_SERVER_LOAD_POLL_INTERVAL seconds. parseServerLoad() method is called as the callback function.
$this->addJob(JAXL_SERVER_LOAD_POLL_INTERVAL, array($this, 'parseServerLoad'));
To keep the demo simple, I am simply sending the content of /proc/loadavg file as a message to server admins.
function parseServerLoad() {
$loadavg = file_get_contents('/proc/loadavg');
$this->sendMessage(JAXL_SERVER_ADMIN, $loadavg);
}
Running it for your servers:
Follow the following steps to get this started on your server (only Unix, no Windows):
- Checkout from Jaxl trunk
sabhinav:~# sudo svn checkout http://jaxl.googlecode.com/svn/trunk/ jaxl-read-only
- Enter checked out directory
sabhinav:~# cd jaxl-read-only
- Enter your server admin IM contact details
sabhinav:~# sudo vim config.ini.php define('JAXL_SERVER_ADMIN', 'webmaster@foobar.com'); - Enable server administration extension
sabhinav:~# sudo vim index.php include_once("jaxl4serveradmins.class.php"); // include_once("jaxl.class.php"); - Wroom Wroom, start Jaxl
sabhinav:~# sudo php index.php Starting TLS Encryption... Attempting PLAIN Authentication... Starting Session... Requesting Feature List... Requesting Roster List... Setting Status... Done
Tail the jaxl log file in case you are facing any difficulties in the setup.
sabhinav:~# tail -f log/logger.log
You should also consider adding /proc/ directory under open_basedir in php.ini file.
Is it working?
If all is well configured server admins will start getting notifications every 10 seconds which is default value for JAXL_SERVER_LOAD_POLL_INTERVAL.

Writing custom notifications
Above I demonstrate how we can use XMPP and PHP to generate real time system notification. However, you may want to modify parseServerLoad() method to send notifications only when the server load exceeds a certain value. You may also want to add other methods which can notify you of various System and Server level parameters in a similar fashion. Below are a few useful system administration commands:
sabhinav:~# free -m sabhinav:~# vmstat 1 20
Is it really real time?
Since, parseServerLoad() method polls for /proc/loadavg file every 10 seconds, this is not exactly real time. However you can configure JAXL_SERVER_LOAD_POLL_INTERVAL to make it poll faster. You can also use libevent extension in PHP to make it real time in real sense.
Do let me know if you write any interesting functionality, I will be more than happy to include it as a part of current extension.
Pingback: Get real time system & server load notification on any IM using … | Coder Online
Pingback: abcphp.com
Pingback: Abhinav Singh’s Blog: Get real time system & server load notification on any IM using PHP and XMPP | Webs Developer
Pingback: Abhinav Singh’s Blog: Get real time system & server load notification on any IM using PHP and XMPP | Development Blog With Code Updates : Developercast.com
Pingback: Tweets that mention Get real time system & server load notification on any IM using PHP and XMPP | Abhi's Weblog -- Topsy.com
Pingback: vivanno.com::aggregator » Archive » Messagerie instantanée avec PHP & XMPP
Pingback: Recibe información del servidor por IM (Jabber) | Sentido Web
Pingback: Messagerie instantanée avec PHP & XMPP | traffic-internet.net
Pingback: Delicious Bookmarks for March 20th through March 21st « Lâmôlabs
Pingback: Links for 2010-01-19 | Stéphane Thibault