Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peers listing endpoint #61

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@

All notable changes to this project will be documented in this file.

## v0.7.6 - 2024-10-31

### Highlights

* migrate default broker API endpoint to `https://api.bgpkit.com/v3/broker`
* Full API docs is available at `https://api.bgpkit.com/docs`
* add `get_peers` to `BgpkitBroker` struct
* fetches the list of peers for a given collector
* can specify filters the same way as querying MRT files
* available filter functions include:
* `.peers_asn(ASN)`
* `.peers_ip(IP)`
* `.collector_id(COLLECTOR_ID)`
* `.peers_only_full_feed(TRUE/FALSE)`
* returns `Vec<BrokerPeer>`

```rust
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct BrokerPeer {
/// The date of the latest available data.
pub date: NaiveDate,
/// The IP address of the collector peer.
pub ip: IpAddr,
/// The ASN (Autonomous System Number) of the collector peer.
pub asn: u32,
/// The name of the collector.
pub collector: String,
/// The number of IPv4 prefixes.
pub num_v4_pfxs: u32,
/// The number of IPv6 prefixes.
pub num_v6_pfxs: u32,
/// The number of connected ASNs.
pub num_connected_asns: u32,
}
```

## v0.7.5 - 2024-08-23

### [NEW] deploy at fly.io
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ FROM debian:bookworm-slim
# copy the build artifact from the build stage
COPY --from=build /my_project/target/release/bgpkit-broker /usr/local/bin/bgpkit-broker

RUN apt update && apt install -y curl tini
WORKDIR /bgpkit-broker

EXPOSE 40064
ENTRYPOINT bash -c '/usr/local/bin/bgpkit-broker serve bgpkit-broker.sqlite3 --bootstrap --silent'
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/bgpkit-broker"]
CMD ["serve", "bgpkit-broker.sqlite3", "--bootstrap", "--silent"]
36 changes: 36 additions & 0 deletions examples/peers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! This example retrieves a list of full-feed MRT collector peers from route-views.amsix and print
//! out the top 10 peers with the most connected ASNs.
//!
//! Example output
//! ```text
//! 2024-10-31,route-views.amsix,58511,80.249.212.104,2567,960791,0
//! 2024-10-31,route-views.amsix,267613,80.249.213.223,2268,965321,0
//! 2024-10-31,route-views.amsix,267613,2001:7f8:1:0:a500:26:7613:1,2011,0,206667
//! 2024-10-31,route-views.amsix,12779,80.249.209.17,1932,951788,0
//! 2024-10-31,route-views.amsix,9002,2001:7f8:1::a500:9002:1,1896,0,202069
//! 2024-10-31,route-views.amsix,38880,80.249.212.75,1883,992214,0
//! 2024-10-31,route-views.amsix,58511,2001:7f8:1::a505:8511:1,1853,0,216981
//! 2024-10-31,route-views.amsix,9002,80.249.209.216,1318,956345,0
//! 2024-10-31,route-views.amsix,42541,80.249.212.84,1302,952091,0
//! 2024-10-31,route-views.amsix,12779,2001:7f8:1::a501:2779:1,1247,0,201726
//! ```

fn main() {
let broker = bgpkit_broker::BgpkitBroker::new()
.collector_id("route-views.amsix")
.peers_only_full_feed(true);
let mut peers = broker.get_peers().unwrap();
peers.sort_by(|a, b| b.num_connected_asns.cmp(&a.num_connected_asns));
for peer in peers.iter().take(10) {
println!(
"{},{},{},{},{},{},{}",
peer.date,
peer.collector,
peer.asn,
peer.ip,
peer.num_connected_asns,
peer.num_v4_pfxs,
peer.num_v6_pfxs,
);
}
}
1 change: 1 addition & 0 deletions examples/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bgpkit_broker::{BgpkitBroker, BrokerItem};

pub fn main() {
let broker = BgpkitBroker::new()
.broker_url("http://dev.api.bgpkit.com/v3/broker")
.ts_start("1634693400")
.ts_end("1634693400")
.collector_id("rrc00,route-views2");
Expand Down
14 changes: 6 additions & 8 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ primary_region = 'lax'

[http_service]
internal_port = 40064
force_https = true
force_https = false
auto_stop_machines = 'off'
processes = ['app']

[[http_service.checks]]
grace_period = "120s"
interval = "60s"
grace_period = "60s"
interval = "30s"
method = "GET"
timeout = "5s"
path = "/health?max_delay_secs=3600"


[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
size = 'shared-cpu-4x'


[deploy]
strategy = "rolling"
max_unavailable = 1
wait_timeout = "20m"
strategy = "bluegreen"
wait_timeout = "20m"
59 changes: 59 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use clap::{Parser, Subcommand};
use futures::StreamExt;
use itertools::Itertools;
use std::collections::{HashMap, HashSet};
use std::net::IpAddr;
use std::path::Path;
use std::process::exit;
use tabled::settings::Style;
Expand Down Expand Up @@ -185,6 +186,29 @@ enum Commands {
json: bool,
},

/// List public BGP collector peers
Peers {
/// filter by collector ID
#[clap(short, long)]
collector: Option<String>,

/// filter by peer AS number
#[clap(short = 'a', long)]
peer_asn: Option<u32>,

/// filter by peer IP address
#[clap(short = 'i', long)]
peer_ip: Option<IpAddr>,

/// show only full-feed peers
#[clap(short, long)]
full_feed_only: bool,

/// Print out search results in JSON format instead of Markdown table
#[clap(short, long)]
json: bool,
},

/// Streaming live from a broker NATS server
Live {
/// URL to NATS server, e.g. nats://localhost:4222.
Expand Down Expand Up @@ -640,6 +664,41 @@ fn main() {
println!("{}", Table::new(items).with(Style::markdown()));
}
}

Commands::Peers {
collector,
peer_asn,
peer_ip,
full_feed_only,
json,
} => {
let mut broker = BgpkitBroker::new();
// health check first
if broker.health_check().is_err() {
println!("broker instance at {} is not available", broker.broker_url);
return;
}
if let Some(collector_id) = collector {
broker = broker.collector_id(collector_id);
}
if let Some(asn) = peer_asn {
broker = broker.peers_asn(asn);
}
if let Some(ip) = peer_ip {
broker = broker.peers_ip(ip);
}
if full_feed_only {
broker = broker.peers_only_full_feed(true);
}
let items = broker.get_peers().unwrap();

if json {
println!("{}", serde_json::to_string_pretty(&items).unwrap());
} else {
println!("{}", Table::new(items).with(Style::markdown()));
}
}

Commands::Live {
url,
subject,
Expand Down
Loading
Loading