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
use rust_decimal::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use std::net::UdpSocket;
use std::time::{SystemTime, UNIX_EPOCH};

/// 网络传输结构体
#[derive(Serialize, Deserialize, Debug)]
pub struct WebResult<T> {
    pub data: Option<T>,
    pub code: usize,
    pub msg: Option<String>,
}

/// WebResult functions
impl <T>WebResult<T> {
    pub fn success(data: T) -> WebResult<T> {
        let result: WebResult<T> = WebResult {
            data: Some(data),
            code: 200,
            msg: Some("success".to_string()),
        };
        result
    }
}

/// 系统监控指标
#[derive(Serialize, Deserialize, Debug)]
pub struct MonitorRecord {
    #[serde(with = "rust_decimal::serde::float")]
    pub cpu_per: Decimal,
    #[serde(with = "rust_decimal::serde::float")]
    pub mem_per: Decimal,
    #[serde(with = "rust_decimal::serde::float")]
    pub recv_rate: Decimal,
    #[serde(with = "rust_decimal::serde::float")]
    pub sent_rate: Decimal,
    pub tcp_total: usize,
    pub udp_total: usize,
    pub io_reads: u64,
    pub io_writes: u64,
    pub node_ip: String,
    pub timestamp: u64,
    pub group_name: String,
}

impl MonitorRecord {
    pub fn new(cpu_per: Decimal, mem_per: Decimal, recv_rate: Decimal, sent_rate: Decimal,
               tcp_total: usize, udp_total: usize, io_reads: u64, io_writes: u64,
               timestamp: u64, group_name: String) -> MonitorRecord {
        MonitorRecord {
            cpu_per,
            mem_per,
            recv_rate,
            sent_rate,
            tcp_total,
            udp_total,
            io_reads,
            io_writes,
            node_ip: get_local_ip(),
            timestamp,
            group_name: group_name.to_string(),
        }
    }
}

/// 系统信息结构体
#[derive(Serialize, Deserialize, Debug)]
pub struct SysInfoRecord {
    pub boot_time_sec: i64,
    pub cpu_num: u32,
    pub cpu_speed: u64,
    pub mem_total: u64,
    pub disk_total: u64,
    pub disk_free: u64,
    pub hostname: String,
    pub os_name: String,
    pub load_avg_one: f64,
    pub proc_total: u64,
    pub node_ip: String,
    pub timestamp: u64,
    pub group_name: String,
}

impl SysInfoRecord {
    pub fn new(boot_time_sec: i64, cpu_num: u32, cpu_speed: u64, mem_total: u64, disk_total: u64,
               disk_free: u64, hostname: String, os_name: String, load_avg_one: f64,
               proc_total: u64, timestamp: u64, group_name: String,) -> SysInfoRecord {
        SysInfoRecord {
            boot_time_sec,
            cpu_num,
            cpu_speed,
            mem_total,
            disk_total,
            disk_free,
            hostname,
            os_name,
            load_avg_one,
            proc_total,
            node_ip: get_local_ip(),
            timestamp,
            group_name,
        }
    }
}

/// 获取本机IP
pub fn get_local_ip() -> String {
    let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    socket.connect("8.8.8.8:80").unwrap();
    socket.local_addr().unwrap().ip().to_string()
}

/// 获取时间戳
/// > 精确到秒,非毫秒
pub async fn get_timestamp() -> u64 {
    let start = SystemTime::now();
    let since_the_epoch = start
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards");
    let timestamp = since_the_epoch.as_secs();
    timestamp
}

/// Get deploy time line
pub async fn get_time_line() -> u64 {
    let timestamp = get_timestamp().await;
    let time_line = timestamp - 2 * 3600;
    time_line
}