--- projects/libstatgrab/src/statgrab/statgrab.c 2004/04/07 15:50:26 1.26 +++ projects/libstatgrab/src/statgrab/statgrab.c 2004/08/10 18:50:37 1.29 @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - * $Id: statgrab.c,v 1.26 2004/04/07 15:50:26 tdb Exp $ + * $Id: statgrab.c,v 1.29 2004/08/10 18:50:37 ats Exp $ */ #ifdef HAVE_CONFIG_H @@ -71,6 +71,7 @@ repeat_mode_type repeat_mode = REPEAT_NONE; int repeat_time = 1; int use_cpu_percent = 0; int use_diffs = 0; +long float_scale_factor = 0; /* Exit with an error message. */ void die(const char *s) { @@ -352,14 +353,14 @@ void populate_proc() { } void populate_net() { - int n, i; + 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(&n) - : sg_get_network_io_stats(&n); + 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 < n; i++) { + for (i = 0; i < num_io; i++) { const char *name = io[i].interface_name; add_stat(STRING, &io[i].interface_name, @@ -383,11 +384,28 @@ void populate_net() { } } - iface = sg_get_network_iface_stats(&n); + iface = sg_get_network_iface_stats(&num_iface); if (iface != NULL) { - for (i = 0; i < n; i++) { + 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, @@ -471,6 +489,7 @@ void get_stats() { /* Print the value of a stat. */ void print_stat_value(const stat *s) { void *v = s->stat; + double fv; long l; switch (s->type) { @@ -483,10 +502,17 @@ void print_stat_value(const stat *s) { printf("%ld", l); break; case FLOAT: - printf("%f", *(float *)v); - break; case DOUBLE: - printf("%f", *(double *)v); + if (s->type == FLOAT) { + fv = *(float *)v; + } else { + fv = *(double *)v; + } + if (float_scale_factor != 0) { + printf("%ld", (long)(float_scale_factor * fv)); + } else { + printf("%f", fv); + } break; case STRING: /* FIXME escaping? */ @@ -603,6 +629,7 @@ void usage() { " -t DELAY When repeating, wait DELAY seconds between updates (default 1)\n" " -p Display CPU usage differences as percentages rather than\n" " absolute values\n" + " -f SCALE Display floating-point values as integers scaled by FACTOR\n" "\n"); printf("Version %s - report bugs to <%s>.\n", PACKAGE_VERSION, PACKAGE_BUGREPORT); @@ -612,7 +639,7 @@ void usage() { int main(int argc, char **argv) { opterr = 0; while (1) { - int c = getopt(argc, argv, "lbmunsot:p"); + int c = getopt(argc, argv, "lbmunsot:pf:"); if (c == -1) break; switch (c) { @@ -643,6 +670,9 @@ int main(int argc, char **argv) { case 'p': use_cpu_percent = 1; break; + case 'f': + float_scale_factor = atol(optarg); + break; default: usage(); } @@ -665,7 +695,7 @@ int main(int argc, char **argv) { select_interesting(argc - optind, &argv[optind]); - /* We don't care if statgrab_init fails, because we can just display + /* We don't care if sg_init fails, because we can just display the statistics that can be read as non-root. */ sg_init(); if (sg_drop_privileges() != 0)