Add-on function to Sam's voxinfo class

This function checks to see if any of the services on specified servers are listed as down in the Vox info XML page, and sends an email to specified addresses if they are. Thanks to Sam for his fine script. This is nowhere near as elegant as his work. It can easily be modified to send emails on every call, even if services are "on".


Usage: You can run it in a script with cron or call it in your 'status' display page (e.g. Sam's template).


Add this function to the voxinfo class:

////////////////////////////////////////////////////////////////////////////
//  Function to send email if a service is listed as 'off' 
//  in the Vox XML output; call this function after parsing the doc
//
//  $emailTo: the recipient email or comma-seperated emails
//  $emailFrom: the sender's email
//  $thisServer: name of the server this script resides on. if it's on a Vox server
//    this value should be one of the Vox server names, i.e. Moray, Octopus, etc.
//    if this script is not on a Vox server, it can be anything or empty

function sendemail($emailTo='', $emailFrom='', $thisServer='') {

//can't do anything without a recipient email
if(empty($emailTo)) return;

//fake the sender's email
if(empty($emailFrom)) $emailFrom = $_SERVER['SERVER_ADMIN'];

//got some server info, see if any service is 'off'
if($this->numservers > 0) {
  $dontSend = false;
  foreach( $this->servers as $server => $services){
    $loop = 1;
    foreach($services as $service => $value){
      if($value == 'off') {
      //oops, it's SMTP on this server...can't send an email...
        if( strtolower($server) == strtolower($thisServer) && $service == 'SMTP') {
          $dontSend = true;
        }
        if($loop==1) $errorMsg .= '<br>'.$server.': <br>';
        $message .= $service.': '.$value.'<br>';
        $loop++;
      }
    }
    if(isset($message)) $affectedServers[] = $server;
  }
}

//Build and send
if(isset($message) && !$dontSend) {

	$subject = 'Voxinfo Report: '.implode(', ',$affectedServers);

	$message = 'Timestamp: '.date("F j, Y, g:i a").'<br>Status: <br>'.$message;

	$headers = "From: <$emailFrom>\r\n";
	$headers .= "X-Sender: <$emailFrom>\r\n";
	$headers .= "X-Mailer: PHP\r\n"; 
	$headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal
	$headers .= "Return-Path: <$emailFrom>\r\n";
	$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";

	@mail($emailTo, $subject, $message, $headers);
}
return;
}

A simple script to call it:

<?php

//the recipient email
$emailTo = '[email protected]';

//the sender's email
$emailFrom = '[email protected]';

//name of the server this script is on. if it's on a Vox server
//this value should be one of the Vox server names, i.e. Moray, Octopus, etc.
//if this script is not on a Vox server, it can be anything or empty
$thisServer = 'Moray';

require 'voxinfo.php';

// create the voxinfo object
//remove array to parse all servers
$vi = new voxinfo(array('Moray', 'Octopus'));//

//get, parse and send the vox xml doc
if( $vi->getinfo() ) {
	$vi->sendemail($emailTo,$emailFrom,$thisServer);
}


?>

If you prefer, you can place:

$vi->sendemail('[email protected]','[email protected]','Koi');

in the status.php script and just have it run when someone browses that page. Remember to call sendemail() after getinfo() or doit().

The generated email looks something like this:

name: Moray
ip: 64.21.80.13
FTP: off
HTTP: off

name: Octopus
ip: 64.21.80.9
FTP: off
HTTP: off
MySql: on
POP3: on
SMTP: on
uptime: As of 4:40pm up 9 days

That's it. Thanks.

dv