2017-03-22 10:17:13 +00:00
< ? php
/**
* LibreNMS PeeringDB Integration
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-03-22 10:17:13 +00:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2018-03-25 22:50:09 +02:00
* @copyright 2018 Neil Lathwood
2017-03-22 10:17:13 +00:00
* @author Neil Lathwood <neil@lathwood.co.uk>
*/
2021-03-28 17:25:30 -05:00
$asn = strip_tags ( $vars [ 'asn' ]);
$ixid = strip_tags ( $vars [ 'ixid' ]);
$status = strip_tags ( $vars [ 'status' ]);
2017-03-22 10:17:13 +00:00
$sql = ' FROM `pdb_ix_peers` AS `P` LEFT JOIN `pdb_ix` ON `P`.`ix_id` = `pdb_ix`.`ix_id` LEFT JOIN `bgpPeers` ON `P`.`remote_ipaddr4` = `bgpPeers`.`bgpPeerIdentifier` WHERE `P`.`ix_id` = ? AND `remote_ipaddr4` IS NOT NULL' ;
$params = [ $ixid ];
if ( $status === 'connected' ) {
$sql .= ' AND `remote_ipaddr4` = `bgpPeerIdentifier` ' ;
}
if ( $status === 'unconnected' ) {
$sql .= ' AND `bgpPeerRemoteAs` IS NULL ' ;
}
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
2020-07-10 16:17:09 +02:00
$sql .= ' AND (`remote_ipaddr4` LIKE ? OR `remote_asn` LIKE ? OR `P`.`name` LIKE ?)' ;
$params [] = " % $searchPhrase % " ;
$params [] = " % $searchPhrase % " ;
$params [] = " % $searchPhrase % " ;
2017-03-22 10:17:13 +00:00
}
$sql .= ' GROUP BY `bgpPeerIdentifier`, `P`.`name`, `P`.`remote_ipaddr4`, `P`.`peer_id`, `P`.`remote_asn` ' ;
$count_sql = " SELECT COUNT(*) $sql " ;
$total = count ( dbFetchRows ( $count_sql , $params ));
if ( empty ( $total )) {
$total = 0 ;
}
if ( ! isset ( $sort ) || empty ( $sort )) {
$sort = 'remote_asn ASC' ;
}
$sql .= " ORDER BY $sort " ;
if ( isset ( $current )) {
2023-03-13 22:32:22 +01:00
$limit_low = (( $current * $rowCount ) - $rowCount );
2017-03-22 10:17:13 +00:00
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
$sql = " SELECT `P`.`remote_asn`, `P`.`name`, `P`.`remote_ipaddr4`, `P`.`peer_id`, `bgpPeers`.`bgpPeerIdentifier` $sql " ;
foreach ( dbFetchRows ( $sql , $params ) as $peer ) {
if ( $peer [ 'remote_ipaddr4' ] === $peer [ 'bgpPeerIdentifier' ]) {
$connected = '<i class="fa fa-check fa-2x text text-success"></i>' ;
} else {
$connected = '<i class="fa fa-times fa-2x text text-default"></i>' ;
}
$peer_id = $peer [ 'peer_id' ];
$response [] = [
2024-01-05 05:39:12 +01:00
'remote_asn' => $peer [ 'remote_asn' ],
2017-03-22 10:17:13 +00:00
'remote_ipaddr4' => $peer [ 'remote_ipaddr4' ],
2024-01-05 05:39:12 +01:00
'peer' => $peer [ 'name' ],
'connected' => " $connected " ,
'links' => " <a href='https://peeringdb.com/asn/ { $peer [ 'remote_asn' ] } ' target='_blank'><i class='fa fa-database'></i></a> " ,
2017-03-22 10:17:13 +00:00
];
}
$output = [
2024-01-05 05:39:12 +01:00
'current' => $current ,
2017-03-22 10:17:13 +00:00
'rowCount' => $rowCount ,
2024-01-05 05:39:12 +01:00
'rows' => $response ,
'total' => $total ,
2017-03-22 10:17:13 +00:00
];
2021-03-04 07:55:41 -06:00
echo json_encode ( $output , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );