mikrotik_exporter/lib.rs
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2025 Jesof
3
4//! # `MikroTik` Exporter
5//!
6//! Prometheus exporter for `MikroTik` `RouterOS` devices.
7//!
8//! This library provides functionality to collect metrics from `MikroTik` routers
9//! via the `RouterOS` API and expose them in Prometheus format.
10//!
11//! ## Installation
12//!
13//! To use this library in your project, add it to your `Cargo.toml`:
14//!
15//! ```toml
16//! [dependencies]
17//! mikrotik-exporter = "0.3"
18//! ```
19//!
20//! To install the exporter as a binary, use cargo:
21//!
22//! ```bash
23//! cargo install mikrotik-exporter
24//! ```
25//!
26//! ## Grafana Dashboard
27//!
28//! A pre-configured Grafana dashboard is available:
29//! - **ID:** `24875`
30//! - **URL:** [Grafana Dashboard #24875](https://grafana.com/grafana/dashboards/24875-mikrotik-router-monitoring/)
31//!
32//! ## Features
33//!
34//! - **Multi-router support**: Collect metrics from multiple `MikroTik` devices
35//! - **Asynchronous architecture**: Efficient concurrent collection using connection pooling
36//! - **Comprehensive metrics**: Interface statistics, system resources, connection tracking, `WireGuard`
37//! - **Built-in connection pooling**: Automatic connection management with exponential backoff
38//! - **Delta calculation**: Automatic counter delta calculation for accurate rate metrics
39//! - **Startup connectivity testing**: Optional connectivity verification during application startup
40//! - **Health checking**: Built-in health endpoint with router status monitoring
41//!
42//! ## Quick Start
43//!
44//! ```rust,no_run
45//! use std::sync::Arc;
46//! use tokio::sync::watch;
47//! use mikrotik_exporter::{
48//! AppState, Config, ConnectionPool, MetricsRegistry, Result, create_router,
49//! start_collection_loop,
50//! };
51//!
52//! #[tokio::main]
53//! async fn main() -> Result<()> {
54//! let config = Config::from_env();
55//! let metrics = MetricsRegistry::new();
56//! let pool = Arc::new(ConnectionPool::new());
57//! let state = Arc::new(AppState {
58//! config: config.clone(),
59//! metrics: metrics.clone(),
60//! pool: pool.clone(),
61//! });
62//!
63//! let (_shutdown_tx, shutdown_rx) = watch::channel(false);
64//! start_collection_loop(shutdown_rx, Arc::new(config), metrics, pool);
65//!
66//! let app = create_router(state);
67//! let listener = tokio::net::TcpListener::bind("0.0.0.0:9090").await?;
68//! axum::serve(listener, app.into_make_service()).await?;
69//! Ok(())
70//! }
71//! ```
72//!
73//! ## Configuration
74//!
75//! Configuration can be loaded from environment variables using `Config::from_env()`.
76//! See `Config` documentation for available options including startup connectivity testing.
77//!
78//! ## Main modules
79//! - `api`: HTTP API handlers
80//! - `collector`: metrics collection and processing
81//! - `config`: configuration management
82//! - `error`: error types
83//! - `metrics`: metrics parsing and registry
84//! - `mikrotik`: `MikroTik` device interaction
85//! - `prelude`: commonly used types and traits
86//!
87//! ## Performance Optimizations
88//!
89//! - **DashMap-based metrics registry**: Lock-free concurrent access for better performance
90//! - **Efficient delta calculations**: Minimal overhead for counter metric processing
91//! - **Connection pooling**: Reuse connections to reduce authentication overhead
92//! - **Incremental cleanup**: Periodic cleanup of stale metrics to prevent memory growth
93
94mod api;
95mod collector;
96mod config;
97mod error;
98mod metrics;
99mod mikrotik;
100pub mod prelude;
101
102// Re-export commonly used types
103/// Application configuration
104pub use config::{Config, RouterConfig};
105
106/// Application error and result type
107pub use error::{AppError, Result};
108
109/// HTTP API router and state
110pub use api::{AppState, create_router};
111
112/// Metrics collection loop
113pub use collector::start_collection_loop;
114
115/// Metrics registry and labels
116pub use metrics::{MetricsRegistry, RouterLabels};
117
118/// `MikroTik` connection pool and metric input types
119pub use mikrotik::{
120 CertificateStats, ConnectionPool, ConnectionTrackingStats, FirewallRuleStats, InterfaceStats,
121 RouterMetrics, SystemResource, WireGuardPeerStats,
122};
123
124/// `RouterOS` wire protocol length encoding (public for tests)
125pub use mikrotik::encode_length;