Source
/*
** Zabbix
** Copyright (C) 2001-2023 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.
**/
int proc_mem(AGENT_REQUEST *request, AGENT_RESULT *result)
{
DIR *dir;
int proc;
struct dirent *entries;
zbx_stat_t buf;
struct passwd *usrinfo;
struct prpsinfo psinfo;
char filename[MAX_STRING_LEN];
char *procname, *proccomm, *param;
double memsize = -1;
int do_task, pgsize = getpagesize(), proccount = 0, invalid_user = 0;
pid_t curr_pid = getpid();
if (4 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}
procname = get_rparam(request, 0);
param = get_rparam(request, 1);
if (NULL != param && '\0' != *param)
{
if (NULL == (usrinfo = getpwnam(param)))
invalid_user = 1;
}
else
usrinfo = NULL;
param = get_rparam(request, 2);
if (NULL == param || '\0' == *param || 0 == strcmp(param, "sum")) /* default parameter */
do_task = ZBX_DO_SUM;
else if (0 == strcmp(param, "avg"))
do_task = ZBX_DO_AVG;
else if (0 == strcmp(param, "max"))
do_task = ZBX_DO_MAX;
else if (0 == strcmp(param, "min"))
do_task = ZBX_DO_MIN;
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
return SYSINFO_RET_FAIL;
}
proccomm = get_rparam(request, 3);
if (1 == invalid_user) /* handle 0 for non-existent user after all parameters have been parsed and validated */
goto out;
if (NULL == (dir = opendir("/proc")))
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc: %s", zbx_strerror(errno)));
return SYSINFO_RET_FAIL;
}
while (NULL != (entries = readdir(dir)))
{
zbx_strscpy(filename, "/proc/");
zbx_strlcat(filename, entries->d_name, MAX_STRING_LEN);
if (0 == zbx_stat(filename, &buf))