Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diskspace.c « win32 « zbxsysinfo « libs « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80b475a96f7f1006549c1149a3aff267b5e4747d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "zbxsysinfo.h"
#include "../sysinfo.h"

#include "zbxstr.h"
#include "zbxjson.h"
#include "zbxalgo.h"
#include "log.h"

typedef struct
{
	char		*fsname;
	char		*fstype;
	char		*fslabel;
	char		*fsdrivetype;
	zbx_uint64_t	total;
	zbx_uint64_t	not_used;
	zbx_uint64_t	used;
	double		pfree;
	double		pused;
}
zbx_wmpoint_t;


static wchar_t	*zbx_wcsdup2(const char *filename, int line, wchar_t *old, const wchar_t *str)
{
	int	retry;
	wchar_t	*ptr = NULL;

	zbx_free(old);

	for (retry = 10; 0 < retry && NULL == ptr; ptr = wcsdup(str), retry--)
		;

	if (NULL != ptr)
		return ptr;

	zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_wcsdup: out of memory. Requested " ZBX_FS_SIZE_T " bytes.",
			filename, line, (zbx_fs_size_t)((wcslen(str) + 1) * sizeof(wchar_t)));

	exit(EXIT_FAILURE);
}

static int	wmpoint_compare_func(const void *d1, const void *d2)
{
	const zbx_wmpoint_t	*m1 = *(const zbx_wmpoint_t **)d1;
	const zbx_wmpoint_t	*m2 = *(const zbx_wmpoint_t **)d2;

	return strcmp(m1->fsname, m2->fsname);
}

static int	get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *not_used,
		zbx_uint64_t *used, double *pfree, double *pused, char **error)
{
	wchar_t		*wpath;
	ULARGE_INTEGER	freeBytes, totalBytes;

	wpath = zbx_utf8_to_unicode(fs);
	if (0 == GetDiskFreeSpaceEx(wpath, &freeBytes, &totalBytes, NULL))
	{
		zbx_free(wpath);
		*error = zbx_dsprintf(NULL, "Cannot obtain filesystem information: %s",
				strerror_from_system(GetLastError()));
		zabbix_log(LOG_LEVEL_DEBUG,"%s failed with error: %s",__func__, *error);
		return SYSINFO_RET_FAIL;
	}
	zbx_free(wpath);

	*total = totalBytes.QuadPart;
	*not_used = freeBytes.QuadPart;
	*used = totalBytes.QuadPart - freeBytes.QuadPart;
	*pfree = (double)(__int64)freeBytes.QuadPart * 100. / (double)(__int64)totalBytes.QuadPart;
	*pused = (double)((__int64)totalBytes.QuadPart - (__int64)freeBytes.QuadPart) * 100. /
			(double)(__int64)totalBytes.QuadPart;

	return SYSINFO_RET_OK;

}

static int	vfs_fs_size(AGENT_REQUEST *request, AGENT_RESULT *result, HANDLE timeout_event)
{
	char		*path, *mode;
	char		*error;
	zbx_uint64_t	total, used, free;
	double		pused,pfree;

	/* 'timeout_event' argument is here to make the vfs_fs_size() prototype as required by */
	/* zbx_execute_threaded_metric() on MS Windows */
	ZBX_UNUSED(timeout_event);

	if (2 < request->nparam)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
		return SYSINFO_RET_FAIL;
	}

	path = get_rparam(request, 0);
	mode = get_rparam(request, 1);

	if (NULL == path || '\0' == *path)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
		return SYSINFO_RET_FAIL;
	}

	if (SYSINFO_RET_OK != get_fs_size_stat(path, &total, &free, &used, &pfree, &pused, &error))
	{
		SET_MSG_RESULT(result, error);
		return SYSINFO_RET_FAIL;
	}

	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
		SET_UI64_RESULT(result, total);
	else if (0 == strcmp(mode, "free"))
		SET_UI64_RESULT(result, free);
	else if (0 == strcmp(mode, "used"))
		SET_UI64_RESULT(result, used);
	else if (0 == strcmp(mode, "pfree"))
		SET_DBL_RESULT(result, pfree);
	else if (0 == strcmp(mode, "pused"))
		SET_DBL_RESULT(result, pused);
	else
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
		return SYSINFO_RET_FAIL;
	}

	return SYSINFO_RET_OK;
}

int	VFS_FS_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	return zbx_execute_threaded_metric(vfs_fs_size, request, result);
}

static const char	*get_drive_type_string(UINT type)
{
	switch (type)
	{
		case DRIVE_UNKNOWN:
			return "unknown";
		case DRIVE_NO_ROOT_DIR:
			return "norootdir";
		case DRIVE_REMOVABLE:
			return "removable";
		case DRIVE_FIXED:
			return "fixed";
		case DRIVE_REMOTE:
			return "remote";
		case DRIVE_CDROM:
			return "cdrom";
		case DRIVE_RAMDISK:
			return "ramdisk";
		default:
			THIS_SHOULD_NEVER_HAPPEN;
			return "unknown";
	}
}

static void	get_fs_data(const wchar_t* path, char **fsname, char **fstype, char **fslabel, char **fsdrivetype)
{
	wchar_t	fs_name[MAX_PATH + 1], vol_name[MAX_PATH + 1], *long_path = NULL;
	size_t	sz;

	*fsname = zbx_unicode_to_utf8(path);
	if (0 < (sz = strlen(*fsname)) && '\\' == (*fsname)[--sz])
		(*fsname)[sz] = '\0';

	/* add \\?\ prefix if path exceeds MAX_PATH */
	if (MAX_PATH < (sz = wcslen(path) + 1) && 0 != wcsncmp(path, L"\\\\?\\", 4))
	{
		/* allocate memory buffer enough to hold null-terminated path and prefix */
		long_path = (wchar_t*)zbx_malloc(long_path, (sz + 4) * sizeof(wchar_t));

		long_path[0] = L'\\';
		long_path[1] = L'\\';
		long_path[2] = L'?';
		long_path[3] = L'\\';

		memcpy(long_path + 4, path, sz * sizeof(wchar_t));
		path = long_path;
	}

	if (FALSE != GetVolumeInformation(path, vol_name, ARRSIZE(vol_name), NULL, NULL, NULL, fs_name,
			ARRSIZE(fs_name)))
	{
		*fstype = zbx_unicode_to_utf8(fs_name);
		*fslabel = zbx_unicode_to_utf8(vol_name);
	}
	else
	{
		*fstype = zbx_strdup(NULL, "UNKNOWN");
		*fslabel = zbx_strdup(NULL, "");
	}

	*fsdrivetype = zbx_strdup(NULL, get_drive_type_string(GetDriveType(path)));

	zbx_free(long_path);
}

static int	add_fs_to_vector(zbx_vector_ptr_t *mntpoints, wchar_t *path, char **error)
{
	zbx_wmpoint_t	*mntpoint;
	zbx_uint64_t	total, not_used, used;
	double		pfree, pused;
	char 		*fsname = NULL, *fstype = NULL, *fslabel = NULL, *fsdrivetype = NULL;

	get_fs_data(path, &fsname, &fstype, &fslabel, &fsdrivetype);

	if (SYSINFO_RET_OK != get_fs_size_stat(fsname, &total, &not_used, &used, &pfree, &pused, error))
	{
		zbx_free(fsname);
		zbx_free(fstype);
		zbx_free(fsdrivetype);
		return FAIL;
	}

	mntpoint = (zbx_wmpoint_t *)zbx_malloc(NULL, sizeof(zbx_wmpoint_t));
	mntpoint->fsname = fsname;
	mntpoint->fstype = fstype;
	mntpoint->fslabel = fslabel;
	mntpoint->fsdrivetype = fsdrivetype;
	mntpoint->total = total;
	mntpoint->not_used = not_used;
	mntpoint->used = used;
	mntpoint->pfree = pfree;
	mntpoint->pused = pused;
	zbx_vector_ptr_append(mntpoints, mntpoint);

	return SUCCEED;
}

static void	zbx_wmpoints_free(zbx_wmpoint_t *mpoint)
{
	zbx_free(mpoint->fsname);
	zbx_free(mpoint->fstype);
	zbx_free(mpoint->fslabel);
	zbx_free(mpoint->fsdrivetype);
	zbx_free(mpoint);
}

static int	get_mount_paths(zbx_vector_ptr_t *mount_paths, char **error)
{
#define zbx_wcsdup(old, str)		zbx_wcsdup2(__FILE__, __LINE__, old, str)
	wchar_t	*buffer = NULL, volume_name[MAX_PATH + 1], *p;
	DWORD	size_dw, last_error;
	HANDLE	volume = INVALID_HANDLE_VALUE;
	size_t	sz;
	int	ret = FAIL;

	/* make an initial call to GetLogicalDriveStrings() to get the necessary size into the dwSize variable */
	if (0 == (size_dw = GetLogicalDriveStrings(0, buffer)))
	{
		*error = zbx_strdup(*error, "Cannot obtain necessary buffer size from system.");
		return FAIL;
	}

	buffer = (wchar_t *)zbx_malloc(buffer, (size_dw + 1) * sizeof(wchar_t));

	/* make a second call to GetLogicalDriveStrings() to get the actual data we require */
	if (0 == (size_dw = GetLogicalDriveStrings(size_dw, buffer)))
	{
		*error = zbx_strdup(*error, "Cannot obtain necessary buffer size from system.");
		goto out;
	}

	/* add drive letters */
	for (p = buffer, sz = wcslen(p); sz > 0; p += sz + 1, sz = wcslen(p))
		zbx_vector_ptr_append(mount_paths, zbx_wcsdup(NULL, p));

	if (INVALID_HANDLE_VALUE == (volume = FindFirstVolume(volume_name, ARRSIZE(volume_name))))
	{
		*error = zbx_strdup(*error, "Cannot find a volume.");
		goto out;
	}

	/* search volumes for mount point folder paths */
	do
	{
		while (FALSE == GetVolumePathNamesForVolumeName(volume_name, buffer, size_dw, &size_dw))
		{
			if (ERROR_MORE_DATA != (last_error = GetLastError()))
			{
				*error = zbx_dsprintf(*error, "Cannot obtain a list of filesystems: %s",
						strerror_from_system(last_error));
				goto out;
			}

			buffer = (wchar_t*)zbx_realloc(buffer, size_dw * sizeof(wchar_t));
		}

		for (p = buffer, sz = wcslen(p); sz > 0; p += sz + 1, sz = wcslen(p))
		{
			/* add mount point folder paths but skip drive letters */
			if (3 < sz)
				zbx_vector_ptr_append(mount_paths, zbx_wcsdup(NULL, p));
		}

	} while (FALSE != FindNextVolume(volume, volume_name, ARRSIZE(volume_name)));

	if (ERROR_NO_MORE_FILES != (last_error = GetLastError()))
	{
		*error = zbx_dsprintf(*error, "Cannot obtain complete list of filesystems.",
				strerror_from_system(last_error));
		goto out;
	}

	ret = SUCCEED;
out:
	if (INVALID_HANDLE_VALUE != volume)
		FindVolumeClose(volume);

	zbx_free(buffer);

	return ret;
#undef zbx_wcsdup
}

int	VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	struct zbx_json		j;
	int			i, ret = SYSINFO_RET_FAIL;
	zbx_vector_ptr_t	mount_paths;
	char			*error = NULL, *fsname, *fstype, *fslabel, *fsdrivetype;

	zbx_vector_ptr_create(&mount_paths);
	zbx_json_initarray(&j, ZBX_JSON_STAT_BUF_LEN);

	if (FAIL == get_mount_paths(&mount_paths, &error))
	{
		SET_MSG_RESULT(result, error);
		goto out;
	}

	for (i = 0; i < mount_paths.values_num; i++)
	{
		get_fs_data(mount_paths.values[i], &fsname, &fstype, &fslabel, &fsdrivetype);

		zbx_json_addobject(&j, NULL);
		zbx_json_addstring(&j, ZBX_LLD_MACRO_FSNAME, fsname, ZBX_JSON_TYPE_STRING);
		zbx_json_addstring(&j, ZBX_LLD_MACRO_FSTYPE, fstype, ZBX_JSON_TYPE_STRING);
		zbx_json_addstring(&j, ZBX_LLD_MACRO_FSLABEL, fslabel, ZBX_JSON_TYPE_STRING);
		zbx_json_addstring(&j, ZBX_LLD_MACRO_FSDRIVETYPE, fsdrivetype, ZBX_JSON_TYPE_STRING);
		zbx_json_close(&j);

		zbx_free(fsname);
		zbx_free(fstype);
		zbx_free(fsdrivetype);
	}

	zbx_json_close(&j);

	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));
	ret = SYSINFO_RET_OK;
out:
	zbx_vector_ptr_clear_ext(&mount_paths, (zbx_clean_func_t)zbx_ptr_free);
	zbx_vector_ptr_destroy(&mount_paths);

	zbx_json_free(&j);

	return ret;
}

static int	vfs_fs_get(AGENT_REQUEST *request, AGENT_RESULT *result, HANDLE timeout_event)
{
	size_t			sz;
	struct zbx_json		j;
	zbx_vector_ptr_t	mntpoints;
	zbx_wmpoint_t		*mpoint;
	int			i, ret = SYSINFO_RET_FAIL;
	char			*error = NULL;
	zbx_vector_ptr_t	mount_paths;

	zbx_vector_ptr_create(&mount_paths);
	zbx_json_initarray(&j, ZBX_JSON_STAT_BUF_LEN);

	if (FAIL == get_mount_paths(&mount_paths, &error))
	{
		SET_MSG_RESULT(result, error);
		goto out;
	}

	/* 'timeout_event' argument is here to make the vfs_fs_size() prototype as required by */
	/* zbx_execute_threaded_metric() on MS Windows */
	ZBX_UNUSED(timeout_event);
	zbx_vector_ptr_create(&mntpoints);

	for (i = 0; i < mount_paths.values_num; i++)
	{
		if (FAIL == add_fs_to_vector(&mntpoints, mount_paths.values[i], &error))
		{
			zabbix_log(LOG_LEVEL_DEBUG, "%s", error);
			zbx_free(error);
			continue;
		}
	}

	zbx_vector_ptr_clear_ext(&mount_paths, (zbx_clean_func_t)zbx_ptr_free);
	if (FAIL == get_mount_paths(&mount_paths, &error))
	{
		SET_MSG_RESULT(result, error);
		goto out;
	}

	for (i = 0; i < mount_paths.values_num; i++)
	{
		zbx_wmpoint_t	mpoint_local;
		int		idx;

		mpoint_local.fsname = zbx_unicode_to_utf8(mount_paths.values[i]);
		if (0 < (sz = strlen(mpoint_local.fsname)) && '\\' == mpoint_local.fsname[--sz])
			mpoint_local.fsname[sz] = '\0';

		if (FAIL != (idx = zbx_vector_ptr_search(&mntpoints, &mpoint_local, wmpoint_compare_func)))
		{
			mpoint = (zbx_wmpoint_t *)mntpoints.values[idx];
			zbx_json_addobject(&j, NULL);
			zbx_json_addstring(&j, ZBX_SYSINFO_TAG_FSNAME, mpoint->fsname, ZBX_JSON_TYPE_STRING);
			zbx_json_addstring(&j, ZBX_SYSINFO_TAG_FSTYPE, mpoint->fstype, ZBX_JSON_TYPE_STRING);
			zbx_json_addstring(&j, ZBX_SYSINFO_TAG_FSLABEL, mpoint->fslabel, ZBX_JSON_TYPE_STRING);
			zbx_json_addstring(&j, ZBX_SYSINFO_TAG_FSDRIVETYPE, mpoint->fsdrivetype, ZBX_JSON_TYPE_STRING);
			zbx_json_addobject(&j, ZBX_SYSINFO_TAG_BYTES);
			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_TOTAL, mpoint->total);
			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_FREE, mpoint->not_used);
			zbx_json_adduint64(&j, ZBX_SYSINFO_TAG_USED, mpoint->used);
			zbx_json_addfloat(&j, ZBX_SYSINFO_TAG_PFREE, mpoint->pfree);
			zbx_json_addfloat(&j, ZBX_SYSINFO_TAG_PUSED, mpoint->pused);
			zbx_json_close(&j);
			zbx_json_close(&j);
		}
		zbx_free(mpoint_local.fsname);
	}

	zbx_json_close(&j);

	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));
	ret = SYSINFO_RET_OK;
out:
	zbx_vector_ptr_clear_ext(&mount_paths, (zbx_clean_func_t)zbx_ptr_free);
	zbx_vector_ptr_destroy(&mount_paths);

	zbx_json_free(&j);
	zbx_vector_ptr_clear_ext(&mntpoints, (zbx_clean_func_t)zbx_wmpoints_free);
	zbx_vector_ptr_destroy(&mntpoints);

	return ret;
}

int	VFS_FS_GET(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	return zbx_execute_threaded_metric(vfs_fs_get, request, result);
}