-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzbx_lua.c
116 lines (102 loc) · 4.93 KB
/
zbx_lua.c
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
#include "sysinc.h"
#include "module.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int zbx_module_lua_version(AGENT_REQUEST *request, AGENT_RESULT *result);
static int zbx_module_lua_execute(AGENT_REQUEST *request, AGENT_RESULT *result);
static ZBX_METRIC keys[] =
/* KEY FLAG FUNCTION TEST PARAMETERS */
{
{"lua.version", 0, zbx_module_lua_version, NULL},
{"lua.execute", CF_HAVEPARAMS, zbx_module_lua_execute, NULL},
{NULL}
};
/******************************************************************************
* *
* Function: zbx_module_api_version *
* *
* Purpose: returns version number of the module interface *
* *
* Return value: ZBX_MODULE_API_VERSION - version of module.h module is *
* compiled with, in order to load module successfully Zabbix *
* MUST be compiled with the same version of this header file *
* *
******************************************************************************/
int zbx_module_api_version(void)
{
return ZBX_MODULE_API_VERSION;
}
/******************************************************************************
* *
* 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(void)
{
srand(time(NULL));
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(void)
{
return ZBX_MODULE_OK;
}
/******************************************************************************
* *
* 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;
}
static int zbx_module_lua_version(AGENT_REQUEST *request, AGENT_RESULT *result)
{
SET_STR_RESULT(result, strdup(LUA_VERSION));
return SYSINFO_RET_OK;
}
static int zbx_module_lua_execute(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *script;
lua_State *L = luaL_newstate();
int res;
if (1 != request->nparam)
{
SET_MSG_RESULT(result, strdup("Invalid number of parameters"));
return SYSINFO_RET_FAIL;
}
script = get_rparam(request, 0);
luaL_openlibs(L);
res = luaL_dofile(L, script);
if (res != LUA_OK)
{
SET_MSG_RESULT(result, strdup("Could not load file"));
lua_close(L);
return SYSINFO_RET_FAIL;
}
lua_close(L);
return SYSINFO_RET_OK;
}