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:
- ID:
24875 - URL: Grafana Dashboard #24875
§Features
- Multi-router support: Collect metrics from multiple
MikroTikdevices - 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 handlerscollector: metrics collection and processingconfig: configuration managementerror: error typesmetrics: metrics parsing and registrymikrotik:MikroTikdevice interactionprelude: 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
- Certificate
Stats MikroTikconnection pool and metric input types Types for router metrics and statistics Certificate information from aMikroTikrouter- Config
- Application configuration Application-wide configuration
- Connection
Pool MikroTikconnection pool and metric input types Connection pool for routers Connection pool for reusingRouterOSconnections- Connection
Tracking Stats MikroTikconnection pool and metric input types Types for router metrics and statistics Connection tracking statistics per source address- Firewall
Rule Stats MikroTikconnection pool and metric input types Types for router metrics and statistics Statistics for firewall rules- Interface
Stats MikroTikconnection pool and metric input types Types for router metrics and statistics Statistics for a network interface- Metrics
Registry - Metrics registry and labels Prometheus metrics registry
- Router
Config - Application configuration
Configuration for a single
MikroTikrouter - Router
Labels - Metrics registry and labels Labels for router-level metrics
- Router
Metrics MikroTikconnection pool and metric input types Types for router metrics and statistics Complete metrics snapshot from a router- System
Resource MikroTikconnection pool and metric input types Types for router metrics and statistics System resource information from aMikroTikrouter- Wire
Guard Peer Stats MikroTikconnection pool and metric input types Types for router metrics and statistics Statistics for aWireGuardpeer
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 RouterOSwire 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