1
1
extern crate libc;
2
2
3
- use std:: { ffi, ptr } ;
3
+ use std:: ffi;
4
4
use libc:: { c_char, c_int, c_uint, uint64_t, c_double, malloc, strncpy} ;
5
5
6
6
// Return codes used by module during (un)initialization
@@ -35,70 +35,63 @@ pub const AR_MESSAGE: c_int = 32;
35
35
pub const SYSINFO_RET_OK : c_int = 0 ;
36
36
pub const SYSINFO_RET_FAIL : c_int = 1 ;
37
37
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 ,
49
44
}
50
45
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 ( ) ,
79
50
flags : flags,
80
51
function : function,
81
- test_param : c_test_param ,
52
+ test_param : ffi :: CString :: new ( test_param ) . unwrap ( ) ,
82
53
}
83
54
}
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 }
84
77
}
85
78
86
79
#[ repr( C ) ]
87
80
#[ derive( Copy ) ]
88
- pub struct ZabbixRequest {
81
+ pub struct AGENT_REQUEST {
89
82
key : * const c_char ,
90
83
nparam : c_int ,
91
84
params : * const * const c_char ,
92
85
lastlogsize : uint64_t ,
93
86
mtime : c_int ,
94
87
}
95
88
96
- impl Clone for ZabbixRequest {
89
+ impl Clone for AGENT_REQUEST {
97
90
fn clone ( & self ) -> Self { * self }
98
91
}
99
92
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 ] > {
102
95
unsafe {
103
96
let len = ( * request) . nparam ;
104
97
let mut v = Vec :: new ( ) ;
@@ -132,7 +125,7 @@ impl Clone for zbx_log_t {
132
125
133
126
#[ repr( C ) ]
134
127
#[ derive( Copy ) ]
135
- pub struct ZabbixResult {
128
+ pub struct AGENT_RESULT {
136
129
_type : c_int ,
137
130
ui64 : uint64_t ,
138
131
dbl : c_double ,
@@ -142,57 +135,47 @@ pub struct ZabbixResult {
142
135
logs : * const * const zbx_log_t ,
143
136
}
144
137
145
- impl Clone for ZabbixResult {
138
+ impl Clone for AGENT_RESULT {
146
139
fn clone ( & self ) -> Self { * self }
147
140
}
148
141
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 ) {
151
144
unsafe {
152
145
( * result) . _type |= AR_UINT64 ;
153
146
( * result) . ui64 = value as uint64_t ;
154
147
}
155
148
}
156
149
157
- pub fn set_f64_result ( result : * mut ZabbixResult , value : f64 ) {
150
+ pub fn set_f64_result ( result : * mut AGENT_RESULT , value : f64 ) {
158
151
unsafe {
159
152
( * result) . _type |= AR_DOUBLE ;
160
153
( * result) . dbl = value as c_double ;
161
154
}
162
155
}
163
156
164
- pub fn set_str_result ( result : * mut ZabbixResult , value : & str ) {
157
+ pub fn set_str_result ( result : * mut AGENT_RESULT , value : & str ) {
165
158
unsafe {
166
159
( * result) . _type |= AR_STRING ;
167
160
( * result) . _str = string_to_malloc_ptr ( value) ;
168
161
}
169
162
}
170
163
171
- pub fn set_text_result ( result : * mut ZabbixResult , value : & str ) {
164
+ pub fn set_text_result ( result : * mut AGENT_RESULT , value : & str ) {
172
165
unsafe {
173
166
( * result) . _type |= AR_TEXT ;
174
167
( * result) . text = string_to_malloc_ptr ( value) ;
175
168
}
176
169
}
177
170
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 ) {
181
172
unsafe {
182
173
( * result) . _type |= AR_MESSAGE ;
183
174
( * result) . msg = string_to_malloc_ptr ( value) ;
184
175
}
185
176
}
186
- }
187
177
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(...)
196
179
}
197
180
198
181
// 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 {
208
191
dst
209
192
}
210
193
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