1use chrono::prelude::*;
3use serde_json::Value;
4use std::collections::HashMap;
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
9pub struct ZabbixRequest {
10 request: &'static str,
11 host: String,
12 clock: i64,
13 ns: i64,
14 data: Value,
15}
16
17impl ZabbixRequest {
18 pub fn new(request: &'static str, host: &str, data: Value) -> Self {
19 let host = String::from(host);
20 let clock = Local::now().timestamp();
22 let ns = 0;
23 Self {
24 request,
25 host,
26 clock,
27 ns,
28 data,
29 }
30 }
31
32 pub fn str(&self) -> String {
33 serde_json::to_string(&self).unwrap_or_else(|_| "[]".to_string())
34 }
35}
36
37#[derive(Serialize, Deserialize, Debug, Clone)] pub struct ZabbixMetric {
41 pub host: String,
42 pub key: String,
43 pub value: String,
44 clock: i64,
45 }
47
48impl ZabbixMetric {
49 pub fn new(host: &str, key: &str, value: &str) -> Self {
50 let host = String::from(host);
51 let key = String::from(key);
52 let value = String::from(value);
53 let clock = Local::now().timestamp();
54 Self {
55 host,
56 key,
57 value,
58 clock,
59 }
60 }
61}
62
63#[derive(Serialize, Deserialize, Debug, Clone)]
66pub struct ZabbixDiscovery {
67 data: Vec<HashMap<String, String>>,
68}
69
70impl ZabbixDiscovery {
71 pub fn new(param: &str, value: Vec<String>) -> Self {
72 let mut data = Vec::new();
73
74 for v in value {
75 let k = String::from(param);
76 let mut d = HashMap::new();
78 d.insert(k, v);
79 data.push(d);
80 }
81
82 Self { data }
83 }
84
85 pub fn str(&self) -> String {
86 match serde_json::to_string(&self) {
87 Ok(c) => c,
88 Err(_) => "[]".to_string(),
89 }
90 }
91}
92
93#[derive(Serialize, Deserialize, Debug, Clone)]
96pub struct ZabbixHost {
97 host: String,
98 host_metadata: &'static str,
99 ip: &'static str,
100 port: u16,
101 clock: i64,
102}
103
104impl ZabbixHost {
105 pub fn new(host: &str) -> Self {
106 let host = String::from(host);
107 let host_metadata = "DBMP";
108 let ip = "127.0.0.1";
109 let port = 10050;
110 let clock = Local::now().timestamp();
111 Self {
112 host,
113 host_metadata,
114 ip,
115 port,
116 clock,
117 }
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn test_zabbix_request() {
127 let req = ZabbixRequest::new("REQUEST", "HOST", Value::Null);
128 assert!(req.str().contains("\"request\":\"REQUEST\""));
131 assert!(req.str().contains("\"host\":\"HOST\""));
132 assert_eq!("REQUEST", req.request);
133 assert_eq!("HOST", req.host);
134
135 let hosts = [
137 ZabbixHost::new("host1"),
138 ZabbixHost::new("host2"),
139 ];
140 let hosts =
141 serde_json::to_value(hosts).unwrap_or_else(|_| Value::String("NOHOST".to_string()));
142 let req1 = ZabbixRequest::new("REQUEST", "HOST", hosts);
143 assert!(req1.data.is_array());
146 assert!(req1.str().contains("\"host\":\"host1\""));
147 }
148
149 #[test]
150 fn test_zabbix_discovery() {
151 let data = vec!["A".to_string(), "B".to_string()];
152 let req = ZabbixDiscovery::new("{#APPNO}", data);
153 assert!(req.str().contains("\"{#APPNO}\":\"A\""));
156 assert!(req.str().contains("\"{#APPNO}\":\"B\""));
157 assert_eq!(2, req.data.len());
158 }
159}