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 "symbols.h"
23 
VM_MEMORY_SIZE(AGENT_REQUEST * request,AGENT_RESULT * result)24 int     VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
25 {
26 	PERFORMANCE_INFORMATION pfi;
27 	MEMORYSTATUSEX		ms_ex;
28 	MEMORYSTATUS		ms;
29 	char			*mode;
30 
31 	if (1 < request->nparam)
32 	{
33 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
34 		return SYSINFO_RET_FAIL;
35 	}
36 
37 	mode = get_rparam(request, 0);
38 
39 	if (NULL != mode && 0 == strcmp(mode, "cached"))
40 	{
41 		if (NULL == zbx_GetPerformanceInfo)
42 		{
43 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain system information."));
44 			return SYSINFO_RET_FAIL;
45 		}
46 
47 		zbx_GetPerformanceInfo(&pfi, sizeof(PERFORMANCE_INFORMATION));
48 
49 		SET_UI64_RESULT(result, (zbx_uint64_t)pfi.SystemCache * pfi.PageSize);
50 
51 		return SYSINFO_RET_OK;
52 	}
53 
54 	if (NULL != zbx_GlobalMemoryStatusEx)
55 	{
56 		ms_ex.dwLength = sizeof(MEMORYSTATUSEX);
57 
58 		zbx_GlobalMemoryStatusEx(&ms_ex);
59 
60 		if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
61 			SET_UI64_RESULT(result, ms_ex.ullTotalPhys);
62 		else if (0 == strcmp(mode, "free"))
63 			SET_UI64_RESULT(result, ms_ex.ullAvailPhys);
64 		else if (0 == strcmp(mode, "used"))
65 			SET_UI64_RESULT(result, ms_ex.ullTotalPhys - ms_ex.ullAvailPhys);
66 		else if (0 == strcmp(mode, "pused") && 0 != ms_ex.ullTotalPhys)
67 			SET_DBL_RESULT(result, (ms_ex.ullTotalPhys - ms_ex.ullAvailPhys) / (double)ms_ex.ullTotalPhys * 100);
68 		else if (0 == strcmp(mode, "available"))
69 			SET_UI64_RESULT(result, ms_ex.ullAvailPhys);
70 		else if (0 == strcmp(mode, "pavailable") && 0 != ms_ex.ullTotalPhys)
71 			SET_DBL_RESULT(result, ms_ex.ullAvailPhys / (double)ms_ex.ullTotalPhys * 100);
72 		else
73 		{
74 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
75 			return SYSINFO_RET_FAIL;
76 		}
77 	}
78 	else
79 	{
80 		GlobalMemoryStatus(&ms);
81 
82 		if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
83 			SET_UI64_RESULT(result, ms.dwTotalPhys);
84 		else if (0 == strcmp(mode, "free"))
85 			SET_UI64_RESULT(result, ms.dwAvailPhys);
86 		else if (0 == strcmp(mode, "used"))
87 			SET_UI64_RESULT(result, ms.dwTotalPhys - ms.dwAvailPhys);
88 		else if (0 == strcmp(mode, "pused") && 0 != ms.dwTotalPhys)
89 			SET_DBL_RESULT(result, (ms.dwTotalPhys - ms.dwAvailPhys) / (double)ms.dwTotalPhys * 100);
90 		else if (0 == strcmp(mode, "available"))
91 			SET_UI64_RESULT(result, ms.dwAvailPhys);
92 		else if (0 == strcmp(mode, "pavailable") && 0 != ms.dwTotalPhys)
93 			SET_DBL_RESULT(result, ms.dwAvailPhys / (double)ms.dwTotalPhys * 100);
94 		else
95 		{
96 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
97 			return SYSINFO_RET_FAIL;
98 		}
99 	}
100 
101 	return SYSINFO_RET_OK;
102 }
103