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 "../../zabbix_server/vmware/vmware.h"
26 #include "preproc.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_vmware_stats_t	vmware_stats;
45 	zbx_wcache_info_t	wcache_info;
46 	zbx_process_info_t	process_stats[ZBX_PROCESS_TYPE_COUNT];
47 	int			proc_type;
48 
49 	DCget_count_stats_all(&count_stats);
50 
51 	/* zabbix[boottime] */
52 	zbx_json_adduint64(json, "boottime", CONFIG_SERVER_STARTUP_TIME);
53 
54 	/* zabbix[uptime] */
55 	zbx_json_adduint64(json, "uptime", time(NULL) - CONFIG_SERVER_STARTUP_TIME);
56 
57 	/* zabbix[hosts] */
58 	zbx_json_adduint64(json, "hosts", count_stats.hosts);
59 
60 	/* zabbix[items] */
61 	zbx_json_adduint64(json, "items", count_stats.items);
62 
63 	/* zabbix[item_unsupported] */
64 	zbx_json_adduint64(json, "item_unsupported", count_stats.items_unsupported);
65 
66 	/* zabbix[requiredperformance] */
67 	zbx_json_addfloat(json, "requiredperformance", count_stats.requiredperformance);
68 
69 	/* zabbix[preprocessing_queue] */
70 	zbx_json_adduint64(json, "preprocessing_queue", zbx_preprocessor_get_queue_size());
71 
72 	zbx_get_zabbix_stats_ext(json);
73 
74 	/* zabbix[rcache,<cache>,<mode>] */
75 	zbx_json_addobject(json, "rcache");
76 	zbx_json_adduint64(json, "total", *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_TOTAL));
77 	zbx_json_adduint64(json, "free", *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_FREE));
78 	zbx_json_addfloat(json, "pfree", *(double *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_PFREE));
79 	zbx_json_adduint64(json, "used", *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_USED));
80 	zbx_json_addfloat(json, "pused", *(double *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_PUSED));
81 	zbx_json_close(json);
82 
83 	/* zabbix[version] */
84 	zbx_json_addstring(json, "version", ZABBIX_VERSION, ZBX_JSON_TYPE_STRING);
85 
86 	/* zabbix[wcache,<cache>,<mode>] */
87 	DCget_stats_all(&wcache_info);
88 	zbx_json_addobject(json, "wcache");
89 
90 	zbx_json_addobject(json, "values");
91 	zbx_json_adduint64(json, "all", wcache_info.stats.history_counter);
92 	zbx_json_adduint64(json, "float", wcache_info.stats.history_float_counter);
93 	zbx_json_adduint64(json, "uint", wcache_info.stats.history_uint_counter);
94 	zbx_json_adduint64(json, "str", wcache_info.stats.history_str_counter);
95 	zbx_json_adduint64(json, "log", wcache_info.stats.history_log_counter);
96 	zbx_json_adduint64(json, "text", wcache_info.stats.history_text_counter);
97 	zbx_json_adduint64(json, "not supported", wcache_info.stats.notsupported_counter);
98 	zbx_json_close(json);
99 
100 	zbx_json_addobject(json, "history");
101 	zbx_json_addfloat(json, "pfree", 100 * (double)wcache_info.history_free / wcache_info.history_total);
102 	zbx_json_adduint64(json, "free", wcache_info.history_free);
103 	zbx_json_adduint64(json, "total", wcache_info.history_total);
104 	zbx_json_adduint64(json, "used", wcache_info.history_total - wcache_info.history_free);
105 	zbx_json_addfloat(json, "pused", 100 * (double)(wcache_info.history_total - wcache_info.history_free) /
106 			wcache_info.history_total);
107 	zbx_json_close(json);
108 
109 	zbx_json_addobject(json, "index");
110 	zbx_json_addfloat(json, "pfree", 100 * (double)wcache_info.index_free / wcache_info.index_total);
111 	zbx_json_adduint64(json, "free", wcache_info.index_free);
112 	zbx_json_adduint64(json, "total", wcache_info.index_total);
113 	zbx_json_adduint64(json, "used", wcache_info.index_total - wcache_info.index_free);
114 	zbx_json_addfloat(json, "pused", 100 * (double)(wcache_info.index_total - wcache_info.index_free) /
115 			wcache_info.index_total);
116 	zbx_json_close(json);
117 
118 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
119 	{
120 		zbx_json_addobject(json, "trend");
121 		zbx_json_addfloat(json, "pfree", 100 * (double)wcache_info.trend_free / wcache_info.trend_total);
122 		zbx_json_adduint64(json, "free", wcache_info.trend_free);
123 		zbx_json_adduint64(json, "total", wcache_info.trend_total);
124 		zbx_json_adduint64(json, "used", wcache_info.trend_total - wcache_info.trend_free);
125 		zbx_json_addfloat(json, "pused", 100 * (double)(wcache_info.trend_total - wcache_info.trend_free) /
126 				wcache_info.trend_total);
127 		zbx_json_close(json);
128 	}
129 
130 	zbx_json_close(json);
131 
132 	/* zabbix[vmware,buffer,<mode>] */
133 	if (SUCCEED == zbx_vmware_get_statistics(&vmware_stats))
134 	{
135 		zbx_json_addobject(json, "vmware");
136 		zbx_json_adduint64(json, "total", vmware_stats.memory_total);
137 		zbx_json_adduint64(json, "free", vmware_stats.memory_total - vmware_stats.memory_used);
138 		zbx_json_addfloat(json, "pfree", (double)(vmware_stats.memory_total - vmware_stats.memory_used) /
139 				vmware_stats.memory_total * 100);
140 		zbx_json_adduint64(json, "used", vmware_stats.memory_used);
141 		zbx_json_addfloat(json, "pused", (double)vmware_stats.memory_used / vmware_stats.memory_total * 100);
142 		zbx_json_close(json);
143 	}
144 
145 	/* zabbix[process,<type>,<mode>,<state>] */
146 	zbx_json_addobject(json, "process");
147 
148 	if (SUCCEED == zbx_get_all_process_stats(process_stats))
149 	{
150 		for (proc_type = 0; proc_type < ZBX_PROCESS_TYPE_COUNT; proc_type++)
151 		{
152 			if (0 == process_stats[proc_type].count)
153 				continue;
154 
155 			zbx_json_addobject(json, get_process_type_string(proc_type));
156 			zbx_json_addobject(json, "busy");
157 			zbx_json_addfloat(json, "avg", process_stats[proc_type].busy_avg);
158 			zbx_json_addfloat(json, "max", process_stats[proc_type].busy_max);
159 			zbx_json_addfloat(json, "min", process_stats[proc_type].busy_min);
160 			zbx_json_close(json);
161 			zbx_json_addobject(json, "idle");
162 			zbx_json_addfloat(json, "avg", process_stats[proc_type].idle_avg);
163 			zbx_json_addfloat(json, "max", process_stats[proc_type].idle_max);
164 			zbx_json_addfloat(json, "min", process_stats[proc_type].idle_min);
165 			zbx_json_close(json);
166 			zbx_json_adduint64(json, "count", process_stats[proc_type].count);
167 			zbx_json_close(json);
168 		}
169 	}
170 
171 	zbx_json_close(json);
172 }
173