When something goes wrong with your provider (server issues, DNS issues etc), the corresponding gateway in FusionPBX fails and you won't probably notice this for a while until someone contacts you through other means. This has happened to me on many occasions, so I wrote this script to check gateway status using CRON and send out email alerts in case there's a failure. The script would also attempt to restart FreeSWITCH if it is not running.
gwfailalert.php
Note:- You'll need an MTA running on your system for the script to send out emails; I use sSMTP which is very easy to set up. Popular MTAs are Sendmail, Postfix and Exim in no particular order.
gwfailalert.php
PHP:
<?php
/* Author: EasyBB on fusionpbxforums.
* Permission is hereby granted to anyone to use this code for any purpose
* they see fit. This code comes with no warranties of any kind,
* implied or expressed and this author is not responsible for any damage
* or loss resulting from using this code directly or indirectly.
* You may edit or modify the code as you like, again, at your own risk.
*/
//************************ gwfailalert.php *****************************
// Last edited: 2016 11 01.
// Tested on: Debian 8 Jessie.
// Dependencies: mail transfer agent (MTA), mailutils, php5+
//
// This PHP script is to be used with FreeSWITCH/ FusionPBX server to send
// email alert upon Gateway failure.
//
// You'll need a working mail transfer agent (ssmtp, sendmail etc) running
// on the FusionPBX server. https://help.ubuntu.com/community/EmailAlerts
// ssmtp is the simplest of all. Remember to disable other MTAs if you wish
// to go with ssmtp:
// http://linuxpitstop.com/install-ssmtp-to-send-emails-to-gmail-and-office3655/
//
// Please note that the script needs to be invoked from CRON.
// Recommended to set 30 min interval.
// e.g. */30 * * * * /usr/bin/php /root/gwfailalert.php > /dev/null 2>&1
//**********************************************************************
/*******************************************
******* EDIT BELOW VALUES TO SUIT *********
******************************************/
// Enter the email to receive alerts (mandatory)
$mailto = "your_to_email@gmail.com";
// Enter the from email address or leave it as it is.
$mailfrom = "your_from_email@gmail.com";
// Enter a friendly sender name or leave it as it is.
$fromname = "FusionPBX";
// Enter an email subject or leave it as it is.
$mailsub = "FusionPBX failure alert!";
// Location of fs_cli command.
$fscli = "/usr/bin/fs_cli";
//$fscli = "/usr/local/fs_cli";
/*******************************************
****** DO NOT EDIT BELOW THIS LINE *******
******************************************/
function checkFailed(){
global $fscli;
$checkgw = trim(shell_exec($fscli . ' -x "sofia profile external gwlist down"'));
if($checkgw == "-ERR no reply"){
return false;
}else{
return explode(" ", $checkgw);
}
}
function sendMail($msg){
global $fromname, $mailfrom, $mailsub, $mailto;
// Format from name
$from = $fromname . "\<" . $mailfrom . "\>";
// Email command structure
$recipe = 'echo "%s" | mail -a "Content-Type: text/html" -s "%s" -aFrom:%s "%s"';
// Format email command
$cooked = sprintf($recipe,$msg,$mailsub,$from,$mailto);
// Send email
shell_exec($cooked);
}
function checkFS(){
$fs = trim(shell_exec('pidof "freeswitch"'));
if(! strlen($fs)){
return false;
}else{
return true;
}
}
$msg = "";
if(! checkFS()){
$msg .= "FreeSWITCH isn't running, trying to restart... <br />";
$msg .= shell_exec('systemctl restart freeswitch.service');
sleep(5);
if(! checkFS()){
$msg .= "<br /> FreeSWITCH wouldn't start, ATTEND IMMEDIATELY; exiting script... <br />";
sendMail($msg);
exit;
}else{
$msg .= "<br /> FreeSWITCH is up and running... <br />";
}
}
for($i = 1; $i <= 2; $i++){
if(checkFailed()){
foreach ($failed = checkFailed() as $gw){
shell_exec($fscli . ' -x "sofia profile external killgw ' . $gw .'"');
}
sleep(2);
shell_exec($fscli . ' -x "sofia profile external rescan"');
}
if($i < 2){
sleep(5);
}else{
sleep(1);
}
}
if(checkFailed()){
$xmlstatus = shell_exec($fscli . ' -x "sofia xmlstatus"');
$xml = simplexml_load_string($xmlstatus);
$msg .= "<br /> FusionPBX Status: <br /> <br />";
// Get gateway status
foreach($xml-> gateway as $element){
$msg .= $element->type . " : " . $element->data . " : " . $element->state . "<br />";
}
$msg .= "<br />";
// Get SIP profile status
foreach($xml -> profile as $element){
$msg .= $element->type . " : " . $element->name . " : " . $element->state . "<br />";
}
$msg .= "<br />";
// Get ALIAS status
foreach($xml -> alias as $element){
$msg .= $element->type . " : " . $element->name . " : " . $element->data . " : " . $element->state . "<br />";
}
sendMail($msg);
}
?>
Note:- You'll need an MTA running on your system for the script to send out emails; I use sSMTP which is very easy to set up. Popular MTAs are Sendmail, Postfix and Exim in no particular order.
Last edited: