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
//! 基于 rust 实现的 zabbix proxy,实现了基本的代理功能。
//!
use super::Result;
use super::protocol::ZabbixProtocol;
use super::request::{ZabbixHost, ZabbixMetric, ZabbixRequest};
use super::response::Response;
use serde_json::Value;
use std::collections::{HashMap, HashSet};

#[derive(Serialize, Deserialize, Debug, Clone)]
enum ProxyResponse {
    RESPONSE(Response),
    CONFIG(Value),
}

/// zabbix proxy
/// 实现了 proxy 的基本功能
///
#[derive(Debug, Clone)]
pub struct ZabbixProxy {
    name: String,
    proto: ZabbixProtocol,
}

impl ZabbixProxy {
    pub const PROXY_CONFIG: &'static str = "proxy config";
    pub const HISTORY_DATA: &'static str = "history data";
    pub const PROXY_HEARTBEAT: &'static str = "proxy heartbeat";
    pub const AUTO_REGISTRATION: &'static str = "auto registration";

    pub fn new(name: &str, server: &str, port: u16) -> Self {
        let name = String::from(name);
        let proto = ZabbixProtocol::new(server, port);
        Self { name, proto }
    }

    fn send_request(&self, req: &ZabbixRequest, is_config: bool) -> Result<ProxyResponse> {
        let read_data = self.proto.send(&req.str())?;
        let response = if is_config {
            ProxyResponse::CONFIG(serde_json::from_slice(&read_data)?)
        } else {
            ProxyResponse::RESPONSE(serde_json::from_slice(&read_data)?)
        };

        Ok(response)
    }

    ///
    /// 从ZABBNIX服务端获取代理配置信息
    ///
    pub fn get_config(&self) -> Option<Value> {
        let req = ZabbixRequest::new(Self::PROXY_CONFIG, &self.name, Value::Null);
        if let Ok(r) = self.send_request(&req, true) {
            if let ProxyResponse::CONFIG(c) = r {
                return Some(c);
            }
        }
        None
    }

    ///
    /// 自动注册主机
    ///
    pub fn auto_register(&self, hosts: Vec<ZabbixHost>) -> Result<bool> {
        let hosts = serde_json::to_value(hosts)?;
        let req = ZabbixRequest::new(Self::AUTO_REGISTRATION, &self.name, hosts);
        if let Ok(r) = self.send_request(&req, false) {
            if let ProxyResponse::RESPONSE(c) = r {
                return Ok(c.success());
            }
        }
        Ok(false)
    }

    ///
    /// 向服务端发送心跳信息
    ///
    pub fn heart_beat(&self) -> Result<bool> {
        let req = ZabbixRequest::new(Self::PROXY_HEARTBEAT, &self.name, Value::Null);
        if let Ok(r) = self.send_request(&req, false) {
            if let ProxyResponse::RESPONSE(c) = r {
                return Ok(c.success());
            }
        }
        Ok(false)
    }

    ///
    /// 向服务端发送历史数据
    ///
    pub fn send_data(&self, data: &[ZabbixMetric]) -> Result<bool> {
        let data = serde_json::to_value(data)?;
        let req = ZabbixRequest::new(Self::HISTORY_DATA, &self.name, data);
        if let Ok(r) = self.send_request(&req, false) {
            if let ProxyResponse::RESPONSE(c) = r {
                //trace!("{:?}", c);
                return Ok(c.success() && c.ok());
            }
        }
        Ok(false)
    }
}

/// 扩展代理功能
impl ZabbixProxy {
    pub fn get_proxy_config(&self, compress: &[&str]) -> Option<(HashSet<Host>, HashSet<Item>)> {
        if let Some(v) = self.get_config() {
            let h = Host::from(get_item(&v["hosts"]["fields"], &v["hosts"]["data"]));
            let i = Item::from(
                get_item(&v["items"]["fields"], &v["items"]["data"]),
                compress,
            );

            return Some((h, i));
        }
        None
    }

    pub fn get_proxy_config_item(&self, compress: &[&str]) -> Option<Vec<ItemHost>> {
        if let Some((hosts, items)) = self.get_proxy_config(compress) {
            let ih = items
                .into_iter()
                .filter(|p| {
                    hosts
                        .iter()
                        .map(|q| q.hostid)
                        .collect::<Vec<_>>()
                        .contains(&p.hostid)
                })
                .map(|p| ItemHost {
                    host: hosts
                        .iter()
                        .filter(|q| q.hostid == p.hostid)
                        .collect::<Vec<_>>()[0]
                        .clone(),
                    item: p,
                })
                .collect();

            return Some(ih);
        }
        None
    }

    pub fn get_proxy_config_host(&self, compress: &[&str]) -> Option<Vec<HostItem>> {
        if let Some((hosts, items)) = self.get_proxy_config(compress) {
            let it = |x| {
                items
                    .clone()
                    .into_iter()
                    .filter(|p| p.hostid == x)
                    .collect::<Vec<_>>()
            };
            let hi = hosts
                .into_iter()
                .map(|p| HostItem {
                    items: it(p.hostid),
                    host: p,
                })
                .collect();

            return Some(hi);
        }
        None
    }
}

fn get_item(field: &Value, data: &Value) -> Vec<HashMap<String, Value>> {
    let mut result = Vec::new();
    if let Some(field) = field.as_array() {
        let field = field.iter().map(|x| x.as_str());
        if let Some(data) = data.as_array() {
            for x in data.iter() {
                let mut hm: HashMap<String, Value> = HashMap::new();
                let y = field.clone().zip(x.as_array().unwrap().iter());
                for z in y {
                    hm.insert(z.0.unwrap().to_string(), z.1.clone());
                }
                result.push(hm);
            }
        }
    }
    result
}

/*
fn get_item(field: &Value, data: &Value) -> Vec<HashMap<String, Value>> {
    let mut result = Vec::new();

    if let Some(field) = field.as_array() {
        let field = field.iter().map(|x| x.as_str());
        let data = data.as_array().unwrap();
        for x in data.iter() {
            let mut hm: HashMap<String, Value> = HashMap::new();
            let y = field.clone().zip(x.as_array().unwrap().iter());
            for z in y {
                hm.insert(z.0.unwrap().to_string(), z.1.clone());
            }
            result.push(hm);
        }
    }
    result
}
*/

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct HostItem {
    pub host: Host,
    pub items: Vec<Item>,
}

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct ItemHost {
    pub item: Item,
    pub host: Host,
}

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct Host {
    pub hostid: i64,
    pub host: String,
    //pub name: String,
}

impl Host {
    pub fn new(hostid: i64, host: String) -> Self {
        Self { hostid, host }
    }

    pub fn from(data: Vec<HashMap<String, Value>>) -> HashSet<Self> {
        let mut result = HashSet::new();
        for d in data {
            if let Some(0) = d["status"].as_i64() {
                let hostid = d["hostid"].as_i64().unwrap();
                let host = d["host"].as_str().unwrap();
                result.insert(Self::new(hostid, host.to_string()));
            }
        }
        result
    }
}

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct Item {
    pub itemid: i64,
    pub hostid: i64,
    pub key_: String,
    pub delay: u32,
}

impl Item {
    pub fn new(itemid: i64, hostid: i64, key_: String, delay: u32) -> Self {
        Self {
            itemid,
            hostid,
            key_,
            delay,
        }
    }

    pub fn from(data: Vec<HashMap<String, Value>>, compress: &[&str]) -> HashSet<Self> {
        let mut result = HashSet::new();
        for d in data {
            if let Some(0) = d["status"].as_i64() {
                let delay = trans(d["delay"].as_str().expect("delay"));
                if delay == 0 {
                    continue;
                }
                let itemid = d["itemid"].as_i64().unwrap();
                let hostid = d["hostid"].as_i64().unwrap();
                let mut key_ = d["key_"].as_str().expect("key_");

                for s in compress {
                    key_ = key_.split(s).next().unwrap();
                }

                result.insert(Self::new(itemid, hostid, key_.to_string(), delay));
            }
        }
        result
    }
}

fn trans(input: &str) -> u32 {
    if let Ok(result) = input.parse() {
        return result;
    }

    if let Ok(x) = input.to_lowercase().parse::<humantime::Duration>() {
        return x.as_secs() as u32;
    }

    0
}

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

    #[test]
    fn test_item_from() {
        let mut data: Vec<HashMap<String, Value>> = vec![];

        for x in 0..5 {
            let mut item = HashMap::new();
            item.insert("status".to_string(), json!(0));
            item.insert("itemid".to_string(), json!(1));
            item.insert("hostid".to_string(), json!(3010 + x % 2));
            item.insert("delay".to_string(), json!("30s"));
            item.insert("key_".to_string(), json!(format!("df[{}]", x)));
            data.push(item);
        }

        //let compress = ["[", "_"];
        let compress = ["["];

        let items = Item::from(data.clone(), &compress);
        assert_eq!(2, items.len());

        let items = Item::from(data.clone(), &[]);
        assert_eq!(5, items.len());
    }

    #[test]
    fn test_trans() {
        assert_eq!(trans("15"), 15);
        assert_eq!(trans("15s"), 15);
        assert_eq!(trans("15S"), 15);
        assert_eq!(trans("5m"), 300);
        assert_eq!(trans("2h"), 7200);
        assert_eq!(trans("1d"), 86400);
    }
}