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
//!
use chrono::prelude::*;
use serde_json::Value;
use std::collections::HashMap;

///
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ZabbixRequest {
    request: &'static str,
    host: String,
    clock: i64,
    ns: i64,
    data: Value,
}

impl ZabbixRequest {
    pub fn new(request: &'static str, host: &str, data: Value) -> Self {
        let host = String::from(host);
        //let clock = Utc::now().timestamp();
        let clock = Local::now().timestamp();
        let ns = 0;
        Self {
            request,
            host,
            clock,
            ns,
            data,
        }
    }

    pub fn str(&self) -> String {
        serde_json::to_string(&self).unwrap_or_else(|_| "[]".to_string())
    }
}

///
///
#[derive(Serialize, Deserialize, Debug, Clone)] //, PartialEq)]
pub struct ZabbixMetric {
    pub host: String,
    pub key: String,
    pub value: String,
    clock: i64,
    //ns: i64,
}

impl ZabbixMetric {
    pub fn new(host: &str, key: &str, value: &str) -> Self {
        let host = String::from(host);
        let key = String::from(key);
        let value = String::from(value);
        let clock = Local::now().timestamp();
        Self {
            host,
            key,
            value,
            clock,
        }
    }
}

///
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ZabbixDiscovery {
    data: Vec<HashMap<String, String>>,
}

impl ZabbixDiscovery {
    pub fn new(param: &str, value: Vec<String>) -> Self {
        let mut data = Vec::new();

        for v in value {
            let k = String::from(param);
            //let k = String::from("{#APPNO}");
            let mut d = HashMap::new();
            d.insert(k, v);
            data.push(d);
        }

        Self { data }
    }

    pub fn str(&self) -> String {
        match serde_json::to_string(&self) {
            Ok(c) => c,
            Err(_) => "[]".to_string(),
        }
    }
}

///
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ZabbixHost {
    host: String,
    host_metadata: &'static str,
    ip: &'static str,
    port: u16,
    clock: i64,
}

impl ZabbixHost {
    pub fn new(host: &str) -> Self {
        let host = String::from(host);
        let host_metadata = "DBMP";
        let ip = "127.0.0.1";
        let port = 10050;
        let clock = Local::now().timestamp();
        Self {
            host,
            host_metadata,
            ip,
            port,
            clock,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_zabbix_request() {
        let req = ZabbixRequest::new("REQUEST", "HOST", Value::Null);
        //"{\"request\":\"REQUEST\",\"host\":\"HOST\",\"clock\":1547466205,\"ns\":0,\"data\":null}"

        assert!(req.str().contains("\"request\":\"REQUEST\""));
        assert!(req.str().contains("\"host\":\"HOST\""));
        assert_eq!("REQUEST", req.request);
        assert_eq!("HOST", req.host);

        //hosts: Vec<ZabbixHost>
        let hosts = [
            ZabbixHost::new("host1"),
            ZabbixHost::new("host2"),
        ];
        let hosts =
            serde_json::to_value(hosts).unwrap_or_else(|_| Value::String("NOHOST".to_string()));
        let req1 = ZabbixRequest::new("REQUEST", "HOST", hosts);
        //"{\"request\":\"REQUEST\",\"host\":\"HOST\",\"clock\":1547467120,\"ns\":0,\"data\":[{\"clock\":1547467120,\"host\":\"host1\",\"host_metadata\":\"DBMP\",\"ip\":\"127.0.0.1\",\"port\":10050},{\"clock\":1547467120,\"host\":\"host2\",\"host_metadata\":\"DBMP\",\"ip\":\"127.0.0.1\",\"port\":10050}]}"

        assert!(req1.data.is_array());
        assert!(req1.str().contains("\"host\":\"host1\""));
    }

    #[test]
    fn test_zabbix_discovery() {
        let data = vec!["A".to_string(), "B".to_string()];
        let req = ZabbixDiscovery::new("{#APPNO}", data);
        //"{\"data\":[{\"{#APPNO}\":\"A\"},{\"{#APPNO}\":\"B\"}]}"

        assert!(req.str().contains("\"{#APPNO}\":\"A\""));
        assert!(req.str().contains("\"{#APPNO}\":\"B\""));
        assert_eq!(2, req.data.len());
    }
}