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 "valuecache.h"
22 #include "preproc.h"
23 #include "zbxlld.h"
24 #include "checks_internal.h"
25 
26 /******************************************************************************
27  *                                                                            *
28  * Function: zbx_get_value_internal_ext                                       *
29  *                                                                            *
30  * Purpose: processes program type (server) specific internal checks          *
31  *                                                                            *
32  * Parameters: param1  - [IN] the first parameter                             *
33  *             request - [IN] the request                                     *
34  *             result  - [OUT] the result                                     *
35  *                                                                            *
36  * Return value: SUCCEED - data successfully retrieved and stored in result   *
37  *               NOTSUPPORTED - requested item is not supported               *
38  *               FAIL - not a server specific internal check                  *
39  *                                                                            *
40  * Comments: This function is used to process server specific internal checks *
41  *           before generic internal checks are processed.                    *
42  *                                                                            *
43  ******************************************************************************/
zbx_get_value_internal_ext(const char * param1,const AGENT_REQUEST * request,AGENT_RESULT * result)44 int	zbx_get_value_internal_ext(const char *param1, const AGENT_REQUEST *request, AGENT_RESULT *result)
45 {
46 	int	nparams, ret = NOTSUPPORTED;
47 	char	*param2, *param3;
48 
49 	nparams = get_rparams_num(request);
50 
51 	if (0 == strcmp(param1, "triggers"))			/* zabbix["triggers"] */
52 	{
53 		if (1 != nparams)
54 		{
55 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
56 			goto out;
57 		}
58 
59 		SET_UI64_RESULT(result, DCget_trigger_count());
60 	}
61 	else if (0 == strcmp(param1, "history") ||		/* zabbix["history"] */
62 			0 == strcmp(param1, "history_log") ||	/* zabbix["history_log"] */
63 			0 == strcmp(param1, "history_str") ||	/* zabbix["history_str"] */
64 			0 == strcmp(param1, "history_text") ||	/* zabbix["history_text"] */
65 			0 == strcmp(param1, "history_uint"))	/* zabbix["history_uint"] */
66 	{
67 		if (1 != nparams)
68 		{
69 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
70 			goto out;
71 		}
72 
73 		SET_UI64_RESULT(result, DBget_row_count(param1));
74 	}
75 	else if (0 == strcmp(param1, "trends") ||			/* zabbix["trends"] */
76 			0 == strcmp(param1, "trends_uint"))	/* zabbix["trends_uint"] */
77 	{
78 		if (1 != nparams)
79 		{
80 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
81 			goto out;
82 		}
83 
84 		SET_UI64_RESULT(result, DBget_row_count(param1));
85 	}
86 	else if (0 == strcmp(param1, "proxy"))			/* zabbix["proxy",<hostname>,"lastaccess" OR "delay"] */
87 	{
88 		int	value, res;
89 		char	*error = NULL;
90 
91 		/* this item is always processed by server */
92 
93 		if (3 != nparams)
94 		{
95 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
96 			goto out;
97 		}
98 
99 		param2 = get_rparam(request, 2);
100 
101 		if (0 == strcmp(param2, "lastaccess"))
102 		{
103 			res = DBget_proxy_lastaccess(get_rparam(request, 1), &value, &error);
104 		}
105 		else if (0 == strcmp(param2, "delay"))
106 		{
107 			int	lastaccess;
108 
109 			if (SUCCEED == (res = DCget_proxy_delay_by_name(get_rparam(request, 1), &value, &error)) &&
110 					SUCCEED == (res = DBget_proxy_lastaccess(get_rparam(request, 1), &lastaccess,
111 					&error)))
112 			{
113 				value += (int)time(NULL) - lastaccess;
114 			}
115 		}
116 		else
117 		{
118 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
119 			goto out;
120 		}
121 
122 		if (SUCCEED != res)
123 		{
124 			SET_MSG_RESULT(result, error);
125 			goto out;
126 		}
127 
128 		SET_UI64_RESULT(result, value);
129 	}
130 	else if (0 == strcmp(param1, "vcache"))
131 	{
132 		zbx_vc_stats_t	stats;
133 
134 		if (FAIL == zbx_vc_get_statistics(&stats))
135 		{
136 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Value cache is disabled."));
137 			goto out;
138 		}
139 
140 		if (2 > nparams || nparams > 3)
141 		{
142 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
143 			goto out;
144 		}
145 
146 		param2 = get_rparam(request, 1);
147 		if (NULL == (param3 = get_rparam(request, 2)))
148 			param3 = "";
149 
150 		if (0 == strcmp(param2, "buffer"))
151 		{
152 			if (0 == strcmp(param3, "free"))
153 				SET_UI64_RESULT(result, stats.free_size);
154 			else if (0 == strcmp(param3, "pfree"))
155 				SET_DBL_RESULT(result, (double)stats.free_size / stats.total_size * 100);
156 			else if (0 == strcmp(param3, "total"))
157 				SET_UI64_RESULT(result, stats.total_size);
158 			else if (0 == strcmp(param3, "used"))
159 				SET_UI64_RESULT(result, stats.total_size - stats.free_size);
160 			else if (0 == strcmp(param3, "pused"))
161 				SET_DBL_RESULT(result, (double)(stats.total_size - stats.free_size) /
162 						stats.total_size * 100);
163 			else
164 			{
165 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
166 				goto out;
167 			}
168 		}
169 		else if (0 == strcmp(param2, "cache"))
170 		{
171 			if (0 == strcmp(param3, "hits"))
172 				SET_UI64_RESULT(result, stats.hits);
173 			else if (0 == strcmp(param3, "requests"))
174 				SET_UI64_RESULT(result, stats.hits + stats.misses);
175 			else if (0 == strcmp(param3, "misses"))
176 				SET_UI64_RESULT(result, stats.misses);
177 			else if (0 == strcmp(param3, "mode"))
178 				SET_UI64_RESULT(result, stats.mode);
179 			else
180 			{
181 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
182 				goto out;
183 			}
184 		}
185 		else
186 		{
187 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
188 			goto out;
189 		}
190 	}
191 	else if (0 == strcmp(param1, "lld_queue"))
192 	{
193 		zbx_uint64_t	value;
194 		char		*error = NULL;
195 
196 		if (1 != nparams)
197 		{
198 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
199 			goto out;
200 		}
201 
202 		if (FAIL == zbx_lld_get_queue_size(&value, &error))
203 		{
204 			SET_MSG_RESULT(result, error);
205 			goto out;
206 		}
207 
208 		SET_UI64_RESULT(result, value);
209 	}
210 	else
211 	{
212 		ret = FAIL;
213 		goto out;
214 	}
215 
216 	ret = SUCCEED;
217 out:
218 	return ret;
219 }
220