Skip to main content

Crate mikrotik_exporter

Crate mikrotik_exporter 

Source
Expand description

§MikroTik Exporter

Prometheus exporter for MikroTik RouterOS devices.

This library provides functionality to collect metrics from MikroTik routers via the RouterOS API and expose them in Prometheus format.

§Installation

To use this library in your project, add it to your Cargo.toml:

[dependencies]
mikrotik-exporter = "0.3"

To install the exporter as a binary, use cargo:

cargo install mikrotik-exporter

§Grafana Dashboard

A pre-configured Grafana dashboard is available:

§Features

  • Multi-router support: Collect metrics from multiple MikroTik devices
  • Asynchronous architecture: Efficient concurrent collection using connection pooling
  • Comprehensive metrics: Interface statistics, system resources, connection tracking, WireGuard
  • Built-in connection pooling: Automatic connection management with exponential backoff
  • Delta calculation: Automatic counter delta calculation for accurate rate metrics
  • Startup connectivity testing: Optional connectivity verification during application startup
  • Health checking: Built-in health endpoint with router status monitoring

§Quick Start

use std::sync::Arc;
use tokio::sync::watch;
use mikrotik_exporter::{
    AppState, Config, ConnectionPool, MetricsRegistry, Result, create_router,
    start_collection_loop,
};

#[tokio::main]
async fn main() -> Result<()> {
    let config = Config::from_env();
    let metrics = MetricsRegistry::new();
    let pool = Arc::new(ConnectionPool::new());
    let state = Arc::new(AppState {
        config: config.clone(),
        metrics: metrics.clone(),
        pool: pool.clone(),
    });

    let (_shutdown_tx, shutdown_rx) = watch::channel(false);
    start_collection_loop(shutdown_rx, Arc::new(config), metrics, pool);

    let app = create_router(state);
    let listener = tokio::net::TcpListener::bind("0.0.0.0:9090").await?;
    axum::serve(listener, app.into_make_service()).await?;
    Ok(())
}

§Configuration

Configuration can be loaded from environment variables using Config::from_env(). See Config documentation for available options including startup connectivity testing.

§Main modules

  • api: HTTP API handlers
  • collector: metrics collection and processing
  • config: configuration management
  • error: error types
  • metrics: metrics parsing and registry
  • mikrotik: MikroTik device interaction
  • prelude: commonly used types and traits

§Performance Optimizations

  • DashMap-based metrics registry: Lock-free concurrent access for better performance
  • Efficient delta calculations: Minimal overhead for counter metric processing
  • Connection pooling: Reuse connections to reduce authentication overhead
  • Incremental cleanup: Periodic cleanup of stale metrics to prevent memory growth

Modules§

prelude
Prelude module for convenient imports

Structs§

AppState
HTTP API router and state Application state shared with endpoints
CertificateStats
MikroTik connection pool and metric input types Types for router metrics and statistics Certificate information from a MikroTik router
Config
Application configuration Application-wide configuration
ConnectionPool
MikroTik connection pool and metric input types Connection pool for routers Connection pool for reusing RouterOS connections
ConnectionTrackingStats
MikroTik connection pool and metric input types Types for router metrics and statistics Connection tracking statistics per source address
FirewallRuleStats
MikroTik connection pool and metric input types Types for router metrics and statistics Statistics for firewall rules
InterfaceStats
MikroTik connection pool and metric input types Types for router metrics and statistics Statistics for a network interface
MetricsRegistry
Metrics registry and labels Prometheus metrics registry
RouterConfig
Application configuration Configuration for a single MikroTik router
RouterLabels
Metrics registry and labels Labels for router-level metrics
RouterMetrics
MikroTik connection pool and metric input types Types for router metrics and statistics Complete metrics snapshot from a router
SystemResource
MikroTik connection pool and metric input types Types for router metrics and statistics System resource information from a MikroTik router
WireGuardPeerStats
MikroTik connection pool and metric input types Types for router metrics and statistics Statistics for a WireGuard peer

Enums§

AppError
Application error and result type Main application error type

Functions§

create_router
HTTP API router and state Creates the main Axum router with all endpoints
encode_length
RouterOS wire protocol length encoding (public for tests)
start_collection_loop
Metrics collection loop Starts the background metrics collection loop

Type Aliases§

Result
Application error and result type Convenient alias for Result with application error