@@ -211,7 +211,7 @@ public function getList(): array
211
211
public function postGetRemotesList (): array
212
212
{
213
213
$ query = 'SELECT ns.id, ns.ns_ip_address as ip, ns.name FROM nagios_server as ns ' .
214
- 'JOIN remote_servers as rs ON rs.ip = ns.ns_ip_address ' .
214
+ 'JOIN remote_servers as rs ON rs.server_id = ns.id ' .
215
215
'WHERE rs.is_connected = 1 ' ;
216
216
$ statement = $ this ->pearDB ->query ($ query );
217
217
@@ -469,6 +469,7 @@ public function postLinkCentreonRemoteServer(): array
469
469
470
470
// add server to the list of remote servers in database (table remote_servers)
471
471
$ this ->addServerToListOfRemotes (
472
+ (int ) $ serverId ,
472
473
$ serverIP ,
473
474
$ centreonPath ,
474
475
$ httpMethod ,
@@ -532,6 +533,7 @@ public function authorize($action, $user, $isInternal = false)
532
533
/**
533
534
* Add server ip in table of remote servers
534
535
*
536
+ * @param int $serverId the poller id
535
537
* @param string $serverIP the IP of the server
536
538
* @param string $centreonPath the path to access to Centreon
537
539
* @param string $httpMethod the method to access to server (HTTP/HTTPS)
@@ -540,42 +542,54 @@ public function authorize($action, $user, $isInternal = false)
540
542
* @param bool $noProxy to do not use configured proxy
541
543
*/
542
544
private function addServerToListOfRemotes (
545
+ int $ serverId ,
543
546
string $ serverIP ,
544
547
string $ centreonPath ,
545
548
string $ httpMethod ,
546
549
string $ httpPort ,
547
550
bool $ noCheckCertificate ,
548
551
bool $ noProxy
549
552
): void {
550
- $ dbAdapter = $ this ->getDi ()[\Centreon \ServiceProvider::CENTREON_DB_MANAGER ]->getAdapter ('configuration_db ' );
551
- $ date = date ('Y-m-d H:i:s ' );
552
-
553
- $ sql = 'SELECT * FROM `remote_servers` WHERE `ip` = ? ' ;
554
- $ dbAdapter ->query ($ sql , [$ serverIP ]);
555
- $ hasIpInTable = (bool )$ dbAdapter ->count ();
553
+ $ currentDate = date ('Y-m-d H:i:s ' );
556
554
557
- if ($ hasIpInTable ) {
558
- $ sql = 'UPDATE `remote_servers` SET
559
- `is_connected` = ?, `connected_at` = ?, `centreon_path` = ?,
560
- `no_check_certificate` = ?, `no_proxy` = ?
561
- WHERE `ip` = ? ' ;
562
- $ data = ['1 ' , $ date , $ centreonPath , ($ noCheckCertificate ?: 0 ), ($ noProxy ?: 0 ), $ serverIP ];
563
- $ dbAdapter ->query ($ sql , $ data );
555
+ $ statement = $ this ->pearDB ->prepare ('SELECT 1 FROM `remote_servers` WHERE `server_id` = :server_id ' );
556
+ $ statement ->bindValue (':server_id ' , $ serverId , \PDO ::PARAM_INT );
557
+ $ statement ->execute ();
558
+ $ remoteAlreadyExists = (bool ) $ statement ->rowCount ();
559
+
560
+ if ($ remoteAlreadyExists ) {
561
+ $ updateStatement = $ this ->pearDB ->prepare (
562
+ 'UPDATE `remote_servers` SET
563
+ `is_connected` = 1, `connected_at` = :connected_at, `centreon_path` = :centreon_path,
564
+ `no_check_certificate` = :no_check_certificate, `no_proxy` = :no_proxy, `ip_address` = :ip_address
565
+ WHERE `server_id` = :server_id '
566
+ );
567
+ $ updateStatement ->bindValue (':connected_at ' , $ currentDate , \PDO ::PARAM_STR );
568
+ $ updateStatement ->bindValue (':centreon_path ' , $ centreonPath , \PDO ::PARAM_STR );
569
+ $ updateStatement ->bindValue (':no_check_certificate ' , $ noCheckCertificate ? '1 ' : '0 ' , \PDO ::PARAM_STR );
570
+ $ updateStatement ->bindValue (':no_proxy ' , $ noProxy ? '1 ' : '0 ' , \PDO ::PARAM_STR );
571
+ $ updateStatement ->bindValue (':ip_address ' , $ serverIP , \PDO ::PARAM_STR );
572
+ $ updateStatement ->bindValue (':server_id ' , $ serverId , \PDO ::PARAM_INT );
573
+ $ updateStatement ->execute ();
564
574
} else {
565
- $ data = [
566
- 'ip ' => $ serverIP ,
567
- 'app_key ' => '' ,
568
- 'version ' => '' ,
569
- 'is_connected ' => '1 ' ,
570
- 'created_at ' => $ date ,
571
- 'connected_at ' => $ date ,
572
- 'centreon_path ' => $ centreonPath ,
573
- 'http_method ' => $ httpMethod ,
574
- 'http_port ' => $ httpPort ?: null ,
575
- 'no_check_certificate ' => $ noCheckCertificate ?: 0 ,
576
- 'no_proxy ' => $ noProxy ?: 0
577
- ];
578
- $ dbAdapter ->insert ('remote_servers ' , $ data );
575
+ $ insertStatement = $ this ->pearDB ->prepare (
576
+ 'INSERT INTO `remote_servers`
577
+ (`ip`, `app_key`, `version`, `is_connected`, `created_at`, `connected_at`, `centreon_path`,
578
+ `http_method`, `http_port`, `no_check_certificate`, `no_proxy`, `server_id`)
579
+ VALUES
580
+ (:ip_address, "", "", 1, :created_at, :connected_at, :centreon_path, :http_method, :http_port,
581
+ :no_check_certificate, :no_proxy, :server_id) '
582
+ );
583
+ $ insertStatement ->bindValue (':ip_address ' , $ serverIP , \PDO ::PARAM_STR );
584
+ $ insertStatement ->bindValue (':created_at ' , $ currentDate , \PDO ::PARAM_STR );
585
+ $ insertStatement ->bindValue (':connected_at ' , $ currentDate , \PDO ::PARAM_STR );
586
+ $ insertStatement ->bindValue (':centreon_path ' , $ centreonPath , \PDO ::PARAM_STR );
587
+ $ insertStatement ->bindValue (':http_method ' , $ httpMethod , \PDO ::PARAM_STR );
588
+ $ insertStatement ->bindValue (':http_port ' , $ httpPort ?: null , \PDO ::PARAM_INT );
589
+ $ insertStatement ->bindValue (':no_check_certificate ' , $ noCheckCertificate ? '1 ' : '0 ' , \PDO ::PARAM_STR );
590
+ $ insertStatement ->bindValue (':no_proxy ' , $ noProxy ? '1 ' : '0 ' , \PDO ::PARAM_STR );
591
+ $ insertStatement ->bindValue (':server_id ' , $ serverId , \PDO ::PARAM_INT );
592
+ $ insertStatement ->execute ();
579
593
}
580
594
}
581
595
0 commit comments