-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.rs
144 lines (135 loc) · 4.55 KB
/
exec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::{
alerts::Alert,
probes::{Notification, Probe, HAS_INCIDENT, NO_INCIDENT},
};
use anyhow::Result;
use async_trait::async_trait;
use lazy_static::lazy_static;
use prometheus::{register_counter_vec, register_gauge_vec, CounterVec, GaugeVec};
use serde_derive::Deserialize;
use sled::{Db, IVec};
use slug::slugify;
use std::{collections::HashMap, process::Command};
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Exec {
name: Option<String>,
schedule: Option<String>,
cmd: String,
args: Option<Vec<String>>,
}
lazy_static! {
static ref RUNS_TOTAL: CounterVec = register_counter_vec!(
"probe_exec_runs_total",
"run counter for exec probe plugin",
&["plugin", "cmd"]
)
.unwrap();
static ref TRIGGERED_TOTAL: CounterVec = register_counter_vec!(
"probe_exec_triggered_total",
"triggered counter for exec probe plugin",
&["plugin", "cmd"]
)
.unwrap();
static ref TRIGGERED: GaugeVec = register_gauge_vec!(
"probe_exec_triggered",
"Exec probe plugin triggered",
&["plugin", "cmd"]
)
.unwrap();
}
#[async_trait]
impl Probe for Exec {
fn new() -> Self {
Self {
..Default::default()
}
}
fn local_schedule(&self) -> Option<String> {
self.schedule.to_owned()
}
fn slug(&self) -> String {
slugify(format!(
"exec-{}-{}",
self.cmd,
self.args.to_owned().unwrap_or_default().join("-")
))
}
async fn observe(
&self,
store: &Db,
alerts: &HashMap<String, Vec<Box<dyn Alert>>>,
) -> Result<()> {
log::info!("executing command {:?} with args {:?}", self.cmd, self.args);
RUNS_TOTAL
.with_label_values(&["probe.exec", &self.cmd])
.inc();
let stored = store.get(self.slug().as_bytes())?;
let mut to_store = NO_INCIDENT;
let mut triggered = 0;
let mut cmd = Command::new(&self.cmd);
if let Some(args) = &self.args {
cmd.args(args);
}
match cmd.output() {
Err(err) => {
log::error!(
"failed executing command {} with args {:?}: {}",
self.cmd,
self.args,
err
);
}
Ok(output) => {
if !output.status.success() {
log::info!(
"_TRIGGERED_: command {} with args {:?} got code {}",
self.cmd,
self.args,
output.status,
);
if stored.is_none() || stored == Some(IVec::from(NO_INCIDENT)) {
log::warn!(
"_NOTIFY_: command {} with args {:?} got code {}",
self.cmd,
self.args,
output.status,
);
self.notify(
alerts,
Notification {
from: "exec".to_owned(),
name: self.name("exec", self.name.to_owned()),
check: format!(
"command `{}` with args `{:?}`",
self.cmd, self.args
),
title: format!(
"`{}` `{:?}` got code {}",
self.cmd, self.args, output.status
),
message: format!(
"{}: {}",
output.status,
String::from_utf8_lossy(&output.stderr)
),
message_html: None,
message_entries: None,
},
)
.await?;
}
TRIGGERED_TOTAL
.with_label_values(&["probe.exec", &self.cmd])
.inc();
triggered = 1;
to_store = HAS_INCIDENT;
}
}
};
store.insert(self.slug().as_bytes(), to_store)?;
TRIGGERED
.with_label_values(&["probe.exec", &self.cmd])
.set(triggered as f64);
Ok(())
}
}