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 "zbxjson.h"
22 #include "dbcache.h"
23 #include "zbxself.h"
24 #include "valuecache.h"
25 #include "preproc.h"
26 #include "../../zabbix_server/vmware/vmware.h"
27 
28 #include "zabbix_stats.h"
29 
30 extern unsigned char	program_type;
31 
32 /******************************************************************************
33  *                                                                            *
34  * Function: zbx_send_zabbix_stats                                            *
35  *                                                                            *
36  * Purpose: collects all metrics required for Zabbix stats request            *
37  *                                                                            *
38  * Parameters: json - [OUT] the json data                                     *
39  *                                                                            *
40  ******************************************************************************/
zbx_get_zabbix_stats(struct zbx_json * json)41 void	zbx_get_zabbix_stats(struct zbx_json *json)
42 {
43 	zbx_config_cache_info_t	count_stats;
44 	zbx_vc_stats_t		vc_stats;
45 	zbx_vmware_stats_t	vmware_stats;
46 	zbx_wcache_info_t	wcache_info;
47 	zbx_process_info_t	process_stats[ZBX_PROCESS_TYPE_COUNT];
48 	int			proc_type;
49 
50 	DCget_count_stats_all(&count_stats);
51 
52 	/* zabbix[boottime] */
53 	zbx_json_adduint64(json, "boottime", CONFIG_SERVER_STARTUP_TIME);
54 
55 	/* zabbix[uptime] */
56 	zbx_json_adduint64(json, "uptime", time(NULL) - CONFIG_SERVER_STARTUP_TIME);
57 
58 	/* zabbix[hosts] */
59 	zbx_json_adduint64(json, "hosts", count_stats.hosts);
60 
61 	/* zabbix[items] */
62 	zbx_json_adduint64(json, "items", count_stats.items);
63 
64 	/* zabbix[item_unsupported] */
65 	zbx_json_adduint64(json, "item_unsupported", count_stats.items_unsupported);
66 
67 	/* zabbix[requiredperformance] */
68 	zbx_json_addfloat(json, "requiredperformance", count_stats.requiredperformance);
69 
70 	/* zabbix[preprocessing_queue] */
71 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
72 		zbx_json_adduint64(json, "preprocessing_queue", zbx_preprocessor_get_queue_size());
73 
74 	/* zabbix[triggers] */
75 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
76 		zbx_json_adduint64(json, "triggers", DCget_trigger_count());
77 
78 	/* zabbix[vcache,...] */
79 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER) && SUCCEED == zbx_vc_get_statistics(&vc_stats))
80 	{
81 		zbx_json_addobject(json, "vcache");
82 
83 		zbx_json_addobject(json, "buffer");
84 		zbx_json_adduint64(json, "total", vc_stats.total_size);
85 		zbx_json_adduint64(json, "free", vc_stats.free_size);
86 		zbx_json_addfloat(json, "pfree", (double)vc_stats.free_size / vc_stats.total_size * 100);
87 		zbx_json_adduint64(json, "used", vc_stats.total_size - vc_stats.free_size);
88 		zbx_json_addfloat(json, "pused", (double)(vc_stats.total_size - vc_stats.free_size) /
89 				vc_stats.total_size * 100);
90 		zbx_json_close(json);
91 
92 		zbx_json_addobject(json, "cache");
93 		zbx_json_adduint64(json, "requests", vc_stats.hits + vc_stats.misses);
94 		zbx_json_adduint64(json, "hits", vc_stats.hits);
95 		zbx_json_adduint64(json, "misses", vc_stats.misses);
96 		zbx_json_adduint64(json, "mode", vc_stats.mode);
97 		zbx_json_close(json);
98 
99 		zbx_json_close(json);
100 	}
101 
102 	/* zabbix[rcache,<cache>,<mode>] */
103 	zbx_json_addobject(json, "rcache");
104 	zbx_json_adduint64(json, "total", *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_TOTAL));
105 	zbx_json_adduint64(json, "free", *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_FREE));
106 	zbx_json_addfloat(json, "pfree", *(double *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_PFREE));
107 	zbx_json_adduint64(json, "used", *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_USED));
108 	zbx_json_addfloat(json, "pused", *(double *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_PUSED));
109 	zbx_json_close(json);
110 
111 	/* zabbix[wcache,<cache>,<mode>] */
112 	DCget_stats_all(&wcache_info);
113 	zbx_json_addobject(json, "wcache");
114 
115 	zbx_json_addobject(json, "values");
116 	zbx_json_adduint64(json, "all", wcache_info.stats.history_counter);
117 	zbx_json_adduint64(json, "float", wcache_info.stats.history_float_counter);
118 	zbx_json_adduint64(json, "uint", wcache_info.stats.history_uint_counter);
119 	zbx_json_adduint64(json, "str", wcache_info.stats.history_str_counter);
120 	zbx_json_adduint64(json, "log", wcache_info.stats.history_log_counter);
121 	zbx_json_adduint64(json, "text", wcache_info.stats.history_text_counter);
122 	zbx_json_adduint64(json, "not supported", wcache_info.stats.notsupported_counter);
123 	zbx_json_close(json);
124 
125 	zbx_json_addobject(json, "history");
126 	zbx_json_addfloat(json, "pfree", 100 * (double)wcache_info.history_free / wcache_info.history_total);
127 	zbx_json_adduint64(json, "free", wcache_info.history_free);
128 	zbx_json_adduint64(json, "total", wcache_info.history_total);
129 	zbx_json_adduint64(json, "used", wcache_info.history_total - wcache_info.history_free);
130 	zbx_json_addfloat(json, "pused", 100 * (double)(wcache_info.history_total - wcache_info.history_free) /
131 			wcache_info.history_total);
132 	zbx_json_close(json);
133 
134 	zbx_json_addobject(json, "index");
135 	zbx_json_addfloat(json, "pfree", 100 * (double)wcache_info.index_free / wcache_info.index_total);
136 	zbx_json_adduint64(json, "free", wcache_info.index_free);
137 	zbx_json_adduint64(json, "total", wcache_info.index_total);
138 	zbx_json_adduint64(json, "used", wcache_info.index_total - wcache_info.index_free);
139 	zbx_json_addfloat(json, "pused", 100 * (double)(wcache_info.index_total - wcache_info.index_free) /
140 			wcache_info.index_total);
141 	zbx_json_close(json);
142 
143 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
144 	{
145 		zbx_json_addobject(json, "trend");
146 		zbx_json_addfloat(json, "pfree", 100 * (double)wcache_info.trend_free / wcache_info.trend_total);
147 		zbx_json_adduint64(json, "free", wcache_info.trend_free);
148 		zbx_json_adduint64(json, "total", wcache_info.trend_total);
149 		zbx_json_adduint64(json, "used", wcache_info.trend_total - wcache_info.trend_free);
150 		zbx_json_addfloat(json, "pused", 100 * (double)(wcache_info.trend_total - wcache_info.trend_free) /
151 				wcache_info.trend_total);
152 		zbx_json_close(json);
153 	}
154 
155 	zbx_json_close(json);
156 
157 	/* zabbix[vmware,buffer,<mode>] */
158 	if (SUCCEED == zbx_vmware_get_statistics(&vmware_stats))
159 	{
160 		zbx_json_addobject(json, "vmware");
161 		zbx_json_adduint64(json, "total", vmware_stats.memory_total);
162 		zbx_json_adduint64(json, "free", vmware_stats.memory_total - vmware_stats.memory_used);
163 		zbx_json_addfloat(json, "pfree", (double)(vmware_stats.memory_total - vmware_stats.memory_used) /
164 				vmware_stats.memory_total * 100);
165 		zbx_json_adduint64(json, "used", vmware_stats.memory_used);
166 		zbx_json_addfloat(json, "pused", (double)vmware_stats.memory_used / vmware_stats.memory_total * 100);
167 		zbx_json_close(json);
168 	}
169 
170 	/* zabbix[process,<type>,<mode>,<state>] */
171 	zbx_json_addobject(json, "process");
172 
173 	if (SUCCEED == zbx_get_all_process_stats(process_stats))
174 	{
175 		for (proc_type = 0; proc_type < ZBX_PROCESS_TYPE_COUNT; proc_type++)
176 		{
177 			if (0 == process_stats[proc_type].count)
178 				continue;
179 
180 			zbx_json_addobject(json, get_process_type_string(proc_type));
181 			zbx_json_addobject(json, "busy");
182 			zbx_json_addfloat(json, "avg", process_stats[proc_type].busy_avg);
183 			zbx_json_addfloat(json, "max", process_stats[proc_type].busy_max);
184 			zbx_json_addfloat(json, "min", process_stats[proc_type].busy_min);
185 			zbx_json_close(json);
186 			zbx_json_addobject(json, "idle");
187 			zbx_json_addfloat(json, "avg", process_stats[proc_type].idle_avg);
188 			zbx_json_addfloat(json, "max", process_stats[proc_type].idle_max);
189 			zbx_json_addfloat(json, "min", process_stats[proc_type].idle_min);
190 			zbx_json_close(json);
191 			zbx_json_adduint64(json, "count", process_stats[proc_type].count);
192 			zbx_json_close(json);
193 		}
194 	}
195 
196 	zbx_json_close(json);
197 }
198