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.7 by ats, Fri Aug 29 06:49:46 2003 UTC vs.
Revision 1.9 by ats, Sun Aug 31 13:23:19 2003 UTC

# Line 66 | Line 66 | display_mode_type display_mode = DISPLAY_LINUX;
66   repeat_mode_type repeat_mode = REPEAT_NONE;
67   int repeat_time = 1;
68   int use_cpu_percent = 0;
69 + int use_diffs = 0;
70  
71   /* Exit with an error message. */
72   void die(const char *s) {
# Line 139 | Line 140 | void add_stat(stat_type type, void *stat, ...) {
140          ++num_stats;
141   }
142  
143 < /* Compare two stats by name, for sorting purposes. */
143 > /* Compare two stats by name for qsort and bsearch. */
144   int stats_compare(const void *a, const void *b) {
145          return strcmp(((stat *)a)->name, ((stat *)b)->name);
146   }
147  
148 < /* Clear and rebuild the stats array. */
149 < void get_stats(int use_diffs) {
150 <        cpu_states_t *cpu_s;
151 <        cpu_percent_t *cpu_p;
151 <        mem_stat_t *mem;
152 <        load_stat_t *load;
153 <        user_stat_t *user;
154 <        swap_stat_t *swap;
155 <        general_stat_t *gen;
156 <        disk_stat_t *disk;
157 <        diskio_stat_t *diskio;
158 <        process_stat_t *proc;
159 <        network_stat_t *net;
160 <        page_stat_t *page;
161 <        static int zero = 0;
162 <        int n, i;
148 > /* Compare up to the length of the key for bsearch. */
149 > int stats_compare_prefix(const void *key, const void *item) {
150 >        const char *kn = ((stat *)key)->name;
151 >        const char *in = ((stat *)item)->name;
152  
153 <        clear_stats();
153 >        return strncmp(kn, in, strlen(kn));
154 > }
155  
156 + void populate_const() {
157 +        static int zero = 0;
158 +
159          /* Constants, for use with MRTG mode. */
160          add_stat(INT, &zero, "const", "0", NULL);
161 + }
162  
163 <        /* FIXME when only fetching some stats, it'd be more efficient to only
170 <           do the libstatgrab calls needed, rather than fetching everything. */
171 <
163 > void populate_cpu() {
164          if (use_cpu_percent) {
165 <                cpu_p = cpu_percent_usage();
165 >                cpu_percent_t *cpu_p = cpu_percent_usage();
166 >
167                  if (cpu_p != NULL) {
168                          add_stat(FLOAT, &cpu_p->user,
169                                   "cpu", "user", NULL);
# Line 188 | Line 181 | void get_stats(int use_diffs) {
181                                   "cpu", "time_taken", NULL);
182                  }
183          } else {
184 +                cpu_states_t *cpu_s;
185 +
186                  cpu_s = use_diffs ? get_cpu_diff() : get_cpu_totals();
187                  if (cpu_s != NULL) {
188                          add_stat(LONG_LONG, &cpu_s->user,
# Line 208 | Line 203 | void get_stats(int use_diffs) {
203                                   "cpu", "systime", NULL);
204                  }
205          }
206 + }
207  
208 <        mem = get_memory_stats();
208 > void populate_mem() {
209 >        mem_stat_t *mem = get_memory_stats();
210 >
211          if (mem != NULL) {
212                  add_stat(LONG_LONG, &mem->total, "mem", "total", NULL);
213                  add_stat(LONG_LONG, &mem->free, "mem", "free", NULL);
214                  add_stat(LONG_LONG, &mem->used, "mem", "used", NULL);
215                  add_stat(LONG_LONG, &mem->cache, "mem", "cache", NULL);
216          }
217 + }
218  
219 <        load = get_load_stats();
219 > void populate_load() {
220 >        load_stat_t *load = get_load_stats();
221 >
222          if (load != NULL) {
223                  add_stat(DOUBLE, &load->min1, "load", "min1", NULL);
224                  add_stat(DOUBLE, &load->min5, "load", "min5", NULL);
225                  add_stat(DOUBLE, &load->min15, "load", "min15", NULL);
226          }
227 + }
228  
229 <        user = get_user_stats();
229 > void populate_user() {
230 >        user_stat_t *user = get_user_stats();
231 >
232          if (user != NULL) {
233                  add_stat(INT, &user->num_entries, "user", "num", NULL);
234                  add_stat(STRING, &user->name_list, "user", "names", NULL);
235          }
236 + }
237  
238 <        swap = get_swap_stats();
238 > void populate_swap() {
239 >        swap_stat_t *swap = get_swap_stats();
240 >
241          if (swap != NULL) {
242                  add_stat(LONG_LONG, &swap->total, "swap", "total", NULL);
243                  add_stat(LONG_LONG, &swap->used, "swap", "used", NULL);
244                  add_stat(LONG_LONG, &swap->free, "swap", "free", NULL);
245          }
246 + }
247  
248 <        gen = get_general_stats();
248 > void populate_general() {
249 >        general_stat_t *gen = get_general_stats();
250 >
251          if (gen != NULL) {
252                  add_stat(STRING, &gen->os_name,
253                           "general", "os_name", NULL);
# Line 249 | Line 259 | void get_stats(int use_diffs) {
259                  add_stat(STRING, &gen->hostname, "general", "hostname", NULL);
260                  add_stat(TIME_T, &gen->uptime, "general", "uptime", NULL);
261          }
262 + }
263  
264 <        disk = get_disk_stats(&n);
264 > void populate_fs() {
265 >        int n, i;
266 >        disk_stat_t *disk = get_disk_stats(&n);
267 >
268          if (disk != NULL) {
269                  for (i = 0; i < n; i++) {
270                          /* FIXME it'd be nicer if libstatgrab did this */
# Line 281 | Line 295 | void get_stats(int use_diffs) {
295                                   "fs", name, "free_inodes", NULL);
296                  }
297          }
298 + }
299  
300 + void populate_disk() {
301 +        int n, i;
302 +        diskio_stat_t *diskio;
303 +
304          diskio = use_diffs ? get_diskio_stats_diff(&n) : get_diskio_stats(&n);
305          if (diskio != NULL) {
306                  for (i = 0; i < n; i++) {
# Line 297 | Line 316 | void get_stats(int use_diffs) {
316                                   "disk", name, "systime", NULL);
317                  }
318          }
319 + }
320  
321 <        proc = get_process_stats();
321 > void populate_proc() {
322 >        process_stat_t *proc = get_process_stats();
323 >
324          if (proc != NULL) {
325                  add_stat(INT, &proc->total, "proc", "total", NULL);
326                  add_stat(INT, &proc->running, "proc", "running", NULL);
# Line 306 | Line 328 | void get_stats(int use_diffs) {
328                  add_stat(INT, &proc->stopped, "proc", "stopped", NULL);
329                  add_stat(INT, &proc->zombie, "proc", "zombie", NULL);
330          }
331 + }
332  
333 + void populate_net() {
334 +        int n, i;
335 +        network_stat_t *net;
336 +
337          net = use_diffs ? get_network_stats_diff(&n) : get_network_stats(&n);
338          if (net != NULL) {
339                  for (i = 0; i < n; i++) {
# Line 322 | Line 349 | void get_stats(int use_diffs) {
349                                   "net", name, "systime", NULL);
350                  }
351          }
352 + }
353  
354 + void populate_page() {
355 +        page_stat_t *page;
356 +
357          page = use_diffs ? get_page_stats_diff() : get_page_stats();
358          if (page != NULL) {
359                  add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL);
360                  add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL);
361                  add_stat(LONG_LONG, &page->systime, "page", "systime", NULL);
362          }
363 + }
364  
365 + typedef struct {
366 +        const char *name;
367 +        void (*populate)();
368 +        int interesting;
369 + } toplevel;
370 + toplevel toplevels[] = {
371 +        {"const.", populate_const, 0},
372 +        {"cpu.", populate_cpu, 0},
373 +        {"mem.", populate_mem, 0},
374 +        {"load.", populate_load, 0},
375 +        {"user.", populate_user, 0},
376 +        {"swap.", populate_swap, 0},
377 +        {"general.", populate_general, 0},
378 +        {"fs.", populate_fs, 0},
379 +        {"disk.", populate_disk, 0},
380 +        {"proc.", populate_proc, 0},
381 +        {"net.", populate_net, 0},
382 +        {"page.", populate_page, 0},
383 +        {NULL, NULL, 0}
384 + };
385 +
386 + /* Set the "interesting" flag on the sections that we actually need to
387 +   fetch. */
388 + void select_interesting(int argc, char **argv) {
389 +        toplevel *t;
390 +
391 +        if (argc == 0) {
392 +                for (t = &toplevels[0]; t->name != NULL; t++)
393 +                        t->interesting = 1;
394 +        } else {
395 +                int i;
396 +
397 +                for (i = 0; i < argc; i++) {
398 +                        for (t = &toplevels[0]; t->name != NULL; t++) {
399 +                                if (strncmp(argv[i], t->name,
400 +                                            strlen(t->name)) == 0) {
401 +                                        t->interesting = 1;
402 +                                        break;
403 +                                }
404 +                        }
405 +                }
406 +        }
407 + }
408 +
409 + /* Clear and rebuild the stats array. */
410 + void get_stats() {
411 +        toplevel *t;
412 +
413 +        clear_stats();
414 +
415 +        for (t = &toplevels[0]; t->name != NULL; t++) {
416 +                if (t->interesting)
417 +                        t->populate();
418 +        }
419 +
420          qsort(stats, num_stats, sizeof *stats, stats_compare);
421   }
422  
# Line 389 | Line 476 | void print_stats(int argc, char **argv) {
476          } else {
477                  /* Print selected stats. */
478                  for (i = optind; i < argc; i++) {
479 +                        char *name = argv[i];
480                          stat key;
481 <                        const stat *s;
481 >                        const stat *s, *end;
482 >                        int (*compare)(const void *, const void *);
483  
484 <                        key.name = argv[i];
484 >                        key.name = name;
485 >                        if (name[strlen(name) - 1] == '.')
486 >                                compare = stats_compare_prefix;
487 >                        else
488 >                                compare = stats_compare;
489 >
490                          s = (const stat *)bsearch(&key, stats, num_stats,
491 <                                                  sizeof *stats,
492 <                                                  stats_compare);
493 <                        if (s != NULL) {
491 >                                                  sizeof *stats, compare);
492 >                        if (s == NULL) {
493 >                                printf("Unknown stat %s\n", name);
494 >                                continue;
495 >                        }
496 >
497 >                        /* Find the range of stats the user wanted. */
498 >                        for (; s >= stats; s--) {
499 >                                if (compare(&key, s) != 0)
500 >                                        break;
501 >                        }
502 >                        s++;
503 >                        for (end = s; end < &stats[num_stats]; end++) {
504 >                                if (compare(&key, end) != 0)
505 >                                        break;
506 >                        }
507 >
508 >                        /* And print them. */
509 >                        for (; s < end; s++) {
510                                  print_stat(s);
511                          }
512                  }
# Line 405 | Line 515 | void print_stats(int argc, char **argv) {
515  
516   void usage() {
517          printf("Usage: statgrab [OPTION]... [STAT]...\n"
518 <               "Display system statistics (all statistics by default).\n"
518 >               "Display system statistics.\n"
519 >               "\n"
520 >               "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
521 >               "statistics starting with that prefix.\n"
522                 "\n");
523          printf("  -l         Linux sysctl-style output (default)\n"
524                 "  -b         BSD sysctl-style output\n"
# Line 472 | Line 585 | int main(int argc, char **argv) {
585          if (use_cpu_percent && repeat_mode == REPEAT_NONE)
586                  die("CPU percentage usage display requires stat differences");
587  
588 +        if (repeat_mode == REPEAT_NONE)
589 +                use_diffs = 0;
590 +        else
591 +                use_diffs = 1;
592 +
593 +        select_interesting(argc - optind, &argv[optind]);
594 +
595          switch (repeat_mode) {
596          case REPEAT_NONE:
597 <                get_stats(0);
597 >                get_stats();
598                  print_stats(argc, argv);
599                  break;
600          case REPEAT_ONCE:
601 <                get_stats(1);
601 >                get_stats();
602                  sleep(repeat_time);
603 <                get_stats(1);
603 >                get_stats();
604                  print_stats(argc, argv);
605                  break;
606          case REPEAT_FOREVER:
607                  while (1) {
608 <                        get_stats(1);
608 >                        get_stats();
609                          print_stats(argc, argv);
610                          printf("\n");
611                          sleep(repeat_time);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines