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 
24 #define ZBX_DEV_PFX	"/dev/"
25 
26 typedef struct
27 {
28 	zbx_uint64_t	nread;
29 	zbx_uint64_t	nwritten;
30 	zbx_uint64_t	reads;
31 	zbx_uint64_t	writes;
32 }
33 zbx_perfstat_t;
34 
get_diskstat(const char * devname,zbx_uint64_t * dstat)35 int	get_diskstat(const char *devname, zbx_uint64_t *dstat)
36 {
37 	return FAIL;
38 }
39 
get_perfstat_io(const char * devname,zbx_perfstat_t * zp,char ** error)40 static int	get_perfstat_io(const char *devname, zbx_perfstat_t *zp, char **error)
41 {
42 #if defined(HAVE_LIBPERFSTAT)
43 	int	err;
44 
45 	if ('\0' != *devname)
46 	{
47 		perfstat_id_t	name;
48 		perfstat_disk_t	data;
49 
50 		strscpy(name.name, devname);
51 
52 		if (0 < (err = perfstat_disk(&name, &data, sizeof(data), 1)))
53 		{
54 			zp->nread = data.rblks * data.bsize;
55 			zp->nwritten = data.wblks * data.bsize;
56 			zp->reads = data.xrate;
57 			zp->writes = data.xfers - data.xrate;
58 
59 			return SYSINFO_RET_OK;
60 		}
61 	}
62 	else
63 	{
64 		perfstat_disk_total_t	data;
65 
66 		if (0 < (err = perfstat_disk_total(NULL, &data, sizeof(data), 1)))
67 		{
68 			zp->nread = data.rblks * 512;
69 			zp->nwritten = data.wblks * 512;
70 			zp->reads = data.xrate;
71 			zp->writes = data.xfers - data.xrate;
72 
73 			return SYSINFO_RET_OK;
74 		}
75 	}
76 
77 	if (0 == err)
78 		*error = zbx_strdup(NULL, "Cannot obtain system information.");
79 	else
80 		*error = zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno));
81 
82 	return SYSINFO_RET_FAIL;
83 #else
84 	*error = zbx_strdup(NULL, "Agent was compiled without support for Perfstat API.");
85 	return SYSINFO_RET_FAIL;
86 #endif
87 }
88 
VFS_DEV_READ_BYTES(const char * devname,AGENT_RESULT * result)89 static int	VFS_DEV_READ_BYTES(const char *devname, AGENT_RESULT *result)
90 {
91 	zbx_perfstat_t	zp;
92 	char		*error;
93 
94 	if (SYSINFO_RET_OK != get_perfstat_io(devname, &zp, &error))
95 	{
96 		SET_MSG_RESULT(result, error);
97 		return SYSINFO_RET_FAIL;
98 	}
99 
100 	SET_UI64_RESULT(result, zp.nread);
101 
102 	return SYSINFO_RET_OK;
103 }
104 
VFS_DEV_READ_OPERATIONS(const char * devname,AGENT_RESULT * result)105 static int	VFS_DEV_READ_OPERATIONS(const char *devname, AGENT_RESULT *result)
106 {
107 	zbx_perfstat_t	zp;
108 	char		*error;
109 
110 	if (SYSINFO_RET_OK != get_perfstat_io(devname, &zp, &error))
111 	{
112 		SET_MSG_RESULT(result, error);
113 		return SYSINFO_RET_FAIL;
114 	}
115 
116 	SET_UI64_RESULT(result, zp.reads);
117 
118 	return SYSINFO_RET_OK;
119 }
120 
VFS_DEV_WRITE_BYTES(const char * devname,AGENT_RESULT * result)121 static int	VFS_DEV_WRITE_BYTES(const char *devname, AGENT_RESULT *result)
122 {
123 	zbx_perfstat_t	zp;
124 	char		*error;
125 
126 	if (SYSINFO_RET_OK != get_perfstat_io(devname, &zp, &error))
127 	{
128 		SET_MSG_RESULT(result, error);
129 		return SYSINFO_RET_FAIL;
130 	}
131 
132 	SET_UI64_RESULT(result, zp.nwritten);
133 
134 	return SYSINFO_RET_OK;
135 }
136 
VFS_DEV_WRITE_OPERATIONS(const char * devname,AGENT_RESULT * result)137 static int	VFS_DEV_WRITE_OPERATIONS(const char *devname, AGENT_RESULT *result)
138 {
139 	zbx_perfstat_t	zp;
140 	char		*error;
141 
142 	if (SYSINFO_RET_OK != get_perfstat_io(devname, &zp, &error))
143 	{
144 		SET_MSG_RESULT(result, error);
145 		return SYSINFO_RET_FAIL;
146 	}
147 
148 	SET_UI64_RESULT(result, zp.writes);
149 
150 	return SYSINFO_RET_OK;
151 }
152 
VFS_DEV_READ(AGENT_REQUEST * request,AGENT_RESULT * result)153 int	VFS_DEV_READ(AGENT_REQUEST *request, AGENT_RESULT *result)
154 {
155 	const char	*devname, *type;
156 	int		ret;
157 
158 	if (2 < request->nparam)
159 	{
160 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
161 		return SYSINFO_RET_FAIL;
162 	}
163 
164 	devname = get_rparam(request, 0);
165 
166 	if (NULL == devname || 0 == strcmp("all", devname))
167 		devname = "";
168 	else if (0 == strncmp(ZBX_DEV_PFX, devname, ZBX_CONST_STRLEN(ZBX_DEV_PFX)))
169 		devname += ZBX_CONST_STRLEN(ZBX_DEV_PFX);
170 
171 	type = get_rparam(request, 1);
172 
173 	if (NULL == type || '\0' == *type || 0 == strcmp(type, "operations"))
174 		ret = VFS_DEV_READ_OPERATIONS(devname, result);
175 	else if (0 == strcmp(type, "bytes"))
176 		ret = VFS_DEV_READ_BYTES(devname, result);
177 	else
178 	{
179 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
180 		return SYSINFO_RET_FAIL;
181 	}
182 
183 	return ret;
184 }
185 
VFS_DEV_WRITE(AGENT_REQUEST * request,AGENT_RESULT * result)186 int	VFS_DEV_WRITE(AGENT_REQUEST *request, AGENT_RESULT *result)
187 {
188 	const char	*devname, *type;
189 	int		ret;
190 
191 	if (2 < request->nparam)
192 	{
193 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
194 		return SYSINFO_RET_FAIL;
195 	}
196 
197 	devname = get_rparam(request, 0);
198 
199 	if (NULL == devname || 0 == strcmp("all", devname))
200 		devname = "";
201 	else if (0 == strncmp(ZBX_DEV_PFX, devname, ZBX_CONST_STRLEN(ZBX_DEV_PFX)))
202 		devname += ZBX_CONST_STRLEN(ZBX_DEV_PFX);
203 
204 	type = get_rparam(request, 1);
205 
206 	if (NULL == type || '\0' == *type || 0 == strcmp(type, "operations"))
207 		ret = VFS_DEV_WRITE_OPERATIONS(devname, result);
208 	else if (0 == strcmp(type, "bytes"))
209 		ret = VFS_DEV_WRITE_BYTES(devname, result);
210 	else
211 	{
212 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
213 		return SYSINFO_RET_FAIL;
214 	}
215 
216 	return ret;
217 }
218