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 
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)25 static int	get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free,
26 		zbx_uint64_t *used, double *pfree, double *pused, char **error)
27 {
28 #ifdef HAVE_SYS_STATVFS_H
29 #	define ZBX_STATFS	statvfs
30 #	define ZBX_BSIZE	f_frsize
31 #else
32 #	define ZBX_STATFS	statfs
33 #	define ZBX_BSIZE	f_bsize
34 #endif
35 	struct ZBX_STATFS	s;
36 
37 	if (NULL == fs || '\0' == *fs)
38 	{
39 		*error = zbx_strdup(NULL, "Filesystem name cannot be empty.");
40 		return SYSINFO_RET_FAIL;
41 	}
42 
43 	if (0 != ZBX_STATFS(fs, &s))
44 	{
45 		*error = zbx_dsprintf(NULL, "Cannot obtain filesystem information: %s", zbx_strerror(errno));
46 		return SYSINFO_RET_FAIL;
47 	}
48 
49 	/* Available space could be negative (top bit set) if we hit disk space */
50 	/* reserved for non-privileged users. Treat it as 0.                    */
51 	if (0 != ZBX_IS_TOP_BIT_SET(s.f_bavail))
52 		s.f_bavail = 0;
53 
54 	if (NULL != total)
55 		*total = (zbx_uint64_t)s.f_blocks * s.ZBX_BSIZE;
56 
57 	if (NULL != free)
58 		*free = (zbx_uint64_t)s.f_bavail * s.ZBX_BSIZE;
59 
60 	if (NULL != used)
61 		*used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.ZBX_BSIZE;
62 
63 	if (NULL != pfree)
64 	{
65 		if (0 != s.f_blocks - s.f_bfree + s.f_bavail)
66 			*pfree = (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail);
67 		else
68 			*pfree = 0;
69 	}
70 
71 	if (NULL != pused)
72 	{
73 		if (0 != s.f_blocks - s.f_bfree + s.f_bavail)
74 			*pused = 100.0 - (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail);
75 		else
76 			*pused = 0;
77 	}
78 
79 	return SYSINFO_RET_OK;
80 }
81 
vfs_fs_size(AGENT_REQUEST * request,AGENT_RESULT * result)82 static int	vfs_fs_size(AGENT_REQUEST *request, AGENT_RESULT *result)
83 {
84 	char		*fsname, *mode, *error;
85 	zbx_uint64_t	total, free, used;
86 	double		pfree, pused;
87 
88 	if (2 < request->nparam)
89 	{
90 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
91 		return SYSINFO_RET_FAIL;
92 	}
93 
94 	fsname = get_rparam(request, 0);
95 	mode = get_rparam(request, 1);
96 
97 	if (SYSINFO_RET_OK != get_fs_size_stat(fsname, &total, &free, &used, &pfree, &pused, &error))
98 	{
99 		SET_MSG_RESULT(result, error);
100 		return SYSINFO_RET_FAIL;
101 	}
102 
103 	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))	/* default parameter */
104 		SET_UI64_RESULT(result, total);
105 	else if (0 == strcmp(mode, "free"))
106 		SET_UI64_RESULT(result, free);
107 	else if (0 == strcmp(mode, "used"))
108 		SET_UI64_RESULT(result, used);
109 	else if (0 == strcmp(mode, "pfree"))
110 		SET_DBL_RESULT(result, pfree);
111 	else if (0 == strcmp(mode, "pused"))
112 		SET_DBL_RESULT(result, pused);
113 	else
114 	{
115 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
116 		return SYSINFO_RET_FAIL;
117 	}
118 
119 	return SYSINFO_RET_OK;
120 }
121 
VFS_FS_SIZE(AGENT_REQUEST * request,AGENT_RESULT * result)122 int	VFS_FS_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
123 {
124 	return zbx_execute_threaded_metric(vfs_fs_size, request, result);
125 }
126 
VFS_FS_DISCOVERY(AGENT_REQUEST * request,AGENT_RESULT * result)127 int	VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
128 {
129 	char		line[MAX_STRING_LEN], *p, *mpoint, *mtype;
130 	FILE		*f;
131 	struct zbx_json	j;
132 
133 	if (NULL == (f = fopen("/proc/mounts", "r")))
134 	{
135 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/mounts: %s", zbx_strerror(errno)));
136 		return SYSINFO_RET_FAIL;
137 	}
138 
139 	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);
140 
141 	zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);
142 
143 	while (NULL != fgets(line, sizeof(line), f))
144 	{
145 		if (NULL == (p = strchr(line, ' ')))
146 			continue;
147 
148 		mpoint = ++p;
149 
150 		if (NULL == (p = strchr(mpoint, ' ')))
151 			continue;
152 
153 		*p = '\0';
154 
155 		mtype = ++p;
156 
157 		if (NULL == (p = strchr(mtype, ' ')))
158 			continue;
159 
160 		*p = '\0';
161 
162 		zbx_json_addobject(&j, NULL);
163 		zbx_json_addstring(&j, "{#FSNAME}", mpoint, ZBX_JSON_TYPE_STRING);
164 		zbx_json_addstring(&j, "{#FSTYPE}", mtype, ZBX_JSON_TYPE_STRING);
165 		zbx_json_close(&j);
166 	}
167 
168 	zbx_fclose(f);
169 
170 	zbx_json_close(&j);
171 
172 	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));
173 
174 	zbx_json_free(&j);
175 
176 	return SYSINFO_RET_OK;
177 }
178