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.25 by tdb, Tue Apr 6 14:53:00 2004 UTC vs.
Revision 1.29 by ats, Tue Aug 10 18:50:37 2004 UTC

# Line 71 | Line 71 | repeat_mode_type repeat_mode = REPEAT_NONE;
71   int repeat_time = 1;
72   int use_cpu_percent = 0;
73   int use_diffs = 0;
74 + long float_scale_factor = 0;
75  
76   /* Exit with an error message. */
77   void die(const char *s) {
# Line 170 | Line 171 | void populate_cpu() {
171  
172                  if (cpu_p != NULL) {
173                          add_stat(FLOAT, &cpu_p->user,
174 <                                 "cpu", "user", NULL);
174 >                                 "cpu", "user", NULL);
175                          add_stat(FLOAT, &cpu_p->kernel,
176 <                                 "cpu", "kernel", NULL);
176 >                                 "cpu", "kernel", NULL);
177                          add_stat(FLOAT, &cpu_p->idle,
178 <                                 "cpu", "idle", NULL);
178 >                                 "cpu", "idle", NULL);
179                          add_stat(FLOAT, &cpu_p->iowait,
180 <                                 "cpu", "iowait", NULL);
180 >                                 "cpu", "iowait", NULL);
181                          add_stat(FLOAT, &cpu_p->swap,
182 <                                 "cpu", "swap", NULL);
182 >                                 "cpu", "swap", NULL);
183                          add_stat(FLOAT, &cpu_p->nice,
184 <                                 "cpu", "nice", NULL);
184 >                                 "cpu", "nice", NULL);
185                          add_stat(TIME_T, &cpu_p->time_taken,
186 <                                 "cpu", "time_taken", NULL);
186 >                                 "cpu", "time_taken", NULL);
187                  }
188          } else {
189                  sg_cpu_stats *cpu_s;
190  
191                  cpu_s = use_diffs ? sg_get_cpu_stats_diff()
192 <                                  : sg_get_cpu_stats();
192 >                                  : sg_get_cpu_stats();
193                  if (cpu_s != NULL) {
194                          add_stat(LONG_LONG, &cpu_s->user,
195 <                                 "cpu", "user", NULL);
195 >                                 "cpu", "user", NULL);
196                          add_stat(LONG_LONG, &cpu_s->kernel,
197 <                                 "cpu", "kernel", NULL);
197 >                                 "cpu", "kernel", NULL);
198                          add_stat(LONG_LONG, &cpu_s->idle,
199 <                                 "cpu", "idle", NULL);
199 >                                 "cpu", "idle", NULL);
200                          add_stat(LONG_LONG, &cpu_s->iowait,
201 <                                 "cpu", "iowait", NULL);
201 >                                 "cpu", "iowait", NULL);
202                          add_stat(LONG_LONG, &cpu_s->swap,
203 <                                 "cpu", "swap", NULL);
203 >                                 "cpu", "swap", NULL);
204                          add_stat(LONG_LONG, &cpu_s->nice,
205 <                                 "cpu", "nice", NULL);
205 >                                 "cpu", "nice", NULL);
206                          add_stat(LONG_LONG, &cpu_s->total,
207 <                                 "cpu", "total", NULL);
207 >                                 "cpu", "total", NULL);
208                          add_stat(TIME_T, &cpu_s->systime,
209 <                                 "cpu", "systime", NULL);
209 >                                 "cpu", "systime", NULL);
210                  }
211          }
212   }
# Line 256 | Line 257 | void populate_general() {
257  
258          if (host != NULL) {
259                  add_stat(STRING, &host->os_name,
260 <                         "general", "os_name", NULL);
260 >                         "general", "os_name", NULL);
261                  add_stat(STRING, &host->os_release,
262 <                         "general", "os_release", NULL);
262 >                         "general", "os_release", NULL);
263                  add_stat(STRING, &host->os_version,
264 <                         "general", "os_version", NULL);
264 >                         "general", "os_version", NULL);
265                  add_stat(STRING, &host->platform, "general", "platform", NULL);
266                  add_stat(STRING, &host->hostname, "general", "hostname", NULL);
267                  add_stat(TIME_T, &host->uptime, "general", "uptime", NULL);
# Line 293 | Line 294 | void populate_fs() {
294                                  *p = '_';
295  
296                          add_stat(STRING, &disk[i].device_name,
297 <                                 "fs", name, "device_name", NULL);
297 >                                 "fs", name, "device_name", NULL);
298                          add_stat(STRING, &disk[i].fs_type,
299 <                                 "fs", name, "fs_type", NULL);
299 >                                 "fs", name, "fs_type", NULL);
300                          add_stat(STRING, &disk[i].mnt_point,
301 <                                 "fs", name, "mnt_point", NULL);
301 >                                 "fs", name, "mnt_point", NULL);
302                          add_stat(LONG_LONG, &disk[i].size,
303 <                                 "fs", name, "size", NULL);
303 >                                 "fs", name, "size", NULL);
304                          add_stat(LONG_LONG, &disk[i].used,
305 <                                 "fs", name, "used", NULL);
305 >                                 "fs", name, "used", NULL);
306                          add_stat(LONG_LONG, &disk[i].avail,
307 <                                 "fs", name, "avail", NULL);
307 >                                 "fs", name, "avail", NULL);
308                          add_stat(LONG_LONG, &disk[i].total_inodes,
309 <                                 "fs", name, "total_inodes", NULL);
309 >                                 "fs", name, "total_inodes", NULL);
310                          add_stat(LONG_LONG, &disk[i].used_inodes,
311 <                                 "fs", name, "used_inodes", NULL);
311 >                                 "fs", name, "used_inodes", NULL);
312                          add_stat(LONG_LONG, &disk[i].free_inodes,
313 <                                 "fs", name, "free_inodes", NULL);
313 >                                 "fs", name, "free_inodes", NULL);
314  
315                          free(buf);
316                  }
# Line 321 | Line 322 | void populate_disk() {
322          sg_disk_io_stats *diskio;
323  
324          diskio = use_diffs ? sg_get_disk_io_stats_diff(&n)
325 <                           : sg_get_disk_io_stats(&n);
325 >                           : sg_get_disk_io_stats(&n);
326          if (diskio != NULL) {
327                  for (i = 0; i < n; i++) {
328                          const char *name = diskio[i].disk_name;
329          
330                          add_stat(STRING, &diskio[i].disk_name,
331 <                                 "disk", name, "disk_name", NULL);
331 >                                 "disk", name, "disk_name", NULL);
332                          add_stat(LONG_LONG, &diskio[i].read_bytes,
333 <                                 "disk", name, "read_bytes", NULL);
333 >                                 "disk", name, "read_bytes", NULL);
334                          add_stat(LONG_LONG, &diskio[i].write_bytes,
335 <                                 "disk", name, "write_bytes", NULL);
335 >                                 "disk", name, "write_bytes", NULL);
336                          add_stat(TIME_T, &diskio[i].systime,
337 <                                 "disk", name, "systime", NULL);
337 >                                 "disk", name, "systime", NULL);
338                  }
339          }
340   }
# Line 352 | Line 353 | void populate_proc() {
353   }
354  
355   void populate_net() {
356 <        int n, i;
356 >        int num_io, num_iface, i;
357          sg_network_io_stats *io;
358          sg_network_iface_stats *iface;
359  
360 <        io = use_diffs ? sg_get_network_io_stats_diff(&n)
361 <                       : sg_get_network_io_stats(&n);
360 >        io = use_diffs ? sg_get_network_io_stats_diff(&num_io)
361 >                       : sg_get_network_io_stats(&num_io);
362          if (io != NULL) {
363 <                for (i = 0; i < n; i++) {
363 >                for (i = 0; i < num_io; i++) {
364                          const char *name = io[i].interface_name;
365          
366                          add_stat(STRING, &io[i].interface_name,
367 <                                 "net", name, "interface_name", NULL);
367 >                                 "net", name, "interface_name", NULL);
368                          add_stat(LONG_LONG, &io[i].tx,
369 <                                 "net", name, "tx", NULL);
369 >                                 "net", name, "tx", NULL);
370                          add_stat(LONG_LONG, &io[i].rx,
371 <                                 "net", name, "rx", NULL);
371 >                                 "net", name, "rx", NULL);
372                          add_stat(LONG_LONG, &io[i].ipackets,
373 <                                 "net", name, "ipackets", NULL);
373 >                                 "net", name, "ipackets", NULL);
374                          add_stat(LONG_LONG, &io[i].opackets,
375 <                                 "net", name, "opackets", NULL);
375 >                                 "net", name, "opackets", NULL);
376                          add_stat(LONG_LONG, &io[i].ierrors,
377 <                                 "net", name, "ierrors", NULL);
377 >                                 "net", name, "ierrors", NULL);
378                          add_stat(LONG_LONG, &io[i].oerrors,
379 <                                 "net", name, "oerrors", NULL);
379 >                                 "net", name, "oerrors", NULL);
380                          add_stat(LONG_LONG, &io[i].collisions,
381 <                                 "net", name, "collisions", NULL);
381 >                                 "net", name, "collisions", NULL);
382                          add_stat(TIME_T, &io[i].systime,
383 <                                 "net", name, "systime", NULL);
383 >                                 "net", name, "systime", NULL);
384                  }
385          }
386  
387 <        iface = sg_get_network_iface_stats(&n);
387 >        iface = sg_get_network_iface_stats(&num_iface);
388          if (iface != NULL) {
389 <                for (i = 0; i < n; i++) {
389 >                for (i = 0; i < num_iface; i++) {
390                          const char *name = iface[i].interface_name;
391 +                        int had_io = 0, j;
392  
393 +                        /* If there wasn't a corresponding io stat,
394 +                           add interface_name from here. */
395 +                        if (io != NULL) {
396 +                                for (j = 0; j < num_io; j++) {
397 +                                        if (strcmp(io[j].interface_name,
398 +                                                   name) == 0) {
399 +                                                had_io = 1;
400 +                                                break;
401 +                                        }
402 +                                }
403 +                        }
404 +                        if (!had_io) {
405 +                                add_stat(STRING, &iface[i].interface_name,
406 +                                        "net", name, "interface_name", NULL);
407 +                        }
408 +
409                          add_stat(INT, &iface[i].speed,
410 <                                 "net", name, "speed", NULL);
410 >                                 "net", name, "speed", NULL);
411                          add_stat(BOOL, &iface[i].up,
412 <                                 "net", name, "up", NULL);
412 >                                 "net", name, "up", NULL);
413                          add_stat(DUPLEX, &iface[i].dup,
414 <                                 "net", name, "duplex", NULL);
414 >                                 "net", name, "duplex", NULL);
415                  }
416          }
417   }
# Line 444 | Line 462 | void select_interesting(int argc, char **argv) {
462                  for (i = 0; i < argc; i++) {
463                          for (t = &toplevels[0]; t->name != NULL; t++) {
464                                  if (strncmp(argv[i], t->name,
465 <                                            strlen(t->name)) == 0) {
465 >                                            strlen(t->name)) == 0) {
466                                          t->interesting = 1;
467                                          break;
468                                  }
# Line 471 | Line 489 | void get_stats() {
489   /* Print the value of a stat. */
490   void print_stat_value(const stat *s) {
491          void *v = s->stat;
492 +        double fv;
493          long l;
494  
495          switch (s->type) {
# Line 483 | Line 502 | void print_stat_value(const stat *s) {
502                  printf("%ld", l);
503                  break;
504          case FLOAT:
486                printf("%f", *(float *)v);
487                break;
505          case DOUBLE:
506 <                printf("%f", *(double *)v);
506 >                if (s->type == FLOAT) {
507 >                        fv = *(float *)v;
508 >                } else {
509 >                        fv = *(double *)v;
510 >                }
511 >                if (float_scale_factor != 0) {
512 >                        printf("%ld", (long)(float_scale_factor * fv));
513 >                } else {
514 >                        printf("%f", fv);
515 >                }
516                  break;
517          case STRING:
518                  /* FIXME escaping? */
# Line 557 | Line 583 | void print_stats(int argc, char **argv) {
583                                  s = NULL;
584                          } else {
585                                  s = (const stat *)bsearch(&key, stats,
586 <                                                          num_stats,
587 <                                                          sizeof *stats,
588 <                                                          compare);
586 >                                                          num_stats,
587 >                                                          sizeof *stats,
588 >                                                          compare);
589                          }
590  
591                          if (s == NULL) {
# Line 593 | Line 619 | void usage() {
619                 "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
620                 "statistics starting with that prefix.\n"
621                 "\n");
622 <        printf("  -l         Linux sysctl-style output (default)\n"
623 <               "  -b         BSD sysctl-style output\n"
624 <               "  -m         MRTG-compatible output\n"
625 <               "  -u         Plain output (only show values)\n"
626 <               "  -n         Display cumulative stats once (default)\n"
627 <               "  -s         Display stat differences repeatedly\n"
628 <               "  -o         Display stat differences once\n"
622 >        printf("  -l     Linux sysctl-style output (default)\n"
623 >               "  -b     BSD sysctl-style output\n"
624 >               "  -m     MRTG-compatible output\n"
625 >               "  -u     Plain output (only show values)\n"
626 >               "  -n     Display cumulative stats once (default)\n"
627 >               "  -s     Display stat differences repeatedly\n"
628 >               "  -o     Display stat differences once\n"
629                 "  -t DELAY   When repeating, wait DELAY seconds between updates (default 1)\n"
630 <               "  -p         Display CPU usage differences as percentages rather than\n"
631 <               "             absolute values\n"
630 >               "  -p     Display CPU usage differences as percentages rather than\n"
631 >               "             absolute values\n"
632 >               "  -f SCALE   Display floating-point values as integers scaled by FACTOR\n"
633                 "\n");
634          printf("Version %s - report bugs to <%s>.\n",
635                 PACKAGE_VERSION, PACKAGE_BUGREPORT);
# Line 612 | Line 639 | void usage() {
639   int main(int argc, char **argv) {
640          opterr = 0;
641          while (1) {
642 <                int c = getopt(argc, argv, "lbmunsot:p");
642 >                int c = getopt(argc, argv, "lbmunsot:pf:");
643                  if (c == -1)
644                          break;
645                  switch (c) {
# Line 643 | Line 670 | int main(int argc, char **argv) {
670                  case 'p':
671                          use_cpu_percent = 1;
672                          break;
673 +                case 'f':
674 +                        float_scale_factor = atol(optarg);
675 +                        break;
676                  default:
677                          usage();
678                  }
# Line 665 | Line 695 | int main(int argc, char **argv) {
695  
696          select_interesting(argc - optind, &argv[optind]);
697  
698 <        /* We don't care if statgrab_init fails, because we can just display
698 >        /* We don't care if sg_init fails, because we can just display
699             the statistics that can be read as non-root. */
700          sg_init();
701          if (sg_drop_privileges() != 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines