--- projects/libstatgrab/src/statgrab/statgrab.c 2005/04/25 12:04:30 1.33 +++ projects/libstatgrab/src/statgrab/statgrab.c 2010/10/03 18:35:59 1.38 @@ -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.33 2005/04/25 12:04:30 tdb Exp $ + * $Id: statgrab.c,v 1.38 2010/10/03 18:35:59 tdb Exp $ */ #ifdef HAVE_CONFIG_H @@ -60,9 +60,13 @@ typedef struct { char *name; stat_type type; void *stat; -} stat; +} stat_item; +/* AIX: +statgrab.c:63: error: 'stat' redeclared as different kind of symbol +/usr/include/sys/stat.h:320: error: previous declaration of 'stat' was here + */ -stat *stats = NULL; +stat_item *stat_items = NULL; int num_stats = 0; int alloc_stats = 0; #define INCREMENT_STATS 64 @@ -86,7 +90,7 @@ void clear_stats() { int i; for (i = 0; i < num_stats; i++) - free(stats[i].name); + free(stat_items[i].name); num_stats = 0; } @@ -121,41 +125,42 @@ void add_stat(stat_type type, void *stat, ...) { break; partlen = strlen(part); memcpy(p, part, partlen); - p += partlen; + + /* Replace spaces and dots with underscores. */ + while (partlen-- > 0) { + if (*p == ' ' || *p == '.') + *p = '_'; + p++; + } + *p++ = '.'; } va_end(ap); *--p = '\0'; - /* Replace spaces with underscores. */ - for (p = name; *p != '\0'; p++) { - if (*p == ' ') - *p = '_'; - } - /* Stretch the stats array if necessary. */ if (num_stats >= alloc_stats) { alloc_stats += INCREMENT_STATS; - stats = realloc(stats, alloc_stats * sizeof *stats); - if (stats == NULL) + stat_items = realloc(stat_items, alloc_stats * sizeof *stat_items); + if (stat_items == NULL) die("out of memory"); } - stats[num_stats].name = name; - stats[num_stats].type = type; - stats[num_stats].stat = stat; + stat_items[num_stats].name = name; + stat_items[num_stats].type = type; + stat_items[num_stats].stat = stat; ++num_stats; } /* Compare two stats by name for qsort and bsearch. */ int stats_compare(const void *a, const void *b) { - return strcmp(((stat *)a)->name, ((stat *)b)->name); + return strcmp(((stat_item *)a)->name, ((stat_item *)b)->name); } /* Compare up to the length of the key for bsearch. */ int stats_compare_prefix(const void *key, const void *item) { - const char *kn = ((stat *)key)->name; - const char *in = ((stat *)item)->name; + const char *kn = ((stat_item *)key)->name; + const char *in = ((stat_item *)item)->name; return strncmp(kn, in, strlen(kn)); } @@ -313,6 +318,20 @@ void populate_fs() { "fs", name, "used_inodes", NULL); add_stat(LONG_LONG, &disk[i].free_inodes, "fs", name, "free_inodes", NULL); + add_stat(LONG_LONG, &disk[i].avail_inodes, + "fs", name, "avail_inodes", NULL); + add_stat(LONG_LONG, &disk[i].io_size, + "fs", name, "io_size", NULL); + add_stat(LONG_LONG, &disk[i].block_size, + "fs", name, "block_size", NULL); + add_stat(LONG_LONG, &disk[i].total_blocks, + "fs", name, "total_blocks", NULL); + add_stat(LONG_LONG, &disk[i].free_blocks, + "fs", name, "free_blocks", NULL); + add_stat(LONG_LONG, &disk[i].avail_blocks, + "fs", name, "avail_blocks", NULL); + add_stat(LONG_LONG, &disk[i].used_blocks, + "fs", name, "used_blocks", NULL); free(buf); } @@ -412,7 +431,7 @@ void populate_net() { "net", name, "speed", NULL); add_stat(BOOL, &iface[i].up, "net", name, "up", NULL); - add_stat(DUPLEX, &iface[i].dup, + add_stat(DUPLEX, &iface[i].duplex, "net", name, "duplex", NULL); } } @@ -473,7 +492,7 @@ void select_interesting(int argc, char **argv) { } } -/* Clear and rebuild the stats array. */ +/* Clear and rebuild the stat_items array. */ void get_stats() { toplevel *t; @@ -484,12 +503,12 @@ void get_stats() { t->populate(); } - if (stats != NULL) - qsort(stats, num_stats, sizeof *stats, stats_compare); + if (stat_items != NULL) + qsort(stat_items, num_stats, sizeof *stat_items, stats_compare); } -/* Print the value of a stat. */ -void print_stat_value(const stat *s) { +/* Print the value of a stat_item. */ +void print_stat_value(const stat_item *s) { void *v = s->stat; double fv; long lv; @@ -497,14 +516,22 @@ void print_stat_value(const stat *s) { switch (s->type) { case LONG_LONG: +#ifdef WIN32 /* Windows printf does not understand %lld, so use %I64d instead */ + printf("%I64d", *(long long *)v); +#else printf("%lld", *(long long *)v); +#endif break; case BYTES: llv = *(long long *)v; if (bytes_scale_factor != 0) { llv /= bytes_scale_factor; } +#ifdef WIN32 + printf("%I64d", llv); +#else printf("%lld", llv); +#endif break; case TIME_T: /* FIXME option for formatted time? */ @@ -551,7 +578,7 @@ void print_stat_value(const stat *s) { } /* Print the name and value of a stat. */ -void print_stat(const stat *s) { +void print_stat(const stat_item *s) { switch (display_mode) { case DISPLAY_LINUX: printf("%s = ", s->name); @@ -574,13 +601,13 @@ void print_stats(int argc, char **argv) { if (argc == optind) { /* Print all stats. */ for (i = 0; i < num_stats; i++) - print_stat(&stats[i]); + print_stat(&stat_items[i]); } else { /* Print selected stats. */ for (i = optind; i < argc; i++) { char *name = argv[i]; - stat key; - const stat *s, *end; + stat_item key; + const stat_item *s, *end; int (*compare)(const void *, const void *); key.name = name; @@ -589,12 +616,12 @@ void print_stats(int argc, char **argv) { else compare = stats_compare; - if (stats == NULL) { + if (stat_items == NULL) { s = NULL; } else { - s = (const stat *)bsearch(&key, stats, + s = (const stat_item *)bsearch(&key, stat_items, num_stats, - sizeof *stats, + sizeof *stat_items, compare); } @@ -604,12 +631,12 @@ void print_stats(int argc, char **argv) { } /* Find the range of stats the user wanted. */ - for (; s >= stats; s--) { + for (; s >= stat_items; s--) { if (compare(&key, s) != 0) break; } s++; - for (end = s; end < &stats[num_stats]; end++) { + for (end = s; end < &stat_items[num_stats]; end++) { if (compare(&key, end) != 0) break; } @@ -720,6 +747,7 @@ int main(int argc, char **argv) { /* We don't care if sg_init fails, because we can just display the statistics that can be read as non-root. */ sg_init(); + sg_snapshot(); if (sg_drop_privileges() != 0) die("Failed to drop setuid/setgid privileges"); @@ -731,6 +759,7 @@ int main(int argc, char **argv) { case REPEAT_ONCE: get_stats(); sleep(repeat_time); + sg_snapshot(); get_stats(); print_stats(argc, argv); break; @@ -740,6 +769,7 @@ int main(int argc, char **argv) { print_stats(argc, argv); printf("\n"); sleep(repeat_time); + sg_snapshot(); } } @@ -747,6 +777,8 @@ int main(int argc, char **argv) { printf("\n"); printf("statgrab\n"); } + + sg_shutdown(); return 0; }