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 "sysinfo.h"
22 #include "zbxjson.h"
23 #include "log.h"
24 #include "zbxalgo.h"
25 #include "inodes.h"
26 
get_fs_size_stat(const char * fs,zbx_uint64_t * total,zbx_uint64_t * free,zbx_uint64_t * used,double * pfree,double * pused,char ** error)27 static int	get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free,
28 		zbx_uint64_t *used, double *pfree, double *pused, char **error)
29 {
30 #ifdef HAVE_SYS_STATVFS_H
31 #	define ZBX_STATFS	statvfs
32 #	define ZBX_BSIZE	f_frsize
33 #else
34 #	define ZBX_STATFS	statfs
35 #	define ZBX_BSIZE	f_bsize
36 #endif
37 	struct ZBX_STATFS	s;
38 
39 	if (0 != ZBX_STATFS(fs, &s))
40 	{
41 		*error = zbx_dsprintf(NULL, "Cannot obtain filesystem information: %s", zbx_strerror(errno));
42 		zabbix_log(LOG_LEVEL_DEBUG,"%s failed with error: %s",__func__, *error);
43 		return SYSINFO_RET_FAIL;
44 	}
45 
46 	/* Available space could be negative (top bit set) if we hit disk space */
47 	/* reserved for non-privileged users. Treat it as 0.                    */
48 	if (0 != ZBX_IS_TOP_BIT_SET(s.f_bavail))
49 		s.f_bavail = 0;
50 
51 	if (NULL != total)
52 		*total = (zbx_uint64_t)s.f_blocks * s.ZBX_BSIZE;
53 
54 	if (NULL != free)
55 		*free = (zbx_uint64_t)s.f_bavail * s.ZBX_BSIZE;
56 
57 	if (NULL != used)
58 		*used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.ZBX_BSIZE;
59 
60 	if (NULL != pfree)
61 	{
62 		if (0 != s.f_blocks - s.f_bfree + s.f_bavail)
63 			*pfree = (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail);
64 		else
65 			*pfree = 0;
66 	}
67 
68 	if (NULL != pused)
69 	{
70 		if (0 != s.f_blocks - s.f_bfree + s.f_bavail)
71 			*pused = 100.0 - (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail);
72 		else
73 			*pused = 0;
74 	}
75 
76 	return SYSINFO_RET_OK;
77 }
78 
VFS_FS_USED(const char * fs,AGENT_RESULT * result)79 static int	VFS_FS_USED(const char *fs, AGENT_RESULT *result)
80 {
81 	zbx_uint64_t	value;
82 	char		*error;
83 
84 	if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, &value, NULL, NULL, &error))
85 	{
86 		SET_MSG_RESULT(result, error);
87 		return SYSINFO_RET_FAIL;
88 	}
89 
90 	SET_UI64_RESULT(result, value);
91 
92 	return SYSINFO_RET_OK;
93 }
94 
VFS_FS_FREE(const char * fs,AGENT_RESULT * result)95 static int	VFS_FS_FREE(const char *fs, AGENT_RESULT *result)
96 {
97 	zbx_uint64_t	value;
98 	char		*error;
99 
100 	if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, &value, NULL, NULL, NULL, &error))
101 	{
102 		SET_MSG_RESULT(result, error);
103 		return SYSINFO_RET_FAIL;
104 	}
105 
106 	SET_UI64_RESULT(result, value);
107 
108 	return SYSINFO_RET_OK;
109 }
110 
VFS_FS_TOTAL(const char * fs,AGENT_RESULT * result)111 static int	VFS_FS_TOTAL(const char *fs, AGENT_RESULT *result)
112 {
113 	zbx_uint64_t	value;
114 	char		*error;
115 
116 	if (SYSINFO_RET_OK != get_fs_size_stat(fs, &value, NULL, NULL, NULL, NULL, &error))
117 	{
118 		SET_MSG_RESULT(result, error);
119 		return SYSINFO_RET_FAIL;
120 	}
121 
122 	SET_UI64_RESULT(result, value);
123 
124 	return SYSINFO_RET_OK;
125 }
126 
VFS_FS_PFREE(const char * fs,AGENT_RESULT * result)127 static int	VFS_FS_PFREE(const char *fs, AGENT_RESULT *result)
128 {
129 	double	value;
130 	char	*error;
131 
132 	if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, NULL, &value, NULL, &error))
133 	{
134 		SET_MSG_RESULT(result, error);
135 		return SYSINFO_RET_FAIL;
136 	}
137 
138 	SET_DBL_RESULT(result, value);
139 
140 	return SYSINFO_RET_OK;
141 }
142 
VFS_FS_PUSED(const char * fs,AGENT_RESULT * result)143 static int	VFS_FS_PUSED(const char *fs, AGENT_RESULT *result)
144 {
145 	double	value;
146 	char	*error;
147 
148 	if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, NULL, NULL, &value, &error))
149 	{
150 		SET_MSG_RESULT(result, error);
151 		return SYSINFO_RET_FAIL;
152 	}
153 
154 	SET_DBL_RESULT(result, value);
155 
156 	return SYSINFO_RET_OK;
157 }
158 
vfs_fs_size(AGENT_REQUEST * request,AGENT_RESULT * result)159 static int	vfs_fs_size(AGENT_REQUEST *request, AGENT_RESULT *result)
160 {
161 	char	*fsname, *mode;
162 
163 	if (2 < request->nparam)
164 	{
165 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
166 		return SYSINFO_RET_FAIL;
167 	}
168 
169 	fsname = get_rparam(request, 0);
170 	mode = get_rparam(request, 1);
171 
172 	if (NULL == fsname || '\0' == *fsname)
173 	{
174 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
175 		return SYSINFO_RET_FAIL;
176 	}
177 
178 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))	/* default parameter */
179 		return VFS_FS_TOTAL(fsname, result);
180 	if (0 == strcmp(mode, "free"))
181 		return VFS_FS_FREE(fsname, result);
182 	if (0 == strcmp(mode, "pfree"))
183 		return VFS_FS_PFREE(fsname, result);
184 	if (0 == strcmp(mode, "used"))
185 		return VFS_FS_USED(fsname, result);
186 	if (0 == strcmp(mode, "pused"))
187 		return VFS_FS_PUSED(fsname, result);
188 
189 	SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
190 
191 	return SYSINFO_RET_FAIL;
192 }
193 
VFS_FS_SIZE(AGENT_REQUEST * request,AGENT_RESULT * result)194 int	VFS_FS_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
195 {
196 	return zbx_execute_threaded_metric(vfs_fs_size, request, result);
197 }
198 
VFS_FS_DISCOVERY(AGENT_REQUEST * request,AGENT_RESULT * result)199 int	VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
200 {
201 	int		i, rc;
202 	struct statfs	*mntbuf;
203 	struct zbx_json	j;
204 
205 	if (0 == (rc = getmntinfo(&mntbuf, MNT_WAIT)))
206 	{
207 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
208 		return SYSINFO_RET_FAIL;
209 	}
210 
211 	zbx_json_initarray(&j, ZBX_JSON_STAT_BUF_LEN);
212 
213 	for (i = 0; i < rc; i++)
214 	{
215 		zbx_json_addobject(&j, NULL);
216 		zbx_json_addstring(&j, ZBX_LLD_MACRO_FSNAME, mntbuf[i].f_mntonname, ZBX_JSON_TYPE_STRING);
217 		zbx_json_addstring(&j, ZBX_LLD_MACRO_FSTYPE, mntbuf[i].f_fstypename, ZBX_JSON_TYPE_STRING);
218 		zbx_json_close(&j);
219 	}
220 
221 	zbx_json_close(&j);
222 
223 	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));
224 
225 	zbx_json_free(&j);
226 
227 	return SYSINFO_RET_OK;
228 }
229 
vfs_fs_get(AGENT_REQUEST * request,AGENT_RESULT * result)230 static int	vfs_fs_get(AGENT_REQUEST *request, AGENT_RESULT *result)
231 {
232 	int			i, rc;
233 	struct statfs		*mntbuf;
234 	struct zbx_json		j;
235 	zbx_uint64_t		total, not_used, used;
236 	zbx_uint64_t		itotal, inot_used, iused;
237 	double			pfree, pused;
238 	double			ipfree, ipused;
239 	char 			*error;
240 	zbx_vector_ptr_t	mntpoints;
241 	zbx_mpoint_t		*mntpoint;
242 	int			ret = SYSINFO_RET_FAIL;
243 	char 			*mpoint;
244 
245 	if (0 == (rc = getmntinfo(&mntbuf, MNT_WAIT)))
246 	{
247 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
248 		return SYSINFO_RET_FAIL;
249 	}
250 
251 	zbx_vector_ptr_create(&mntpoints);
252 
253 	for (i = 0; i < rc; i++)
254 	{
255 		mpoint = mntbuf[i].f_mntonname;
256 		if (SYSINFO_RET_OK != get_fs_size_stat(mpoint, &total, &not_used, &used, &pfree, &pused,&error))
257 		{
258 			zbx_free(error);
259 			continue;
260 		}
261 		if (SYSINFO_RET_OK != get_fs_inode_stat(mpoint, &itotal, &inot_used, &iused, &ipfree, &ipused, "pused",
262 				&error))
263 		{
264 			zbx_free(error);
265 			continue;
266 		}
267 
268 		mntpoint = (zbx_mpoint_t *)zbx_malloc(NULL, sizeof(zbx_mpoint_t));
269 		zbx_strlcpy(mntpoint->fsname, mpoint, MAX_STRING_LEN);
270 		zbx_strlcpy(mntpoint->fstype, mntbuf[i].f_fstypename, MAX_STRING_LEN);
271 		mntpoint->bytes.total = total;
272 		mntpoint->bytes.used = used;
273 		mntpoint->bytes.not_used = not_used;
274 		mntpoint->bytes.pfree = pfree;
275 		mntpoint->bytes.pused = pused;
276 		mntpoint->inodes.total = itotal;
277 		mntpoint->inodes.used = iused;
278 		mntpoint->inodes.not_used = inot_used;
279 		mntpoint->inodes.pfree = ipfree;
280 		mntpoint->inodes.pused = ipused;
281 
282 		zbx_vector_ptr_append(&mntpoints, mntpoint);
283 	}
284 
285 	if (0 == (rc = getmntinfo(&mntbuf, MNT_WAIT)))
286 	{
287 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
288 		goto out;
289 	}
290 	zbx_json_initarray(&j, ZBX_JSON_STAT_BUF_LEN);
291 
292 	for (i = 0; i < rc; i++)
293 	{
294 		int idx;
295 		mpoint = mntbuf[i].f_mntonname;
296 
297 		if (FAIL != (idx = zbx_vector_ptr_search(&mntpoints, mpoint, ZBX_DEFAULT_STR_COMPARE_FUNC)))
298 		{
299 			mntpoint = (zbx_mpoint_t *)mntpoints.values[idx];
300 			zbx_json_addobject(&j, NULL);
301 			zbx_json_addstring(&j, ZBX_SYSINFO_TAG_FSNAME, mntpoint->fsname, ZBX_JSON_TYPE_STRING);
302 			zbx_json_addstring(&j, ZBX_SYSINFO_TAG_FSTYPE, mntpoint->fstype, ZBX_JSON_TYPE_STRING);
303 			zbx_json_addobject(&j, ZBX_SYSINFO_TAG_BYTES);
304 			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_TOTAL, mntpoint->bytes.total);
305 			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_FREE, mntpoint->bytes.not_used);
306 			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_USED, mntpoint->bytes.used);
307 			zbx_json_addfloat(&j, ZBX_SYSINFO_TAG_PFREE, mntpoint->bytes.pfree);
308 			zbx_json_addfloat(&j, ZBX_SYSINFO_TAG_PUSED, mntpoint->bytes.pused);
309 			zbx_json_close(&j);
310 			zbx_json_addobject(&j, ZBX_SYSINFO_TAG_INODES);
311 			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_TOTAL, mntpoint->inodes.total);
312 			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_FREE, mntpoint->inodes.not_used);
313 			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_USED, mntpoint->inodes.used);
314 			zbx_json_addfloat(&j, ZBX_SYSINFO_TAG_PFREE, mntpoint->inodes.pfree);
315 			zbx_json_addfloat(&j, ZBX_SYSINFO_TAG_PUSED, mntpoint->inodes.pused);
316 			zbx_json_close(&j);
317 			zbx_json_close(&j);
318 		}
319 	}
320 
321 	zbx_json_close(&j);
322 
323 	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));
324 
325 	zbx_json_free(&j);
326 	ret = SYSINFO_RET_OK;
327 out:
328 	zbx_vector_ptr_clear_ext(&mntpoints, (zbx_clean_func_t)zbx_mpoints_free);
329 	zbx_vector_ptr_destroy(&mntpoints);
330 
331 	return ret;
332 }
333 
VFS_FS_GET(AGENT_REQUEST * request,AGENT_RESULT * result)334 int	VFS_FS_GET(AGENT_REQUEST *request, AGENT_RESULT *result)
335 {
336 	return zbx_execute_threaded_metric(vfs_fs_get, request, result);
337 
338 }
339