From d07afd914d1a33846dbf4358ee8eae218f3102ea Mon Sep 17 00:00:00 2001 From: Sergey Kipnis Date: Fri, 20 Oct 2023 18:50:36 +0200 Subject: [PATCH] add base section processing - corresponds first phase in mssql.vbs, no access to db Change-Id: Icca7b3a097d9588842703e79acdeea8f5b6b23c4 --- packages/check-sql/src/emit.rs | 25 ++++++++++++++++++ packages/check-sql/src/lib.rs | 1 + packages/check-sql/src/main.rs | 14 +++++++--- packages/check-sql/src/ms_sql/api.rs | 39 ++++++++++++++++++++++++++-- 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 packages/check-sql/src/emit.rs diff --git a/packages/check-sql/src/emit.rs b/packages/check-sql/src/emit.rs new file mode 100644 index 00000000000..277b21fcff7 --- /dev/null +++ b/packages/check-sql/src/emit.rs @@ -0,0 +1,25 @@ +// Copyright (C) 2023 Checkmk GmbH - License: GNU General Public License v2 +// This file is part of Checkmk (https://checkmk.com). It is subject to the terms and +// conditions defined in the file COPYING, which is part of this source code package. + +pub fn header(name: &str, separator: Option) -> String { + match separator { + Some(s) => { + let sep = s as u8; + format!("<<<{name}:sep({sep:0>2})>>>\n") + } + None => format!("<<<{name}>>>\n"), + } +} + +#[cfg(test)] +mod test { + use super::*; + #[test] + fn test_header() { + assert_eq!(header("name", Some('\n')), "<<>>\n"); + assert_eq!(header("name", Some('\t')), "<<>>\n"); + assert_eq!(header("name", Some('|')), "<<>>\n"); + assert_eq!(header("name", None), "<<>>\n"); + } +} diff --git a/packages/check-sql/src/lib.rs b/packages/check-sql/src/lib.rs index 1be9f714aca..d51d9a755b5 100644 --- a/packages/check-sql/src/lib.rs +++ b/packages/check-sql/src/lib.rs @@ -5,5 +5,6 @@ pub mod args; pub mod config; mod constants; +pub mod emit; pub mod ms_sql; pub mod setup; diff --git a/packages/check-sql/src/main.rs b/packages/check-sql/src/main.rs index b4c0b8761dd..73ebd65e555 100644 --- a/packages/check-sql/src/main.rs +++ b/packages/check-sql/src/main.rs @@ -3,12 +3,20 @@ // conditions defined in the file COPYING, which is part of this source code package. use anyhow::Result; use check_sql::setup; -use log::info; #[tokio::main] async fn main() -> Result<()> { let config = setup::init(std::env::args_os())?; - config.exec().await?; - info!("Success"); + match config.exec().await { + Ok(output) => { + print!("{output}"); + log::info!("Success"); + } + Err(e) => { + print!("{e}"); + log::error!("{e}") + } + }; + Ok(()) } diff --git a/packages/check-sql/src/ms_sql/api.rs b/packages/check-sql/src/ms_sql/api.rs index d63197cfa26..db64efdb586 100644 --- a/packages/check-sql/src/ms_sql/api.rs +++ b/packages/check-sql/src/ms_sql/api.rs @@ -3,6 +3,7 @@ // conditions defined in the file COPYING, which is part of this source code package. use crate::config::CheckConfig; +use crate::emit::header; use anyhow::Result; use tiberius::{AuthMethod, Client, Config}; @@ -21,9 +22,43 @@ pub enum Credentials<'a> { }, } +pub struct Section { + pub name: String, + pub separator: Option, +} + impl CheckConfig { - pub async fn exec(&self) -> Result<()> { - Ok(()) + pub async fn exec(&self) -> Result { + if let Some(ms_sql) = self.ms_sql() { + let sqls = ms_sql.sqls(); + let always: Vec
= sqls.get_filtered_always().iter().map(to_section).collect(); + let cached: Vec
= sqls.get_filtered_cached().iter().map(to_section).collect(); + Ok(always + .iter() + .chain(cached.iter()) + .map(|s| header(&s.name, s.separator)) + .collect::>() + .join("")) + } else { + anyhow::bail!("No Config") + } + } +} + +fn to_section(name: &String) -> Section { + Section { + name: name.to_owned(), + separator: get_section_separator(name), + } +} + +fn get_section_separator(name: &str) -> Option { + match name { + "instance" | "database" | "counters" | "blocked_sessions" | "transactionlogs" + | "datafiles" | "cluster" => Some('|'), + "jobs" | "mirroring" | "availability_groups" => Some('\t'), + "tablespaces" | "connections" => None, + _ => None, } }