Skip to content
Open
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
5 changes: 3 additions & 2 deletions rust-plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ edition = "2024"
lalrpop = "0.23.1"

[dependencies]
env_logger = "0.11.11"
lalrpop-util = { version = "0.23.1", features = ["lexer"] }
lexopt = "0.3.2"
log = "0.4.33"
rasn = "0.28.13"
rasn-smi = "0.28.13"
rasn-snmp = "0.28.13"
regex = "1.13.1"
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.151"
snafu = "0.9.2"
tracing = "0.1"
tracing-chrome = "0.7"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[dev-dependencies]
criterion = { version = "0.8.2", features = ["html_reports"] }
Expand Down
35 changes: 35 additions & 0 deletions rust-plugins/examples/new-traffic-rate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"collect": {
"snmp": [
{
"name": "if",
"oid": "1.3.6.1.2.1.2.2.1",
"query": "Walk",
"labels": {
"1.2": "descr",
"1.10": "in",
"1.16": "out"
},
"rate": true
}
]
},
"compute": {
"metrics": [
{
"name": "traffic.in.bytespersecond",
"value": "{if.in}",
"prefix": "{if.descr}",
"uom": "B/s",
"threshold-suffix": "in"
},
{
"name": "traffic.out.bytespersecond",
"value": "{if.out}",
"prefix": "{if.descr}",
"uom": "B/s",
"threshold-suffix": "out"
}
]
}
}
2 changes: 1 addition & 1 deletion rust-plugins/src/compute/ast.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Abstract syntax tree and expression evaluation.

use crate::snmp::SnmpResult;
use log::{info, trace, warn};
use std::str;
use tracing::{info, trace, warn};

/// An expression node in the AST.
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions rust-plugins/src/compute/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Lexical analyzer for tokenizing mathematical expressions.

use log::{error, trace};
use std::str;
use tracing::{error, trace};

/// Type alias for LALRPOP's expected token type with location and error information.
pub type Spanned<Tok, Loc, Error> = Result<(Loc, Tok, Loc), Error>;
Expand Down Expand Up @@ -177,7 +177,7 @@ mod test {
use crate::compute::lexer::{Lexer, Tok};

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
let _ = tracing_subscriber::fmt().with_test_writer().try_init();
}

#[test]
Expand Down
14 changes: 10 additions & 4 deletions rust-plugins/src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use self::ast::ExprResult;
use self::lexer::{LexicalError, Tok};
use crate::snmp::SnmpResult;
use lalrpop_util::{ParseError, lalrpop_mod};
use log::{debug, trace};
use regex::Regex;
use serde::Deserialize;
use tracing::{debug, trace};

lalrpop_mod!(grammar);

Expand Down Expand Up @@ -108,7 +108,13 @@ impl<'a> Parser<'a> {
/// Replaces `{identifier}` with values from SNMP results, handling both
/// scalar and vector values appropriately.
pub fn eval_str(&self, expr: &'a str) -> Result<ExprResult, String> {
let re = Regex::new(r"\{[a-zA-Z_][a-zA-Z0-9_.]*\}").unwrap();
// Compiled once for the whole process: eval_str runs for every
// template of every metric, recompiling the regex each time is waste.
static MACRO_RE: std::sync::OnceLock<Regex> = std::sync::OnceLock::new();
let re = MACRO_RE.get_or_init(|| {
// The pattern is a compile-time constant: it cannot fail to build.
Regex::new(r"\{[a-zA-Z_][a-zA-Z0-9_.]*\}").expect("static regex")
});
let mut suffix = expr;
let mut result: ExprResult = ExprResult::Empty;
trace!("[eval_str] suffix: {:?} - re: {:?}", &suffix, &re);
Expand Down Expand Up @@ -153,11 +159,11 @@ impl<'a> Parser<'a> {
mod test {
use crate::compute::{Parser, ast::ExprResult, grammar, lexer};
use crate::snmp::SnmpResult;
use log::{debug, info};
use std::collections::HashMap;
use tracing::{debug, info};

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
let _ = tracing_subscriber::fmt().with_test_writer().try_init();
}

#[test]
Expand Down
43 changes: 43 additions & 0 deletions rust-plugins/src/generic/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,49 @@ pub enum Error {
))]
FailedToConnectToHost { url: String, os: String },

#[snafu(display(
"SNMP agent returned an error: {} (status {}, index {})",
name,
status,
index
))]
SnmpAgentError {
name: &'static str,
status: u32,
index: u32,
},

#[snafu(display("SNMP agent is misbehaving: OID {} is not increasing during walk", oid))]
OidNotIncreasing { oid: String },

#[snafu(display(
"SNMP walk aborted: the agent returned more than {} values for a single subtree",
max
))]
WalkTooLarge { max: usize },

#[snafu(display("SNMP collection exceeded the global timeout of {}s", seconds))]
CollectTimeout { seconds: u64 },

#[snafu(display(
"Could not persist the state file {} ({}): rates would be computed against a stale reference",
path,
reason
))]
StatefileWrite { path: String, reason: String },

#[snafu(display(
"No valid SNMP response from {} after {} attempts (timeout {}s per attempt)",
url,
attempts,
timeout
))]
RequestTimeout {
url: String,
attempts: u32,
timeout: u64,
},

#[snafu(transparent)]
Io { source: io::Error },
#[snafu(transparent)]
Expand Down
Loading
Loading