Source
zbx_snprintf(command, sizeof(command), "netstat -an | grep '^udp.*\\.%hu[^.].*\\*\\.\\*' | wc -l", port);
/*
** 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.
**/
static struct ifmibdata ifmd;
static int get_ifmib_general(const char *if_name, char **error)
{
int mib[6], ifcount;
size_t len;
if (NULL == if_name || '\0'== *if_name)
{
*error = zbx_strdup(NULL, "Network interface name cannot be empty.");
return SYSINFO_RET_FAIL;
}
mib[0] = CTL_NET;
mib[1] = PF_LINK;
mib[2] = NETLINK_GENERIC;
mib[3] = IFMIB_SYSTEM;
mib[4] = IFMIB_IFCOUNT;
len = sizeof(ifcount);
if (-1 == sysctl(mib, 5, &ifcount, &len, NULL, 0))
{
*error = zbx_dsprintf(NULL, "Cannot obtain number of network interfaces: %s", zbx_strerror(errno));
return SYSINFO_RET_FAIL;
}
mib[3] = IFMIB_IFDATA;
mib[5] = IFDATA_GENERAL;
len = sizeof(ifmd);
for (mib[4] = 1; mib[4] <= ifcount; mib[4]++)
{
if (-1 == sysctl(mib, 6, &ifmd, &len, NULL, 0))
{
if (ENOENT == errno)
continue;
*error = zbx_dsprintf(NULL, "Cannot obtain network interface information: %s",
zbx_strerror(errno));
return SYSINFO_RET_FAIL;
}
if (0 == strcmp(ifmd.ifmd_name, if_name))
return SYSINFO_RET_OK;
}
*error = zbx_strdup(NULL, "Cannot find information for this network interface.");
return SYSINFO_RET_FAIL;
}
int NET_IF_IN(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *if_name, *mode, *error;
if (2 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}
if_name = get_rparam(request, 0);
mode = get_rparam(request, 1);
if (SYSINFO_RET_FAIL == get_ifmib_general(if_name, &error))
{