| 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 |
| 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 |
| 37 |
|
FLOAT, |
| 38 |
|
DOUBLE, |
| 39 |
|
STRING, |
| 40 |
< |
INT |
| 40 |
> |
INT, |
| 41 |
> |
BOOL, |
| 42 |
> |
DUPLEX |
| 43 |
|
} stat_type; |
| 44 |
|
|
| 45 |
|
typedef enum { |
| 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 |
|
|
| 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, |
| 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 |
|
} |
| 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) { |
| 363 |
|
"net", name, "tx", NULL); |
| 364 |
|
add_stat(LONG_LONG, &net[i].rx, |
| 365 |
|
"net", name, "rx", NULL); |
| 366 |
+ |
add_stat(LONG_LONG, &net[i].ipackets, |
| 367 |
+ |
"net", name, "ipackets", NULL); |
| 368 |
+ |
add_stat(LONG_LONG, &net[i].opackets, |
| 369 |
+ |
"net", name, "opackets", NULL); |
| 370 |
+ |
add_stat(LONG_LONG, &net[i].ierrors, |
| 371 |
+ |
"net", name, "ierrors", NULL); |
| 372 |
+ |
add_stat(LONG_LONG, &net[i].oerrors, |
| 373 |
+ |
"net", name, "oerrors", NULL); |
| 374 |
+ |
add_stat(LONG_LONG, &net[i].collisions, |
| 375 |
+ |
"net", name, "collisions", NULL); |
| 376 |
|
add_stat(TIME_T, &net[i].systime, |
| 377 |
|
"net", name, "systime", NULL); |
| 378 |
|
} |
| 379 |
|
} |
| 380 |
+ |
|
| 381 |
+ |
iface = get_network_iface_stats(&n); |
| 382 |
+ |
if (iface != NULL) { |
| 383 |
+ |
for (i = 0; i < n; i++) { |
| 384 |
+ |
const char *name = iface[i].interface_name; |
| 385 |
+ |
|
| 386 |
+ |
add_stat(INT, &iface[i].speed, |
| 387 |
+ |
"net", name, "speed", NULL); |
| 388 |
+ |
add_stat(BOOL, &iface[i].up, |
| 389 |
+ |
"net", name, "up", NULL); |
| 390 |
+ |
add_stat(DUPLEX, &iface[i].dup, |
| 391 |
+ |
"net", name, "duplex", NULL); |
| 392 |
+ |
} |
| 393 |
+ |
} |
| 394 |
|
} |
| 395 |
|
|
| 396 |
|
void populate_page() { |
| 400 |
|
if (page != NULL) { |
| 401 |
|
add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL); |
| 402 |
|
add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL); |
| 403 |
< |
add_stat(LONG_LONG, &page->systime, "page", "systime", NULL); |
| 403 |
> |
add_stat(TIME_T, &page->systime, "page", "systime", NULL); |
| 404 |
|
} |
| 405 |
|
} |
| 406 |
|
|
| 459 |
|
t->populate(); |
| 460 |
|
} |
| 461 |
|
|
| 462 |
< |
qsort(stats, num_stats, sizeof *stats, stats_compare); |
| 462 |
> |
if (stats != NULL) |
| 463 |
> |
qsort(stats, num_stats, sizeof *stats, stats_compare); |
| 464 |
|
} |
| 465 |
|
|
| 466 |
|
/* Print the value of a stat. */ |
| 467 |
|
void print_stat_value(const stat *s) { |
| 468 |
|
void *v = s->stat; |
| 469 |
+ |
long l; |
| 470 |
|
|
| 471 |
|
switch (s->type) { |
| 472 |
|
case LONG_LONG: |
| 474 |
|
break; |
| 475 |
|
case TIME_T: |
| 476 |
|
/* FIXME option for formatted time? */ |
| 477 |
< |
printf("%ld", *(time_t *)v); |
| 477 |
> |
l = *(time_t *)v; |
| 478 |
> |
printf("%ld", l); |
| 479 |
|
break; |
| 480 |
|
case FLOAT: |
| 481 |
|
printf("%f", *(float *)v); |
| 490 |
|
case INT: |
| 491 |
|
printf("%d", *(int *)v); |
| 492 |
|
break; |
| 493 |
+ |
case BOOL: |
| 494 |
+ |
printf("%s", *(int *)v ? "true" : "false"); |
| 495 |
+ |
break; |
| 496 |
+ |
case DUPLEX: |
| 497 |
+ |
switch (*(statgrab_duplex *) v) { |
| 498 |
+ |
case FULL_DUPLEX: |
| 499 |
+ |
printf("full"); |
| 500 |
+ |
break; |
| 501 |
+ |
case HALF_DUPLEX: |
| 502 |
+ |
printf("half"); |
| 503 |
+ |
break; |
| 504 |
+ |
default: |
| 505 |
+ |
printf("unknown"); |
| 506 |
+ |
break; |
| 507 |
+ |
} |
| 508 |
+ |
break; |
| 509 |
|
} |
| 510 |
|
} |
| 511 |
|
|
| 537 |
|
} else { |
| 538 |
|
/* Print selected stats. */ |
| 539 |
|
for (i = optind; i < argc; i++) { |
| 540 |
+ |
char *name = argv[i]; |
| 541 |
|
stat key; |
| 542 |
< |
const stat *s; |
| 542 |
> |
const stat *s, *end; |
| 543 |
> |
int (*compare)(const void *, const void *); |
| 544 |
|
|
| 545 |
< |
key.name = argv[i]; |
| 546 |
< |
s = (const stat *)bsearch(&key, stats, num_stats, |
| 547 |
< |
sizeof *stats, |
| 548 |
< |
stats_compare); |
| 549 |
< |
if (s != NULL) { |
| 545 |
> |
key.name = name; |
| 546 |
> |
if (name[strlen(name) - 1] == '.') |
| 547 |
> |
compare = stats_compare_prefix; |
| 548 |
> |
else |
| 549 |
> |
compare = stats_compare; |
| 550 |
> |
|
| 551 |
> |
if (stats == NULL) { |
| 552 |
> |
s = NULL; |
| 553 |
> |
} else { |
| 554 |
> |
s = (const stat *)bsearch(&key, stats, |
| 555 |
> |
num_stats, |
| 556 |
> |
sizeof *stats, |
| 557 |
> |
compare); |
| 558 |
> |
} |
| 559 |
> |
|
| 560 |
> |
if (s == NULL) { |
| 561 |
> |
printf("Unknown stat %s\n", name); |
| 562 |
> |
continue; |
| 563 |
> |
} |
| 564 |
> |
|
| 565 |
> |
/* Find the range of stats the user wanted. */ |
| 566 |
> |
for (; s >= stats; s--) { |
| 567 |
> |
if (compare(&key, s) != 0) |
| 568 |
> |
break; |
| 569 |
> |
} |
| 570 |
> |
s++; |
| 571 |
> |
for (end = s; end < &stats[num_stats]; end++) { |
| 572 |
> |
if (compare(&key, end) != 0) |
| 573 |
> |
break; |
| 574 |
> |
} |
| 575 |
> |
|
| 576 |
> |
/* And print them. */ |
| 577 |
> |
for (; s < end; s++) { |
| 578 |
|
print_stat(s); |
| 579 |
|
} |
| 580 |
|
} |
| 583 |
|
|
| 584 |
|
void usage() { |
| 585 |
|
printf("Usage: statgrab [OPTION]... [STAT]...\n" |
| 586 |
< |
"Display system statistics (all statistics by default).\n" |
| 586 |
> |
"Display system statistics.\n" |
| 587 |
> |
"\n" |
| 588 |
> |
"If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n" |
| 589 |
> |
"statistics starting with that prefix.\n" |
| 590 |
|
"\n"); |
| 591 |
|
printf(" -l Linux sysctl-style output (default)\n" |
| 592 |
|
" -b BSD sysctl-style output\n" |
| 659 |
|
use_diffs = 1; |
| 660 |
|
|
| 661 |
|
select_interesting(argc - optind, &argv[optind]); |
| 662 |
+ |
|
| 663 |
+ |
/* We don't care if statgrab_init fails, because we can just display |
| 664 |
+ |
the statistics that can be read as non-root. */ |
| 665 |
+ |
statgrab_init(); |
| 666 |
+ |
if (statgrab_drop_privileges() != 0) |
| 667 |
+ |
die("Failed to drop setuid/setgid privileges"); |
| 668 |
|
|
| 669 |
|
switch (repeat_mode) { |
| 670 |
|
case REPEAT_NONE: |