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 "log.h"
23 
vfs_fs_inode(AGENT_REQUEST * request,AGENT_RESULT * result)24 static int	vfs_fs_inode(AGENT_REQUEST *request, AGENT_RESULT *result)
25 {
26 #ifdef HAVE_SYS_STATVFS_H
27 #	define ZBX_STATFS	statvfs
28 #	define ZBX_FFREE	f_favail
29 #else
30 #	define ZBX_STATFS	statfs
31 #	define ZBX_FFREE	f_ffree
32 #endif
33 	char			*fsname, *mode;
34 	zbx_uint64_t		total;
35 	struct ZBX_STATFS	s;
36 
37 	if (2 < request->nparam)
38 	{
39 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
40 		return SYSINFO_RET_FAIL;
41 	}
42 
43 	fsname = get_rparam(request, 0);
44 	mode = get_rparam(request, 1);
45 
46 	if (NULL == fsname || '\0' == *fsname)
47 	{
48 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
49 		return SYSINFO_RET_FAIL;
50 	}
51 
52 	if (0 != ZBX_STATFS(fsname, &s))
53 	{
54 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain filesystem information: %s",
55 				zbx_strerror(errno)));
56 		return SYSINFO_RET_FAIL;
57 	}
58 
59 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))	/* default parameter */
60 	{
61 		SET_UI64_RESULT(result, s.f_files);
62 	}
63 	else if (0 == strcmp(mode, "free"))
64 	{
65 		SET_UI64_RESULT(result, s.ZBX_FFREE);
66 	}
67 	else if (0 == strcmp(mode, "used"))
68 	{
69 		SET_UI64_RESULT(result, s.f_files - s.f_ffree);
70 	}
71 	else if (0 == strcmp(mode, "pfree"))
72 	{
73 		total = s.f_files;
74 #ifdef HAVE_SYS_STATVFS_H
75 		total -= s.f_ffree - s.f_favail;
76 #endif
77 		if (0 != total)
78 			SET_DBL_RESULT(result, (double)(100.0 * s.ZBX_FFREE) / total);
79 		else
80 		{
81 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero."));
82 			return SYSINFO_RET_FAIL;
83 		}
84 	}
85 	else if (0 == strcmp(mode, "pused"))
86 	{
87 		total = s.f_files;
88 #ifdef HAVE_SYS_STATVFS_H
89 		total -= s.f_ffree - s.f_favail;
90 #endif
91 		if (0 != total)
92 		{
93 			SET_DBL_RESULT(result, 100.0 - (double)(100.0 * s.ZBX_FFREE) / total);
94 		}
95 		else
96 		{
97 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero."));
98 			return SYSINFO_RET_FAIL;
99 		}
100 	}
101 	else
102 	{
103 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
104 		return SYSINFO_RET_FAIL;
105 	}
106 
107 	return SYSINFO_RET_OK;
108 }
109 
VFS_FS_INODE(AGENT_REQUEST * request,AGENT_RESULT * result)110 int	VFS_FS_INODE(AGENT_REQUEST *request, AGENT_RESULT *result)
111 {
112 	return zbx_execute_threaded_metric(vfs_fs_inode, request, result);
113 }
114