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
|
/*
** Zabbix
** Copyright (C) 2001-2019 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 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 "common.h"
#include "dbcache.h"
#include "log.h"
/******************************************************************************
* *
* Function: lld_macro_paths_compare *
* *
* Purpose: sorting function to sort LLD macros by unique name *
* *
******************************************************************************/
int zbx_lld_macro_paths_compare(const void *d1, const void *d2)
{
const zbx_lld_macro_path_t *r1 = *(const zbx_lld_macro_path_t **)d1;
const zbx_lld_macro_path_t *r2 = *(const zbx_lld_macro_path_t **)d2;
return strcmp(r1->lld_macro, r2->lld_macro);
}
/******************************************************************************
* *
* Function: lld_macro_paths_get *
* *
* Purpose: retrieve list of LLD macros *
* *
* Parameters: lld_ruleid - [IN] LLD id *
* lld_macro_paths - [OUT] use json path to extract from jp_row *
* error - [OUT] in case json path is invalid *
* *
******************************************************************************/
int zbx_lld_macro_paths_get(zbx_uint64_t lld_ruleid, zbx_vector_ptr_t *lld_macro_paths, char **error)
{
DB_RESULT result;
DB_ROW row;
zbx_lld_macro_path_t *lld_macro_path;
int ret = SUCCEED;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
result = DBselect(
"select lld_macro,path"
" from lld_macro_path"
" where itemid=" ZBX_FS_UI64
" order by lld_macro",
lld_ruleid);
while (NULL != (row = DBfetch(result)))
{
zbx_jsonpath_t path;
if (SUCCEED != (ret = zbx_jsonpath_compile(row[1], &path)))
{
*error = zbx_dsprintf(*error, "Cannot process LLD macro \"%s\": %s.\n", row[0],
zbx_json_strerror());
break;
}
zbx_jsonpath_clear(&path);
lld_macro_path = (zbx_lld_macro_path_t *)zbx_malloc(NULL, sizeof(zbx_lld_macro_path_t));
lld_macro_path->lld_macro = zbx_strdup(NULL, row[0]);
lld_macro_path->path = zbx_strdup(NULL, row[1]);
zbx_vector_ptr_append(lld_macro_paths, lld_macro_path);
}
DBfree_result(result);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
return ret;
}
/******************************************************************************
* *
* Function: lld_macro_path_free *
* *
* Purpose: release resources allocated by lld macro path *
* *
* Parameters: lld_macro_path - [IN] json path to extract from lld_row *
* *
******************************************************************************/
void zbx_lld_macro_path_free(zbx_lld_macro_path_t *lld_macro_path)
{
zbx_free(lld_macro_path->path);
zbx_free(lld_macro_path->lld_macro);
zbx_free(lld_macro_path);
}
/******************************************************************************
* *
* Function: zbx_lld_macro_value_by_name *
* *
* Purpose: get value of LLD macro using json path if available or by *
* searching for such key in key value pairs of array entry *
* *
* Parameters: jp_row - [IN] the lld data row *
* lld_macro_paths - [IN] use json path to extract from jp_row *
* macro - [IN] LLD macro *
* value - [OUT] value extracted from jp_row *
* *
******************************************************************************/
int zbx_lld_macro_value_by_name(const struct zbx_json_parse *jp_row, const zbx_vector_ptr_t *lld_macro_paths,
const char *macro, char **value)
{
zbx_lld_macro_path_t lld_macro_path_local, *lld_macro_path;
int index;
size_t value_alloc = 0;
lld_macro_path_local.lld_macro = (char *)macro;
if (FAIL != (index = zbx_vector_ptr_bsearch(lld_macro_paths, &lld_macro_path_local,
zbx_lld_macro_paths_compare)))
{
lld_macro_path = (zbx_lld_macro_path_t *)lld_macro_paths->values[index];
if (SUCCEED == zbx_jsonpath_query(jp_row, lld_macro_path->path, value) && NULL != *value)
return SUCCEED;
return FAIL;
}
else
return zbx_json_value_by_name_dyn(jp_row, macro, value, &value_alloc);
}
|