Listing 5 The Nagios script that connects to the Asterisk Manager and stores
database keys based on the hostname and IP address of the host that
has gone down. It then originates a call to extension 555, which is
the handoff point from Nagios to Asterisk. You pass in the
variables via the command line, which also allows you to run the
script manually for testing.
#!/usr/bin/php -q
<?
if((!isset($argv[1])) || (!isset($argv[2])) || (!isset($argv[3])))
{
echo "\nNagios toAsterisk Notification Script \n";
echo "Usage: callphone.php hostname ipaddress \n\n";
exit;
}
$timeout = 10;
$host = "192.168.7.10";
$port = "5038";
$socket = fsockopen($host,$port, $errno, $errstr, $timeout);
fputs($socket, "Action: Login\r\n");
fputs($socket, "Username: nagiosr\n");
fputs($socket, "Secret: verysecretpassword\r\n\r\n");
storeKey("hostname", $argv[1], $socket);
storeKey("ipaddress", $argv[2], $socket);
placeCall("SIP/nagios_softphone","SIP/nagios_softphone", $socket);
fputs($socket, "Action: Logoff\r\n\r\n");
// call debugInfo($socket) before you close the socket to print
// debugging information
// debugInfo($socket);
// close the socket connection to the asterisk server
fclose($socket);
///////////////////////////////////////////////////////////////////
// FUNCTION DECLARATIONS
///////////////////////////////////////////////////////////////////
function storeKey($key, $keyvalue, $socket)
{
fputs($socket, "Action: DBPut\r\n");
fputs($socket, "Family: nagios\r\n");
fputs($socket, "Key: ".$key."\r\n");
fputs($socket, "Val: ".$keyvalue."\r\n\r\n");
}
function debugInfo($socket)
{
$return = "";
while(!feof($socket))
{
$return .= fgets($socket, 4096);
}
echo $return;
}
function placeCall($channel, $exten, $socket)
{
// channel to the 555 extension which is routed to an AGI script
fputs($socket, "Action: Originate\r\n");
// The commented line below demonstrates an outbound call to
// 1-555-777-1234 with 9 as the dialing prefix.
//fputs($socket, "Channel: Zap/g2/915557771234\r\n");
fputs($socket, "Channel: SIP/nagios_softphone\r\n");
fputs($socket, "Context: nagios\r\n");
fputs($socket, "Exten: ".$exten."\r\n");
fputs($socket, "Priority: 1\r\n\r\n");
}
?>
|