2003-08-26 05:08:58 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* init_osspecific()
|
|
|
|
*
|
|
|
|
* Init function
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void init_osspecific(netdata* data)
|
|
|
|
{
|
|
|
|
data->watchif = -1;
|
2003-08-31 20:54:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf( stderr, "The netload plugin was initialized for FreeBSD.\n" );
|
|
|
|
#endif
|
2003-08-26 05:08:58 +08:00
|
|
|
}
|
|
|
|
|
2003-08-25 04:05:32 +08:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* checkinterface()
|
|
|
|
*
|
|
|
|
* check if a given interface exists and is up.
|
|
|
|
* return TRUE if it does and FALSE if not
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
int checkinterface(netdata* data)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
int validinterface = FALSE;
|
|
|
|
|
|
|
|
int i, num_iface;
|
|
|
|
size_t len;
|
|
|
|
int name[6];
|
|
|
|
struct ifmibdata ifmd;
|
|
|
|
|
2003-08-31 20:54:36 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf( stderr, "Checking the interface '%s' now ...\n", data->ifdata.if_name );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-08-25 04:05:32 +08:00
|
|
|
len = sizeof(num_iface);
|
|
|
|
sysctlbyname("net.link.generic.system.ifcount", &num_iface, &len, NULL, 0);
|
|
|
|
for (i=1; i <= num_iface; i++)
|
|
|
|
{
|
|
|
|
name[0] = CTL_NET;
|
|
|
|
name[1] = PF_LINK;
|
|
|
|
name[2] = NETLINK_GENERIC;
|
|
|
|
name[3] = IFMIB_IFDATA;
|
|
|
|
name[4] = i;
|
|
|
|
name[5] = IFDATA_GENERAL;
|
|
|
|
|
|
|
|
len = sizeof(ifmd);
|
|
|
|
sysctl(name, 6, &ifmd, &len, NULL, 0);
|
2003-08-26 05:08:58 +08:00
|
|
|
if (strcmp(ifmd.ifmd_name, (char *)data->ifdata.if_name) == 0)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* now we have an interface and just have to see if it's up
|
|
|
|
* in case we just want to debug media types we disable
|
|
|
|
* IFF_UP flags
|
|
|
|
*/
|
|
|
|
#ifndef MEDIADEBUG
|
|
|
|
if (ifmd.ifmd_flags & IFF_UP)
|
|
|
|
#endif
|
|
|
|
validinterface = TRUE;
|
|
|
|
break; /* in any case we can stop searching here */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return validinterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* get_stat()
|
|
|
|
*
|
|
|
|
* use sysctl() to read the statistics and fill statistics struct
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
int get_stat(netdata* data)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* use sysctl() to get the right interface number if !dev_opened
|
|
|
|
* then read the data directly from the ifmd_data struct
|
|
|
|
*/
|
|
|
|
|
|
|
|
int i, num_iface;
|
|
|
|
size_t len;
|
|
|
|
int name[6];
|
|
|
|
struct ifmibdata ifmd;
|
|
|
|
unsigned long rx_o, tx_o;
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
if (!data->dev_opened)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
len = sizeof(num_iface);
|
|
|
|
sysctlbyname("net.link.generic.system.ifcount", &num_iface, &len,
|
|
|
|
NULL, 0);
|
|
|
|
for (i=1; i <= num_iface; i++)
|
|
|
|
{
|
|
|
|
name[0] = CTL_NET;
|
|
|
|
name[1] = PF_LINK;
|
|
|
|
name[2] = NETLINK_GENERIC;
|
|
|
|
name[3] = IFMIB_IFDATA;
|
|
|
|
name[4] = i;
|
|
|
|
name[5] = IFDATA_GENERAL;
|
|
|
|
|
|
|
|
len = sizeof(ifmd);
|
|
|
|
sysctl(name, 6, &ifmd, &len, NULL, 0);
|
2003-08-26 05:08:58 +08:00
|
|
|
if (strcmp(ifmd.ifmd_name, (char *)data->ifdata.if_name) == 0)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
/* got the right interface */
|
2003-08-31 20:54:36 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf( stderr, "Got the right interface.\n");
|
|
|
|
#endif
|
2003-08-26 05:08:58 +08:00
|
|
|
data->watchif = i;
|
|
|
|
data->dev_opened++;
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
2003-08-31 20:54:36 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf( stderr, "Got NOT the right interface.\n");
|
|
|
|
#endif
|
|
|
|
}
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* in any case read the struct and record statistics */
|
|
|
|
name[0] = CTL_NET;
|
|
|
|
name[1] = PF_LINK;
|
|
|
|
name[2] = NETLINK_GENERIC;
|
|
|
|
name[3] = IFMIB_IFDATA;
|
2003-08-26 05:08:58 +08:00
|
|
|
name[4] = data->watchif;
|
2003-08-25 04:05:32 +08:00
|
|
|
name[5] = IFDATA_GENERAL;
|
|
|
|
|
|
|
|
len = sizeof(ifmd);
|
|
|
|
sysctl(name, 6, &ifmd, &len, NULL, 0);
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
rx_o = data->stats.rx_bytes; tx_o = data->stats.tx_bytes;
|
2003-08-25 04:05:32 +08:00
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.tx_packets = ifmd.ifmd_data.ifi_opackets;
|
|
|
|
data->stats.rx_packets = ifmd.ifmd_data.ifi_ipackets;
|
|
|
|
data->stats.rx_bytes = ifmd.ifmd_data.ifi_ibytes;
|
|
|
|
data->stats.tx_bytes = ifmd.ifmd_data.ifi_obytes;
|
|
|
|
data->stats.rx_errors = ifmd.ifmd_data.ifi_ierrors;
|
|
|
|
data->stats.tx_errors = ifmd.ifmd_data.ifi_oerrors;
|
2003-08-25 04:05:32 +08:00
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
if (rx_o > data->stats.rx_bytes)
|
|
|
|
data->stats.rx_over++;
|
|
|
|
if (tx_o > data->stats.tx_bytes)
|
|
|
|
data->stats.tx_over++;
|
2003-08-25 04:05:32 +08:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|