Source
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
static int get_swap_size(zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused,
char **error)
{
int mib[2];
size_t len;
struct uvmexp v;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP;
len = sizeof(v);
if (0 != sysctl(mib, 2, &v, &len, NULL, 0))
{
*error = zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno));
return SYSINFO_RET_FAIL;
}
/* int pagesize; size of a page (PAGE_SIZE): must be power of 2 */
/* int swpages; number of PAGE_SIZE'ed swap pages */
/* int swpginuse; number of swap pages in use */
if (NULL != total)
*total = (zbx_uint64_t)v.swpages * v.pagesize;
if (NULL != free)
*free = (zbx_uint64_t)(v.swpages - v.swpginuse) * v.pagesize;
if (NULL != used)
*used = (zbx_uint64_t)v.swpginuse * v.pagesize;
if (NULL != pfree)
*pfree = 0 != v.swpages ? (double)(100.0 * (v.swpages - v.swpginuse)) / v.swpages : 100;
if (NULL != pused)
*pused = 0 != v.swpages ? (double)(100.0 * v.swpginuse) / v.swpages : 0;
return SYSINFO_RET_OK;
}
static int system_swap_total(AGENT_RESULT *result)
{
zbx_uint64_t value;
char *error;
if (SYSINFO_RET_OK != get_swap_size(&value, NULL, NULL, NULL, NULL, &error))
{
SET_MSG_RESULT(result, error);
return SYSINFO_RET_FAIL;
}