feat: emqx监控邮件报警

This commit is contained in:
caoqianming 2025-02-21 13:41:59 +08:00
commit c1387eb285
4 changed files with 1869 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1773
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "amail"
version = "0.1.0"
edition = "2021"
[dependencies]
lettre = "0.11.13"
reqwest = { version = "0.12.12", features = ["json"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
tokio = { version = "1.43.0", features = ["full"] }

84
src/main.rs Normal file
View File

@ -0,0 +1,84 @@
use std::time::{Duration, SystemTime};
use reqwest::Client;
use serde_json::Value;
use tokio::{self, time::sleep};
use lettre::{message::header::ContentType, transport::smtp::authentication::Credentials, Message, SmtpTransport, Transport};
// use base64::prelude::*;
async fn send_mail(subject:&str) {
let email = Message::builder()
.from("909355014@qq.com".parse().unwrap())
.to("909355014@qq.com".parse().unwrap())
.subject(subject).header(ContentType::TEXT_PLAIN).body(String::from("")).unwrap();
let creds = Credentials::new("909355014@qq.com".to_owned(), "kfspmywnuelubfgg".to_owned());
let mailer = SmtpTransport::relay("smtp.qq.com")
.unwrap().credentials(creds).build();
match mailer.send(&email) {
Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {e:?}"),
}
}
#[tokio::main]
async fn main() {
let xtime = SystemTime::now() - Duration::new(30*60, 0);
let mut xtime1 = xtime.clone();
let mut xtime2 = xtime.clone();
let mut xtime0 = xtime.clone();
let ip_path = "http://127.0.0.1:18083/api/v5/clients".to_string();
let client = Client::new();
let appid = "d1a29ffc0c004008".to_string();
let appsecret = "TZKXCyj7rqMPJuVZA3X0fJqdY8dq8QxTjeZq4RFJJOL".to_string();
// let auth_header = format!("Basic {}", BASE64_STANDARD.encode(format!("{}:{}", appid, appsecret)));
loop {
let resp = client.get(&ip_path).query(&[("_page", 1), ("_limit", 100)])
.header("Content-Type", "application/json")
.basic_auth(&appid, Some(&appsecret))
.send().await;
let now = SystemTime::now();
match resp {
Ok(res) => {
if res.status().is_success() {
let mut hfnf_105_online:bool = false;
let mut hfnf_online:bool = false;
let v:serde_json::Value= res.json().await.unwrap();
if let Some(data) = v.get("data").and_then(Value::as_array) {
println!("{:?} --- {:?}", now, data);
for item in data {
if let Some(clientid) = item.get("clientid").and_then(Value::as_str) {
if clientid == "hfnf_105" {
hfnf_105_online = true;
}
if clientid == "hfnf" {
hfnf_online = true
}
}
}
}
if hfnf_105_online == false && now.duration_since(xtime1).unwrap()>= Duration::new(60*30, 0){
send_mail("emqx_hfnf_105_offline").await;
xtime1 = now;
}
if hfnf_online == false && now.duration_since(xtime2).unwrap()>= Duration::new(60*30, 0){
send_mail("emqx_hfnf_offline").await;
xtime2 = now;
}
}
else{
if now.duration_since(xtime0).unwrap() >= Duration::new(60*30, 0) {
send_mail("emqx_500").await;
xtime0 = now;
}
}
}
Err(_) => {
if now.duration_since(xtime0).unwrap() >= Duration::new(60*30, 0) {
send_mail("emqx_500").await;
xtime0 = now;
}
}
}
sleep(Duration::from_secs(60)).await;
}
}