百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

monocle

> 编程语言
开源

使用单镜头查看所有 BGP 数据。

181 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

使用单镜头查看所有 BGP 数据。

Monocle

See through all Border Gateway Protocol (BGP) data with a monocle.

Table of Contents

  • Install
    • Using cargo
    • Using homebrew on macOS
    • Using cargo-binstall
  • Library Usage
  • Documentation
  • Usage
    • monocle parse
      • Output Format
      • Filter Files
    • monocle search
    • monocle watch
    • monocle rib
    • monocle time
    • monocle inspect
    • monocle country
    • monocle as2rel
    • monocle pfx2as
    • monocle rpki
      • monocle rpki validate
      • monocle rpki roas
      • monocle rpki aspas
    • monocle ip
    • monocle config
    • monocle server

Install

Using cargo

bash
cargo install monocle

Using homebrew on macOS

bash
brew install monocle

Using cargo-binstall

Install cargo-binstall first:

bash
cargo install cargo-binstall

Then install monocle using cargo binstall

bash
cargo binstall monocle

Using Docker

Pull the pre-built image or build locally:

bash
# Build the image locally
docker build -t bgpkit/monocle:latest .

# Or use docker compose
docker compose build

Run monocle commands:

…

Library Usage

Monocle can also be used as a library in your Rust projects. Add it to your Cargo.toml:

toml
[dependencies]
# Default: full CLI binary with all features
monocle = "1.1"

# Library only - all lenses and database operations
monocle = { version = "1.1", default-features = false, features = ["lib"] }

# Library + HTTP server
monocle = { version = "1.1", default-features = false, features = ["server"] }

Feature Tiers

Monocle uses a simplified feature system with three options:

Feature Description Implies
lib Complete library (database + all lenses + display) -
server HTTP/SSE server for programmatic API access lib
cli (default) Full CLI binary with all functionality lib, server

Documentation

The following documentation files are available in the repository:

File Description
README.md (this file) User-facing CLI and library overview
ARCHITECTURE.md Overall project structure and design principles
DEVELOPMENT.md Contributor guide for adding lenses and fixing bugs
AGENTS.md AI coding agent guidelines and code style
CHANGELOG.md Version history and breaking changes
src/server/README.md HTTP/SSE API specification
src/lens/README.md Lens module patterns and conventions
src/database/README.md Database module overview
examples/README.md Usage examples by feature tier

Architecture

The library is organized into the following core modules:

  • database: All database functionality (requires lib feature)

    • core: Connection management and schema definitions
    • session: One-time storage for search results
    • monocle: Main monocle database with ASInfo, AS2Rel, RPKI, and Pfx2as caching
  • lens: High-level business logic (requires lib feature)

    • time: Time parsing and formatting lens
    • country: Country code/name lookup lens
    • ip: IP information lookup lens
    • parse: MRT file parsing lens with progress tracking
    • search: BGP message search lens with progress tracking
    • rpki: RPKI validation and data lens
    • pfx2as: Prefix-to-AS mapping types
    • as2rel: AS-level relationships lens
    • inspect: Unified AS/prefix inspection lens
  • server: HTTP/SSE API server (requires server feature)

For detailed architecture documentation, see ARCHITECTURE.md.

Example: Using Lenses

rust
use monocle::database::MonocleDatabase;
use monocle::lens::inspect::{InspectLens, InspectQueryOptions};

fn main() -> anyhow::Result {
    // Open the monocle database
    let db = MonocleDatabase::open_in_dir("~/.local/share/monocle")?;
    
    // Create a lens
    let lens = InspectLens::new(&db);
    
    // Query AS information
    let options = InspectQueryOptions::default();
    let results = lens.query_asn(13335, &options)?;
    
    println!("AS{}: {}", results.asn, results.name.unwrap_or_default());
    
    Ok(())
}

Example: Parse MRT Files with Progress

…

Usage

Subcommands:

  • parse: parse individual MRT files
  • search: search for matching messages from all available public MRT files
  • rib: reconstruct final RIB state at one or more arbitrary timestamps
  • server: start a HTTP/SSE server for programmatic access
  • inspect: unified AS and prefix information lookup
  • country: utility to look up country name and code
  • time: utility to convert time between unix timestamp and RFC3339 string
  • as2rel: AS-level relationship lookup between ASNs
  • pfx2as: prefix-to-ASN mapping lookup with RPKI validation
  • rpki: RPKI validation and ROA/ASPA listing
  • ip: IP information lookup
  • config: configuration display and database management (refresh, backup, sources)

Global Options

All commands support the following global options:

  • --format : Output format (table, markdown, json, json-pretty, json-line, psv)
  • --json: Shortcut for --format json-pretty
  • --debug: Print debug information

Top-level help menu:

…

monocle parse

Parsing a single MRT file given a local path or a remote URL.

…

Multi-value Filters

The parse and search commands support filtering by multiple values with OR logic:

bash
# Match elements from ANY of the specified origin ASNs
monocle parse file.mrt -o 13335,15169,8075

# Match ANY of the specified prefixes
monocle parse file.mrt -p 1.1.1.0/24,8.8.8.0/24

# Match elements from ANY of the specified peer ASNs
monocle parse file.mrt -J 174,2914

Negative Filters

Use the ! prefix to exclude values:

bash
# Exclude elements from AS13335
monocle parse file.mrt -o '!13335'

# Exclude elements from AS13335 AND AS15169
monocle parse file.mrt -o '!13335,!15169'

Note: Cannot mix positive and negative values in the same filter.

Filter Files

For large filter sets that exceed command-line argument limits, or for reusable filter definitions, use --filter-file (JSON) or --prefix-file (newline text).

--prefix-file — a plain text file with one prefix per line. Blank lines and # comments are ignored. This is the most ergonomic option for the common RIB-extract → filter-updates workflow:

bash
# Step 1: Extract prefix list from a RIB dump
monocle parse -o 64496 rib.gz | cut -d'|' -f5 | sort -u > prefixes.txt

# Step 2: Filter subsequent updates using the prefix file
monocle parse --prefix-file prefixes.txt updates.gz

Prefix file format:

# Prefixes originated by AS64496 (extracted from RIB)
192.0.2.0/24
198.51.100.0/24

# IPv6 prefixes
2001:db8::/32

--filter-file — a structured JSON file supporting all filter dimensions:

json
{
  "prefixes": ["192.0.2.0/24", "2001:db8::/32"],
  "origin_asns": ["64496"],
  "peer_asns": ["174", "6939"],
  "communities": ["64496:100", "*:200"],
  "as_path_regex": "174 64496$",
  "elem_type": "w",
  "include_sub": true,
  "start_ts": "2025-01-01T00:00:00Z",
  "end_ts": "2025-01-01T01:00:00Z"
}

All fields are optional — include only the dimensions you need. String-typed fields use the same syntax as CLI flags, including ! prefix for negation.

Combining with CLI flags — file-based filters merge with CLI filter flags. Within each dimension, values are unioned (OR logic). For example:

bash
# CLI prefix + file prefixes are unioned
monocle parse -p 10.0.0.0/8 --filter-file filters.json updates.gz

For scalar fields (as_path_regex, elem_type, time fields), CLI flags take precedence over file values. Boolean flags (include_super, include_sub) are OR-ed.

Using both file types together:

bash
monocle parse \
  --filter-file as_filters.json \
  --prefix-file extra_prefixes.txt \
  updates.gz

Prefixes from both sources are unioned into a single prefix filter.

Field Selection

Use -f or --fields to select which columns to display:

bash
# Show only prefix, as_path, and origin
monocle parse file.mrt -f prefix,as_path,origin

# Available fields: type, timestamp, peer_ip, peer_asn, prefix, path_id, as_path,
#   origin_asns, origin, next_hop, local_pref, med, communities, atomic, aggr_asn,
#   aggr_ip, only-to-customer, collector

Output Sorting

Use --order-by and --order to sort the output:

bash
# Sort by timestamp ascending (default)
monocle parse file.mrt --order-by timestamp

# Sort by prefix descending
monocle parse file.mrt --order-by prefix --order desc

Timestamp Format

Use --time-format to change timestamp output format (applies to all output formats including json, json-line, and json-pretty):

bash
# Unix timestamp (default) — numeric `timestamp` field in JSON
monocle parse file.mrt --time-format unix

# RFC3339/ISO 8601 format — string `timestamp` field in JSON
monocle parse file.mrt --time-format rfc3339

Example: parse a remote MRT file and show only announcements for a specific prefix:

…

Output Format

The output contains the following fields:

Field Description
type Message type: announce or withdraw
timestamp Message timestamp in UTC
peer_ip IP address of the BGP peer
peer_asn ASN of the BGP peer
peer_bgp_id BGP Identifier (Router ID) of the peer, when available
prefix Network prefix being announced/withdrawn
as_path AS path (space-separated)
origin Origin type: IGP, EGP, or INCOMPLETE
origin_asns Origin AS number(s)
next_hop Next hop IP address
local_pref Local preference value
med Multi-exit discriminator
communities BGP communities
atomic Atomic aggregate flag
aggr_asn Aggregator ASN
aggr_ip Aggregator IP
only_to_customer OTC attribute (RFC 9234). Select it with --fields only-to-customer; the JSON/PSV key is only_to_customer
unknown Unknown attributes
deprecated Deprecated attributes
collector Collector name (for search results)

JSON output example:

json
{
  "type": "announce",
  "timestamp": "2024-01-01T00:00:44Z",
  "peer_ip": "2001:7f8:4::9d85:1",
  "peer_asn": 40325,
  "peer_bgp_id": "192.0.2.254",
  "prefix": "1.1.1.0/24",
  "as_path": "40325 13335",
  "origin": "IGP",
  "origin_asns": [13335],
  "next_hop": "2001:7f8:4::9d85:1",
  "local_pref": null,
  "med": null,
  "communities": [],
  "atomic": false,
  "aggr_asn": null,
  "aggr_ip": null,
  "only_to_customer": null,
  "unknown": null,
  "deprecated": null,
  "collector": null
}

monocle search

Search for BGP messages from all available public MRT files using BGPKIT Broker.

…

Local Caching

Enable local caching with --use-cache (default XDG cache path) or --cache-dir (custom path):

bash
# Cache MRT files under $XDG_CACHE_HOME/monocle
monocle search -t 2024-01-01 -d 1h -p 1.1.1.0/24 --use-cache

# Cache MRT files to a custom directory
monocle search -t 2024-01-01 -d 1h -p 8.8.8.0/24 --cache-dir /tmp/mrt-cache

# --cache-dir overrides --use-cache when both are specified
mon

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言