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', '[email protected]'); 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 "Donen"; $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', '[email protected]');
- 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
alternatively
cat /proc/loadaverage | sendxmpp [email protected]
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
iknow it uses XMPP to send messages, but I do not know how to use this at all. It is my understanding that I should be able to make a bot which chats for me when I am away if I were to create my own client page, which would parse the chats with my data. Where would I begin if I wanted to create a custom client, and how could I make it parse messages and autorespond in a set manner? My intended usage: autoresponder for when I am AFK, with a decent AI (which I can make.)
For all your custom needs you will have to code it down for yourself. Jaxl is a library which hides xmpp internals from it’s users, by providing convinient methods to write your app.
AI etc all should be written by the developers. If you need customization, kindly contact me through email. Hopefully I will be able to help you out a bit.
Yeah I’m with you acekard. It doesn’t seem to work in any event. I just get a php relocation error no matter what I do.
Hi,
What are you trying to do when you get PHP relocation error. Can you elaborate?
I am just following the instructions, set up all the config in the includes (the same credentials work fine with a Perl bot I created) and when I issues ‘php index.php” I get the php relocation error.
Hi kblack,
Doing a quick google tells me it’s more of a problem with your PHP installation and not with the library itself. http://www.google.co.in/search?q=php+relocation+error Hope it helps.
Abhinav
Thanks for trying to help me, I discovered the problem is centered around how OpenSSL is handled within the framework.
2 problems, first the script includes can’t accurately detect the OS and thinks its Windows, which is strange because it is vanilla CentOS 5.
Second is the includes aren’t getting along with OpenSSL within the jaxl framework.
PHP behaves normally within all other instances. So I think I have to mutate the script somewhere. When I figure that out I’ll post it back here…
Pingback: Delicious Bookmarks for March 20th through March 21st « Lâmôlabs
Pingback: Links for 2010-01-19 | Stéphane Thibault
is it possible to use jaxl library with php and mysql with out installing it…
What exactly do u mean by without installing it? Jaxl library is written in PHP, all you need is to download, extract and include core/jaxl.class.php to get started.
See sample applications for more info on how to use Jaxl library.
MySQL is something which you can always connect to 🙂
Announcing Jaxl v3.x – asynchronous, non-blocking I/O, event based PHP client/server library – http://abhinavsingh.com/blog/2012/07/announcing-jaxl-v3-x-asynchronous-non-blocking-io-event-based-php-clientserver-library/