reflect: reflect
This commit is contained in:
63
src/main.rs
63
src/main.rs
@@ -29,6 +29,7 @@ lazy_static!{
|
|||||||
static ref CHECK_FAIL_MAP: Mutex<HashMap<String, u64>> = Mutex::new(HashMap::new());
|
static ref CHECK_FAIL_MAP: Mutex<HashMap<String, u64>> = Mutex::new(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
panic::set_hook(Box::new(|panic_info| {
|
panic::set_hook(Box::new(|panic_info| {
|
||||||
error!("Panic in running keeprunningd: {:?}", panic_info);
|
error!("Panic in running keeprunningd: {:?}", panic_info);
|
||||||
@@ -51,10 +52,7 @@ fn main() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if simple_logging::log_to_file("/var/log/keeprunningd.log", LevelFilter::Info).is_err()
|
init_logger();
|
||||||
&& simple_logging::log_to_file("/tmp/keeprunningd.log", LevelFilter::Info).is_err() {
|
|
||||||
simple_logging::log_to_stderr(LevelFilter::Info);
|
|
||||||
}
|
|
||||||
|
|
||||||
let keep_running_config = Arc::new(keep_running_config);
|
let keep_running_config = Arc::new(keep_running_config);
|
||||||
for check_cnt in 0.. {
|
for check_cnt in 0.. {
|
||||||
@@ -131,30 +129,11 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
|
|||||||
|
|
||||||
if check_lines.is_empty() { // if check fail!
|
if check_lines.is_empty() { // if check fail!
|
||||||
info!("Send DingTalk notification!");
|
info!("Send DingTalk notification!");
|
||||||
use tokio::runtime;
|
block_send_notify(&keep_running_config.notify_token, &keep_running_config_item.title, fail_count);
|
||||||
match runtime::Builder::new().basic_scheduler().enable_all().build() {
|
|
||||||
Err(err) => error!("Prepare tokio runtime error: {}", err),
|
|
||||||
Ok(mut rt) => {
|
|
||||||
let mut sb = String::with_capacity(1024);
|
|
||||||
sb.push_str(&format!("Check failed: {}, fail count: {}", &keep_running_config_item.title, fail_count));
|
|
||||||
sb.push_str(&format!("\ncheck time: {:?}", Local::now()));
|
|
||||||
rt.block_on(send_notify(&keep_running_config.notify_token, &sb));
|
|
||||||
},
|
|
||||||
}
|
|
||||||
match &keep_running_config_item.restart_command {
|
match &keep_running_config_item.restart_command {
|
||||||
Some(restart_command) if fail_count > 1 => {
|
Some(restart_command) if fail_count > 1 => {
|
||||||
info!("Fail count is {} > 1, try restart command: {:?}", fail_count, restart_command);
|
info!("Fail count is {} > 1, try restart command: {:?}", fail_count, restart_command);
|
||||||
let mut restart_command_iter = restart_command.iter();
|
restart_cmd(restart_command);
|
||||||
if let Some(program) = restart_command_iter.next() {
|
|
||||||
let mut cmd = Command::new(program);
|
|
||||||
for arg in restart_command_iter {
|
|
||||||
cmd.arg(arg);
|
|
||||||
}
|
|
||||||
match cmd.spawn() {
|
|
||||||
Ok(child) => info!("Ran process success, process id: {}", child.id()),
|
|
||||||
Err(e) => error!("Run restart command failed: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => ( /* IGNORE */ ),
|
_ => ( /* IGNORE */ ),
|
||||||
}
|
}
|
||||||
@@ -168,6 +147,19 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn block_send_notify(notify_token: &str, title: &str, fail_count: u64) {
|
||||||
|
use tokio::runtime;
|
||||||
|
match runtime::Builder::new().basic_scheduler().enable_all().build() {
|
||||||
|
Err(err) => error!("Prepare tokio runtime error: {}", err),
|
||||||
|
Ok(mut rt) => {
|
||||||
|
let mut sb = String::with_capacity(1024);
|
||||||
|
sb.push_str(&format!("Check failed: {}, fail count: {}", title, fail_count));
|
||||||
|
sb.push_str(&format!("\ncheck time: {:?}", Local::now()));
|
||||||
|
rt.block_on(send_notify(notify_token, &sb));
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn send_notify(notify_token: &str, text: &str) {
|
async fn send_notify(notify_token: &str, text: &str) {
|
||||||
if notify_token.is_empty() {
|
if notify_token.is_empty() {
|
||||||
info!("Notify token is empty, do not send DingTalk notification");
|
info!("Notify token is empty, do not send DingTalk notification");
|
||||||
@@ -181,6 +173,27 @@ async fn send_notify(notify_token: &str, text: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn restart_cmd(restart_command: &[String]) {
|
||||||
|
let mut restart_command_iter = restart_command.iter();
|
||||||
|
if let Some(program) = restart_command_iter.next() {
|
||||||
|
let mut cmd = Command::new(program);
|
||||||
|
for arg in restart_command_iter {
|
||||||
|
cmd.arg(arg);
|
||||||
|
}
|
||||||
|
match cmd.spawn() {
|
||||||
|
Ok(child) => info!("Ran process success, process id: {}", child.id()),
|
||||||
|
Err(e) => error!("Run restart command failed: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_logger() {
|
||||||
|
if simple_logging::log_to_file("/var/log/keeprunningd.log", LevelFilter::Info).is_err()
|
||||||
|
&& simple_logging::log_to_file("/tmp/keeprunningd.log", LevelFilter::Info).is_err() {
|
||||||
|
simple_logging::log_to_stderr(LevelFilter::Info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn ps_aux() -> Option<String> {
|
fn ps_aux() -> Option<String> {
|
||||||
let mut cmd = Command::new("ps");
|
let mut cmd = Command::new("ps");
|
||||||
cmd.arg("aux");
|
cmd.arg("aux");
|
||||||
|
|||||||
Reference in New Issue
Block a user