Supervision des extensions enregistré sur un autre IPBX.

Added by Sébastien Le Moal 3 months ago

Tout est dans le titre...
Seul problème, nous avions mis ça en place sur des solutions sous freepbx, elastix.

Mais le plus gros du boulot est la, ne reste qu'a adapté :)

Nous avions créer un VPN entre les 2 IPBX.

  1. Création de l'user mysql distant
  2. Se connecter en ssh sur le serveur distant
    mysql -p_motdepasse -e "CREATE USER 'customhint'@'127.0.0.1' IDENTIFIED BY 'MotDePasseDeLAMortQuiTue';
    GRANT SELECT ON * . *
    TO  'customhint'@'$ipRemoteServer'
    IDENTIFIED BY  'MotDePasseDeLAMortQuiTue'
    WITH MAX_QUERIES_PER_HOUR 0
    MAX_CONNECTIONS_PER_HOUR 0
    MAX_UPDATES_PER_HOUR 0
    MAX_USER_CONNECTIONS 0 ;" 
    
    # Création de l'user AMI distant
    /etc/asterisk/manager_custom.conf
    [customhint]
    secret = MotDePasseDeLAMortQuiTue
    deny=0.0.0.0/0.0.0.0
    permit=127.0.0.1/255.255.255.0 ;Ip Adresse du serveur distant
    read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
    write = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
    
  1. Modifier le fichier /etc/asterisk/extensions_custom.conf
  2. Y Ajouter l'include à la fin du contexte [from-internal-custom]
    include => custom_hint
    

#Puis à la fin du fichier:

#include extensions_custom_hint.conf

  1. Créer le dossier qui contiendra les custom hint
    touch /etc/asterisk/extensions_custom_hint.conf
    
  1. Mettre les bons droits sur les fichiers
    chmod +x -R /var/www/html/modules/remote_hint/
    chmod 777 /etc/asterisk/extensions_custom_hint.conf
    

#Créer un cron tab pour mettre a jours le fichier /etc/asterisk/extensions_custom_hint.conf toute les heures.

crontab -e
# m h  dom mon dow   command
0 */1 * * * php /var/www/html/module/remote_hint/customhint.php

dans /var/www/html/module/remote_hint/ ajouter les 2 fichiers ci-dessous

customhint.php

<?php

/**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** 
 * customhint.php
 * 
 * Created by Sébastien Le Moal (Tiscom)
 * 
 * Licensed under GNU GPL version 2 or any later version.
 * 
 * Usage:
 * This script is meant to be used on a FreePBX system running version 2.9 or higher. 
 * When executed, the script queries the remote elastix server asks its extension list 
 * and create custom device in the file:  /etc/asterisk/extensions_custom_hint.conf
 * 
 **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****/
set_time_limit(5);

// Remote server mysql 
$remote_server = "ip_ipbx_distant";
$user = "customhint";
$pass = "MotDePasseDeLAMortQuiTue";
$database = "asterisk";

// Remote server host/IP, AMI credentials
$remote_name = "customhint";
$remote_secret = "MotDePasseDeLAMortQuiTue";
$remote_context = "from-internal";

$linkID = mysql_connect($remote_server, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT user, description FROM devices ORDER BY description ASC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$monfichier = fopen('/etc/asterisk/extensions_custom_hint.conf', 'w+');
fputs($monfichier, '[customhint]');

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++) {
    $row = mysql_fetch_assoc($resultID);

    // User config param
    $device = 'Custom:Remote'.$row['user'];  // must match a custom hint already defined on the local system
    // Connect to local machine with FreePBX bootstrap, requires FreePBX 2.9 or higher
    if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
        include_once('/etc/asterisk/freepbx.conf');
    }

    // connect to remote Asterisk machine using AMI credentials and get status of Extension 103
    $remote = new AGI_AsteriskManager();
    if ($remote->connect($remote_server, $remote_name, $remote_secret)) {
        $foo = $remote->ExtensionState($row['user'], $remote_context);
        $remote->disconnect();
    } 
    else {
        echo "Can not connect to remote AGI";
    }
    //add custom hints 
    fputs($monfichier, 'exten => '.$row['user'].',hint,Custom:Remote'.$row['user']); 

}
fputs($monfichier, 'exten => n,Return()');
fclose($monfichier);
?>

remotehintpoll.php
<?php

/**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** 
 * remotehintpoll.php
 * 
 * Created by lgaetz 
 * improved by Sébastien Le Moal (Tiscom) 
 *
 * Licensed under GNU GPL version 2 or any later verison.
 * 
 * Usage:
 * This script is meant to be used on a FreePBX system running version 2.9 or higher. When
 * executed, the script polls a remote Asterisk server, checks the status of a specific 
 * extension on the remote server, and then updates a local hint to the same value.  
 * 
 **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****/
set_time_limit(5);

// Remote server ssh 
$remote_server = "ip-distante";
$user = "customhint";
$pass = "MotDePasseDeLAMortQuiTue";
$database = "asterisk";

// Remote server host/IP, AMI credentials and 
$remote_name = "customhint";
$remote_secret = "MotDePasseDeLAMortQuiTue";
$remote_context = "from-internal";

$boucle = 1;

while ($boucle == 1) {
    // Recupération de la liste des utilisateurs distant
    $linkID = mysql_connect($remote_server, $user, $pass) or die("Could not connect to host.");
    mysql_select_db($database, $linkID) or die("Could not find database.");

    $query = "SELECT user, description FROM devices ORDER BY description ASC";
    $resultID = mysql_query($query, $linkID) or die("Data not found.");

    for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++) {
        $row = mysql_fetch_assoc($resultID);

        // User config param
        $device = 'Custom:Remote'.$row['user'];  // must match a custom hint already defined on the local system
        // Connect to local machine with FreePBX bootstrap, requires FreePBX 2.9 or higher
        if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
            include_once('/etc/asterisk/freepbx.conf');
        }

        // connect to remote Asterisk machine using AMI credentials and get status of Extension 103
        $remote = new AGI_AsteriskManager();
        if ($remote->connect($remote_server, $remote_name, $remote_secret)) {
            $foo = $remote->ExtensionState($row['user'], $remote_context);
            $remote->disconnect();
        } 
        else {
            echo "Can not connect to remote AGI";
        }

        //print_r($foo);  //for debug

        // Based on value of remote extension status, change local custom device state to match
        // edit $device to reflect name of local custom device as defined in extensions_custom.conf
        // in the [from-internal-custom] section add a line similar to:
        // exten => 103,hint,Custom:Remote103
        // Make sure that the number does not conflict with something else

        if($astman->connected()) {    
            switch ($foo['Status']) {
                case -1:
                    echo $device.' : Extension not found <br />';
                    $cmd = $astman->Command('devstate change '.$device.' UNKNOWN');
                    break;
                case 0:
                    echo $device.' : Idle <br />';
                    $cmd = $astman->Command('devstate change '.$device.' NOT_INUSE');
                    break;
                case 1:
                    echo $device.' : In Use <br />';
                    $cmd = $astman->Command('devstate change '.$device.' INUSE');
                    break;
                case 2:
                    echo $device.' : Busy <br />';
                    $cmd = $astman->Command('devstate change '.$device.' BUSY');
                    break;
                case 4:
                    echo $device.' : Unavailable <br />';
                    $cmd = $astman->Command('devstate change '.$device.' UNAVAILABLE');
                    break;
                case 8:
                    echo $device.' : Ringing <br />';
                    $cmd = $astman->Command('devstate change '.$device.' RINGING');
                    break;
                case 9:
                    echo $device.' : Ringing <br />';
                    $cmd = $astman->Command('devstate change '.$device.' RINGING');
                    break;
                case 16:
                    echo $device.' : On Hold <br />';
                    $cmd = $astman->Command('devstate change '.$device.' ONHOLD');
                    break;
                default:
                   echo $device.' : Extension not found <br />';
                   $cmd = $astman->Command('devstate change '.$device.' UNKNOWN');            
            }
        } else {
            echo "Can not connect to local AGI";
        }
    }
sleep(1);
}
?>