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/>.
**/
/******************************************************************************
* *
* Purpose: gets swap usage statistics *
* *
* Return value: SUCCEED if swap usage statistics retrieved successfully *
* FAIL otherwise *
* *
* Comments: we try to imitate "swap -l". *
* *
******************************************************************************/
static int get_swapinfo(zbx_uint64_t *total, zbx_uint64_t *free1, char **error)
{
int i, cnt, cnt2, page_size, ret = FAIL;
struct swaptable *swt = NULL;
struct swapent *ste;
static char path[256];
/* get total number of swap entries */
if (-1 == (cnt = swapctl(SC_GETNSWP, 0)))
{
*error = zbx_dsprintf(NULL, "Cannot obtain number of swap entries: %s", zbx_strerror(errno));
return FAIL;
}
if (0 == cnt)
{
*total = *free1 = 0;
return SUCCEED;
}
/* allocate space to hold count + n swapents */
swt = (struct swaptable *)zbx_malloc(swt, sizeof(struct swaptable) + (cnt - 1) * sizeof(struct swapent));
swt->swt_n = cnt;
/* fill in ste_path pointers: we don't care about the paths, so we point them all to the same buffer */
ste = &(swt->swt_ent[0]);
i = cnt;
while (--i >= 0)
{
ste++->ste_path = path;
}
/* grab all swap info */
if (-1 == (cnt2 = swapctl(SC_LIST, swt)))
{