2003-08-26 05:08:58 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* init_osspecific()
|
|
|
|
*
|
|
|
|
* Init function
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void init_osspecific(netdata* data)
|
|
|
|
{
|
|
|
|
/* nothing */
|
2003-08-31 20:54:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf( stderr, "The netload plugin was initialized for Sun Solaris.\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, 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 sockfd, i, numifs, numifreqs;
|
|
|
|
size_t bufsize;
|
|
|
|
char *buf;
|
|
|
|
struct ifreq ifr, *ifrp;
|
|
|
|
struct ifconf ifc;
|
|
|
|
unsigned long rx_o, tx_o;
|
|
|
|
|
|
|
|
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
perror("socket");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (ioctl(sockfd, SIOCGIFNUM, (char *) &numifs) < 0)
|
|
|
|
{
|
|
|
|
perror("SIOCGIFNUM");
|
|
|
|
close(sockfd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
bufsize = ((size_t) numifs) * sizeof(struct ifreq);
|
|
|
|
buf = (char *) malloc(bufsize);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
perror("malloc");
|
|
|
|
close(sockfd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifc.ifc_len = bufsize;
|
|
|
|
ifc.ifc_buf = buf;
|
|
|
|
|
|
|
|
if (ioctl(sockfd, SIOCGIFCONF, (char *) &ifc) < 0)
|
|
|
|
{
|
|
|
|
perror("SIOCGIFCONF");
|
|
|
|
close(sockfd);
|
|
|
|
free(buf);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifrp = ifc.ifc_req;
|
|
|
|
numifreqs = ifc.ifc_len / sizeof(struct ifreq);
|
|
|
|
|
|
|
|
for (i = 0; i < numifreqs; i++, ifrp++)
|
|
|
|
{
|
|
|
|
memset((char *)&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
|
|
|
|
/* do not check for loopback device as it cannot be monitored */
|
|
|
|
if (!strncmp(ifr.ifr_name, "lo", 2))
|
|
|
|
continue;
|
|
|
|
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
|
|
|
|
{
|
|
|
|
perror("SIOCGIFFLAGS");
|
|
|
|
continue;
|
|
|
|
}
|
2003-08-26 05:08:58 +08:00
|
|
|
if (!strcmp(data->ifdata.if_name, ifr.ifr_name) && (ifr.ifr_flags & IFF_UP))
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
validinterface = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
return validinterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* get_stat()
|
|
|
|
*
|
|
|
|
* use the Solaris kstat_*() interface to gather statistics
|
|
|
|
* We have to open/close *kc each time :(
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2003-08-26 05:08:58 +08:00
|
|
|
int get_stat(netdata* data)
|
2003-08-25 04:05:32 +08:00
|
|
|
{
|
|
|
|
kstat_t *ksp;
|
|
|
|
kstat_named_t *knp;
|
|
|
|
kstat_ctl_t *kc;
|
|
|
|
unsigned long rx_o, tx_o;
|
|
|
|
|
|
|
|
if ((kc = kstat_open()) == NULL)
|
|
|
|
{
|
|
|
|
perror("kstat_open()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ksp = kstat_lookup(kc, NULL, -1, data->ifdata.if_name);
|
2003-08-25 04:05:32 +08:00
|
|
|
if (ksp && kstat_read(kc, ksp, NULL) >= 0)
|
|
|
|
{
|
|
|
|
knp = (kstat_named_t *)kstat_data_lookup(ksp, "opackets");
|
|
|
|
if (knp)
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.tx_packets = knp->value.ui32;
|
2003-08-25 04:05:32 +08:00
|
|
|
knp = (kstat_named_t *)kstat_data_lookup(ksp, "ipackets");
|
|
|
|
if (knp)
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.rx_packets = knp->value.ui32;
|
2003-08-25 04:05:32 +08:00
|
|
|
knp = (kstat_named_t *)kstat_data_lookup(ksp, "obytes");
|
|
|
|
if (knp)
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.tx_bytes = knp->value.ui32;
|
2003-08-25 04:05:32 +08:00
|
|
|
knp = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes");
|
|
|
|
if (knp)
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.rx_bytes = knp->value.ui32;
|
2003-08-25 04:05:32 +08:00
|
|
|
knp = (kstat_named_t *)kstat_data_lookup(ksp, "oerrors");
|
|
|
|
if (knp)
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.tx_errors = knp->value.ui32;
|
2003-08-25 04:05:32 +08:00
|
|
|
knp = (kstat_named_t *)kstat_data_lookup(ksp, "ierrors");
|
|
|
|
if (knp)
|
2003-08-26 05:08:58 +08:00
|
|
|
data->stats.rx_errors = knp->value.ui32;
|
2003-08-25 04:05:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kstat_close(kc);
|
|
|
|
|
|
|
|
/* check for overflows */
|
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;
|
|
|
|
}
|