Source
/*
** Zabbix
** 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 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.
**/
/******************************************************************************
* *
* Function: get_swapinfo *
* *
* Purpose: get 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;