diff --git a/src/clouds/defs.rs b/src/clouds/defs.rs index c077979..7621fc6 100644 --- a/src/clouds/defs.rs +++ b/src/clouds/defs.rs @@ -284,9 +284,16 @@ pub struct TsksrvcInfo { pub info: serde_yaml::Value, } #[derive(Clone, Debug, Serialize, Deserialize, Default)] -pub struct TsksrvcsHostInfo { +pub struct AppsrvcInfo { + pub name: String, + pub target: String, + pub info: serde_yaml::Value, +} +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +pub struct SrvcsHostInfo { pub hostname: String, pub tsksrvcs: Vec, + pub appsrvcs: Vec, } #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct HostInfo { diff --git a/src/clouds/on_clouds.rs b/src/clouds/on_clouds.rs index 3c22121..e92edc5 100644 --- a/src/clouds/on_clouds.rs +++ b/src/clouds/on_clouds.rs @@ -15,6 +15,7 @@ use crate::clouds::defs::{ Cloud, Provider, TskSrvc, + App, MainResourcesConfig, }; use crate::defs::{ @@ -28,7 +29,7 @@ use crate::defs::{ CloudItem, CloudCheckItem }; -use crate::clouds::defs::{TsksrvcInfo}; +use crate::clouds::defs::{TsksrvcInfo,AppsrvcInfo}; use crate::clouds::upcloud::{get_upcloud_info,run_on_upcloud}; @@ -337,7 +338,7 @@ pub async fn liveness_srvr_tsksrvcs(source: &str, cntrllrs: &Vec, tsksr if debug > 2 { println!("livenes: {} -> {}",&tsksrvc.name,&serverstring); } - let res_info = match liveness_check(&source,&cntrllrs,&serverstring,&name).await { + let res_info = match liveness_check(&source,&cntrllrs,&serverstring,"",&name).await { Ok(_) => "ok", Err(e) => { if tsksrvc.critical == IsCritical::yes { @@ -363,6 +364,71 @@ pub async fn liveness_srvr_tsksrvcs(source: &str, cntrllrs: &Vec, tsksr } tsksrvcs_info.to_owned() } +pub async fn parse_srvr_appsrvcs(hostname: &str, sshaccess: &SSHAccess, appsrvcs: &Vec,req_tsksrvcs: &str) -> Vec { + let mut appsrvcs_info: Vec = Vec::new(); + for appsrvc in appsrvcs.iter() { + let name = format!("{}",&appsrvc.name); + if req_tsksrvcs == "all" || req_tsksrvcs.contains(&name) { + // TODO ssh &srv.hostname to get &name in "yaml" + //println!("{} {} {}",&hostname,sshaccess.user,&tksrvc.name); + appsrvcs_info.push(AppsrvcInfo { + name: format!("{}",&appsrvc.name), + info: run_ssh_on_srvr(&hostname, &name, "", &sshaccess) + .unwrap_or_else(|e| { + eprintln!("run_ssh_on_srvr for {}: {}",&appsrvc.name,e); + serde_yaml::Value::default() + }), + target: format!("{}",&appsrvc.target), + }); + } + } + appsrvcs_info.to_owned() +} +pub async fn liveness_srvr_appsrvcs(source: &str, cntrllrs: &Vec, appsrvcs: &Vec, req_tsksrvc: &str) -> Vec { + let mut appsrvcs_info: Vec = Vec::new(); + let debug=envmnt::get_isize("DEBUG",0); + for appsrvc in appsrvcs.iter() { + // match format!("{}",&appsrvc.name).as_str() { + // "pause" => continue, + // "scale" => continue, + // "systemfix" => continue, + // _ => { + let name = format!("{}",&appsrvc.name); + if appsrvc.liveness.is_empty() || (req_tsksrvc != "" && !req_tsksrvc.contains(&name)) { + continue; + } + let serverstring = format!("{}",&appsrvc.liveness); + let live_req = format!("{}",&appsrvc.req); + if debug > 2 { + println!("livenes: {} -> {}",&appsrvc.name,&serverstring); + } + let res_info = match liveness_check(&source,&cntrllrs,&serverstring,&live_req,&name).await { + Ok(_) => "ok", + Err(e) => { + if appsrvc.critical == IsCritical::yes { + let monitor_name = "WUJI_MONITOR"; + println!("{} critical livenes: {} -> {}",envmnt::get_or(&monitor_name,""),&appsrvc.name,&serverstring); + } + if debug > 0 { + eprint!("liveness_check error: {}",e); + } + "err" + }, + }; + // println!("{} info: {}",&appsrvc.name,&res_info); + appsrvcs_info.push(AppsrvcInfo { + name: format!("{}",&appsrvc.name), + info: serde_yaml::from_str(res_info).unwrap_or_else(|e| { + eprintln!("Serde liveness Error: {} {} -> {}",&source,&serverstring,e); + serde_yaml::Value::default() + }), + target: format!("{}",&appsrvc.target), + }); + // }, + // } + } + appsrvcs_info.to_owned() +} pub async fn get_provider_info(provider: &str, hostname: &str, cmd: &str , cfg_path: &str) -> String { match provider { "upcloud" => { @@ -546,7 +612,7 @@ pub async fn create_cloud_check(req_tsksrvcs: &str,reqenv: &ReqEnv,entries: Vec< String::from("") })); } - if req_tsksrvcs.contains("liveness") { + if req_tsksrvcs.contains("liveness") || req_tsksrvcs.contains("apps") { let mut groups: Vec = Vec::new(); // cfg.groups = cfg.groups.map(|grp| grp.with_resources(grp.path.to_owned())).collect(); for grp in cfg.groups.iter() { @@ -555,6 +621,8 @@ pub async fn create_cloud_check(req_tsksrvcs: &str,reqenv: &ReqEnv,entries: Vec< let liveness: Option; if req_tsksrvcs.contains("liveness") { liveness = Some(on_cloud_name_req("liveness",&cloud,&reqenv,"","",&itm.path).await); + } else if req_tsksrvcs.contains("apps") { + liveness = Some(on_cloud_name_req("apps",&cloud,&reqenv,"","",&itm.path).await); } else { liveness = itm.liveness.to_owned(); } @@ -615,10 +683,13 @@ pub async fn on_cloud_req(reqname: &str,env_cloud: &Cloud,reqenv: &ReqEnv,req_ts } else { entries = vec!(source.to_string()); } - if reqname == "check_job" { - create_cloud_check(req_tsksrvcs,reqenv,entries,cloud).await - } else { - create_cloud_config(reqname,req_tsksrvcs,reqenv,entries,cloud).await + match reqname { + "check_job" => + create_cloud_check(req_tsksrvcs,reqenv,entries,cloud).await, + "apps_check_job" => + create_cloud_check(req_tsksrvcs,reqenv,entries,cloud).await, + _ => + create_cloud_config(reqname,req_tsksrvcs,reqenv,entries,cloud).await, } } pub async fn get_cloud_cache_req(reqenv: &ReqEnv,cloud: &Cloud, reqname: &str, reqname_job: &str) -> Result<()> { @@ -706,4 +777,39 @@ pub async fn get_cloud_check(reqenv: &ReqEnv) -> String { // println!("{}: [cloud check] -> {}\n",&now,&output_path); // } // Ok(()) +} +pub async fn run_apps_check(reqenv: &ReqEnv,cloud: &Cloud) -> Result<()> { + let debug = envmnt::get_isize("DEBUG",0); + if debug > 0 { + println!("apps check ... {:?} ",chrono::Utc::now()); + } + let config = reqenv.config(); + let output_path = format!("{}/apps.json",&config.check_path); + let now = chrono::Utc::now().timestamp(); + envmnt::set(format!("LAST_APPS_CHECK{}",&output_path), format!("{}",&now)); + let result = on_cloud_req("apps_check_job",&cloud,&reqenv,"apps","","*").await; + // println!("{}",&output_path); + if Path::new(&output_path).exists() { + fs::remove_file(&output_path)?; + } + let mut file = OpenOptions::new().write(true).create(true).open(&output_path)?; + file.write_all(result.as_bytes())?; + if debug > 0 { + println!("{}: [apps check] -> {}\n",&now,&output_path); + } + Ok(()) +} +pub async fn get_apps_check(reqenv: &ReqEnv) -> String { + let debug = envmnt::get_isize("DEBUG",0); + if debug > 0 { + println!("apps check ... {:?} ",chrono::Utc::now()); + } + let config = reqenv.config(); + let output_path = format!("{}/apps.json",&config.check_path); + let output_data = fs::read_to_string(&output_path).with_context(|| format!("Failed to read json apps check 'outut_path' from {}", &output_path)) + .unwrap_or_else(|e| { + eprintln!("read file {}: {}",&output_path,e); + String::from("") + }); + output_data } \ No newline at end of file diff --git a/src/clouds/upcloud.rs b/src/clouds/upcloud.rs index 09644f7..e51e770 100644 --- a/src/clouds/upcloud.rs +++ b/src/clouds/upcloud.rs @@ -6,8 +6,8 @@ use std::path::Path; use crate::clouds::defs::{Cloud}; use crate::providers::defs::upcloud::{ResourcesConfig,ConfigResources}; use crate::providers::upcloud::{parse_resources_cfg,make_config_resources}; -use crate::clouds::defs::{TsksrvcsHostInfo,HostInfo}; -use crate::clouds::on_clouds::{parse_srvr_tsksrvcs,liveness_srvr_tsksrvcs}; +use crate::clouds::defs::{SrvcsHostInfo,HostInfo}; +use crate::clouds::on_clouds::{parse_srvr_tsksrvcs,liveness_srvr_tsksrvcs,liveness_srvr_appsrvcs,parse_srvr_appsrvcs}; pub async fn load_upcloud_config(cfg_data: String) -> Result { let mut res_cfg: ResourcesConfig = serde_yaml::from_str(&cfg_data)?; @@ -102,7 +102,7 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so "status" => { match req_tsksrvc { _ => { - let mut hosts_tsksrvcs_info: Vec = Vec::new(); + let mut srvcs_hosts_info: Vec = Vec::new(); for srvr in cloud_config.servers.iter() { if req_srvrs == "" || req_srvrs.contains(&srvr.hostname) { // let srvr=&cloud_config.servers[0]; @@ -111,10 +111,11 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so // res.push_str(&str_host); // TODO check if is alive // res.push_str( - hosts_tsksrvcs_info.push(TsksrvcsHostInfo { + srvcs_hosts_info.push(SrvcsHostInfo { hostname: srvr.hostname.to_owned(), tsksrvcs: parse_srvr_tsksrvcs(&srvr.hostname, &srvr.sshAccess, &srvr.tsksrvcs,&req_tsksrvc).await, - }); + appsrvcs: parse_srvr_appsrvcs(&srvr.hostname, &srvr.sshAccess, &srvr.apps,&req_tsksrvc).await, + }); // ); // res.push('\n'); } @@ -124,7 +125,7 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so // println!("{}",&res); // let tsks_yaml: Vec = serde_yaml::from_str(&res) // .unwrap_or_else(|e| { eprintln!("{}",e); Vec::new() }); - serde_json::to_string(&hosts_tsksrvcs_info) + serde_json::to_string(&srvcs_hosts_info) .unwrap_or_else(|e| { eprintln!("{}",e); String::from("")}) } } @@ -132,16 +133,35 @@ pub async fn run_on_upcloud(reqname: &str,req_tsksrvc: &str, req_srvrs: &str, so "liveness" => { match req_tsksrvc { _ => { - let mut hosts_tsksrvcs_info: Vec = Vec::new(); + let mut srvcs_hosts_info: Vec = Vec::new(); for srvr in cloud_config.servers.iter() { if req_srvrs == "" || req_srvrs.contains(&srvr.hostname) { - hosts_tsksrvcs_info.push(TsksrvcsHostInfo { + srvcs_hosts_info.push(SrvcsHostInfo { hostname: srvr.hostname.to_owned(), tsksrvcs: liveness_srvr_tsksrvcs(&source,&cloud_config.cntrllrs,&srvr.tsksrvcs,&req_tsksrvc).await, + appsrvcs: Vec::new(), }); } }; - serde_json::to_string(&hosts_tsksrvcs_info) + serde_json::to_string(&srvcs_hosts_info) + .unwrap_or_else(|e| { eprintln!("serde liveness: {}",e); String::from("")}) + } + } + }, + "apps" => { + match req_tsksrvc { + _ => { + let mut srvcs_hosts_info: Vec = Vec::new(); + for srvr in cloud_config.servers.iter() { + if req_srvrs == "" || req_srvrs.contains(&srvr.hostname) { + srvcs_hosts_info.push(SrvcsHostInfo { + hostname: srvr.hostname.to_owned(), + tsksrvcs: Vec::new(), + appsrvcs: liveness_srvr_appsrvcs(&source,&cloud_config.cntrllrs,&srvr.apps,&req_tsksrvc).await, + }); + } + }; + serde_json::to_string(&srvcs_hosts_info) .unwrap_or_else(|e| { eprintln!("serde liveness: {}",e); String::from("")}) } }