-
-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathstats.rs
More file actions
392 lines (349 loc) · 15.5 KB
/
stats.rs
File metadata and controls
392 lines (349 loc) · 15.5 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#![allow(unused)]
use anyhow::Result;
use chrono::{Datelike, Local, Timelike};
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::borrow::Cow;
use std::collections::binary_heap::Iter;
use std::collections::HashMap;
use std::fmt::Write as _;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::sync::mpsc::sync_channel;
use std::sync::mpsc::SyncSender;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::config::Host;
use crate::notifier::{Event, Notifier};
use crate::payload::{HostStat, StatsResp};
const SAVE_INTERVAL: u64 = 60;
static STAT_SENDER: OnceCell<SyncSender<Cow<HostStat>>> = OnceCell::new();
pub struct StatsMgr {
resp_json: Arc<Mutex<String>>,
stats_data: Arc<Mutex<StatsResp>>,
}
impl StatsMgr {
pub fn new() -> Self {
Self {
resp_json: Arc::new(Mutex::new("{}".to_string())),
stats_data: Arc::new(Mutex::new(StatsResp::new())),
}
}
fn load_last_network(&mut self, hosts_map: &mut HashMap<String, Host>) {
let contents = fs::read_to_string("stats.json").unwrap_or_default();
if contents.is_empty() {
return;
}
if let Ok(stats_json) = serde_json::from_str::<serde_json::Value>(contents.as_str()) {
if let Some(servers) = stats_json["servers"].as_array() {
for v in servers {
if let (Some(name), Some(last_network_in), Some(last_network_out)) = (
v["name"].as_str(),
v["last_network_in"].as_u64(),
v["last_network_out"].as_u64(),
) {
if let Some(srv) = hosts_map.get_mut(name) {
srv.last_network_in = last_network_in;
srv.last_network_out = last_network_out;
trace!("{} => last in/out ({}/{}))", &name, last_network_in, last_network_out);
}
} else {
error!("invalid json => {:?}", v);
}
}
trace!("load stats.json succ!");
}
} else {
warn!("ignore invalid stats.json");
}
}
pub fn init(
&mut self,
cfg: &'static crate::config::Config,
notifies: Arc<Mutex<Vec<Box<dyn Notifier + Send>>>>,
) -> Result<()> {
let hosts_map_base = Arc::new(Mutex::new(cfg.hosts_map.clone()));
// load last_network_in/out
if let Ok(mut hosts_map) = hosts_map_base.lock() {
self.load_last_network(&mut hosts_map);
}
let (stat_tx, stat_rx) = sync_channel(512);
STAT_SENDER.set(stat_tx).unwrap();
let (notifier_tx, notifier_rx) = sync_channel(512);
let stat_map: Arc<Mutex<HashMap<String, Cow<HostStat>>>> = Arc::new(Mutex::new(HashMap::new()));
// stat_rx thread
thread::spawn({
let hosts_group_map = cfg.hosts_group_map.clone();
let hosts_map = hosts_map_base.clone();
let stat_map = stat_map.clone();
let notifier_tx = notifier_tx.clone();
move || loop {
while let Ok(mut stat) = stat_rx.recv() {
trace!("recv stat `{:?}", stat);
let mut stat_t = stat.to_mut();
// group mode
if !stat_t.gid.is_empty() {
if stat_t.alias.is_empty() {
stat_t.alias = stat_t.name.to_string();
}
if let Ok(mut hosts_map) = hosts_map.lock() {
let host = hosts_map.get(&stat_t.name);
if host.is_none() || !host.unwrap().gid.eq(&stat_t.gid) {
if let Some(group) = hosts_group_map.get(&stat_t.gid) {
// 名称不变,换组了,更新组配置 & last in/out
let mut inst = group.inst_host(&stat_t.name);
if let Some(o) = host {
inst.last_network_in = o.last_network_in;
inst.last_network_out = o.last_network_out;
};
hosts_map.insert(stat_t.name.to_string(), inst);
} else {
continue;
}
}
}
}
//
if let Ok(mut hosts_map) = hosts_map.lock() {
let host_info = hosts_map.get_mut(&stat_t.name);
if host_info.is_none() {
error!("invalid stat `{:?}", stat_t);
continue;
}
let info = host_info.unwrap();
if info.disabled {
continue;
}
// 补齐
if stat_t.location.is_empty() {
stat_t.location = info.location.to_string();
}
if stat_t.host_type.is_empty() {
stat_t.host_type = info.r#type.to_owned();
}
stat_t.notify = info.notify && stat_t.notify;
stat_t.pos = info.pos;
stat_t.disabled = info.disabled;
stat_t.weight += info.weight;
stat_t.labels = info.labels.to_owned();
// !group
if !info.alias.is_empty() {
stat_t.alias = info.alias.to_owned();
}
info.latest_ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
stat_t.latest_ts = info.latest_ts;
// last_network_in/out
if !stat_t.vnstat {
let local_now = Local::now();
if info.last_network_in == 0
|| (stat_t.network_in != 0 && info.last_network_in > stat_t.network_in)
|| (local_now.day() == info.monthstart
&& local_now.hour() == 0
&& local_now.minute() < 5)
{
info.last_network_in = stat_t.network_in;
info.last_network_out = stat_t.network_out;
} else {
stat_t.last_network_in = info.last_network_in;
stat_t.last_network_out = info.last_network_out;
}
}
// uptime str
let day = (stat_t.uptime as f64 / 3600.0 / 24.0) as i64;
if day > 0 {
stat_t.uptime_str = format!("{day} 天");
} else {
stat_t.uptime_str = format!(
"{:02}:{:02}:{:02}",
(stat_t.uptime as f64 / 3600.0) as i64,
(stat_t.uptime as f64 / 60.0) as i64 % 60,
stat_t.uptime % 60
);
}
info!("update stat `{:?}", stat_t);
if let Ok(mut host_stat_map) = stat_map.lock() {
if let Some(pre_stat) = host_stat_map.get(&stat_t.name) {
if stat_t.ip_info.is_none() {
stat_t.ip_info = pre_stat.ip_info.to_owned();
}
if stat_t.notify && (pre_stat.latest_ts + cfg.offline_threshold < stat_t.latest_ts) {
// node up notify
notifier_tx.send((Event::NodeUp, stat.clone()));
}
}
host_stat_map.insert(stat.name.to_string(), stat);
//trace!("{:?}", host_stat_map);
}
}
}
}
});
// timer thread
thread::spawn({
let resp_json = self.resp_json.clone();
let stats_data = self.stats_data.clone();
let hosts_map = hosts_map_base.clone();
let stat_map = stat_map.clone();
let notifier_tx = notifier_tx.clone();
let mut latest_notify_ts = 0_u64;
let mut latest_save_ts = 0_u64;
let mut latest_group_gc = 0_u64;
let mut latest_alert_check_ts = 0_u64;
move || loop {
thread::sleep(Duration::from_millis(500));
let mut resp = StatsResp::new();
let now = resp.updated;
let mut notified = false;
// group gc
if latest_group_gc + cfg.group_gc < now {
latest_group_gc = now;
//
if let Ok(mut hosts_map) = hosts_map.lock() {
hosts_map.retain(|_, o| o.gid.is_empty() || o.latest_ts + cfg.group_gc >= now);
}
//
if let Ok(mut stat_map) = stat_map.lock() {
stat_map.retain(|_, o| o.gid.is_empty() || o.latest_ts + cfg.group_gc >= now);
}
}
if let Ok(mut host_stat_map) = stat_map.lock() {
for (_, stat) in host_stat_map.iter_mut() {
if stat.disabled {
resp.servers.push(stat.as_ref().clone());
continue;
}
let stat = stat.borrow_mut();
let o = stat.to_mut();
// 30s 下线
if o.latest_ts + cfg.offline_threshold < now {
o.online4 = false;
o.online6 = false;
}
// labels
const OS_LIST: [&str; 10] = [
"centos", "debian", "ubuntu", "arch", "windows", "macos", "pi", "android", "linux", "freebsd"
];
if !o.labels.contains("os=") {
if let Some(sys_info) = &o.sys_info {
let os_r = format!("{} {}",sys_info.os_release.to_lowercase(),sys_info.os_name.to_lowercase());
for s in OS_LIST.iter() {
if os_r.contains(s) {
if o.labels.is_empty() {
write!(o.labels, "os={s}");
} else {
write!(o.labels, ";os={s}");
}
break;
}
}
}
}
// client notify
if o.notify {
// notify check /30 s
if latest_notify_ts + cfg.notify_interval < now {
if o.online4 || o.online6 {
notifier_tx.send((Event::Custom, stat.clone()));
} else {
o.disabled = true;
notifier_tx.send((Event::NodeDown, stat.clone()));
}
notified = true;
}
}
resp.servers.push(stat.as_ref().clone());
}
if notified {
latest_notify_ts = now;
}
}
resp.servers.sort_by(|a, b| {
if a.weight != b.weight {
return a.weight.cmp(&b.weight).reverse();
}
if a.pos != b.pos {
return a.pos.cmp(&b.pos);
}
// same group
a.alias.cmp(&b.alias)
});
// last_network_in/out save /60s
if latest_save_ts + SAVE_INTERVAL < now {
latest_save_ts = now;
if !resp.servers.is_empty() {
if let Ok(mut file) = File::create("stats.json") {
file.write_all(serde_json::to_string(&resp).unwrap().as_bytes());
file.flush();
trace!("save stats.json succ!");
} else {
error!("save stats.json fail!");
}
}
}
//
if let Ok(mut o) = resp_json.lock() {
*o = serde_json::to_string(&resp).unwrap();
}
if let Ok(mut o) = stats_data.lock() {
*o = resp;
}
}
});
// notify thread
thread::spawn(move || loop {
while let Ok(msg) = notifier_rx.recv() {
let (e, stat) = msg;
let notifiers = &*notifies.lock().unwrap();
trace!("recv notify => {:?}, {:?}", e, stat);
for notifier in notifiers {
trace!("{} notify {:?} => {:?}", notifier.kind(), e, stat);
notifier.notify(&e, stat.borrow());
}
}
});
Ok(())
}
pub fn get_stats(&self) -> Arc<Mutex<StatsResp>> {
self.stats_data.clone()
}
pub fn get_stats_json(&self) -> String {
self.resp_json.lock().unwrap().to_string()
}
pub fn report(&self, data: serde_json::Value) -> Result<()> {
lazy_static! {
static ref SENDER: SyncSender<Cow<'static, HostStat>> = STAT_SENDER.get().unwrap().clone();
}
match serde_json::from_value(data) {
Ok(stat) => {
trace!("send stat => {:?} ", stat);
SENDER.send(Cow::Owned(stat));
}
Err(err) => {
error!("report error => {:?}", err);
}
};
Ok(())
}
pub fn get_all_info(&self) -> Result<serde_json::Value> {
let data = self.stats_data.lock().unwrap();
let mut resp_json = serde_json::to_value(&*data)?;
// for skip_serializing
if let Some(srv_list) = resp_json["servers"].as_array_mut() {
for (idx, stat) in data.servers.iter().enumerate() {
if let Some(srv) = srv_list[idx].as_object_mut() {
srv.insert("ip_info".into(), serde_json::to_value(stat.ip_info.as_ref())?);
srv.insert("sys_info".into(), serde_json::to_value(stat.sys_info.as_ref())?);
}
}
} else {
// todo!()
};
Ok(resp_json)
}
}