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 #ifndef ZABBIX_ZBXSERVER_H
21 #define ZABBIX_ZBXSERVER_H
22 
23 #include "common.h"
24 #include "db.h"
25 #include "dbcache.h"
26 #include "zbxjson.h"
27 #include "zbxvariant.h"
28 #include "zbxeval.h"
29 
30 #define MACRO_TYPE_MESSAGE_NORMAL	0x00000001
31 #define MACRO_TYPE_MESSAGE_RECOVERY	0x00000002
32 #define MACRO_TYPE_TRIGGER_URL		0x00000004
33 #define MACRO_TYPE_TRIGGER_EXPRESSION	0x00000008
34 #define MACRO_TYPE_TRIGGER_DESCRIPTION	0x00000010	/* name */
35 #define MACRO_TYPE_TRIGGER_COMMENTS	0x00000020	/* description */
36 #define MACRO_TYPE_ITEM_KEY		0x00000040
37 #define MACRO_TYPE_INTERFACE_ADDR	0x00000100
38 #define MACRO_TYPE_COMMON		0x00000400
39 #define MACRO_TYPE_PARAMS_FIELD		0x00000800
40 #define MACRO_TYPE_SCRIPT		0x00001000
41 #define MACRO_TYPE_SNMP_OID		0x00002000
42 #define MACRO_TYPE_HTTPTEST_FIELD	0x00004000
43 #define MACRO_TYPE_LLD_FILTER		0x00008000
44 #define MACRO_TYPE_ALERT		0x00010000
45 #define MACRO_TYPE_TRIGGER_TAG		0x00020000
46 #define MACRO_TYPE_JMX_ENDPOINT		0x00040000
47 #define MACRO_TYPE_MESSAGE_ACK		0x00080000
48 #define MACRO_TYPE_HTTP_RAW		0x00100000
49 #define MACRO_TYPE_HTTP_JSON		0x00200000
50 #define MACRO_TYPE_HTTP_XML		0x00400000
51 #define MACRO_TYPE_ALLOWED_HOSTS	0x00800000
52 #define MACRO_TYPE_ITEM_TAG		0x01000000
53 #define MACRO_TYPE_EVENT_NAME		0x02000000	/* event name in trigger configuration */
54 #define MACRO_TYPE_SCRIPT_PARAMS_FIELD	0x04000000
55 #define MACRO_TYPE_SCRIPT_NORMAL	0x08000000
56 #define MACRO_TYPE_SCRIPT_RECOVERY	0x10000000
57 #define MACRO_TYPE_REPORT		0x20000000
58 
59 #define MACRO_EXPAND_NO			0
60 #define MACRO_EXPAND_YES		1
61 
62 #define STR_CONTAINS_MACROS(str)	(NULL != strchr(str, '{'))
63 
64 int	evaluate_function(char **value, DC_ITEM *item, const char *function, const char *parameter,
65 		const zbx_timespec_t *ts, char **error);
66 int	evaluate_function2(zbx_variant_t *value, DC_ITEM *item, const char *function, const char *parameter,
67 		const zbx_timespec_t *ts, char **error);
68 
69 int	zbx_is_trigger_function(const char *name, size_t len);
70 
71 int	substitute_simple_macros(const zbx_uint64_t *actionid, const DB_EVENT *event, const DB_EVENT *r_event,
72 		const zbx_uint64_t *userid, const zbx_uint64_t *hostid, const DC_HOST *dc_host, const DC_ITEM *dc_item,
73 		const DB_ALERT *alert, const DB_ACKNOWLEDGE *ack, const char *tz, char **data, int macro_type,
74 		char *error, int maxerrlen);
75 
76 int	substitute_simple_macros_unmasked(const zbx_uint64_t *actionid, const DB_EVENT *event, const DB_EVENT *r_event,
77 		const zbx_uint64_t *userid, const zbx_uint64_t *hostid, const DC_HOST *dc_host, const DC_ITEM *dc_item,
78 		const DB_ALERT *alert, const DB_ACKNOWLEDGE *ack, const char *tz, char **data, int macro_type,
79 		char *error, int maxerrlen);
80 
81 void	evaluate_expressions(zbx_vector_ptr_t *triggers, const zbx_vector_uint64_t *history_itemids,
82 		const DC_ITEM *history_items, const int *history_errcodes);
83 void	prepare_triggers(DC_TRIGGER **triggers, int triggers_num);
84 
85 void	zbx_format_value(char *value, size_t max_len, zbx_uint64_t valuemapid,
86 		const char *units, unsigned char value_type);
87 
88 void	zbx_determine_items_in_expressions(zbx_vector_ptr_t *trigger_order, const zbx_uint64_t *itemids, int item_num);
89 
90 #define ZBX_EXPRESSION_NORMAL		0
91 #define ZBX_EXPRESSION_AGGREGATE	1
92 
93 typedef struct
94 {
95 	zbx_eval_context_t	*ctx;
96 	zbx_vector_ptr_t	queries;
97 	int			mode;
98 	int			one_num;
99 	int			many_num;
100 	zbx_uint64_t		hostid;
101 
102 	/* cache to resolve one item queries */
103 	zbx_host_key_t		*hostkeys;
104 	DC_ITEM			*dcitems_hk;
105 	int			*errcodes_hk;
106 
107 	/* cache to resolve many item queries */
108 	zbx_vector_ptr_t	groups;
109 	zbx_vector_ptr_t	itemtags;
110 	zbx_vector_ptr_t	dcitem_refs;
111 	DC_ITEM			*dcitems;
112 	int			*errcodes;
113 	int			dcitems_num;
114 }
115 zbx_expression_eval_t;
116 
117 void	zbx_expression_eval_init(zbx_expression_eval_t *eval, int mode, zbx_eval_context_t *ctx);
118 void	zbx_expression_eval_clear(zbx_expression_eval_t *eval);
119 void	zbx_expression_eval_resolve_item_hosts(zbx_expression_eval_t *eval, const DC_ITEM *item);
120 void	zbx_expression_eval_resolve_trigger_hosts(zbx_expression_eval_t *eval, const DB_TRIGGER *trigger);
121 int	zbx_expression_eval_execute(zbx_expression_eval_t *eval, const zbx_timespec_t *ts, zbx_variant_t *value,
122 		char **error);
123 
124 /* lld macro context */
125 #define ZBX_MACRO_ANY		(ZBX_TOKEN_LLD_MACRO | ZBX_TOKEN_LLD_FUNC_MACRO | ZBX_TOKEN_USER_MACRO)
126 #define ZBX_MACRO_JSON		(ZBX_MACRO_ANY | ZBX_TOKEN_JSON)
127 #define ZBX_MACRO_SIMPLE	(ZBX_MACRO_ANY | ZBX_TOKEN_SIMPLE_MACRO)
128 #define ZBX_MACRO_FUNC		(ZBX_MACRO_ANY | ZBX_TOKEN_FUNC_MACRO)
129 
130 int	substitute_lld_macros(char **data, const struct zbx_json_parse *jp_row, const zbx_vector_ptr_t *lld_macro_paths,
131 		int flags, char *error, size_t max_error_len);
132 int	substitute_key_macros(char **data, zbx_uint64_t *hostid, DC_ITEM *dc_item, const struct zbx_json_parse *jp_row,
133 		const zbx_vector_ptr_t *lld_macro_paths, int macro_type, char *error, size_t maxerrlen);
134 int	substitute_key_macros_unmasked(char **data, zbx_uint64_t *hostid, DC_ITEM *dc_item,
135 		const struct zbx_json_parse *jp_row, const zbx_vector_ptr_t *lld_macro_paths, int macro_type,
136 		char *error, size_t maxerrlen);
137 int	substitute_function_lld_param(const char *e, size_t len, unsigned char key_in_param,
138 		char **exp, size_t *exp_alloc, size_t *exp_offset, const struct zbx_json_parse *jp_row,
139 		const zbx_vector_ptr_t *lld_macro_paths, char *error, size_t max_error_len);
140 int	substitute_macros_xml(char **data, const DC_ITEM *item, const struct zbx_json_parse *jp_row,
141 		const zbx_vector_ptr_t *lld_macro_paths, char *error, int maxerrlen);
142 int	substitute_macros_xml_unmasked(char **data, const DC_ITEM *item, const struct zbx_json_parse *jp_row,
143 		const zbx_vector_ptr_t *lld_macro_paths, char *error, int maxerrlen);
144 void	zbx_substitute_item_name_macros(DC_ITEM *dc_item, const char *name, char **replace_to);
145 int	substitute_macros_in_json_pairs(char **data, const struct zbx_json_parse *jp_row,
146 		const zbx_vector_ptr_t *lld_macro_paths, char *error, int maxerrlen);
147 int	xml_xpath_check(const char *xpath, char *error, size_t errlen);
148 
149 int	zbx_substitute_expression_lld_macros(char **data, zbx_uint64_t rules, const struct zbx_json_parse *jp_row,
150 		const zbx_vector_ptr_t *lld_macro_paths, char **error);
151 
152 #endif
153