1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "common.h"
21 #include "dbcache.h"
22 #include "log.h"
23 #include "db.h"
24 
zbx_lld_override_operation_free(zbx_lld_override_operation_t * override_operation)25 void	zbx_lld_override_operation_free(zbx_lld_override_operation_t *override_operation)
26 {
27 	zbx_vector_db_tag_ptr_clear_ext(&override_operation->tags, zbx_db_tag_free);
28 	zbx_vector_db_tag_ptr_destroy(&override_operation->tags);
29 
30 	zbx_vector_uint64_destroy(&override_operation->templateids);
31 
32 	zbx_free(override_operation->value);
33 	zbx_free(override_operation->delay);
34 	zbx_free(override_operation->history);
35 	zbx_free(override_operation->trends);
36 	zbx_free(override_operation);
37 }
38 
39 /******************************************************************************
40  *                                                                            *
41  * Function: lld_override_operations_load_tags                                *
42  *                                                                            *
43  * Purpose: load tag override operations from database                        *
44  *                                                                            *
45  * Parameters: overrideids - [IN] the lld overrideids, sorted                 *
46  *             sql         - [IN/OUT] the sql query buffer                    *
47  *             sql_alloc   - [IN/OUT] the sql query buffer size               *
48  *             ops         - [IN/OUT] the lld override operations, sorted by  *
49  *                                    override_operationid                    *
50  *                                                                            *
51  ******************************************************************************/
lld_override_operations_load_tags(const zbx_vector_uint64_t * overrideids,char ** sql,size_t * sql_alloc,zbx_vector_ptr_t * ops)52 static void	lld_override_operations_load_tags(const zbx_vector_uint64_t *overrideids, char **sql, size_t *sql_alloc,
53 		zbx_vector_ptr_t *ops)
54 {
55 	size_t				sql_offset = 0;
56 	DB_RESULT			result;
57 	DB_ROW				row;
58 	zbx_lld_override_operation_t	*op = NULL;
59 
60 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
61 
62 	zbx_strcpy_alloc(sql, sql_alloc, &sql_offset,
63 			"select o.lld_override_operationid,ot.tag,ot.value"
64 			" from lld_override_operation o,lld_override_optag ot"
65 			" where o.lld_override_operationid=ot.lld_override_operationid"
66 			" and");
67 	DBadd_condition_alloc(sql, sql_alloc, &sql_offset, "o.lld_overrideid", overrideids->values,
68 			overrideids->values_num);
69 	zbx_snprintf_alloc(sql, sql_alloc, &sql_offset, " and o.operationobject in (%d,%d,%d)",
70 			ZBX_LLD_OVERRIDE_OP_OBJECT_TRIGGER, ZBX_LLD_OVERRIDE_OP_OBJECT_HOST,
71 			ZBX_LLD_OVERRIDE_OP_OBJECT_ITEM);
72 	zbx_strcpy_alloc(sql, sql_alloc, &sql_offset, " order by o.lld_override_operationid");
73 
74 	result = DBselect("%s", *sql);
75 	while (NULL != (row = DBfetch(result)))
76 	{
77 		zbx_uint64_t	override_operationid;
78 		zbx_db_tag_t	*tag;
79 
80 		ZBX_STR2UINT64(override_operationid, row[0]);
81 		if (NULL == op || op->override_operationid != override_operationid)
82 		{
83 			int	index;
84 
85 			if (FAIL == (index = zbx_vector_ptr_bsearch(ops, &override_operationid,
86 					ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)))
87 			{
88 				THIS_SHOULD_NEVER_HAPPEN;
89 				continue;
90 			}
91 			op = (zbx_lld_override_operation_t *)ops->values[index];
92 		}
93 
94 		tag = (zbx_db_tag_t *)zbx_malloc(NULL, sizeof(zbx_db_tag_t));
95 		tag->tag = zbx_strdup(NULL, row[1]);
96 		tag->value = zbx_strdup(NULL, row[2]);
97 		zbx_vector_db_tag_ptr_append(&op->tags, tag);
98 	}
99 	DBfree_result(result);
100 
101 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
102 }
103 
104 /******************************************************************************
105  *                                                                            *
106  * Function: lld_override_operations_load_templates                           *
107  *                                                                            *
108  * Purpose: load template lld override operations from database               *
109  *                                                                            *
110  * Parameters: overrideids - [IN] the lld overrideids, sorted                 *
111  *             sql         - [IN/OUT] the sql query buffer                    *
112  *             sql_alloc   - [IN/OUT] the sql query buffer size               *
113  *             ops         - [IN/OUT] the lld override operations, sorted by  *
114  *                                    override_operationid                    *
115  *                                                                            *
116  ******************************************************************************/
lld_override_operations_load_templates(const zbx_vector_uint64_t * overrideids,char ** sql,size_t * sql_alloc,zbx_vector_ptr_t * ops)117 static void	lld_override_operations_load_templates(const zbx_vector_uint64_t *overrideids, char **sql,
118 		size_t *sql_alloc, zbx_vector_ptr_t *ops)
119 {
120 	size_t				sql_offset = 0;
121 	DB_RESULT			result;
122 	DB_ROW				row;
123 	zbx_lld_override_operation_t	*op = NULL;
124 
125 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
126 
127 	zbx_strcpy_alloc(sql, sql_alloc, &sql_offset,
128 			"select o.lld_override_operationid,ot.templateid"
129 			" from lld_override_operation o,lld_override_optemplate ot"
130 			" where o.lld_override_operationid=ot.lld_override_operationid"
131 			" and");
132 	DBadd_condition_alloc(sql, sql_alloc, &sql_offset, "o.lld_overrideid", overrideids->values,
133 			overrideids->values_num);
134 	zbx_snprintf_alloc(sql, sql_alloc, &sql_offset, " and o.operationobject=%d",
135 			ZBX_LLD_OVERRIDE_OP_OBJECT_HOST);
136 	zbx_strcpy_alloc(sql, sql_alloc, &sql_offset, " order by o.lld_override_operationid");
137 
138 	result = DBselect("%s", *sql);
139 	while (NULL != (row = DBfetch(result)))
140 	{
141 		zbx_uint64_t	templateid, override_operationid;
142 
143 		ZBX_STR2UINT64(override_operationid, row[0]);
144 		if (NULL == op || op->override_operationid != override_operationid)
145 		{
146 			int	index;
147 
148 			if (FAIL == (index = zbx_vector_ptr_bsearch(ops, &override_operationid,
149 					ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)))
150 			{
151 				THIS_SHOULD_NEVER_HAPPEN;
152 				continue;
153 			}
154 			op = (zbx_lld_override_operation_t *)ops->values[index];
155 		}
156 
157 		ZBX_STR2UINT64(templateid, row[1]);
158 		zbx_vector_uint64_append(&op->templateids, templateid);
159 	}
160 	DBfree_result(result);
161 
162 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
163 }
164 
165 /******************************************************************************
166  *                                                                            *
167  * Function: zbx_load_lld_override_operations                                 *
168  *                                                                            *
169  * Purpose: load lld override operations from database                        *
170  *                                                                            *
171  * Parameters: overrideids - [IN] the lld overrideids, sorted                 *
172  *             sql         - [IN/OUT] the sql query buffer                    *
173  *             sql_alloc   - [IN/OUT] the sql query buffer size               *
174  *             ops         - [OUT] the lld override operations, sorted by     *
175  *                                    override_operationid                    *
176  *                                                                            *
177  ******************************************************************************/
zbx_load_lld_override_operations(const zbx_vector_uint64_t * overrideids,char ** sql,size_t * sql_alloc,zbx_vector_ptr_t * ops)178 void	zbx_load_lld_override_operations(const zbx_vector_uint64_t *overrideids, char **sql, size_t *sql_alloc,
179 		zbx_vector_ptr_t *ops)
180 {
181 	size_t				sql_offset = 0;
182 	DB_RESULT			result;
183 	DB_ROW				row;
184 	zbx_lld_override_operation_t	*override_operation = NULL;
185 	zbx_uint64_t			object_mask = 0;
186 
187 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
188 
189 	zbx_strcpy_alloc(sql, sql_alloc, &sql_offset,
190 			"select o.lld_overrideid,o.lld_override_operationid,o.operationobject,o.operator,o.value,"
191 				"s.status,"
192 				"d.discover,"
193 				"p.delay,"
194 				"h.history,"
195 				"t.trends,"
196 				"os.severity,"
197 				"i.inventory_mode"
198 			" from lld_override_operation o"
199 			" left join lld_override_opstatus s"
200 				" on o.lld_override_operationid=s.lld_override_operationid"
201 			" left join lld_override_opdiscover d"
202 				" on o.lld_override_operationid=d.lld_override_operationid"
203 			" left join lld_override_opperiod p"
204 				" on o.lld_override_operationid=p.lld_override_operationid"
205 			" left join lld_override_ophistory h"
206 				" on o.lld_override_operationid=h.lld_override_operationid"
207 			" left join lld_override_optrends t"
208 				" on o.lld_override_operationid=t.lld_override_operationid"
209 			" left join lld_override_opseverity os"
210 				" on o.lld_override_operationid=os.lld_override_operationid"
211 			" left join lld_override_opinventory i"
212 				" on o.lld_override_operationid=i.lld_override_operationid"
213 			" where");
214 	DBadd_condition_alloc(sql, sql_alloc, &sql_offset, "o.lld_overrideid", overrideids->values,
215 			overrideids->values_num);
216 	zbx_strcpy_alloc(sql, sql_alloc, &sql_offset, " order by o.lld_override_operationid");
217 
218 	result = DBselect("%s", *sql);
219 	while (NULL != (row = DBfetch(result)))
220 	{
221 		override_operation = (zbx_lld_override_operation_t *)zbx_malloc(NULL,
222 				sizeof(zbx_lld_override_operation_t));
223 
224 		ZBX_STR2UINT64(override_operation->overrideid, row[0]);
225 
226 		zbx_vector_db_tag_ptr_create(&override_operation->tags);
227 		zbx_vector_uint64_create(&override_operation->templateids);
228 
229 		ZBX_STR2UINT64(override_operation->override_operationid, row[1]);
230 		override_operation->operationtype = (unsigned char)atoi(row[2]);
231 		override_operation->operator = (unsigned char)atoi(row[3]);
232 		override_operation->value = zbx_strdup(NULL, row[4]);
233 
234 		override_operation->status = FAIL == DBis_null(row[5]) ? (unsigned char)atoi(row[5]) :
235 				ZBX_PROTOTYPE_STATUS_COUNT;
236 
237 		override_operation->discover = FAIL == DBis_null(row[6]) ? (unsigned char)atoi(row[6]) :
238 				ZBX_PROTOTYPE_DISCOVER_COUNT;
239 
240 		override_operation->delay = FAIL == DBis_null(row[7]) ? zbx_strdup(NULL, row[7]) :
241 				NULL;
242 		override_operation->history = FAIL == DBis_null(row[8]) ? zbx_strdup(NULL, row[8]) :
243 				NULL;
244 		override_operation->trends = FAIL == DBis_null(row[9]) ? zbx_strdup(NULL, row[9]) :
245 				NULL;
246 		override_operation->severity = FAIL == DBis_null(row[10]) ? (unsigned char)atoi(row[10]) :
247 				TRIGGER_SEVERITY_COUNT;
248 
249 		override_operation->inventory_mode = FAIL == DBis_null(row[11]) ?
250 				(unsigned char)atoi(row[11]) : HOST_INVENTORY_COUNT;
251 
252 		zbx_vector_ptr_append(ops, override_operation);
253 
254 		object_mask |= (1 << override_operation->operationtype);
255 	}
256 	DBfree_result(result);
257 
258 	if (0 != (object_mask & ((1 << ZBX_LLD_OVERRIDE_OP_OBJECT_HOST) | (1 << ZBX_LLD_OVERRIDE_OP_OBJECT_TRIGGER) |
259 			(1 << ZBX_LLD_OVERRIDE_OP_OBJECT_ITEM))))
260 	{
261 		lld_override_operations_load_tags(overrideids, sql, sql_alloc, ops);
262 	}
263 
264 	if (0 != (object_mask & (1 << ZBX_LLD_OVERRIDE_OP_OBJECT_HOST)))
265 		lld_override_operations_load_templates(overrideids, sql, sql_alloc, ops);
266 
267 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
268 }
269