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