/* *************************************************************************** * Read network interfaces statistics. * * IN: * @a Activity structure. * * OUT: * @a Activity structure with statistics. *************************************************************************** */ __read_funct_t wrap_read_net_dev(struct activity *a) { struct stats_net_dev *st_net_dev = (struct stats_net_dev *) a->_buf0; int dev; /* Read network interfaces stats */ dev = read_net_dev(st_net_dev, a->nr); if (!dev) /* No data read. Exit */ return; /* Read duplex and speed info for each interface */ read_if_info(st_net_dev, dev); return; }
int main(int argc, char* argv[]) { struct stats st, st_prev; struct ext_stats ext_st; unsigned long long interval; char *logfile = LOG_FILE; int speed = -1, duplex = -1; /* Usage: ./sysmon [real-time logging switch] [log filename] [print flag] (netif speed) (netif duplex) netif duplex takes C_DUPLEX_HALF=1 or C_DUPLEX_FULL=2 */ if (2 <= argc) realtime_logging = (strcmp("1", argv[1]) == 0); if (3 <= argc) logfile = argv[2]; if (4 <= argc && (strncmp("0x", argv[3], 2) == 0)) print_flag = (int)strtol(argv[3], NULL, 0); /* Expecting 0x... */ if (5 <= argc) speed = atoi(argv[4]); if (6 <= argc) duplex = atoi(argv[5]); printf ("realtime logging : %s\n", realtime_logging ? "enabled" : "disabled"); printf ("log file : %s\n", logfile); printf ("print flag : 0x%08x\n", print_flag); printf ("speed : %d\n", speed); printf ("duplex : %d\n", duplex); #ifndef DEBUG if ((fp = fopen(logfile, "w+")) == NULL) { fprintf(stderr, "opening logfile failed\n"); exit(EXIT_FAILURE); } #else fp = stdout; #endif /* Initialization */ signal(SIGINT, signal_callback_handler); /* Set up a signal handler */ read_stats(&base_stats); clock_gettime(CLOCK_MONOTONIC, &base_time); /* Obtain speed/duplex info for eth0 */ read_net_dev(&st.net, 1); read_if_info(&st.net, 1); /* VMs on Google Clouds do not let you read files under /sys/class/net/eth0/ See also: http://unix.stackexchange.com/questions/77750/checking-the-speed-of-active-network-connections */ base_stats.net.speed = (0 < speed) ? speed : st.net.speed; base_stats.net.duplex = (0 < duplex) ? duplex : st.net.duplex; st = st_prev = base_stats; /* Initialize variables for min/max/mean/var computation */ num_samples = 1; set_max_ext_stats(&min); memset((void *)&max, 0x00, sizeof(struct ext_stats)); memset((void *)&mean, 0x00, sizeof(struct ext_stats)); memset((void *)&var, 0x00, sizeof(struct ext_stats)); /* For real-time logging */ if (realtime_logging) fprint_header(fp, print_flag); /* Get HZ */ get_HZ(); while (1) { /* Anyways, we record stats for average computation */ usleep(INTERVAL_SEC * SEC_TO_MSEC); read_stats(&st); interval = get_interval(st_prev.uptime, st.uptime); /* Compute extended stats from raw stats */ compute_ext_stats(interval, &st, &st_prev, &ext_st); update_ext_stats(num_samples, &ext_st, &min, &max, &mean, &var); if (realtime_logging) fprint_stats(fp, print_flag, &st, &ext_st); /* Update stats (min/max/mean/var) for ext_stats */ st_prev = st; num_samples++; } /* Program never gets here */ printf("Waiting for the SIGINT signal\n"); pause(); return EXIT_SUCCESS; }