Skip to content

Commit 5f5dc2a

Browse files
authored
feature: Peeringdb integration to show the Exchanges and peers for your AS' (librenms#6178)
1 parent a752dc1 commit 5f5dc2a

File tree

1,758 files changed

+178517
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,758 files changed

+178517
-237
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"pear/console_table": "^1.3",
2929
"dapphp/radius": "^2.0",
3030
"php-amqplib/php-amqplib": "^2.0",
31-
"symfony/yaml": "^2.8"
31+
"symfony/yaml": "^2.8",
32+
"rmccue/requests": "^1.7"
3233
},
3334
"require-dev": {
3435
"squizlabs/php_codesniffer": "2.6.*",

daily.php

+4
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,7 @@
170170
);
171171
}
172172
}
173+
174+
if ($options['f'] === 'peeringdb') {
175+
cache_peeringdb();
176+
}

daily.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ main () {
183183
"purgeusers"
184184
"bill_data"
185185
"alert_log"
186-
"rrd_purge");
186+
"rrd_purge"
187+
"peeringdb");
187188
call_daily_php "${options[@]}";
188189
;;
189190
submodules)

doc/Extensions/PeeringDB.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source: Extensions/PeeringDB.md
2+
# PeeringDB Support
3+
4+
LibreNMS has integration with PeeringDB to match up your BGP sessions with the peering exchanges you are connected to.
5+
6+
To enable the integration please do so within the WebUI -> Global Settings -> External Settings -> PeeringDB Integration.
7+
8+
Data will be collated the next time daily.sh is run or you can manually force this by running `php daily.php -f peeringdb`,
9+
the initial collection is delayed for a random amount of time to avoid overloading the PeeringDB API.
10+
11+
Once enabled you will have an additional menu item under Routing -> PeeringDB

html/includes/print-menubar.php

+6
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,12 @@
511511
<li><a href="routing/protocol=bgp/adminstatus=start/state=down/"><i class="fa fa-exclamation-circle fa-fw fa-lg" aria-hidden="true"></i> Alerted BGP (' . $bgp_alerts . ')</a></li>');
512512
}
513513

514+
if (is_admin() === true && $routing_count['bgp'] && $config['peeringdb']['enabled'] === true) {
515+
echo '
516+
<li role="presentation" class="divider"></li>
517+
<li><a href="peering/"><i class="fa fa-hand-o-right fa-fw fa-lg" aria-hidden="true"></i> PeeringDB</a></li>';
518+
}
519+
514520
echo(' </ul>');
515521
?>
516522

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
// Exclude Private and reserved ASN ranges
4+
// 64512 - 65535
5+
// 4200000000 - 4294967295
6+
$sql = " FROM `devices` WHERE `disabled` = 0 AND `ignore` = 0 AND `bgpLocalAs` > 0 AND (`bgpLocalAs` < 64512 OR `bgpLocalAs` > 65535) AND `bgpLocalAs` < 4200000000 ";
7+
8+
if (isset($searchPhrase) && !empty($searchPhrase)) {
9+
$sql .= " AND (`bgpLocalAs` LIKE '%$searchPhrase%')";
10+
}
11+
12+
$count_sql = "SELECT COUNT(*) $sql";
13+
14+
$total = dbFetchCell($count_sql);
15+
if (empty($total)) {
16+
$total = 0;
17+
}
18+
19+
if (!isset($sort) || empty($sort)) {
20+
$sort = 'bgpLocalAs ASC';
21+
}
22+
23+
$sql .= " GROUP BY `bgpLocalAs` ORDER BY $sort";
24+
25+
if (isset($current)) {
26+
$limit_low = (($current * $rowCount) - ($rowCount));
27+
$limit_high = $rowCount;
28+
}
29+
30+
if ($rowCount != -1) {
31+
$sql .= " LIMIT $limit_low,$limit_high";
32+
}
33+
34+
$sql = "SELECT `bgpLocalAs` $sql";
35+
36+
foreach (dbFetchRows($sql) as $asn) {
37+
$astext = get_astext($asn['bgpLocalAs']);
38+
$response[] = array(
39+
'asn' => $asn['bgpLocalAs'],
40+
'asname' => $astext,
41+
'action' => "<a class='btn btn-sm btn-primary' href='" . generate_url(array('page' => 'peering', 'section' => 'ix-list', 'asn' => $asn['bgpLocalAs'])) . "' role='button'>Show connectd IXes</a>",
42+
);
43+
}
44+
45+
$output = array(
46+
'current' => $current,
47+
'rowCount' => $rowCount,
48+
'rows' => $response,
49+
'total' => $total,
50+
);
51+
echo _json_encode($output);

html/includes/table/ix-list.inc.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
*
4+
* LibreNMS PeeringDB Integration
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* @package LibreNMS
20+
* @link http://librenms.org
21+
* @copyright 2017 Neil Lathwood
22+
* @author Neil Lathwood <neil@lathwood.co.uk>
23+
*/
24+
25+
$asn = clean($_POST['asn']);
26+
27+
$sql = " FROM `pdb_ix` WHERE `asn` = ?";
28+
$params = array($asn);
29+
30+
31+
if (isset($searchPhrase) && !empty($searchPhrase)) {
32+
$sql .= " AND (`name` LIKE '%$searchPhrase%')";
33+
}
34+
35+
$count_sql = "SELECT COUNT(*) $sql";
36+
37+
$total = dbFetchCell($count_sql, $params);
38+
if (empty($total)) {
39+
$total = 0;
40+
}
41+
42+
if (!isset($sort) || empty($sort)) {
43+
$sort = 'name ASC';
44+
}
45+
46+
$sql .= " ORDER BY $sort";
47+
48+
if (isset($current)) {
49+
$limit_low = (($current * $rowCount) - ($rowCount));
50+
$limit_high = $rowCount;
51+
}
52+
53+
if ($rowCount != -1) {
54+
$sql .= " LIMIT $limit_low,$limit_high";
55+
}
56+
57+
$sql = "SELECT * $sql";
58+
59+
foreach (dbFetchRows($sql, $params) as $ix) {
60+
$ix_id = $ix['ix_id'];
61+
$response[] = array(
62+
'exchange' => $ix['name'],
63+
'action' => "<a class='btn btn-sm btn-primary' href='" . generate_url(array('page' => 'peering', 'section' => 'ix-peers', 'asn' => $asn, 'ixid' => $ix['ix_id'])) . "' role='button'>Show Peers</a>",
64+
'links' => "<a href='https://peeringdb.com/ix/$ix_id' target='_blank'><i class='fa fa-database'></i></a>",
65+
);
66+
}
67+
68+
$output = array(
69+
'current' => $current,
70+
'rowCount' => $rowCount,
71+
'rows' => $response,
72+
'total' => $total,
73+
);
74+
echo _json_encode($output);

html/includes/table/ix-peers.inc.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
*
4+
* LibreNMS PeeringDB Integration
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* @package LibreNMS
20+
* @link http://librenms.org
21+
* @copyright 2017 Neil Lathwood
22+
* @author Neil Lathwood <neil@lathwood.co.uk>
23+
*/
24+
25+
$asn = clean($_POST['asn']);
26+
$ixid = clean($_POST['ixid']);
27+
$status = clean($_POST['status']);
28+
29+
$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";
30+
$params = array($ixid);
31+
32+
if ($status === 'connected') {
33+
$sql .= " AND `remote_ipaddr4` = `bgpPeerIdentifier` ";
34+
}
35+
36+
if ($status === 'unconnected') {
37+
$sql .= " AND `bgpPeerRemoteAs` IS NULL ";
38+
}
39+
40+
if (isset($searchPhrase) && !empty($searchPhrase)) {
41+
$sql .= " AND (`remote_ipaddr4` LIKE '%$searchPhrase%' OR `remote_asn` LIKE '%$searchPhrase%' OR `P`.`name` LIKE '%$searchPhrase%')";
42+
}
43+
44+
$sql .= ' GROUP BY `bgpPeerIdentifier`, `P`.`name`, `P`.`remote_ipaddr4`, `P`.`peer_id`, `P`.`remote_asn` ';
45+
$count_sql = "SELECT COUNT(*) $sql";
46+
47+
$total = count(dbFetchRows($count_sql, $params));
48+
if (empty($total)) {
49+
$total = 0;
50+
}
51+
52+
if (!isset($sort) || empty($sort)) {
53+
$sort = 'remote_asn ASC';
54+
}
55+
56+
$sql .= " ORDER BY $sort";
57+
58+
if (isset($current)) {
59+
$limit_low = (($current * $rowCount) - ($rowCount));
60+
$limit_high = $rowCount;
61+
}
62+
63+
if ($rowCount != -1) {
64+
$sql .= " LIMIT $limit_low,$limit_high";
65+
}
66+
67+
$sql = "SELECT `P`.`remote_asn`, `P`.`name`, `P`.`remote_ipaddr4`, `P`.`peer_id`, `bgpPeers`.`bgpPeerIdentifier` $sql";
68+
69+
foreach (dbFetchRows($sql, $params) as $peer) {
70+
if ($peer['remote_ipaddr4'] === $peer['bgpPeerIdentifier']) {
71+
$connected = '<i class="fa fa-check fa-2x text text-success"></i>';
72+
} else {
73+
$connected = '<i class="fa fa-times fa-2x text text-default"></i>';
74+
}
75+
$peer_id = $peer['peer_id'];
76+
$response[] = array(
77+
'remote_asn' => $peer['remote_asn'],
78+
'remote_ipaddr4' => $peer['remote_ipaddr4'],
79+
'peer' => $peer['name'],
80+
'connected' => "$connected",
81+
'links' => "<a href='https://peeringdb.com/net/$peer_id' target='_blank'><i class='fa fa-database'></i></a>",
82+
);
83+
}
84+
85+
$output = array(
86+
'current' => $current,
87+
'rowCount' => $rowCount,
88+
'rows' => $response,
89+
'total' => $total,
90+
);
91+
echo _json_encode($output);

html/pages/peering.inc.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
*
4+
* LibreNMS PeeringDB Integration
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* @package LibreNMS
20+
* @link http://librenms.org
21+
* @copyright 2017 Neil Lathwood
22+
* @author Neil Lathwood <neil@lathwood.co.uk>
23+
*/
24+
25+
$no_refresh = true;
26+
27+
switch ($vars['section']) {
28+
case 'ix-list':
29+
require_once 'pages/peering/ix-list.inc.php';
30+
break;
31+
case 'ix-peers':
32+
require_once 'pages/peering/ix-peers.inc.php';
33+
break;
34+
default:
35+
require_once 'pages/peering/as-selection.inc.php';
36+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
*
4+
* LibreNMS PeeringDB Integration
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* @package LibreNMS
20+
* @link http://librenms.org
21+
* @copyright 2017 Neil Lathwood
22+
* @author Neil Lathwood <neil@lathwood.co.uk>
23+
*/
24+
25+
$cache_date = dbFetchCell("SELECT FROM_UNIXTIME(`timestamp`) FROM `pdb_ix` ORDER BY `timestamp` ASC LIMIT 1");
26+
27+
echo "<div class='alert alert-info' role='alert'>Cached date: $cache_date</div>";
28+
29+
?>
30+
31+
<div class="row">
32+
<div class="col-sm-4">
33+
<table id="asn" class="table table-bordered table-hover">
34+
<thead>
35+
<tr>
36+
<th data-column-id="asn">ASN</th>
37+
<th data-column-id="asname" data-searchable="false">AS Name</th>
38+
<th data-column-id="action" data-sortable="false"></th>
39+
</tr>
40+
</thead>
41+
</table>
42+
</div>
43+
</div>
44+
45+
<script>
46+
var grid = $("#asn").bootgrid({
47+
ajax: true,
48+
rowCount: [25,50,100,250,-1],
49+
post: function ()
50+
{
51+
return {
52+
id: 'as-selection',
53+
};
54+
},
55+
url: "ajax_table.php"
56+
});
57+
</script>

0 commit comments

Comments
 (0)