-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmruby_module.c
More file actions
291 lines (260 loc) · 11.6 KB
/
mruby_module.c
File metadata and controls
291 lines (260 loc) · 11.6 KB
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
/*
** Takanori Suzuki <mail.tks@gmail.com>
** Copyright (C) 2001-2014
**
** derived from Zabbix SIA's work under GPL2 or later.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
**/
#include <sysinc.h>
#include <module.h>
#include <common.h>
#include <mruby.h>
#include <mruby/compile.h>
#include <mruby/string.h>
extern char *CONFIG_LOAD_MODULE_PATH;
typedef struct
{
char *path;
mrb_state *mrb;
}
MRB_WITH_PATH;
MRB_WITH_PATH *mrb_list = NULL;
int mrb_list_len = 0;
/* the variable keeps timeout setting for item processing */
static int item_timeout = 0;
int zbx_module_mruby_file(AGENT_REQUEST *request, AGENT_RESULT *result);
int zbx_module_mruby_string(AGENT_REQUEST *request, AGENT_RESULT *result);
static ZBX_METRIC keys[] =
/* KEY FLAG FUNCTION TEST PARAMETERS */
{
{"mruby.file", CF_HAVEPARAMS, zbx_module_mruby_file, NULL},
{"mruby.string", CF_HAVEPARAMS, zbx_module_mruby_string, NULL},
{NULL}
};
/******************************************************************************
* *
* Function: zbx_module_api_version *
* *
* Purpose: returns version number of the module interface *
* *
* Return value: ZBX_MODULE_API_VERSION_ONE - the only version supported by *
* Zabbix currently *
* *
******************************************************************************/
int zbx_module_api_version()
{
return ZBX_MODULE_API_VERSION_ONE;
}
/******************************************************************************
* *
* Function: zbx_module_item_timeout *
* *
* Purpose: set timeout value for processing of items *
* *
* Parameters: timeout - timeout in seconds, 0 - no timeout set *
* *
******************************************************************************/
void zbx_module_item_timeout(int timeout)
{
item_timeout = timeout;
}
/******************************************************************************
* *
* Function: zbx_module_item_list *
* *
* Purpose: returns list of item keys supported by the module *
* *
* Return value: list of item keys *
* *
******************************************************************************/
ZBX_METRIC *zbx_module_item_list()
{
return keys;
}
/******************************************************************************
* *
* Function: zbx_module_mruby_file *
* *
* Purpose: function for "mruby.file" key *
* *
* Return value: SYSINFO_RET_OK *
* *
******************************************************************************/
int zbx_module_mruby_file(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *param1 = NULL;
mrb_value ret;
char mruby_file_path[MAX_BUFFER_LEN];
int i = 0;
int find = 0;
param1 = get_rparam(request, 0);
zbx_snprintf(mruby_file_path, sizeof(mruby_file_path), "%s/%s/%s", CONFIG_LOAD_MODULE_PATH, "mruby", param1);
for (i = 0; i < mrb_list_len; i++)
{
if (!strcmp(mruby_file_path, mrb_list[i].path))
{
find = 1;
break;
}
}
if (find)
{
ret = mrb_funcall(mrb_list[i].mrb, mrb_top_self(mrb_list[i].mrb), "zbx_module_run", 0);
/* ret should be zabbix compatible type, like string, int, float */
if (mrb_list[i].mrb->exc)
SET_TEXT_RESULT(result, strdup(""));
else if (MRB_TT_STRING == ret.tt)
SET_TEXT_RESULT(result, strdup(RSTRING_PTR(ret)));
else if (MRB_TT_FLOAT == ret.tt)
SET_DBL_RESULT(result, mrb_float(ret));
else if (MRB_TT_FIXNUM == ret.tt)
SET_DBL_RESULT(result, mrb_fixnum(ret));
else
SET_TEXT_RESULT(result, strdup(""));
}
else
SET_TEXT_RESULT(result, strdup(""));
return SYSINFO_RET_OK;
}
/******************************************************************************
* *
* Function: zbx_module_mruby_string *
* *
* Purpose: function for "mruby.string" key *
* *
* Return value: SYSINFO_RET_OK *
* *
******************************************************************************/
int zbx_module_mruby_string(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *param1 = NULL;
mrb_state *mrb = mrb_open();
mrb_value ret;
param1 = get_rparam(request, 0);
ret = mrb_load_string(mrb, param1);
/* ret should be zabbix compatible type, like string, int, float */
if (mrb->exc)
SET_TEXT_RESULT(result, strdup(""));
else if (MRB_TT_STRING == ret.tt)
SET_TEXT_RESULT(result, strdup(RSTRING_PTR(ret)));
else if (MRB_TT_FLOAT == ret.tt)
SET_DBL_RESULT(result, mrb_float(ret));
else if (MRB_TT_FIXNUM == ret.tt)
SET_DBL_RESULT(result, mrb_fixnum(ret));
else
SET_TEXT_RESULT(result, strdup(""));
mrb_close(mrb);
return SYSINFO_RET_OK;
}
/******************************************************************************
* *
* Function: search_and_load_mruby_files *
* *
* Purpose: load "*.rb" files in CONFIG_LOAD_MODULE_PATH + "/mruby" *
* *
******************************************************************************/
void search_and_load_mruby_files()
{
char mruby_dir_path[MAX_BUFFER_LEN];
char mruby_file_path[MAX_BUFFER_LEN];
struct dirent *dir_entry = NULL;
DIR *dir = NULL;
FILE *f = NULL;
int i = 0;
zbx_snprintf(mruby_dir_path, sizeof(mruby_dir_path), "%s/%s", CONFIG_LOAD_MODULE_PATH, "mruby");
if (NULL == (dir = opendir(mruby_dir_path)))
return;
for (i = 0; NULL != (dir_entry = readdir(dir)); i++){
if (!strcmp(&(dir_entry->d_name[strlen(dir_entry->d_name)-3]),".rb"))
{
mrb_list_len++;
if (NULL == mrb_list)
mrb_list = zbx_malloc(mrb_list, sizeof(mrb_list) * mrb_list_len);
else
mrb_list = zbx_realloc(mrb_list, sizeof(mrb_list) * mrb_list_len);
zbx_snprintf(mruby_file_path, sizeof(mruby_file_path), "%s/%s", mruby_dir_path, dir_entry->d_name);
mrb_list[mrb_list_len - 1].path = zbx_strdup(NULL, mruby_file_path);
mrb_list[mrb_list_len - 1].mrb = mrb_open();
f = fopen(mruby_file_path, "r");
if(f)
{
mrb_load_file(mrb_list[mrb_list_len - 1].mrb, f);
fclose(f);
}
}
}
closedir(dir);
return;
}
/******************************************************************************
* *
* Function: exec_mruby_function *
* *
* Purpose: execute "function" in loaded "*.rb" files *
* *
******************************************************************************/
void exec_mruby_function(char *function)
{
int i;
for (i = 0; i < mrb_list_len; i++)
{
mrb_funcall(mrb_list[i].mrb, mrb_top_self(mrb_list[i].mrb), function, 0);
}
return;
}
/******************************************************************************
* *
* Function: zbx_module_init *
* *
* Purpose: the function is called on agent startup *
* It should be used to call any initialization routines *
* *
* Return value: ZBX_MODULE_OK - success *
* ZBX_MODULE_FAIL - module initialization failed *
* *
* Comment: the module won't be loaded in case of ZBX_MODULE_FAIL *
* *
******************************************************************************/
int zbx_module_init()
{
search_and_load_mruby_files();
exec_mruby_function("zbx_module_init");
return ZBX_MODULE_OK;
}
/******************************************************************************
* *
* Function: zbx_module_uninit *
* *
* Purpose: the function is called on agent shutdown *
* It should be used to cleanup used resources if there are any *
* *
* Return value: ZBX_MODULE_OK - success *
* ZBX_MODULE_FAIL - function failed *
* *
******************************************************************************/
int zbx_module_uninit()
{
int i;
exec_mruby_function("zbx_module_uninit");
for(i = 0; i < mrb_list_len; i++)
{
zbx_free(mrb_list[i].path);
mrb_close(mrb_list[i].mrb);
}
zbx_free(mrb_list);
return ZBX_MODULE_OK;
}