int main(int argc, char **argv){ sg_network_iface_stats *network_iface_stats; size_t iface_count, i; /* Initialise helper - e.g. logging, if any */ sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL); /* Initialise statgrab */ sg_init(1); /* Drop setuid/setgid privileges. */ if (sg_drop_privileges() != SG_ERROR_NONE) sg_die("Error. Failed to drop privileges", 1); network_iface_stats = sg_get_network_iface_stats(&iface_count); if(network_iface_stats == NULL) sg_die("Failed to get network interface stats", 1); if (argc != 1) { /* If an argument is given, use bsearch to find just that * interface. */ sg_network_iface_stats key; key.interface_name = argv[1]; network_iface_stats = bsearch(&key, network_iface_stats, iface_count, sizeof *network_iface_stats, sg_network_iface_compare_name); if (network_iface_stats == NULL) { fprintf(stderr, "Interface %s not found\n", argv[1]); exit(1); } iface_count = 1; } printf("Name\tSpeed\tDuplex\n"); for(i = 0; i < iface_count; i++) { printf("%s\t%llu\t", network_iface_stats->interface_name, network_iface_stats->speed); switch (network_iface_stats->duplex) { case SG_IFACE_DUPLEX_FULL: printf("full\n"); break; case SG_IFACE_DUPLEX_HALF: printf("half\n"); break; default: printf("unknown\n"); break; } network_iface_stats++; } exit(0); }
void LxQtNetworkMonitor::settingsChanged() { m_iconIndex = mPlugin->settings()->value("icon", 1).toInt(); m_interface = mPlugin->settings()->value("interface").toString(); if (m_interface.isEmpty()) { #ifdef STATGRAB_NEWER_THAN_0_90 size_t count; #else int count; #endif sg_network_iface_stats* stats = sg_get_network_iface_stats(&count); if (count > 0) m_interface = QString(stats[0].interface_name); } m_pic.load(iconName("error")); }
/* * Network interface statistics, * see <tt>sg_get_network_iface_stats(3)</tt> manpage. */ static VALUE statgrab_network_iface_stats(VALUE self) { int entries, i; sg_network_iface_stats *stats; VALUE arr, info; if ((stats = sg_get_network_iface_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("speed")), INT2FIX(stats[i].speed)); switch(stats[i].duplex) { case SG_IFACE_DUPLEX_FULL: rb_hash_aset(info, ID2SYM(rb_intern("duplex")), ID2SYM(rb_intern("full"))); break; case SG_IFACE_DUPLEX_HALF: rb_hash_aset(info, ID2SYM(rb_intern("duplex")), ID2SYM(rb_intern("half"))); break; case SG_IFACE_DUPLEX_UNKNOWN: rb_hash_aset(info, ID2SYM(rb_intern("duplex")), ID2SYM(rb_intern("unknown"))); break; } if (stats[i].up) rb_hash_aset(info, ID2SYM(rb_intern("up")), Qtrue); else rb_hash_aset(info, ID2SYM(rb_intern("up")), Qfalse); 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); } } }