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
|
/*
** Zabbix
** Copyright (C) 2001-2020 Zabbix SIA
**
** 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 envied 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 "common.h"
#include "log.h"
#include "zbxjson.h"
#include "zbxembed.h"
#include "embed.h"
#include "duktape.h"
#include "zabbix.h"
/******************************************************************************
* *
* Function: es_zabbix_dtor *
* *
* Purpose: Curlzabbix destructor *
* *
******************************************************************************/
static duk_ret_t es_zabbix_dtor(duk_context *ctx)
{
ZBX_UNUSED(ctx);
return 0;
}
/******************************************************************************
* *
* Function: es_zabbix_ctor *
* *
* Purpose: Curlzabbix constructor *
* *
******************************************************************************/
static duk_ret_t es_zabbix_ctor(duk_context *ctx)
{
if (!duk_is_constructor_call(ctx))
return DUK_RET_TYPE_ERROR;
duk_push_this(ctx);
duk_push_c_function(ctx, es_zabbix_dtor, 1);
duk_set_finalizer(ctx, -2);
return 0;
}
/******************************************************************************
* *
* Function: es_zabbix_status *
* *
* Purpose: Curlzabbix.Status method *
* *
******************************************************************************/
static duk_ret_t es_zabbix_log(duk_context *ctx)
{
zbx_es_env_t *env;
const char *message;
int level;
duk_memory_functions out_funcs;
level = duk_to_int(ctx, 0);
message = duk_to_string(ctx, 1);
zabbix_log(level, "%s", message);
duk_get_memory_functions(ctx, &out_funcs);
env = (zbx_es_env_t *)out_funcs.udata;
if (NULL == env->json)
return 0;
zbx_json_addobject(env->json, NULL);
zbx_json_adduint64(env->json, "level", (zbx_uint64_t)level);
zbx_json_addstring(env->json, "message", message, ZBX_JSON_TYPE_STRING);
zbx_json_close(env->json);
return 0;
}
static const duk_function_list_entry zabbix_methods[] = {
{"Log", es_zabbix_log, 2},
{NULL, NULL, 0}
};
static int es_zabbix_create_object(duk_context *ctx)
{
duk_push_c_function(ctx, es_zabbix_ctor, 0);
duk_push_object(ctx);
duk_put_function_list(ctx, -1, zabbix_methods);
if (1 != duk_put_prop_string(ctx, -2, "prototype"))
return FAIL;
duk_new(ctx, 0);
duk_put_global_string(ctx, "Zabbix");
return SUCCEED;
}
int zbx_es_init_zabbix(zbx_es_t *es, char **error)
{
if (0 != setjmp(es->env->loc))
{
*error = zbx_strdup(*error, es->env->error);
return FAIL;
}
if (FAIL == es_zabbix_create_object(es->env->ctx))
{
*error = zbx_strdup(*error, duk_safe_to_string(es->env->ctx, -1));
duk_pop(es->env->ctx);
return FAIL;
}
return SUCCEED;
}
|