ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/statgrab/statgrab.c
(Generate patch)

Comparing projects/libstatgrab/src/statgrab/statgrab.c (file contents):
Revision 1.27 by ats, Fri Jul 16 11:17:16 2004 UTC vs.
Revision 1.37 by ats, Fri Mar 17 13:23:05 2006 UTC

# Line 33 | Line 33
33  
34   typedef enum {
35          LONG_LONG = 0,
36 +        BYTES,
37          TIME_T,
38          FLOAT,
39          DOUBLE,
# Line 71 | Line 72 | repeat_mode_type repeat_mode = REPEAT_NONE;
72   int repeat_time = 1;
73   int use_cpu_percent = 0;
74   int use_diffs = 0;
75 + long float_scale_factor = 0;
76 + long long bytes_scale_factor = 0;
77  
78   /* Exit with an error message. */
79   void die(const char *s) {
# Line 118 | Line 121 | void add_stat(stat_type type, void *stat, ...) {
121                          break;
122                  partlen = strlen(part);
123                  memcpy(p, part, partlen);
124 <                p += partlen;
124 >
125 >                /* Replace spaces and dots with underscores. */
126 >                while (partlen-- > 0) {
127 >                        if (*p == ' ' || *p == '.')
128 >                                *p = '_';
129 >                        p++;
130 >                }
131 >
132                  *p++ = '.';
133          }
134          va_end(ap);
135          *--p = '\0';
136  
127        /* Replace spaces with underscores. */
128        for (p = name; *p != '\0'; p++) {
129                if (*p == ' ')
130                        *p = '_';
131        }
132
137          /* Stretch the stats array if necessary. */
138          if (num_stats >= alloc_stats) {
139                  alloc_stats += INCREMENT_STATS;
# Line 214 | Line 218 | void populate_mem() {
218          sg_mem_stats *mem = sg_get_mem_stats();
219  
220          if (mem != NULL) {
221 <                add_stat(LONG_LONG, &mem->total, "mem", "total", NULL);
222 <                add_stat(LONG_LONG, &mem->free, "mem", "free", NULL);
223 <                add_stat(LONG_LONG, &mem->used, "mem", "used", NULL);
224 <                add_stat(LONG_LONG, &mem->cache, "mem", "cache", NULL);
221 >                add_stat(BYTES, &mem->total, "mem", "total", NULL);
222 >                add_stat(BYTES, &mem->free, "mem", "free", NULL);
223 >                add_stat(BYTES, &mem->used, "mem", "used", NULL);
224 >                add_stat(BYTES, &mem->cache, "mem", "cache", NULL);
225          }
226   }
227  
# Line 244 | Line 248 | void populate_swap() {
248          sg_swap_stats *swap = sg_get_swap_stats();
249  
250          if (swap != NULL) {
251 <                add_stat(LONG_LONG, &swap->total, "swap", "total", NULL);
252 <                add_stat(LONG_LONG, &swap->used, "swap", "used", NULL);
253 <                add_stat(LONG_LONG, &swap->free, "swap", "free", NULL);
251 >                add_stat(BYTES, &swap->total, "swap", "total", NULL);
252 >                add_stat(BYTES, &swap->used, "swap", "used", NULL);
253 >                add_stat(BYTES, &swap->free, "swap", "free", NULL);
254          }
255   }
256  
# Line 298 | Line 302 | void populate_fs() {
302                                   "fs", name, "fs_type", NULL);
303                          add_stat(STRING, &disk[i].mnt_point,
304                                   "fs", name, "mnt_point", NULL);
305 <                        add_stat(LONG_LONG, &disk[i].size,
305 >                        add_stat(BYTES, &disk[i].size,
306                                   "fs", name, "size", NULL);
307 <                        add_stat(LONG_LONG, &disk[i].used,
307 >                        add_stat(BYTES, &disk[i].used,
308                                   "fs", name, "used", NULL);
309 <                        add_stat(LONG_LONG, &disk[i].avail,
309 >                        add_stat(BYTES, &disk[i].avail,
310                                   "fs", name, "avail", NULL);
311                          add_stat(LONG_LONG, &disk[i].total_inodes,
312                                   "fs", name, "total_inodes", NULL);
# Line 310 | Line 314 | void populate_fs() {
314                                   "fs", name, "used_inodes", NULL);
315                          add_stat(LONG_LONG, &disk[i].free_inodes,
316                                   "fs", name, "free_inodes", NULL);
317 +                        add_stat(LONG_LONG, &disk[i].avail_inodes,
318 +                                 "fs", name, "avail_inodes", NULL);
319 +                        add_stat(LONG_LONG, &disk[i].io_size,
320 +                                 "fs", name, "io_size", NULL);
321 +                        add_stat(LONG_LONG, &disk[i].block_size,
322 +                                 "fs", name, "block_size", NULL);
323 +                        add_stat(LONG_LONG, &disk[i].total_blocks,
324 +                                 "fs", name, "total_blocks", NULL);
325 +                        add_stat(LONG_LONG, &disk[i].free_blocks,
326 +                                 "fs", name, "free_blocks", NULL);
327 +                        add_stat(LONG_LONG, &disk[i].avail_blocks,
328 +                                 "fs", name, "avail_blocks", NULL);
329 +                        add_stat(LONG_LONG, &disk[i].used_blocks,
330 +                                 "fs", name, "used_blocks", NULL);
331  
332                          free(buf);
333                  }
# Line 328 | Line 346 | void populate_disk() {
346          
347                          add_stat(STRING, &diskio[i].disk_name,
348                                   "disk", name, "disk_name", NULL);
349 <                        add_stat(LONG_LONG, &diskio[i].read_bytes,
349 >                        add_stat(BYTES, &diskio[i].read_bytes,
350                                   "disk", name, "read_bytes", NULL);
351 <                        add_stat(LONG_LONG, &diskio[i].write_bytes,
351 >                        add_stat(BYTES, &diskio[i].write_bytes,
352                                   "disk", name, "write_bytes", NULL);
353                          add_stat(TIME_T, &diskio[i].systime,
354                                   "disk", name, "systime", NULL);
# Line 364 | Line 382 | void populate_net() {
382          
383                          add_stat(STRING, &io[i].interface_name,
384                                   "net", name, "interface_name", NULL);
385 <                        add_stat(LONG_LONG, &io[i].tx,
385 >                        add_stat(BYTES, &io[i].tx,
386                                   "net", name, "tx", NULL);
387 <                        add_stat(LONG_LONG, &io[i].rx,
387 >                        add_stat(BYTES, &io[i].rx,
388                                   "net", name, "rx", NULL);
389                          add_stat(LONG_LONG, &io[i].ipackets,
390                                   "net", name, "ipackets", NULL);
# Line 409 | Line 427 | void populate_net() {
427                                   "net", name, "speed", NULL);
428                          add_stat(BOOL, &iface[i].up,
429                                   "net", name, "up", NULL);
430 <                        add_stat(DUPLEX, &iface[i].dup,
430 >                        add_stat(DUPLEX, &iface[i].duplex,
431                                   "net", name, "duplex", NULL);
432                  }
433          }
# Line 488 | Line 506 | void get_stats() {
506   /* Print the value of a stat. */
507   void print_stat_value(const stat *s) {
508          void *v = s->stat;
509 <        long l;
509 >        double fv;
510 >        long lv;
511 >        long long llv;
512  
513          switch (s->type) {
514          case LONG_LONG:
515 + #ifdef WIN32 /* Windows printf does not understand %lld, so use %I64d instead */
516 +                printf("%I64d", *(long long *)v);
517 + #else
518                  printf("%lld", *(long long *)v);
519 + #endif
520                  break;
521 +        case BYTES:
522 +                llv = *(long long *)v;
523 +                if (bytes_scale_factor != 0) {
524 +                        llv /= bytes_scale_factor;
525 +                }
526 + #ifdef WIN32
527 +                printf("%I64d", llv);
528 + #else
529 +                printf("%lld", llv);
530 + #endif
531 +                break;
532          case TIME_T:
533                  /* FIXME option for formatted time? */
534 <                l = *(time_t *)v;
535 <                printf("%ld", l);
534 >                lv = *(time_t *)v;
535 >                printf("%ld", lv);
536                  break;
537          case FLOAT:
503                printf("%f", *(float *)v);
504                break;
538          case DOUBLE:
539 <                printf("%f", *(double *)v);
539 >                if (s->type == FLOAT) {
540 >                        fv = *(float *)v;
541 >                } else {
542 >                        fv = *(double *)v;
543 >                }
544 >                if (float_scale_factor != 0) {
545 >                        printf("%ld", (long)(float_scale_factor * fv));
546 >                } else {
547 >                        printf("%f", fv);
548 >                }
549                  break;
550          case STRING:
551                  /* FIXME escaping? */
# Line 610 | Line 652 | void usage() {
652                 "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
653                 "statistics starting with that prefix.\n"
654                 "\n");
655 <        printf("  -l     Linux sysctl-style output (default)\n"
656 <               "  -b     BSD sysctl-style output\n"
657 <               "  -m     MRTG-compatible output\n"
658 <               "  -u     Plain output (only show values)\n"
659 <               "  -n     Display cumulative stats once (default)\n"
660 <               "  -s     Display stat differences repeatedly\n"
661 <               "  -o     Display stat differences once\n"
655 >        printf("  -l         Linux sysctl-style output (default)\n"
656 >               "  -b         BSD sysctl-style output\n"
657 >               "  -m         MRTG-compatible output\n"
658 >               "  -u         Plain output (only show values)\n"
659 >               "  -n         Display cumulative stats once (default)\n"
660 >               "  -s         Display stat differences repeatedly\n"
661 >               "  -o         Display stat differences once\n"
662                 "  -t DELAY   When repeating, wait DELAY seconds between updates (default 1)\n"
663 <               "  -p     Display CPU usage differences as percentages rather than\n"
664 <               "             absolute values\n"
663 >               "  -p         Display CPU usage differences as percentages rather than\n"
664 >               "             absolute values\n"
665 >               "  -f FACTOR  Display floating-point values as integers scaled by FACTOR\n"
666 >               "  -K         Display byte counts in kibibytes\n"
667 >               "  -M         Display byte counts in mebibytes\n"
668 >               "  -G         Display byte counts in gibibytes\n"
669                 "\n");
670          printf("Version %s - report bugs to <%s>.\n",
671                 PACKAGE_VERSION, PACKAGE_BUGREPORT);
# Line 629 | Line 675 | void usage() {
675   int main(int argc, char **argv) {
676          opterr = 0;
677          while (1) {
678 <                int c = getopt(argc, argv, "lbmunsot:p");
678 >                int c = getopt(argc, argv, "lbmunsot:pf:KMG");
679                  if (c == -1)
680                          break;
681                  switch (c) {
# Line 660 | Line 706 | int main(int argc, char **argv) {
706                  case 'p':
707                          use_cpu_percent = 1;
708                          break;
709 +                case 'f':
710 +                        float_scale_factor = atol(optarg);
711 +                        break;
712 +                case 'K':
713 +                        bytes_scale_factor = 1024;
714 +                        break;
715 +                case 'M':
716 +                        bytes_scale_factor = 1024 * 1024;
717 +                        break;
718 +                case 'G':
719 +                        bytes_scale_factor = 1024 * 1024 * 1024;
720 +                        break;
721                  default:
722                          usage();
723                  }
# Line 682 | Line 740 | int main(int argc, char **argv) {
740  
741          select_interesting(argc - optind, &argv[optind]);
742  
743 <        /* We don't care if statgrab_init fails, because we can just display
743 >        /* We don't care if sg_init fails, because we can just display
744             the statistics that can be read as non-root. */
745          sg_init();
746 +        sg_snapshot();
747          if (sg_drop_privileges() != 0)
748                  die("Failed to drop setuid/setgid privileges");
749  
# Line 696 | Line 755 | int main(int argc, char **argv) {
755          case REPEAT_ONCE:
756                  get_stats();
757                  sleep(repeat_time);
758 +                sg_snapshot();
759                  get_stats();
760                  print_stats(argc, argv);
761                  break;
# Line 705 | Line 765 | int main(int argc, char **argv) {
765                          print_stats(argc, argv);
766                          printf("\n");
767                          sleep(repeat_time);
768 +                        sg_snapshot();
769                  }
770          }
771  
# Line 712 | Line 773 | int main(int argc, char **argv) {
773                  printf("\n");
774                  printf("statgrab\n");
775          }
776 +
777 +        sg_shutdown();
778  
779          return 0;
780   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines