Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 Listing 6 oct2006.tar

Listing 6 This script will be doing the text-to-speech conversion, and playing the resulting file over the phone via the connected call. Save this as nagios-agi.php in your Asterisk /agi-bin/ directory.

#!/usr/bin/php -q
<?

@ob_implicit_flush(true);
@set_time_limit(6);
$in = fopen("php://stdin","r");
$stdlog = fopen("my_agi.log", "w");


// set this value to true if you want a local log generated for this script

$log = true;

// we read in all of the Asterisk variables returned in 
// order to get to our Nagios set variables.

 while ($env=getOutput()) {
   $s = split(": ",$env);
   $agi[str_replace("agi_","",$s[0])] = trim($s[1]);
   if (($env == "") || ($env == "\n")) {
     break;
   }
 } 


// let Asterisk CLI know what's going on.

echo "VERBOSE \"Processing Nagios Request\" 2\n";
getOutput();

// retrieve the hostname of the server requiring attention

sendCommand("DATABASE GET nagios hostname");
$temp_hostname = getOutput();
$hostname = extractVariable($temp_hostname);

// retrieve the IP address of the server

sendCommand("DATABASE GET nagios ipaddress");
$temp_ipaddress = getOutput();
$ipaddress = extractVariable($temp_ipaddress);

// let's craft our string to pass through the text to speech engine

$speech = "Hello, I am calling to let you know that the ?.$hostname.? \
  at ".$ipaddress." is currently unreachable?;


//now we send this to the Festival speech engine

system("echo $speech | text2wave -f 300 -o nagiosalert.ulaw -otype ulaw -");


// move it into the asterisk sound directory
if(file_exists("/var/lib/asterisk/sounds/nagiosalert.ulaw"))
{

     fputs($stdlog, "Alert File Exists, Deleting...\n");
     unlink("/var/lib/asterisk/sounds/nagiosalert.ulaw");
}


copy("nagiosalert.ulaw","../sounds/nagiosalert.ulaw");


 fclose($in);

 fclose($stdlog);

 exit;




// ***************** FUNCTION DECLARATIONS

// this sends commands through the AGI to Asterisk, takes the command 
// string as a parameter

function sendCommand($command)
{
     global $stdlog, $log;
     if($log)
    {
           fputs($stdlog, "Executed: $command\n");
     }

     echo $command."\n";
}
function getOutput()
{
    
     global $in, $log, $stdlog;
     $input = str_replace("\n", "", fgets($in, 4096));
    
     if($log)
    {
         fputs($stdlog, "Read In: $input\n");
     }

    return $input;
}



function extractVariable($temp_string)
{

      preg_match("/\((.*)\)/", $temp_string, $matchArray);

      if($log)
     {
      fputs($stdlog, "Variable Value Extracted: :".$matchArray[1]);
      }

     return $matchArray[1];
}

?>