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.26 by tdb, Wed Apr 7 15:50:26 2004 UTC vs.
Revision 1.35 by tdb, Wed Jul 13 13:01:24 2005 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 214 | Line 217 | void populate_mem() {
217          sg_mem_stats *mem = sg_get_mem_stats();
218  
219          if (mem != NULL) {
220 <                add_stat(LONG_LONG, &mem->total, "mem", "total", NULL);
221 <                add_stat(LONG_LONG, &mem->free, "mem", "free", NULL);
222 <                add_stat(LONG_LONG, &mem->used, "mem", "used", NULL);
223 <                add_stat(LONG_LONG, &mem->cache, "mem", "cache", NULL);
220 >                add_stat(BYTES, &mem->total, "mem", "total", NULL);
221 >                add_stat(BYTES, &mem->free, "mem", "free", NULL);
222 >                add_stat(BYTES, &mem->used, "mem", "used", NULL);
223 >                add_stat(BYTES, &mem->cache, "mem", "cache", NULL);
224          }
225   }
226  
# Line 244 | Line 247 | void populate_swap() {
247          sg_swap_stats *swap = sg_get_swap_stats();
248  
249          if (swap != NULL) {
250 <                add_stat(LONG_LONG, &swap->total, "swap", "total", NULL);
251 <                add_stat(LONG_LONG, &swap->used, "swap", "used", NULL);
252 <                add_stat(LONG_LONG, &swap->free, "swap", "free", NULL);
250 >                add_stat(BYTES, &swap->total, "swap", "total", NULL);
251 >                add_stat(BYTES, &swap->used, "swap", "used", NULL);
252 >                add_stat(BYTES, &swap->free, "swap", "free", NULL);
253          }
254   }
255  
# Line 298 | Line 301 | void populate_fs() {
301                                   "fs", name, "fs_type", NULL);
302                          add_stat(STRING, &disk[i].mnt_point,
303                                   "fs", name, "mnt_point", NULL);
304 <                        add_stat(LONG_LONG, &disk[i].size,
304 >                        add_stat(BYTES, &disk[i].size,
305                                   "fs", name, "size", NULL);
306 <                        add_stat(LONG_LONG, &disk[i].used,
306 >                        add_stat(BYTES, &disk[i].used,
307                                   "fs", name, "used", NULL);
308 <                        add_stat(LONG_LONG, &disk[i].avail,
308 >                        add_stat(BYTES, &disk[i].avail,
309                                   "fs", name, "avail", NULL);
310                          add_stat(LONG_LONG, &disk[i].total_inodes,
311                                   "fs", name, "total_inodes", NULL);
# Line 310 | Line 313 | void populate_fs() {
313                                   "fs", name, "used_inodes", NULL);
314                          add_stat(LONG_LONG, &disk[i].free_inodes,
315                                   "fs", name, "free_inodes", NULL);
316 +                        add_stat(LONG_LONG, &disk[i].avail_inodes,
317 +                                 "fs", name, "avail_inodes", NULL);
318 +                        add_stat(LONG_LONG, &disk[i].io_size,
319 +                                 "fs", name, "io_size", NULL);
320 +                        add_stat(LONG_LONG, &disk[i].block_size,
321 +                                 "fs", name, "block_size", NULL);
322 +                        add_stat(LONG_LONG, &disk[i].total_blocks,
323 +                                 "fs", name, "total_blocks", NULL);
324 +                        add_stat(LONG_LONG, &disk[i].free_blocks,
325 +                                 "fs", name, "free_blocks", NULL);
326 +                        add_stat(LONG_LONG, &disk[i].avail_blocks,
327 +                                 "fs", name, "avail_blocks", NULL);
328 +                        add_stat(LONG_LONG, &disk[i].used_blocks,
329 +                                 "fs", name, "used_blocks", NULL);
330  
331                          free(buf);
332                  }
# Line 328 | Line 345 | void populate_disk() {
345          
346                          add_stat(STRING, &diskio[i].disk_name,
347                                   "disk", name, "disk_name", NULL);
348 <                        add_stat(LONG_LONG, &diskio[i].read_bytes,
348 >                        add_stat(BYTES, &diskio[i].read_bytes,
349                                   "disk", name, "read_bytes", NULL);
350 <                        add_stat(LONG_LONG, &diskio[i].write_bytes,
350 >                        add_stat(BYTES, &diskio[i].write_bytes,
351                                   "disk", name, "write_bytes", NULL);
352                          add_stat(TIME_T, &diskio[i].systime,
353                                   "disk", name, "systime", NULL);
# Line 352 | Line 369 | void populate_proc() {
369   }
370  
371   void populate_net() {
372 <        int n, i;
372 >        int num_io, num_iface, i;
373          sg_network_io_stats *io;
374          sg_network_iface_stats *iface;
375  
376 <        io = use_diffs ? sg_get_network_io_stats_diff(&n)
377 <                       : sg_get_network_io_stats(&n);
376 >        io = use_diffs ? sg_get_network_io_stats_diff(&num_io)
377 >                       : sg_get_network_io_stats(&num_io);
378          if (io != NULL) {
379 <                for (i = 0; i < n; i++) {
379 >                for (i = 0; i < num_io; i++) {
380                          const char *name = io[i].interface_name;
381          
382                          add_stat(STRING, &io[i].interface_name,
383                                   "net", name, "interface_name", NULL);
384 <                        add_stat(LONG_LONG, &io[i].tx,
384 >                        add_stat(BYTES, &io[i].tx,
385                                   "net", name, "tx", NULL);
386 <                        add_stat(LONG_LONG, &io[i].rx,
386 >                        add_stat(BYTES, &io[i].rx,
387                                   "net", name, "rx", NULL);
388                          add_stat(LONG_LONG, &io[i].ipackets,
389                                   "net", name, "ipackets", NULL);
# Line 383 | Line 400 | void populate_net() {
400                  }
401          }
402  
403 <        iface = sg_get_network_iface_stats(&n);
403 >        iface = sg_get_network_iface_stats(&num_iface);
404          if (iface != NULL) {
405 <                for (i = 0; i < n; i++) {
405 >                for (i = 0; i < num_iface; i++) {
406                          const char *name = iface[i].interface_name;
407 +                        int had_io = 0, j;
408  
409 +                        /* If there wasn't a corresponding io stat,
410 +                           add interface_name from here. */
411 +                        if (io != NULL) {
412 +                                for (j = 0; j < num_io; j++) {
413 +                                        if (strcmp(io[j].interface_name,
414 +                                                   name) == 0) {
415 +                                                had_io = 1;
416 +                                                break;
417 +                                        }
418 +                                }
419 +                        }
420 +                        if (!had_io) {
421 +                                add_stat(STRING, &iface[i].interface_name,
422 +                                        "net", name, "interface_name", NULL);
423 +                        }
424 +
425                          add_stat(INT, &iface[i].speed,
426                                   "net", name, "speed", NULL);
427                          add_stat(BOOL, &iface[i].up,
428                                   "net", name, "up", NULL);
429 <                        add_stat(DUPLEX, &iface[i].dup,
429 >                        add_stat(DUPLEX, &iface[i].duplex,
430                                   "net", name, "duplex", NULL);
431                  }
432          }
# Line 471 | Line 505 | void get_stats() {
505   /* Print the value of a stat. */
506   void print_stat_value(const stat *s) {
507          void *v = s->stat;
508 <        long l;
508 >        double fv;
509 >        long lv;
510 >        long long llv;
511  
512          switch (s->type) {
513          case LONG_LONG:
514                  printf("%lld", *(long long *)v);
515                  break;
516 +        case BYTES:
517 +                llv = *(long long *)v;
518 +                if (bytes_scale_factor != 0) {
519 +                        llv /= bytes_scale_factor;
520 +                }
521 +                printf("%lld", llv);
522 +                break;
523          case TIME_T:
524                  /* FIXME option for formatted time? */
525 <                l = *(time_t *)v;
526 <                printf("%ld", l);
525 >                lv = *(time_t *)v;
526 >                printf("%ld", lv);
527                  break;
528          case FLOAT:
486                printf("%f", *(float *)v);
487                break;
529          case DOUBLE:
530 <                printf("%f", *(double *)v);
530 >                if (s->type == FLOAT) {
531 >                        fv = *(float *)v;
532 >                } else {
533 >                        fv = *(double *)v;
534 >                }
535 >                if (float_scale_factor != 0) {
536 >                        printf("%ld", (long)(float_scale_factor * fv));
537 >                } else {
538 >                        printf("%f", fv);
539 >                }
540                  break;
541          case STRING:
542                  /* FIXME escaping? */
# Line 593 | Line 643 | void usage() {
643                 "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
644                 "statistics starting with that prefix.\n"
645                 "\n");
646 <        printf("  -l     Linux sysctl-style output (default)\n"
647 <               "  -b     BSD sysctl-style output\n"
648 <               "  -m     MRTG-compatible output\n"
649 <               "  -u     Plain output (only show values)\n"
650 <               "  -n     Display cumulative stats once (default)\n"
651 <               "  -s     Display stat differences repeatedly\n"
652 <               "  -o     Display stat differences once\n"
646 >        printf("  -l         Linux sysctl-style output (default)\n"
647 >               "  -b         BSD sysctl-style output\n"
648 >               "  -m         MRTG-compatible output\n"
649 >               "  -u         Plain output (only show values)\n"
650 >               "  -n         Display cumulative stats once (default)\n"
651 >               "  -s         Display stat differences repeatedly\n"
652 >               "  -o         Display stat differences once\n"
653                 "  -t DELAY   When repeating, wait DELAY seconds between updates (default 1)\n"
654 <               "  -p     Display CPU usage differences as percentages rather than\n"
655 <               "             absolute values\n"
654 >               "  -p         Display CPU usage differences as percentages rather than\n"
655 >               "             absolute values\n"
656 >               "  -f FACTOR  Display floating-point values as integers scaled by FACTOR\n"
657 >               "  -K         Display byte counts in kibibytes\n"
658 >               "  -M         Display byte counts in mebibytes\n"
659 >               "  -G         Display byte counts in gibibytes\n"
660                 "\n");
661          printf("Version %s - report bugs to <%s>.\n",
662                 PACKAGE_VERSION, PACKAGE_BUGREPORT);
# Line 612 | Line 666 | void usage() {
666   int main(int argc, char **argv) {
667          opterr = 0;
668          while (1) {
669 <                int c = getopt(argc, argv, "lbmunsot:p");
669 >                int c = getopt(argc, argv, "lbmunsot:pf:KMG");
670                  if (c == -1)
671                          break;
672                  switch (c) {
# Line 643 | Line 697 | int main(int argc, char **argv) {
697                  case 'p':
698                          use_cpu_percent = 1;
699                          break;
700 +                case 'f':
701 +                        float_scale_factor = atol(optarg);
702 +                        break;
703 +                case 'K':
704 +                        bytes_scale_factor = 1024;
705 +                        break;
706 +                case 'M':
707 +                        bytes_scale_factor = 1024 * 1024;
708 +                        break;
709 +                case 'G':
710 +                        bytes_scale_factor = 1024 * 1024 * 1024;
711 +                        break;
712                  default:
713                          usage();
714                  }
# Line 665 | Line 731 | int main(int argc, char **argv) {
731  
732          select_interesting(argc - optind, &argv[optind]);
733  
734 <        /* We don't care if statgrab_init fails, because we can just display
734 >        /* We don't care if sg_init fails, because we can just display
735             the statistics that can be read as non-root. */
736          sg_init();
737          if (sg_drop_privileges() != 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines