Skip to content

Commit

Permalink
add base section processing
Browse files Browse the repository at this point in the history
- corresponds first phase in mssql.vbs, no access to db

Change-Id: Icca7b3a097d9588842703e79acdeea8f5b6b23c4
  • Loading branch information
s-kipnis committed Oct 21, 2023
1 parent e0941a9 commit d07afd9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
25 changes: 25 additions & 0 deletions packages/check-sql/src/emit.rs
Original file line number Diff line number Diff line change
@@ -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<char>) -> 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')), "<<<name:sep(10)>>>\n");
assert_eq!(header("name", Some('\t')), "<<<name:sep(09)>>>\n");
assert_eq!(header("name", Some('|')), "<<<name:sep(124)>>>\n");
assert_eq!(header("name", None), "<<<name>>>\n");
}
}
1 change: 1 addition & 0 deletions packages/check-sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
pub mod args;
pub mod config;
mod constants;
pub mod emit;
pub mod ms_sql;
pub mod setup;
14 changes: 11 additions & 3 deletions packages/check-sql/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
39 changes: 37 additions & 2 deletions packages/check-sql/src/ms_sql/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -21,9 +22,43 @@ pub enum Credentials<'a> {
},
}

pub struct Section {
pub name: String,
pub separator: Option<char>,
}

impl CheckConfig {
pub async fn exec(&self) -> Result<()> {
Ok(())
pub async fn exec(&self) -> Result<String> {
if let Some(ms_sql) = self.ms_sql() {
let sqls = ms_sql.sqls();
let always: Vec<Section> = sqls.get_filtered_always().iter().map(to_section).collect();
let cached: Vec<Section> = sqls.get_filtered_cached().iter().map(to_section).collect();
Ok(always
.iter()
.chain(cached.iter())
.map(|s| header(&s.name, s.separator))
.collect::<Vec<String>>()
.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<char> {
match name {
"instance" | "database" | "counters" | "blocked_sessions" | "transactionlogs"
| "datafiles" | "cluster" => Some('|'),
"jobs" | "mirroring" | "availability_groups" => Some('\t'),
"tablespaces" | "connections" => None,
_ => None,
}
}

Expand Down

0 comments on commit d07afd9

Please sign in to comment.