Source
if (SYSINFO_RET_OK != get_ifdata(if_name, &ibytes, &ipackets, &ierrors, &idropped, NULL, NULL, NULL, NULL, NULL,
/*
** 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 struct nlist kernel_symbols[] =
{
{"_ifnet", N_UNDF, 0, 0, 0},
{"_tcbtable", N_UNDF, 0, 0, 0},
{NULL, 0, 0, 0, 0}
};
static int get_ifdata(const char *if_name,
zbx_uint64_t *ibytes, zbx_uint64_t *ipackets, zbx_uint64_t *ierrors, zbx_uint64_t *idropped,
zbx_uint64_t *obytes, zbx_uint64_t *opackets, zbx_uint64_t *oerrors,
zbx_uint64_t *tbytes, zbx_uint64_t *tpackets, zbx_uint64_t *terrors,
zbx_uint64_t *icollisions, char **error)
{
struct ifnet_head head;
struct ifnet *ifp;
struct ifnet v;
kvm_t *kp;
int ret = SYSINFO_RET_FAIL;
if (NULL == if_name || '\0' == *if_name)
{
*error = zbx_strdup(NULL, "Network interface name cannot be empty.");
return FAIL;
}
if (NULL == (kp = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL))) /* requires root privileges */
{
*error = zbx_strdup(NULL, "Cannot obtain a descriptor to access kernel virtual memory.");
return FAIL;
}
if (N_UNDF == kernel_symbols[IFNET_ID].n_type)
if (0 != kvm_nlist(kp, &kernel_symbols[0]))
kernel_symbols[IFNET_ID].n_type = N_UNDF;
if (N_UNDF != kernel_symbols[IFNET_ID].n_type)
{
int len = sizeof(struct ifnet_head);
if (kvm_read(kp, kernel_symbols[IFNET_ID].n_value, &head, len) >= len)
{
len = sizeof(struct ifnet);
/* if_ibytes; total number of octets received */
/* if_ipackets; packets received on interface */
/* if_ierrors; input errors on interface */
/* if_iqdrops; dropped on input, this interface */
/* if_obytes; total number of octets sent */
/* if_opackets; packets sent on interface */
/* if_oerrors; output errors on interface */
/* if_collisions; collisions on csma interfaces */
if (ibytes)
*ibytes = 0;
if (ipackets)
*ipackets = 0;
if (ierrors)
*ierrors = 0;
if (idropped)
*idropped = 0;
if (obytes)
*obytes = 0;
if (opackets)
*opackets = 0;
if (oerrors)
*oerrors = 0;
if (tbytes)
*tbytes = 0;
if (tpackets)
*tpackets = 0;