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