Previously I put together a post which describes how to build a Kiosk computer using Ubuntu, Blackbox and Firefox. I’m following that up with details on how to monitor the kiosk so that you can be notified when/if the computer or services fail. I’ll break this post into two sections.
1) Monitoring the computer services and network
2) Monitoring FireFox performance
Monitoring the computer services and network
Because our Kiosk computer is an Ubuntu server and running apache/php/mysql locally there are several open source network and service monitoring programs. The one I found most suitable for my solution is called Monit and I found a very good post at Ubuntu Geek describing how to install monit on ubuntu and configure it. I basically followed the Ubuntu Geek tutorial but made some modifications to the config file. My revised process is below.
- Install Monit
sudo apt-get install monit
- Configure Monit. Open /etc/monit/monitrc in your favorite text editor. Below is an example of how I set up my own configuration file. It should be pretty self explanatory.
## Start monit in background (run as daemon) and check the services at 2-minute## intervals.set daemon 120 ## Set syslog logging with the 'daemon' facility.set logfile syslog facility log_daemon ## Set list of mailservers for alert delivery.## I use my ISP's SMTP server for better reliability and means## I don't need an smtp server running on my Kioskset mailserver mail.shawcable.com ## Use event queue if mailserver unavailableset eventqueuebasedir /var/monit # set the base directory where events will be storedslots 100 # optionaly limit the queue size ## You can set the alert recipient hereset alert someone@domain.com # Monitor Apachecheck process apache2 with pidfile /var/run/apache2.pid # Action to be taken when apache failsstart program = "/etc/init.d/apache2 start"stop program = "/etc/init.d/apache2 start"# Admin will notify by mail if below the condition satisfied belowif cpu is greater than 60% for 2 cycles then alertif cpu > 60% for 5 cycles then restartif children > 10 then alertif children > 50 then restartif loadavg(5min) greater than 10 for 8 cycles then stopif 3 restarts within 5 cycles then timeoutgroup servers # Monitor MySQLcheck process mysql with pidfile /var/run/mysqld/mysqld.pidgroup databasestart program = "/etc/init.d/mysql start"stop program = "/etc/init.d/mysql stop"if failed host 127.0.0.1 port 3306 then restartif failed host 127.0.0.1 port 3306 then alertif 5 restarts within 5 cycles then timeout # Monitor SSH Servicecheck process sshd with pidfile /var/run/sshd.pidstart program = "/etc/init.d/ssh start"stop program = "/etc/init.d/ssh stop"if failed port 22 protocol ssh then restartif failed port 22 protocol ssh then alertif 5 restarts within 5 cycles then timeoutgroup programs # Check servicescheck system localhostif loadavg (1min) > 4 then alertif loadavg (5min) > 2 then alertif memory usage > 75% then alertif cpu usage (user) > 70% then alertif cpu usage (system) > 80% then alertif cpu usage (wait) > 20% then alert
- Set Monit to start automagically. Open the file /etc/default/monit and change the startup value to 1. You can now start monit with the following command.
sudo /etc/init.d/monit start
If you want to be able to access Monit’s web based interface remotely then check out the Ubuntu Geek tutorial for more information. I am not enabling this ability in my kiosk at present.
Monitoring FireFox performance
By using Monit we are able to get good alerts regarding the overall health of our Kiosk. But Monit doesn’t tell us is how our Kiosk client (Firefox) is behaving. If Firefox starts to eat up a percentage of your kiosk’s available memory or CPU power you should be notified early.
I myself am not that great at building BASH scripts so I opted to create a PHP script which tests a few conditions and sends an email if Firefox isn’t running or is using too many resources. This script can be run as a cron job every few minutes. My PHP script requires the open source class PHPMailer which makes sending email from PHP a snap.
Here is my php script.
/*** This script gets the CPU and MEM usage of Firefox*/$cpu = 0;$mem = 0;$failed = false;
// Get the PID of firefox$pid = exec('pidof firefox-bin');
// If firefox is running get memoryif ( $pid ){$status_str = exec('ps aux | grep "'. $pid .'" | grep -v grep');$status = explode( ' ', $status_str );// Strip blanksforeach( $status as $k=>$v ){ if ( trim($v) == '' ) unset($status[$k]);}$status = array_values($status);
$cpu = $status[2];$mem = $status[3];
if ( $cpu >= 60 || $mem >= 60 ){ $failed = true; $message = "Firefox is using $cpu% of CPU and $mem% of MEM";}
} else {
// Firefox is not running$failed = true;$message = "Firefox is NOT running";
}
if ( $failed ){$body = date('l jS of F Y h:i:s A',time() )."nn";$body .= $message;
// Create and Send Emailrequire_once( "/class/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();$mail->From = ' firefox@mykiosk ';$mail->FromName = " Firefox Status ";$mail->Host = 'mail.shawcable.com';$mail->Mailer = "smtp";
$mail->Subject = "Firefox Issue on ArtTouch";$mail->AddAddress(" someone@domain.com ");
$mail->IsHtml(0);$mail->Body = $body;
// LOG RESULTSif(! $mail->Send() ) { error_log("There was an error ending Firefox Performance Alert " . $mail->ErrorInfo );}}
If anyone wants to turn this into a bash script instead of PHP and share it that would be great.
Thanks for the informative post. I dread things like this sometimes. It’s rather confusing. What I do is just run my computer through a computer check up at one of those free websites like http://www.pcaholic.com.
Thank you for the comments.