** 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.
"golang.org/x/sys/windows"
"golang.zabbix.com/agent2/pkg/win32"
"golang.zabbix.com/sdk/plugin"
"golang.zabbix.com/sdk/zbxerr"
errorEmptyIpTable = "Empty IP address table returned."
errorCannotFindIf = "Cannot obtain network interface information."
err := plugin.RegisterMetrics(
"net.if.list", "Returns a list of network interfaces in text format.",
"net.if.in", "Returns incoming traffic statistics on network interface.",
"net.if.out", "Returns outgoing traffic statistics on network interface.",
"net.if.total", "Returns sum of incoming and outgoing traffic statistics on network interface.",
"net.if.discovery", "Returns list of network interfaces. Used for low-level discovery.",
panic(zbxerr.New("failed to register metrics").Wrap(err))
func (p *Plugin) nToIP(addr uint32) net.IP {
b := (*[4]byte)(unsafe.Pointer(&addr))
return net.IPv4(b[0], b[1], b[2], b[3])
func (p *Plugin) getIpAddrTable() (addrs []win32.MIB_IPADDRROW, err error) {
var ipTable *win32.MIB_IPADDRTABLE
var sizeIn, sizeOut uint32
if sizeOut, err = win32.GetIpAddrTable(nil, 0, false); err != nil {
buf := make([]byte, sizeIn)
ipTable = (*win32.MIB_IPADDRTABLE)(unsafe.Pointer(&buf[0]))
if sizeOut, err = win32.GetIpAddrTable(ipTable, sizeIn, false); err != nil {
return (*win32.RGMIB_IPADDRROW)(unsafe.Pointer(&ipTable.Table[0]))[:ipTable.NumEntries:ipTable.NumEntries], nil
func (p *Plugin) getIfRowByIP(ipaddr string, ifs []win32.MIB_IF_ROW2) (row *win32.MIB_IF_ROW2) {
if ip = net.ParseIP(ipaddr); ip == nil {
var ips []win32.MIB_IPADDRROW
if ips, err = p.getIpAddrTable(); err != nil {