int get_stats(void) { st.cpu = sg_get_cpu_percents(); if (!st.cpu) { LOG(LOG_INFO, "could not sg_get_cpu_stats"); } st.mem = sg_get_mem_stats(); if (!st.mem) { LOG(LOG_INFO, "could not sg_get_mem_stats"); } st.swap = sg_get_swap_stats(); if (!st.swap) { LOG(LOG_INFO, "could not get_swap_stats"); } st.load = sg_get_load_stats(); if (!st.load) { LOG(LOG_INFO, "could not get_load_stats"); } st.process = sg_get_process_stats(&st.process_entries); if (!st.process) { LOG(LOG_INFO, "could not get_process_stats"); } st.paging = sg_get_page_stats_diff(); if (!st.paging) { LOG(LOG_INFO, "could not get_page_stats_diff"); } st.network = sg_get_network_io_stats_diff(&(st.network_entries)); if (!st.network) { LOG(LOG_INFO, "could not get_network_stats_diff"); } st.diskio = sg_get_disk_io_stats_diff(&(st.diskio_entries)); if (!st.diskio) { LOG(LOG_INFO, "could not get_diskio_stats_diff"); } st.disk = sg_get_fs_stats(&(st.disk_entries)); if (!st.disk) { LOG(LOG_INFO, "could not get_disk_stats"); } st.hostinfo = sg_get_host_info(); if (!st.hostinfo) { LOG(LOG_INFO, "could not get_host_info"); } st.user = sg_get_user_stats(); if (!st.user) { LOG(LOG_INFO, "could not get get_user_stats"); } return 1; }
int get_stats() { stats.cpu_percents = sg_get_cpu_percents(); stats.mem_stats = sg_get_mem_stats(); stats.swap_stats = sg_get_swap_stats(); stats.load_stats = sg_get_load_stats(); stats.process_count = sg_get_process_count(); stats.page_stats = sg_get_page_stats_diff(); stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries)); stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries)); stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries)); stats.host_info = sg_get_host_info(); stats.user_stats = sg_get_user_stats(); return 1; }
void LxQtNetworkMonitor::timerEvent(QTimerEvent *event) { bool matched = false; #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_diff(&num_network_stats); for (int x = 0; x < num_network_stats; x++) { if (m_interface == QString::fromLocal8Bit(network_stats->interface_name)) { if (network_stats->rx != 0 && network_stats->tx != 0) { m_pic.load(iconName("transmit-receive")); } else if (network_stats->rx != 0 && network_stats->tx == 0) { m_pic.load(iconName("receive")); } else if (network_stats->rx == 0 && network_stats->tx != 0) { m_pic.load(iconName("transmit")); } else { m_pic.load(iconName("idle")); } matched = true; break; } network_stats++; } if (!matched) { m_pic.load(iconName("error")); } update(); }
/* * Network statistics difference since last call, * see <tt>sg_get_network_io_stats_diff(3)</tt> manpage. */ static VALUE statgrab_network_io_stats_diff(VALUE self) { int entries, i; sg_network_io_stats *stats; VALUE arr, info, time_now; if ((stats = sg_get_network_io_stats_diff(&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_now = rb_funcall(rb_cTime, rb_intern("now"), 0); rb_hash_aset(info, ID2SYM(rb_intern("last_call")), rb_funcall(time_now, rb_intern("-"), 1, INT2NUM(stats[i].systime))); 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); } } }
int main(int argc, char **argv){ extern char *optarg; int c; /* We default to 1 second updates and displaying in bytes*/ int delay = 1; char units = 'b'; unsigned long long divider = 1; sg_network_io_stats *network_stats; size_t num_network_stats; /* Parse command line options */ while ((c = getopt(argc, argv, "d:bkm")) != -1){ switch (c){ case 'd': delay = atoi(optarg); break; case 'b': units = 'b'; break; case 'k': units = 'k'; divider = 1024; break; case 'm': units = 'm'; divider = 1024 * 1024; break; } } /* Initialise helper - e.g. logging, if any */ sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL); /* Initialise statgrab */ sg_init(1); register_sig_flagger( SIGINT, &quit ); /* Drop setuid/setgid privileges. */ if (sg_drop_privileges() != SG_ERROR_NONE) sg_die("Error. Failed to drop privileges", 1); /* We are not interested in the amount of traffic ever transmitted, just differences. * Because of this, we do nothing for the very first call. */ network_stats = sg_get_network_io_stats_diff(&num_network_stats); if (network_stats == NULL) sg_die("Error. Failed to get network stats", 1); /* Clear the screen ready for display the network stats */ printf("\033[2J"); /* Keep getting the network stats */ while ( (network_stats = sg_get_network_io_stats_diff(&num_network_stats)) != NULL){ size_t x; int line_number = 2; int ch; unsigned long long total_tx=0; unsigned long long total_rx=0; unsigned long long total_ipackets=0; unsigned long long total_opackets=0; unsigned long long total_ierrors=0; unsigned long long total_oerrors=0; unsigned long long total_collisions=0; for(x = 0; x < num_network_stats; x++){ /* Print at location 2, linenumber the interface name */ printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", network_stats->interface_name); /* Print out at the correct location the traffic in the requsted units passed at command time */ printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Interface Rx", network_stats->rx / divider, units); printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Interface Tx", network_stats->tx / divider, units); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface packets in", network_stats->ipackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface packets out", network_stats->opackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface errors in", network_stats->ierrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface errors out", network_stats->oerrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface collisions", network_stats->collisions); printf("\033[%d;2H%-30s : %ld ", line_number++, "Network Interface systime", (long) network_stats->systime); /* Add a blank line between interfaces */ line_number++; /* Add up this interface to the total so we can display a "total" network io" */ total_tx+=network_stats->tx; total_rx+=network_stats->rx; total_ipackets+=network_stats->ipackets; total_opackets+=network_stats->opackets; total_ierrors+=network_stats->ierrors; total_oerrors+=network_stats->oerrors; total_collisions+=network_stats->collisions; /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need * to keep track of the orginal pointer to free later */ network_stats++; } printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", "Total Network IO"); printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Total Rx", total_rx / divider, units); printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Total Tx", total_tx / divider, units); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total packets in", total_ipackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total packets out", total_opackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total errors in", total_ierrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total errors out", total_oerrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total collisions", total_collisions); fflush(stdout); ch = inp_wait(delay); if( quit || (ch == 'q') ) break; } return 0; }
int main(int argc, char **argv){ extern char *optarg; int c; /* We default to 1 second updates and displaying in bytes*/ int delay = 1; char units = 'b'; sg_network_io_stats *network_stats; int num_network_stats; /* Parse command line options */ while ((c = getopt(argc, argv, "d:bkm")) != -1){ switch (c){ case 'd': delay = atoi(optarg); break; case 'b': units = 'b'; break; case 'k': units = 'k'; break; case 'm': units = 'm'; break; } } /* Initialise statgrab */ sg_init(); /* Drop setuid/setgid privileges. */ if (sg_drop_privileges() != 0) { perror("Error. Failed to drop privileges"); return 1; } /* We are not interested in the amount of traffic ever transmitted, just differences. * Because of this, we do nothing for the very first call. */ network_stats = sg_get_network_io_stats_diff(&num_network_stats); if (network_stats == NULL){ perror("Error. Failed to get network stats"); return 1; } /* Clear the screen ready for display the network stats */ printf("\033[2J"); /* Keep getting the network stats */ while ( (network_stats = sg_get_network_io_stats_diff(&num_network_stats)) != NULL){ int x; int line_number = 2; long long total_tx=0; long long total_rx=0; long long total_ipackets=0; long long total_opackets=0; long long total_ierrors=0; long long total_oerrors=0; long long total_collisions=0; for(x = 0; x < num_network_stats; x++){ /* Print at location 2, linenumber the interface name */ printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", network_stats->interface_name); /* Print out at the correct location the traffic in the requsted units passed at command time */ switch(units){ case 'b': printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Interface Rx", network_stats->rx); printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Interface Tx", network_stats->tx); break; case 'k': printf("\033[%d;2H%-30s : %5lld k", line_number++, "Network Interface Rx", (network_stats->rx / 1024)); printf("\033[%d;2H%-30s : %5lld", line_number++, "Network Interface Tx", (network_stats->tx / 1024)); break; case 'm': printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Interface Rx", network_stats->rx / (1024.00*1024.00)); printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Interface Tx", network_stats->tx / (1024.00*1024.00)); } printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface packets in", network_stats->ipackets); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface packets out", network_stats->opackets); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface errors in", network_stats->ierrors); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface errors out", network_stats->oerrors); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface collisions", network_stats->collisions); printf("\033[%d;2H%-30s : %ld ", line_number++, "Network Interface systime", (long) network_stats->systime); /* Add a blank line between interfaces */ line_number++; /* Add up this interface to the total so we can display a "total" network io" */ total_tx+=network_stats->tx; total_rx+=network_stats->rx; total_ipackets+=network_stats->ipackets; total_opackets+=network_stats->opackets; total_ierrors+=network_stats->ierrors; total_oerrors+=network_stats->oerrors; total_collisions+=network_stats->collisions; /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need * to keep track of the orginal pointer to free later */ network_stats++; } printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", "Total Network IO"); switch(units){ case 'b': printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Total Rx", total_rx); printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Total Tx", total_tx); break; case 'k': printf("\033[%d;2H%-30s : %5lld k", line_number++, "Network Total Rx", (total_rx / 1024)); printf("\033[%d;2H%-30s : %5lld k", line_number++, "Network Total Tx", (total_tx / 1024)); break; case 'm': printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Total Rx", (total_rx / (1024.00*1024.00))); printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Total Tx", (total_tx / (1024.00*1024.00))); break; } printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total packets in", total_ipackets); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total packets out", total_opackets); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total errors in", total_ierrors); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total errors out", total_oerrors); printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total collisions", total_collisions); fflush(stdout); sleep(delay); } return 0; }