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.4 by tdb, Wed Aug 27 20:54:19 2003 UTC vs.
Revision 1.13 by ats, Mon Nov 10 23:35:43 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 184 | Line 177 | void get_stats(int use_diffs) {
177                                   "cpu", "swap", NULL);
178                          add_stat(FLOAT, &cpu_p->nice,
179                                   "cpu", "nice", NULL);
180 <                        add_stat(TIME_T, &cpu_s->systime,
180 >                        add_stat(TIME_T, &cpu_p->time_taken,
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 */
271 <                        const char *name = disk[i].device_name,
272 <                                   *p = strrchr(name, '/');
273 <                        if (p != NULL)
274 <                                name = p + 1;
275 <                        if (*name == '\0')
276 <                                name = "root";
277 <        
271 >                        char *buf, *name, *p;
272 >                        const char *device = disk[i].device_name;
273 >
274 >                        if (strcmp(device, "/") == 0)
275 >                                device = "root";
276 >
277 >                        buf = strdup(device);
278 >                        if (buf == NULL)
279 >                                die("out of memory");
280 >
281 >                        name = buf;
282 >                        if (strlen(name) == 2 && name[1] == ':')
283 >                                name[1] = '\0';
284 >                        if (strncmp(name, "/dev/", 5) == 0)
285 >                                name += 5;
286 >                        while ((p = strchr(name, '/')) != NULL)
287 >                                *p = '_';
288 >
289                          add_stat(STRING, &disk[i].device_name,
290                                   "fs", name, "device_name", NULL);
291                          add_stat(STRING, &disk[i].fs_type,
# Line 279 | Line 304 | void get_stats(int use_diffs) {
304                                   "fs", name, "used_inodes", NULL);
305                          add_stat(LONG_LONG, &disk[i].free_inodes,
306                                   "fs", name, "free_inodes", NULL);
307 +
308 +                        free(buf);
309                  }
310          }
311 + }
312  
313 + void populate_disk() {
314 +        int n, i;
315 +        diskio_stat_t *diskio;
316 +
317          diskio = use_diffs ? get_diskio_stats_diff(&n) : get_diskio_stats(&n);
318          if (diskio != NULL) {
319                  for (i = 0; i < n; i++) {
# Line 297 | Line 329 | void get_stats(int use_diffs) {
329                                   "disk", name, "systime", NULL);
330                  }
331          }
332 + }
333  
334 <        proc = get_process_stats();
334 > void populate_proc() {
335 >        process_stat_t *proc = get_process_stats();
336 >
337          if (proc != NULL) {
338                  add_stat(INT, &proc->total, "proc", "total", NULL);
339                  add_stat(INT, &proc->running, "proc", "running", NULL);
# Line 306 | Line 341 | void get_stats(int use_diffs) {
341                  add_stat(INT, &proc->stopped, "proc", "stopped", NULL);
342                  add_stat(INT, &proc->zombie, "proc", "zombie", NULL);
343          }
344 + }
345  
346 + void populate_net() {
347 +        int n, i;
348 +        network_stat_t *net;
349 +
350          net = use_diffs ? get_network_stats_diff(&n) : get_network_stats(&n);
351          if (net != NULL) {
352                  for (i = 0; i < n; i++) {
# Line 322 | Line 362 | void get_stats(int use_diffs) {
362                                   "net", name, "systime", NULL);
363                  }
364          }
365 + }
366  
367 + void populate_page() {
368 +        page_stat_t *page;
369 +
370          page = use_diffs ? get_page_stats_diff() : get_page_stats();
371          if (page != NULL) {
372                  add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL);
373                  add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL);
374 <                add_stat(LONG_LONG, &page->systime, "page", "systime", NULL);
374 >                add_stat(TIME_T, &page->systime, "page", "systime", NULL);
375          }
376 + }
377  
378 + typedef struct {
379 +        const char *name;
380 +        void (*populate)();
381 +        int interesting;
382 + } toplevel;
383 + toplevel toplevels[] = {
384 +        {"const.", populate_const, 0},
385 +        {"cpu.", populate_cpu, 0},
386 +        {"mem.", populate_mem, 0},
387 +        {"load.", populate_load, 0},
388 +        {"user.", populate_user, 0},
389 +        {"swap.", populate_swap, 0},
390 +        {"general.", populate_general, 0},
391 +        {"fs.", populate_fs, 0},
392 +        {"disk.", populate_disk, 0},
393 +        {"proc.", populate_proc, 0},
394 +        {"net.", populate_net, 0},
395 +        {"page.", populate_page, 0},
396 +        {NULL, NULL, 0}
397 + };
398 +
399 + /* Set the "interesting" flag on the sections that we actually need to
400 +   fetch. */
401 + void select_interesting(int argc, char **argv) {
402 +        toplevel *t;
403 +
404 +        if (argc == 0) {
405 +                for (t = &toplevels[0]; t->name != NULL; t++)
406 +                        t->interesting = 1;
407 +        } else {
408 +                int i;
409 +
410 +                for (i = 0; i < argc; i++) {
411 +                        for (t = &toplevels[0]; t->name != NULL; t++) {
412 +                                if (strncmp(argv[i], t->name,
413 +                                            strlen(t->name)) == 0) {
414 +                                        t->interesting = 1;
415 +                                        break;
416 +                                }
417 +                        }
418 +                }
419 +        }
420 + }
421 +
422 + /* Clear and rebuild the stats array. */
423 + void get_stats() {
424 +        toplevel *t;
425 +
426 +        clear_stats();
427 +
428 +        for (t = &toplevels[0]; t->name != NULL; t++) {
429 +                if (t->interesting)
430 +                        t->populate();
431 +        }
432 +
433          qsort(stats, num_stats, sizeof *stats, stats_compare);
434   }
435  
# Line 389 | Line 489 | void print_stats(int argc, char **argv) {
489          } else {
490                  /* Print selected stats. */
491                  for (i = optind; i < argc; i++) {
492 +                        char *name = argv[i];
493                          stat key;
494 <                        const stat *s;
494 >                        const stat *s, *end;
495 >                        int (*compare)(const void *, const void *);
496  
497 <                        key.name = argv[i];
497 >                        key.name = name;
498 >                        if (name[strlen(name) - 1] == '.')
499 >                                compare = stats_compare_prefix;
500 >                        else
501 >                                compare = stats_compare;
502 >
503                          s = (const stat *)bsearch(&key, stats, num_stats,
504 <                                                  sizeof *stats,
505 <                                                  stats_compare);
506 <                        if (s != NULL) {
504 >                                                  sizeof *stats, compare);
505 >                        if (s == NULL) {
506 >                                printf("Unknown stat %s\n", name);
507 >                                continue;
508 >                        }
509 >
510 >                        /* Find the range of stats the user wanted. */
511 >                        for (; s >= stats; s--) {
512 >                                if (compare(&key, s) != 0)
513 >                                        break;
514 >                        }
515 >                        s++;
516 >                        for (end = s; end < &stats[num_stats]; end++) {
517 >                                if (compare(&key, end) != 0)
518 >                                        break;
519 >                        }
520 >
521 >                        /* And print them. */
522 >                        for (; s < end; s++) {
523                                  print_stat(s);
524                          }
525                  }
# Line 405 | Line 528 | void print_stats(int argc, char **argv) {
528  
529   void usage() {
530          printf("Usage: statgrab [OPTION]... [STAT]...\n"
531 <               "Display system statistics (all statistics by default).\n"
531 >               "Display system statistics.\n"
532 >               "\n"
533 >               "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
534 >               "statistics starting with that prefix.\n"
535                 "\n");
536          printf("  -l         Linux sysctl-style output (default)\n"
537                 "  -b         BSD sysctl-style output\n"
# Line 415 | Line 541 | void usage() {
541                 "  -s         Display stat differences repeatedly\n"
542                 "  -o         Display stat differences once\n"
543                 "  -t DELAY   When repeating, wait DELAY seconds between updates (default 1)\n"
544 <               "  -p         Display CPU usage as percentages rather than absolute values\n"
544 >               "  -p         Display CPU usage differences as percentages rather than\n"
545 >               "             absolute values\n"
546                 "\n");
547          printf("Version %s - report bugs to <%s>.\n",
548 <                PACKAGE_VERSION, PACKAGE_BUGREPORT);
548 >               PACKAGE_VERSION, PACKAGE_BUGREPORT);
549          exit(1);
550   }
551  
# Line 464 | Line 591 | int main(int argc, char **argv) {
591          if (display_mode == DISPLAY_MRTG) {
592                  if ((argc - optind) != 2)
593                          die("mrtg mode: must specify exactly two stats");
594 <                if (repeat_mode != REPEAT_NONE)
594 >                if (repeat_mode == REPEAT_FOREVER)
595                          die("mrtg mode: cannot repeat display");
596          }
597  
598 +        if (use_cpu_percent && repeat_mode == REPEAT_NONE)
599 +                die("CPU percentage usage display requires stat differences");
600 +
601 +        if (repeat_mode == REPEAT_NONE)
602 +                use_diffs = 0;
603 +        else
604 +                use_diffs = 1;
605 +
606 +        select_interesting(argc - optind, &argv[optind]);
607 +
608 +        /* We don't care if statgrab_init fails, because we can just display
609 +           the statistics that can be read as non-root. */
610 +        statgrab_init();
611 + #ifdef ALLBSD
612 +        if (setegid(getgid()) != 0)
613 +                die("Failed to lose effective group");
614 + #endif
615 +
616          switch (repeat_mode) {
617          case REPEAT_NONE:
618 <                get_stats(0);
618 >                get_stats();
619                  print_stats(argc, argv);
620                  break;
621          case REPEAT_ONCE:
622 <                get_stats(1);
622 >                get_stats();
623                  sleep(repeat_time);
624 <                get_stats(1);
624 >                get_stats();
625                  print_stats(argc, argv);
626                  break;
627          case REPEAT_FOREVER:
628                  while (1) {
629 <                        get_stats(1);
629 >                        get_stats();
630                          print_stats(argc, argv);
631                          printf("\n");
632                          sleep(repeat_time);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines