Compare commits
4 Commits
7aba5c74a9
...
v0.4.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5aa718e50e | ||
|
|
9bd7685dc3 | ||
|
|
95b3076fe2 | ||
|
|
c39d04d716 |
10
Cargo.toml
10
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rsnmpagent"
|
||||
version = "0.4.1"
|
||||
version = "0.4.2"
|
||||
edition = "2024"
|
||||
|
||||
[profile.release]
|
||||
@@ -20,3 +20,11 @@ hex = "0.4"
|
||||
sha3 = "0.10"
|
||||
diff = "0.1"
|
||||
glob = "0.3"
|
||||
|
||||
|
||||
[package.metadata.generate-rpm]
|
||||
assets = [
|
||||
{ source = "target/release/rsnmpagent", dest = "/usr/sbin/rsnmpagent", mode = "750" },
|
||||
]
|
||||
license = "GPL-3"
|
||||
description = "rsnmpagent for extending snmpd"
|
||||
|
||||
@@ -8,9 +8,9 @@ use std::path::Path;
|
||||
|
||||
pub(crate) fn filesum_filtered(
|
||||
path: &Path,
|
||||
oldfile: &mut String,
|
||||
oldfile: &mut Vec<u8>,
|
||||
diff_string: &mut String,
|
||||
re: Option<®ex::Regex>,
|
||||
re: Option<®ex::bytes::Regex>,
|
||||
) -> io::Result<(bool, String)> {
|
||||
// Open file for hashing
|
||||
match re {
|
||||
@@ -18,30 +18,37 @@ pub(crate) fn filesum_filtered(
|
||||
None => trace!("try to open file {:?} for hashing", path),
|
||||
}
|
||||
let mut hasher = Sha3_256::new();
|
||||
let mut filedata = String::with_capacity(2048);
|
||||
let mut filedata = Vec::with_capacity(2048);
|
||||
let mut changed = false;
|
||||
|
||||
// we read only smal files, so its fast to read the whole file to memory, so we can also du a diff
|
||||
if let Ok(file_contents) = fs::read_to_string(path) {
|
||||
if let Ok(file_contents) = fs::read(path) {
|
||||
if let Some(re) = re {
|
||||
trace!("Filter lines with regex {:?}", re);
|
||||
for line in file_contents.lines() {
|
||||
for line in file_contents.split_inclusive(|&b| b == b'\n') {
|
||||
if re.is_match(line) {
|
||||
trace!("line {} skipped by filter regex", line);
|
||||
trace!("line {} skipped by filter regex", String::from_utf8_lossy(line).trim());
|
||||
continue;
|
||||
}
|
||||
// Update the hasher with the bytes
|
||||
filedata.push_str(line);
|
||||
filedata.extend_from_slice(line);
|
||||
}
|
||||
} else {
|
||||
// we do not have a filter regex, so we could simply paste the file to the hasher
|
||||
trace!("Hash file without filter regex");
|
||||
filedata = file_contents;
|
||||
}
|
||||
hasher.update(format!("{}\n", filedata).as_bytes());
|
||||
|
||||
// Debug to find errors
|
||||
//trace!("Hasher input data {:?}",String::from_utf8_lossy(&filedata));
|
||||
|
||||
// Update the hasher with the bytes
|
||||
hasher.update(&filedata);
|
||||
if !oldfile.is_empty() && *oldfile != filedata {
|
||||
diff_string.clear();
|
||||
for diff in diff::lines(oldfile, &filedata) {
|
||||
let ofs = String::from_utf8_lossy(oldfile);
|
||||
let nfs = String::from_utf8_lossy(&filedata);
|
||||
for diff in diff::lines(&ofs, &nfs) {
|
||||
match diff {
|
||||
Result::Left(l) => {
|
||||
trace!("Diff - {}", l); // Removed line
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use log::{debug, error, info};
|
||||
use regex::Regex;
|
||||
use regex::bytes::Regex as BRegex;
|
||||
|
||||
pub(crate) fn compile_re(regex: Option<String>, name: &str) -> Option<Regex> {
|
||||
if let Some(r) = regex {
|
||||
@@ -19,3 +20,22 @@ pub(crate) fn compile_re(regex: Option<String>, name: &str) -> Option<Regex> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn compile_re_bin(regex: Option<String>, name: &str) -> Option<BRegex> {
|
||||
if let Some(r) = regex {
|
||||
let re = BRegex::new(&r);
|
||||
match re {
|
||||
Ok(r) => {
|
||||
debug!("Sucessfull compiled {} filter regex: {:?}", name, r);
|
||||
Some(r)
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error compiling {} filter regex: {:?}", name, e);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!("No filter regex for {} supplied", name);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ pub mod snmp;
|
||||
use bonding::bonding_status;
|
||||
use config::DataFunctionsFilesum;
|
||||
use filesum::filesum_filtered;
|
||||
use helper::compile_re;
|
||||
use helper::{compile_re, compile_re_bin};
|
||||
use multipath::multipath_status;
|
||||
use processes::Ptypes;
|
||||
use snmp::{Oid, OidData, SnmpData};
|
||||
@@ -189,10 +189,10 @@ fn t_filesum(
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
// allocate some strings for holding file contents between check runs
|
||||
let mut oldpasswd = String::with_capacity(2048);
|
||||
let mut oldshadow = String::with_capacity(2048);
|
||||
let mut oldgroup = String::with_capacity(2048);
|
||||
let mut oldauthkey = String::with_capacity(2048);
|
||||
let mut oldpasswd = Vec::with_capacity(2048);
|
||||
let mut oldshadow = Vec::with_capacity(2048);
|
||||
let mut oldgroup = Vec::with_capacity(2048);
|
||||
let mut oldauthkey = Vec::with_capacity(2048);
|
||||
let mut hash_passwd = String::with_capacity(128);
|
||||
let mut diff_passwd = String::with_capacity(2048);
|
||||
let mut hash_shadow = String::with_capacity(128);
|
||||
@@ -203,10 +203,10 @@ fn t_filesum(
|
||||
let mut diff_authkey = String::with_capacity(2048);
|
||||
|
||||
// allocate Option<Regex> for our regex ...
|
||||
let re_passwd = compile_re(options.passwd, "passwd");
|
||||
let re_shadow = compile_re(options.shadow, "shadow");
|
||||
let re_group = compile_re(options.group, "group");
|
||||
let re_authkey = compile_re(options.authorized_keys, "authorized_keys");
|
||||
let re_passwd = compile_re_bin(options.passwd, "passwd");
|
||||
let re_shadow = compile_re_bin(options.shadow, "shadow");
|
||||
let re_group = compile_re_bin(options.group, "group");
|
||||
let re_authkey = compile_re_bin(options.authorized_keys, "authorized_keys");
|
||||
|
||||
// prepare variables which we use in the whole function
|
||||
let oid_filesum_time = Oid::from_str("6.1.0").unwrap();
|
||||
|
||||
Reference in New Issue
Block a user