Skip to content

Commit faa0e34

Browse files
committed
Update Zabbix Rust library
1 parent 5871f58 commit faa0e34

File tree

1 file changed

+55
-64
lines changed

1 file changed

+55
-64
lines changed

src/lib.rs

+55-64
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate libc;
22

3-
use std::{ffi, ptr};
3+
use std::ffi;
44
use libc::{c_char, c_int, c_uint, uint64_t, c_double, malloc, strncpy};
55

66
// Return codes used by module during (un)initialization
@@ -35,70 +35,63 @@ pub const AR_MESSAGE: c_int = 32;
3535
pub const SYSINFO_RET_OK: c_int = 0;
3636
pub const SYSINFO_RET_FAIL: c_int = 1;
3737

38-
#[repr(C)]
39-
#[derive(Copy)]
40-
pub struct ZabbixMetric {
41-
key: *const c_char,
42-
flags: c_uint,
43-
function: extern "C" fn(*mut ZabbixRequest, *mut ZabbixResult) -> c_int,
44-
test_param: *const c_char,
45-
}
46-
47-
impl Clone for ZabbixMetric {
48-
fn clone(&self) -> Self { *self }
38+
// Type used for creating new Zabbix item keys
39+
pub struct Metric {
40+
pub key: ffi::CString,
41+
pub flags: usize,
42+
pub function: extern "C" fn(*mut AGENT_REQUEST, *mut AGENT_RESULT) -> c_int,
43+
pub test_param: ffi::CString,
4944
}
5045

51-
impl ZabbixMetric {
52-
pub fn new(key: Option<&str>,
53-
flags: Option<u32>,
54-
function: Option<extern "C" fn(*mut ZabbixRequest, *mut ZabbixResult) -> i32>,
55-
test_param: Option<&str>) -> ZabbixMetric {
56-
57-
let c_key: *const c_char = match key {
58-
Some(ref k) => ffi::CString::new(*k).unwrap().as_ptr(),
59-
None => ptr::null(),
60-
};
61-
62-
let flags: c_uint = match flags {
63-
Some(f) => f,
64-
None => CF_NOPARAMS,
65-
};
66-
67-
let function: extern "C" fn(*mut ZabbixRequest, *mut ZabbixResult) -> c_int = match function {
68-
Some(callback) => callback,
69-
None => dummy_callback,
70-
};
71-
72-
let c_test_param: *const c_char = match test_param {
73-
Some(ref t) => ffi::CString::new(*t).unwrap().as_ptr(),
74-
None => ptr::null(),
75-
};
76-
77-
ZabbixMetric {
78-
key: c_key,
46+
impl Metric {
47+
pub fn new(key: &str, flags: usize, function: extern "C" fn(*mut AGENT_REQUEST, *mut AGENT_RESULT) -> c_int, test_param: &str) -> Metric {
48+
Metric {
49+
key: ffi::CString::new(key).unwrap(),
7950
flags: flags,
8051
function: function,
81-
test_param: c_test_param,
52+
test_param: ffi::CString::new(test_param).unwrap(),
8253
}
8354
}
55+
56+
pub fn to_zabbix_item(&self) -> ZBX_METRIC {
57+
ZBX_METRIC {
58+
key: self.key.as_ptr(),
59+
flags: self.flags as c_uint,
60+
function: self.function,
61+
test_param: self.test_param.as_ptr(),
62+
}
63+
}
64+
}
65+
66+
#[repr(C)]
67+
#[derive(Copy)]
68+
pub struct ZBX_METRIC {
69+
pub key: *const c_char,
70+
pub flags: c_uint,
71+
pub function: extern "C" fn(*mut AGENT_REQUEST, *mut AGENT_RESULT) -> c_int,
72+
pub test_param: *const c_char,
73+
}
74+
75+
impl Clone for ZBX_METRIC {
76+
fn clone(&self) -> Self { *self }
8477
}
8578

8679
#[repr(C)]
8780
#[derive(Copy)]
88-
pub struct ZabbixRequest {
81+
pub struct AGENT_REQUEST {
8982
key: *const c_char,
9083
nparam: c_int,
9184
params: *const *const c_char,
9285
lastlogsize: uint64_t,
9386
mtime: c_int,
9487
}
9588

96-
impl Clone for ZabbixRequest {
89+
impl Clone for AGENT_REQUEST {
9790
fn clone(&self) -> Self { *self }
9891
}
9992

100-
impl ZabbixRequest {
101-
pub fn get_params<'a>(request: *mut ZabbixRequest) -> Vec<&'a[u8]> {
93+
impl AGENT_REQUEST {
94+
pub fn get_params<'a>(request: *mut AGENT_REQUEST) -> Vec<&'a[u8]> {
10295
unsafe {
10396
let len = (*request).nparam;
10497
let mut v = Vec::new();
@@ -132,7 +125,7 @@ impl Clone for zbx_log_t {
132125

133126
#[repr(C)]
134127
#[derive(Copy)]
135-
pub struct ZabbixResult {
128+
pub struct AGENT_RESULT {
136129
_type: c_int,
137130
ui64: uint64_t,
138131
dbl: c_double,
@@ -142,57 +135,47 @@ pub struct ZabbixResult {
142135
logs: *const *const zbx_log_t,
143136
}
144137

145-
impl Clone for ZabbixResult {
138+
impl Clone for AGENT_RESULT {
146139
fn clone(&self) -> Self { *self }
147140
}
148141

149-
impl ZabbixResult {
150-
pub fn set_uint64_result(result: *mut ZabbixResult, value: u64) {
142+
impl AGENT_RESULT {
143+
pub fn set_uint64_result(result: *mut AGENT_RESULT, value: u64) {
151144
unsafe {
152145
(*result)._type |= AR_UINT64;
153146
(*result).ui64 = value as uint64_t;
154147
}
155148
}
156149

157-
pub fn set_f64_result(result: *mut ZabbixResult, value: f64) {
150+
pub fn set_f64_result(result: *mut AGENT_RESULT, value: f64) {
158151
unsafe {
159152
(*result)._type |= AR_DOUBLE;
160153
(*result).dbl = value as c_double;
161154
}
162155
}
163156

164-
pub fn set_str_result(result: *mut ZabbixResult, value: &str) {
157+
pub fn set_str_result(result: *mut AGENT_RESULT, value: &str) {
165158
unsafe {
166159
(*result)._type |= AR_STRING;
167160
(*result)._str = string_to_malloc_ptr(value);
168161
}
169162
}
170163

171-
pub fn set_text_result(result: *mut ZabbixResult, value: &str) {
164+
pub fn set_text_result(result: *mut AGENT_RESULT, value: &str) {
172165
unsafe {
173166
(*result)._type |= AR_TEXT;
174167
(*result).text = string_to_malloc_ptr(value);
175168
}
176169
}
177170

178-
// TODO: Implement set_log_result(...)
179-
180-
pub fn set_msg_result(result: *mut ZabbixResult, value: &str) {
171+
pub fn set_msg_result(result: *mut AGENT_RESULT, value: &str) {
181172
unsafe {
182173
(*result)._type |= AR_MESSAGE;
183174
(*result).msg = string_to_malloc_ptr(value);
184175
}
185176
}
186-
}
187177

188-
// Dummy Zabbix item callback function.
189-
// Callback is used by NULL-key Zabbix items to specify the
190-
// end of the items list.
191-
// Do not use this callback directly in your crates.
192-
#[no_mangle]
193-
#[allow(unused_variables)]
194-
pub extern fn dummy_callback(request: *mut ZabbixRequest, result: *mut ZabbixResult) -> c_int {
195-
SYSINFO_RET_OK
178+
// TODO: Implement set_log_result(...)
196179
}
197180

198181
// When the result of a Zabbix item is text (string, text and message)
@@ -208,3 +191,11 @@ unsafe fn string_to_malloc_ptr(src: &str) -> *mut c_char {
208191
dst
209192
}
210193

194+
pub fn create_items(metrics: &Vec<Metric>) -> *const ZBX_METRIC {
195+
let items = metrics
196+
.iter()
197+
.map(|metric| metric.to_zabbix_item())
198+
.collect::<Vec<_>>();
199+
200+
items.as_ptr()
201+
}

0 commit comments

Comments
 (0)