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.8 by ats, Fri Aug 29 06:56:12 2003 UTC vs.
Revision 1.20 by ats, Sat Feb 14 00:06:00 2004 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License
# Line 16 | Line 16
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + *
20 + * $Id$
21   */
22  
23   #ifdef HAVE_CONFIG_H
# Line 35 | Line 37 | typedef enum {
37          FLOAT,
38          DOUBLE,
39          STRING,
40 <        INT
40 >        INT,
41 >        BOOL,
42 >        DUPLEX
43   } stat_type;
44  
45   typedef enum {
# Line 140 | Line 144 | void add_stat(stat_type type, void *stat, ...) {
144          ++num_stats;
145   }
146  
147 < /* Compare two stats by name, for sorting purposes. */
147 > /* Compare two stats by name for qsort and bsearch. */
148   int stats_compare(const void *a, const void *b) {
149          return strcmp(((stat *)a)->name, ((stat *)b)->name);
150   }
151  
152 + /* Compare up to the length of the key for bsearch. */
153 + int stats_compare_prefix(const void *key, const void *item) {
154 +        const char *kn = ((stat *)key)->name;
155 +        const char *in = ((stat *)item)->name;
156 +
157 +        return strncmp(kn, in, strlen(kn));
158 + }
159 +
160   void populate_const() {
161          static int zero = 0;
162  
# Line 260 | Line 272 | void populate_fs() {
272          if (disk != NULL) {
273                  for (i = 0; i < n; i++) {
274                          /* FIXME it'd be nicer if libstatgrab did this */
275 <                        const char *name = disk[i].device_name,
276 <                                   *p = strrchr(name, '/');
277 <                        if (p != NULL)
278 <                                name = p + 1;
279 <                        if (*name == '\0')
280 <                                name = "root";
281 <        
275 >                        char *buf, *name, *p;
276 >                        const char *device = disk[i].device_name;
277 >
278 >                        if (strcmp(device, "/") == 0)
279 >                                device = "root";
280 >
281 >                        buf = strdup(device);
282 >                        if (buf == NULL)
283 >                                die("out of memory");
284 >
285 >                        name = buf;
286 >                        if (strlen(name) == 2 && name[1] == ':')
287 >                                name[1] = '\0';
288 >                        if (strncmp(name, "/dev/", 5) == 0)
289 >                                name += 5;
290 >                        while ((p = strchr(name, '/')) != NULL)
291 >                                *p = '_';
292 >
293                          add_stat(STRING, &disk[i].device_name,
294                                   "fs", name, "device_name", NULL);
295                          add_stat(STRING, &disk[i].fs_type,
# Line 285 | Line 308 | void populate_fs() {
308                                   "fs", name, "used_inodes", NULL);
309                          add_stat(LONG_LONG, &disk[i].free_inodes,
310                                   "fs", name, "free_inodes", NULL);
311 +
312 +                        free(buf);
313                  }
314          }
315   }
# Line 325 | Line 350 | void populate_proc() {
350   void populate_net() {
351          int n, i;
352          network_stat_t *net;
353 +        network_iface_stat_t *iface;
354  
355          net = use_diffs ? get_network_stats_diff(&n) : get_network_stats(&n);
356          if (net != NULL) {
# Line 341 | Line 367 | void populate_net() {
367                                   "net", name, "systime", NULL);
368                  }
369          }
370 +
371 +        iface = get_network_iface_stats(&n);
372 +        if (iface != NULL) {
373 +                for (i = 0; i < n; i++) {
374 +                        const char *name = iface[i].interface_name;
375 +
376 +                        add_stat(INT, &iface[i].speed,
377 +                                 "net", name, "speed", NULL);
378 +                        add_stat(BOOL, &iface[i].up,
379 +                                 "net", name, "up", NULL);
380 +                        add_stat(DUPLEX, &iface[i].dup,
381 +                                 "net", name, "duplex", NULL);
382 +                }
383 +        }
384   }
385  
386   void populate_page() {
# Line 350 | Line 390 | void populate_page() {
390          if (page != NULL) {
391                  add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL);
392                  add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL);
393 <                add_stat(LONG_LONG, &page->systime, "page", "systime", NULL);
393 >                add_stat(TIME_T, &page->systime, "page", "systime", NULL);
394          }
395   }
396  
# Line 437 | Line 477 | void print_stat_value(const stat *s) {
477          case INT:
478                  printf("%d", *(int *)v);
479                  break;
480 +        case BOOL:
481 +                printf("%s", *(int *)v ? "true" : "false");
482 +                break;
483 +        case DUPLEX:
484 +                switch (*(statgrab_duplex *) v) {
485 +                case FULL_DUPLEX:
486 +                        printf("full");
487 +                        break;
488 +                case HALF_DUPLEX:
489 +                        printf("half");
490 +                        break;
491 +                default:
492 +                        printf("unknown");
493 +                        break;
494 +                }
495 +                break;
496          }
497   }
498  
# Line 468 | Line 524 | void print_stats(int argc, char **argv) {
524          } else {
525                  /* Print selected stats. */
526                  for (i = optind; i < argc; i++) {
527 +                        char *name = argv[i];
528                          stat key;
529 <                        const stat *s;
529 >                        const stat *s, *end;
530 >                        int (*compare)(const void *, const void *);
531  
532 <                        key.name = argv[i];
532 >                        key.name = name;
533 >                        if (name[strlen(name) - 1] == '.')
534 >                                compare = stats_compare_prefix;
535 >                        else
536 >                                compare = stats_compare;
537 >
538                          s = (const stat *)bsearch(&key, stats, num_stats,
539 <                                                  sizeof *stats,
540 <                                                  stats_compare);
541 <                        if (s != NULL) {
539 >                                                  sizeof *stats, compare);
540 >                        if (s == NULL) {
541 >                                printf("Unknown stat %s\n", name);
542 >                                continue;
543 >                        }
544 >
545 >                        /* Find the range of stats the user wanted. */
546 >                        for (; s >= stats; s--) {
547 >                                if (compare(&key, s) != 0)
548 >                                        break;
549 >                        }
550 >                        s++;
551 >                        for (end = s; end < &stats[num_stats]; end++) {
552 >                                if (compare(&key, end) != 0)
553 >                                        break;
554 >                        }
555 >
556 >                        /* And print them. */
557 >                        for (; s < end; s++) {
558                                  print_stat(s);
559                          }
560                  }
# Line 484 | Line 563 | void print_stats(int argc, char **argv) {
563  
564   void usage() {
565          printf("Usage: statgrab [OPTION]... [STAT]...\n"
566 <               "Display system statistics (all statistics by default).\n"
566 >               "Display system statistics.\n"
567 >               "\n"
568 >               "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
569 >               "statistics starting with that prefix.\n"
570                 "\n");
571          printf("  -l         Linux sysctl-style output (default)\n"
572                 "  -b         BSD sysctl-style output\n"
# Line 557 | Line 639 | int main(int argc, char **argv) {
639                  use_diffs = 1;
640  
641          select_interesting(argc - optind, &argv[optind]);
642 +
643 +        /* We don't care if statgrab_init fails, because we can just display
644 +           the statistics that can be read as non-root. */
645 +        statgrab_init();
646 +        if (statgrab_drop_privileges() != 0)
647 +                die("Failed to drop setuid/setgid privileges");
648  
649          switch (repeat_mode) {
650          case REPEAT_NONE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines