Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active December 21, 2022 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaetz/ce6ac96ca6de0e60177213ede50e7955 to your computer and use it in GitHub Desktop.
Save lgaetz/ce6ac96ca6de0e60177213ede50e7955 to your computer and use it in GitHub Desktop.
#!/usr/bin/php -q
<?php
/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
*
* Latest Version: https://gist.github.com/lgaetz/ce6ac96ca6de0e60177213ede50e7955
*
* Description:
*
* Script lgaetz-trunk-monitor.php is used in conjunction with the "Monitor Trunk Failures" field in FreePBX
* trunks. Outbound call failures will trigger this script which can be used as a notification.
*
* Save a copy in the Asterisk agi-bin folder, probably /var/lib/asterisk/agi-bin and ensure
* it's owned by the asterisk user and make exectuable.
*
* Usage:
*
* Populate the 'Monitor Trunk Failures' field with "lgaetz-trunk-monitor.php" (wihtout quotes). If this field is
* not showing when you edit a trunk, enable "Display Monitor Trunk Failures Option" in Advanced Settings.
*
* License:
* Released as GNU GPL/2
*
* Version Info:
* 2020-06-27 Rough start, COVID-19 edition - script pretty much does nothing yet
*
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/
// user params
$verbose = false;
// Set up environment, assumes files phpagi.php and sql.php are in the same folder
// use /var/lib/asterisk/agi-bin
require('phpagi.php');
// Initialize classes for FreePBX
$AGI = new AGI();
if ($verbose) $AGI->verbose("Executing lgaetz-trunk-monitor.php script in verbose mode", 3);
// determine trunk name for failed call from channel variables, different method depending on whether it's chan_sip or chan_pjsip
// TODO: add IAX2 logic
$dial_trunk=get_var("DIAL_TRUNK");
if ($verbose) $AGI->verbose("dial_trunk:".$dial_trunk, 3);
$out_dial_trunk_suffix=get_var("OUT_".$dial_trunk."_SUFFIX");
if ($verbose) $AGI->verbose("****************out_dial_trunk_suffix:".$out_dial_trunk_suffix, 3);
if ($out_dial_trunk_suffix=="") {
// chan_sip trunk
$trunk_name=get_var("OUT_".$dial_trunk);
} else {
//pjsip trunk
$trunk_name=get_var("OUT_".$dial_trunk."_SUFFIX");
}
// confirmed working with pjsip and chan_sip trunks, will fail on IAX2
$AGI->verbose("Oubound call faled on trunk: ".$trunk_name, 3);
//TODO: get outbound number, failure cause, etc
//TODO: send email notification
// function to query the value of an Asterisk channel variable
function get_var($value) {
global $AGI;
$r = $AGI->get_variable( $value );
if ($r['result'] == 1) {
$result = $r['data'];
return trim($result);
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment