feat(cluster): add configurable BLF expiry interval in HA menu with 120s default and automatic check-sync notify on failover

This commit is contained in:
sorcier committed 2026-11-06 01:20:16 +01:00
1 parent c1c6eab03c
commit f14185d597
6 files changed
+145 -17

No files matched your search

+31 -12
View File
@@ -42,6 +42,7 @@ class ClusterManager
'cluster_secret' => bin2hex(random_bytes(16)),
'vrid' => 51,
'priority' => 100,
'blf_expiry' => 120,
'sync_interval' => 15,
'auto_sync' => 1,
'last_sync_at' => null,
@@ -52,6 +53,9 @@ class ClusterManager
if (empty($row['peer_port'])) {
$row['peer_port'] = 8443;
}
if (empty($row['blf_expiry'])) {
$row['blf_expiry'] = 120;
}
return $row;
}
@@ -70,6 +74,7 @@ class ClusterManager
$clusterSecret = trim((string)($data['cluster_secret'] ?? ''));
$vrid = max(1, min(255, (int)($data['vrid'] ?? 51)));
$priority = max(1, min(254, (int)($data['priority'] ?? ($role === 'master' ? 100 : 90))));
$blfExpiry = max(30, min(3600, (int)($data['blf_expiry'] ?? 120)));
$syncInterval = max(5, min(3600, (int)($data['sync_interval'] ?? 15)));
$autoSync = !empty($data['auto_sync']) ? 1 : 0;
@@ -130,6 +135,7 @@ class ClusterManager
cluster_secret = :secret,
vrid = :vrid,
priority = :prio,
blf_expiry = :blf_expiry,
sync_interval = :sync_int,
auto_sync = :auto_sync,
updated_at = CURRENT_TIMESTAMP
@@ -137,23 +143,34 @@ class ClusterManager
');
$stmt->execute([
':enabled' => $enabled,
':role' => $role,
':vip' => $virtualIp,
':vmask' => $virtualNetmask,
':iface' => $interface,
':peer' => $peerIp,
':peer_port' => $peerPort,
':secret' => $clusterSecret,
':vrid' => $vrid,
':prio' => $priority,
':sync_int' => $syncInterval,
':auto_sync' => $autoSync,
':enabled' => $enabled,
':role' => $role,
':vip' => $virtualIp,
':vmask' => $virtualNetmask,
':iface' => $interface,
':peer' => $peerIp,
':peer_port' => $peerPort,
':secret' => $clusterSecret,
':vrid' => $vrid,
':prio' => $priority,
':blf_expiry' => $blfExpiry,
':sync_int' => $syncInterval,
':auto_sync' => $autoSync,
]);
// Application automatique de la configuration Keepalived
$applyResult = $this->applyKeepalivedConfig();
// Régénération de la configuration PJSIP et rechargement Asterisk avec le nouveau délai d'abonnement
try {
$gen = new ConfigGenerator($this->db);
$gen->writeAllConfigs();
shell_exec('asterisk -rx "pjsip reload" 2>/dev/null || true');
shell_exec('asterisk -rx "dialplan reload" 2>/dev/null || true');
} catch (\Throwable $e) {
error_log("[CLUSTER] Erreur régénération configs Asterisk : " . $e->getMessage());
}
return [
'success' => true,
'message' => 'Configuration Haute Disponibilité enregistrée et appliquée.',
@@ -723,6 +740,7 @@ class ClusterManager
virtual_ip = :virtual_ip,
virtual_netmask = :virtual_netmask,
vrid = :vrid,
blf_expiry = :blf_expiry,
last_sync_at = CURRENT_TIMESTAMP,
last_sync_status = :status,
last_sync_message = :msg
@@ -737,6 +755,7 @@ class ClusterManager
':virtual_ip' => $localClusterSettings['virtual_ip'] ?? '',
':virtual_netmask' => $localClusterSettings['virtual_netmask'] ?? '24',
':vrid' => $localClusterSettings['vrid'] ?? 51,
':blf_expiry' => (int)($localClusterSettings['blf_expiry'] ?? 120),
':status' => 'synced',
':msg' => 'Base et sons synchronisés depuis le Master',
]);
+53 -5
View File
@@ -15,8 +15,9 @@ class ConfigGenerator
public const MOH_CONF_PATH = '/etc/asterisk/musiconhold.custom.conf';
public const RTP_CONF_PATH = '/etc/asterisk/rtp.conf';
public const FEATURES_CONF_PATH = '/etc/asterisk/features.conf';
public const VOICEMAIL_CONF_PATH = '/etc/asterisk/voicemail.custom.conf';
public const BACKUP_DIR = __DIR__ . '/../storage/backups';
public const VOICEMAIL_CONF_PATH = '/etc/asterisk/voicemail.custom.conf';
public const PJSIP_NOTIFY_CONF_PATH = '/etc/asterisk/pjsip_notify.conf';
public const BACKUP_DIR = __DIR__ . '/../storage/backups';
public function __construct(private PDO $db) {}
@@ -187,6 +188,18 @@ class ConfigGenerator
$lines[] = "; ===========================================================================";
$lines[] = "; POSTES TÉLÉPHONIQUES (EXTENSIONS)";
$lines[] = "; ===========================================================================";
// Détection de l'état du Cluster Haute Disponibilité (HA)
$clusterSt = [];
try {
$clusterSt = $this->db->query('SELECT enabled, blf_expiry FROM cluster_settings WHERE id = 1')->fetch(PDO::FETCH_ASSOC) ?: [];
} catch (\Throwable $e) {}
$isHaActive = !empty($clusterSt['enabled']);
// En mode Haute Disponibilité, on plafonne l'expiration des AOR/souscriptions sur la valeur configurée (par défaut 120s)
// afin que les téléphones se réabonnent très vite en cas de bascule. Sans HA, on conserve le standard 1800s (30 min).
$regExpiration = $isHaActive ? max(30, min(3600, (int)($clusterSt['blf_expiry'] ?? 120))) : 1800;
$subMinExpiry = $isHaActive ? 30 : 0;
foreach ($extensions as $ext) {
$num = trim((string)$ext['extension']);
$name = str_replace('"', '', trim((string)$ext['name']));
@@ -274,9 +287,9 @@ class ConfigGenerator
$lines[] = "type = aor";
$lines[] = "max_contacts = {$maxContacts}";
$lines[] = "remove_existing = yes";
$lines[] = "default_expiration = 120";
$lines[] = "maximum_expiration = 300";
$lines[] = "minimum_expiration = 60";
$lines[] = "default_expiration = {$regExpiration}";
$lines[] = "maximum_expiration = {$regExpiration}";
$lines[] = "minimum_expiration = " . ($isHaActive ? 30 : 60);
$lines[] = "qualify_frequency = 30";
if ($hasVm) {
$lines[] = "mailboxes = {$num}@default";
@@ -1874,6 +1887,39 @@ class ConfigGenerator
return implode("\n", $lines) . "\n";
}
/**
* Génère la configuration des événements SIP Notify (/etc/asterisk/pjsip_notify.conf)
* Profils check-sync pour Yealink, Fanvil, Snom, Polycom, etc.
*/
public function generatePjsipNotifyConf(): string
{
return <<<EOF
; ===========================================================================
; CONFIGURATION DES NOTIFICATIONS SIP PJSIP - NexusPBX
; Profils de resynchronisation immédiate (check-sync) Yealink, Fanvil, Snom, etc.
; ===========================================================================
[check-sync]
Event=>check-sync\;reboot=false
[yealink-check-cfg]
Event=>check-sync\;reboot=false
[snom-check-cfg]
Event=>check-sync\;reboot=false
[polycom-check-cfg]
Event=>check-sync
[cisco-check-cfg]
Event=>check-sync
[aastra-check-cfg]
Event=>check-sync
EOF;
}
public function writeAllConfigs(): array
{
if (!is_dir(self::BACKUP_DIR)) {
@@ -1886,6 +1932,7 @@ class ConfigGenerator
$rtpContent = $this->generateRtpConf();
$featuresContent = $this->generateFeaturesConf();
$voicemailContent = $this->generateVoicemailConf();
$notifyContent = $this->generatePjsipNotifyConf();
$this->writeAtomicWithPermissions(self::PJSIP_CONF_PATH, $pjsipContent);
$this->writeAtomicWithPermissions(self::EXTENSIONS_CONF_PATH, $extensionsContent);
@@ -1893,6 +1940,7 @@ class ConfigGenerator
$this->writeAtomicWithPermissions(self::RTP_CONF_PATH, $rtpContent);
$this->writeAtomicWithPermissions(self::FEATURES_CONF_PATH, $featuresContent);
$this->writeAtomicWithPermissions(self::VOICEMAIL_CONF_PATH, $voicemailContent);
$this->writeAtomicWithPermissions(self::PJSIP_NOTIFY_CONF_PATH, $notifyContent);
// S'assurer que pjsip.conf et extensions.conf incluent bien les fichiers .custom.conf
SystemChecker::ensureAsteriskIncludes();
+6
View File
@@ -329,6 +329,7 @@ class Database
cluster_secret VARCHAR(64) NOT NULL DEFAULT '',
vrid INTEGER NOT NULL DEFAULT 51,
priority INTEGER NOT NULL DEFAULT 100,
blf_expiry INTEGER NOT NULL DEFAULT 120,
sync_interval INTEGER NOT NULL DEFAULT 15,
auto_sync INTEGER NOT NULL DEFAULT 1,
last_sync_at DATETIME DEFAULT NULL,
@@ -813,6 +814,11 @@ class Database
$db->exec("ALTER TABLE cluster_settings ADD COLUMN peer_port INTEGER NOT NULL DEFAULT 8443");
} catch (\Throwable $e) {}
// Migration de la colonne blf_expiry si elle n'existe pas encore
try {
$db->exec("ALTER TABLE cluster_settings ADD COLUMN blf_expiry INTEGER NOT NULL DEFAULT 120");
} catch (\Throwable $e) {}
$count = (int)$db->query('SELECT COUNT(*) FROM cluster_settings')->fetchColumn();
if ($count === 0) {
$defaultSecret = bin2hex(random_bytes(16));