/* do the actual work, get and print stats if verbose */ void get_iface_stats_libstat (char verbose) { sg_network_io_stats *network_stats=NULL; int num_network_stats,current_if_num=0,hidden_if=0; t_iface_speed_stats stats; /* local struct, used to calc total values */ t_iface_speed_stats tmp_if_stats; memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ network_stats = sg_get_network_io_stats(&num_network_stats); if (network_stats == NULL){ deinit(1, "libstatgrab error!\n"); } for (current_if_num=0;current_if_num<num_network_stats;current_if_num++) { tmp_if_stats.bytes.in=network_stats->rx; tmp_if_stats.bytes.out=network_stats->tx; tmp_if_stats.packets.in=network_stats->ipackets; tmp_if_stats.packets.out=network_stats->opackets; tmp_if_stats.errors.in=network_stats->ierrors; tmp_if_stats.errors.out=network_stats->oerrors; hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, network_stats->interface_name, current_if_num, verbose #ifdef IOCTL ,check_if_up(network_stats->interface_name) #else ,1 #endif ); network_stats++; } finish_iface_stats (verbose, stats, hidden_if,current_if_num); return; }
bool LxQtNetworkMonitor::event(QEvent *event) { if (event->type() == QEvent::ToolTip) { #ifdef STATGRAB_NEWER_THAN_0_90 size_t num_network_stats; #else int num_network_stats; #endif sg_network_io_stats *network_stats = sg_get_network_io_stats(&num_network_stats); for (int x = 0; x < num_network_stats; x++) { if (m_interface == QString::fromLocal8Bit(network_stats->interface_name)) { setToolTip(tr("Network interface <b>%1</b>").arg(m_interface) + "<br>" + tr("Transmitted %1").arg(convertUnits(network_stats->tx)) + "<br>" + tr("Received %1").arg(convertUnits(network_stats->rx)) ); } network_stats++; } } return QFrame::event(event); }
/* * Network statistics, see <tt>sg_get_network_io_stats(3)</tt> manpage. */ static VALUE statgrab_network_io_stats(VALUE self) { int entries, i; sg_network_io_stats *stats; VALUE arr, info, time_at; if ((stats = sg_get_network_io_stats(&entries)) == NULL) statgrab_handle_error(); arr = rb_ary_new(); for (i = 0; i < entries; i++) { info = rb_hash_new(); rb_hash_aset(info, ID2SYM(rb_intern("interface_name")), rb_str_new2(stats[i].interface_name)); rb_hash_aset(info, ID2SYM(rb_intern("tx")), INT2NUM(stats[i].tx/1024)); rb_hash_aset(info, ID2SYM(rb_intern("rx")), INT2NUM(stats[i].rx/1024)); rb_hash_aset(info, ID2SYM(rb_intern("ipackets")), INT2NUM(stats[i].ipackets)); rb_hash_aset(info, ID2SYM(rb_intern("opackets")), INT2NUM(stats[i].opackets)); rb_hash_aset(info, ID2SYM(rb_intern("ierrors")), INT2NUM(stats[i].ierrors)); rb_hash_aset(info, ID2SYM(rb_intern("oerrors")), INT2NUM(stats[i].oerrors)); rb_hash_aset(info, ID2SYM(rb_intern("collisions")), INT2NUM(stats[i].collisions)); rb_hash_aset(info, ID2SYM(rb_intern("systime")), INT2NUM(stats[i].systime)); time_at = rb_funcall(rb_cTime, rb_intern("at"), 1, INT2NUM(stats[i].systime)); rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at); rb_ary_push(arr, info); } return arr; }
void populate_net() { int num_io, num_iface, i; sg_network_io_stats *io; sg_network_iface_stats *iface; io = use_diffs ? sg_get_network_io_stats_diff(&num_io) : sg_get_network_io_stats(&num_io); if (io != NULL) { for (i = 0; i < num_io; i++) { const char *name = io[i].interface_name; add_stat(STRING, &io[i].interface_name, "net", name, "interface_name", NULL); add_stat(BYTES, &io[i].tx, "net", name, "tx", NULL); add_stat(BYTES, &io[i].rx, "net", name, "rx", NULL); add_stat(LONG_LONG, &io[i].ipackets, "net", name, "ipackets", NULL); add_stat(LONG_LONG, &io[i].opackets, "net", name, "opackets", NULL); add_stat(LONG_LONG, &io[i].ierrors, "net", name, "ierrors", NULL); add_stat(LONG_LONG, &io[i].oerrors, "net", name, "oerrors", NULL); add_stat(LONG_LONG, &io[i].collisions, "net", name, "collisions", NULL); add_stat(TIME_T, &io[i].systime, "net", name, "systime", NULL); } } iface = sg_get_network_iface_stats(&num_iface); if (iface != NULL) { for (i = 0; i < num_iface; i++) { const char *name = iface[i].interface_name; int had_io = 0, j; /* If there wasn't a corresponding io stat, add interface_name from here. */ if (io != NULL) { for (j = 0; j < num_io; j++) { if (strcmp(io[j].interface_name, name) == 0) { had_io = 1; break; } } } if (!had_io) { add_stat(STRING, &iface[i].interface_name, "net", name, "interface_name", NULL); } add_stat(INT, &iface[i].speed, "net", name, "speed", NULL); add_stat(BOOL, &iface[i].up, "net", name, "up", NULL); add_stat(DUPLEX, &iface[i].duplex, "net", name, "duplex", NULL); } } }
static int interface_read (void) { #if HAVE_GETIFADDRS struct ifaddrs *if_list; struct ifaddrs *if_ptr; /* Darwin/Mac OS X and possible other *BSDs */ #if HAVE_STRUCT_IF_DATA # define IFA_DATA if_data # define IFA_RX_BYTES ifi_ibytes # define IFA_TX_BYTES ifi_obytes # define IFA_RX_PACKT ifi_ipackets # define IFA_TX_PACKT ifi_opackets # define IFA_RX_ERROR ifi_ierrors # define IFA_TX_ERROR ifi_oerrors /* #endif HAVE_STRUCT_IF_DATA */ #elif HAVE_STRUCT_NET_DEVICE_STATS # define IFA_DATA net_device_stats # define IFA_RX_BYTES rx_bytes # define IFA_TX_BYTES tx_bytes # define IFA_RX_PACKT rx_packets # define IFA_TX_PACKT tx_packets # define IFA_RX_ERROR rx_errors # define IFA_TX_ERROR tx_errors #else # error "No suitable type for `struct ifaddrs->ifa_data' found." #endif struct IFA_DATA *if_data; if (getifaddrs (&if_list) != 0) return (-1); for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next) { if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) { if_data = (struct IFA_DATA *) if_ptr->ifa_data; if_submit (if_ptr->ifa_name, "if_octets", if_data->IFA_RX_BYTES, if_data->IFA_TX_BYTES); if_submit (if_ptr->ifa_name, "if_packets", if_data->IFA_RX_PACKT, if_data->IFA_TX_PACKT); if_submit (if_ptr->ifa_name, "if_errors", if_data->IFA_RX_ERROR, if_data->IFA_TX_ERROR); } } freeifaddrs (if_list); /* #endif HAVE_GETIFADDRS */ #elif KERNEL_LINUX FILE *fh; char buffer[1024]; derive_t incoming, outgoing; char *device; char *dummy; char *fields[16]; int numfields; if ((fh = fopen ("/proc/net/dev", "r")) == NULL) { char errbuf[1024]; WARNING ("interface plugin: fopen: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buffer, 1024, fh) != NULL) { if (!(dummy = strchr(buffer, ':'))) continue; dummy[0] = '\0'; dummy++; device = buffer; while (device[0] == ' ') device++; if (device[0] == '\0') continue; numfields = strsplit (dummy, fields, 16); if (numfields < 11) continue; incoming = atoll (fields[0]); outgoing = atoll (fields[8]); if_submit (device, "if_octets", incoming, outgoing); incoming = atoll (fields[1]); outgoing = atoll (fields[9]); if_submit (device, "if_packets", incoming, outgoing); incoming = atoll (fields[2]); outgoing = atoll (fields[10]); if_submit (device, "if_errors", incoming, outgoing); incoming = atoll (fields[3]); outgoing = atoll (fields[11]); if_submit (device, "if_dropped", incoming, outgoing); } fclose (fh); /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT int i; derive_t rx; derive_t tx; char iname[DATA_MAX_NAME_LEN]; if (kc == NULL) return (-1); for (i = 0; i < numif; i++) { if (kstat_read (kc, ksp[i], NULL) == -1) continue; if (unique_name) ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module, ksp[i]->ks_instance, ksp[i]->ks_name); else sstrncpy(iname, ksp[i]->ks_name, sizeof(iname)); /* try to get 64bit counters */ rx = get_kstat_value (ksp[i], "rbytes64"); tx = get_kstat_value (ksp[i], "obytes64"); /* or fallback to 32bit */ if (rx == -1LL) rx = get_kstat_value (ksp[i], "rbytes"); if (tx == -1LL) tx = get_kstat_value (ksp[i], "obytes"); if ((rx != -1LL) || (tx != -1LL)) if_submit (iname, "if_octets", rx, tx); /* try to get 64bit counters */ rx = get_kstat_value (ksp[i], "ipackets64"); tx = get_kstat_value (ksp[i], "opackets64"); /* or fallback to 32bit */ if (rx == -1LL) rx = get_kstat_value (ksp[i], "ipackets"); if (tx == -1LL) tx = get_kstat_value (ksp[i], "opackets"); if ((rx != -1LL) || (tx != -1LL)) if_submit (iname, "if_packets", rx, tx); /* no 64bit error counters yet */ rx = get_kstat_value (ksp[i], "ierrors"); tx = get_kstat_value (ksp[i], "oerrors"); if ((rx != -1LL) || (tx != -1LL)) if_submit (iname, "if_errors", rx, tx); } /* #endif HAVE_LIBKSTAT */ #elif defined(HAVE_LIBSTATGRAB) sg_network_io_stats *ios; int i, num; ios = sg_get_network_io_stats (&num); for (i = 0; i < num; i++) if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx); /* #endif HAVE_LIBSTATGRAB */ #elif defined(HAVE_PERFSTAT) perfstat_id_t id; int i, ifs; if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0) { char errbuf[1024]; WARNING ("interface plugin: perfstat_netinterface: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } if (pnif != nif || ifstat == NULL) { free(ifstat); ifstat = malloc(nif * sizeof (*ifstat)); } pnif = nif; id.name[0]='\0'; if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0) { char errbuf[1024]; WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s", nif, sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } for (i = 0; i < ifs; i++) { if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes); if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets); if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors ); } #endif /* HAVE_PERFSTAT */ return (0); } /* int interface_read */
static int interface_read (void) { #if HAVE_GETIFADDRS struct ifaddrs *if_list; struct ifaddrs *if_ptr; /* Darin/Mac OS X and possible other *BSDs */ #if HAVE_STRUCT_IF_DATA # define IFA_DATA if_data # define IFA_RX_BYTES ifi_ibytes # define IFA_TX_BYTES ifi_obytes # define IFA_RX_PACKT ifi_ipackets # define IFA_TX_PACKT ifi_opackets # define IFA_RX_ERROR ifi_ierrors # define IFA_TX_ERROR ifi_oerrors /* #endif HAVE_STRUCT_IF_DATA */ #elif HAVE_STRUCT_NET_DEVICE_STATS # define IFA_DATA net_device_stats # define IFA_RX_BYTES rx_bytes # define IFA_TX_BYTES tx_bytes # define IFA_RX_PACKT rx_packets # define IFA_TX_PACKT tx_packets # define IFA_RX_ERROR rx_errors # define IFA_TX_ERROR tx_errors #else # error "No suitable type for `struct ifaddrs->ifa_data' found." #endif struct IFA_DATA *if_data; if (getifaddrs (&if_list) != 0) return (-1); for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next) { if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL) continue; if_submit (if_ptr->ifa_name, "if_octets", if_data->IFA_RX_BYTES, if_data->IFA_TX_BYTES); if_submit (if_ptr->ifa_name, "if_packets", if_data->IFA_RX_PACKT, if_data->IFA_TX_PACKT); if_submit (if_ptr->ifa_name, "if_errors", if_data->IFA_RX_ERROR, if_data->IFA_TX_ERROR); } freeifaddrs (if_list); /* #endif HAVE_GETIFADDRS */ #elif KERNEL_LINUX FILE *fh; char buffer[1024]; unsigned long long incoming, outgoing; char *device; char *dummy; char *fields[16]; int numfields; if ((fh = fopen ("/proc/net/dev", "r")) == NULL) { char errbuf[1024]; WARNING ("interface plugin: fopen: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buffer, 1024, fh) != NULL) { if (!(dummy = strchr(buffer, ':'))) continue; dummy[0] = '\0'; dummy++; device = buffer; while (device[0] == ' ') device++; if (device[0] == '\0') continue; numfields = strsplit (dummy, fields, 16); if (numfields < 11) continue; incoming = atoll (fields[0]); outgoing = atoll (fields[8]); if_submit (device, "if_octets", incoming, outgoing); incoming = atoll (fields[1]); outgoing = atoll (fields[9]); if_submit (device, "if_packets", incoming, outgoing); incoming = atoll (fields[2]); outgoing = atoll (fields[10]); if_submit (device, "if_errors", incoming, outgoing); } fclose (fh); /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT int i; unsigned long long rx; unsigned long long tx; if (kc == NULL) return (-1); for (i = 0; i < numif; i++) { if (kstat_read (kc, ksp[i], NULL) == -1) continue; rx = get_kstat_value (ksp[i], "rbytes"); tx = get_kstat_value (ksp[i], "obytes"); if ((rx != -1LL) || (tx != -1LL)) if_submit (ksp[i]->ks_name, "if_octets", rx, tx); rx = get_kstat_value (ksp[i], "ipackets"); tx = get_kstat_value (ksp[i], "opackets"); if ((rx != -1LL) || (tx != -1LL)) if_submit (ksp[i]->ks_name, "if_packets", rx, tx); rx = get_kstat_value (ksp[i], "ierrors"); tx = get_kstat_value (ksp[i], "oerrors"); if ((rx != -1LL) || (tx != -1LL)) if_submit (ksp[i]->ks_name, "if_errors", rx, tx); } /* #endif HAVE_LIBKSTAT */ #elif defined(HAVE_LIBSTATGRAB) sg_network_io_stats *ios; int i, num; ios = sg_get_network_io_stats (&num); for (i = 0; i < num; i++) if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx); #endif /* HAVE_LIBSTATGRAB */ return (0); } /* int interface_read */