33 |
|
|
34 |
|
typedef enum { |
35 |
|
LONG_LONG = 0, |
36 |
+ |
BYTES, |
37 |
|
TIME_T, |
38 |
|
FLOAT, |
39 |
|
DOUBLE, |
72 |
|
int repeat_time = 1; |
73 |
|
int use_cpu_percent = 0; |
74 |
|
int use_diffs = 0; |
75 |
+ |
long float_scale_factor = 0; |
76 |
+ |
long long bytes_scale_factor = 0; |
77 |
|
|
78 |
|
/* Exit with an error message. */ |
79 |
|
void die(const char *s) { |
217 |
|
sg_mem_stats *mem = sg_get_mem_stats(); |
218 |
|
|
219 |
|
if (mem != NULL) { |
220 |
< |
add_stat(LONG_LONG, &mem->total, "mem", "total", NULL); |
221 |
< |
add_stat(LONG_LONG, &mem->free, "mem", "free", NULL); |
222 |
< |
add_stat(LONG_LONG, &mem->used, "mem", "used", NULL); |
223 |
< |
add_stat(LONG_LONG, &mem->cache, "mem", "cache", NULL); |
220 |
> |
add_stat(BYTES, &mem->total, "mem", "total", NULL); |
221 |
> |
add_stat(BYTES, &mem->free, "mem", "free", NULL); |
222 |
> |
add_stat(BYTES, &mem->used, "mem", "used", NULL); |
223 |
> |
add_stat(BYTES, &mem->cache, "mem", "cache", NULL); |
224 |
|
} |
225 |
|
} |
226 |
|
|
247 |
|
sg_swap_stats *swap = sg_get_swap_stats(); |
248 |
|
|
249 |
|
if (swap != NULL) { |
250 |
< |
add_stat(LONG_LONG, &swap->total, "swap", "total", NULL); |
251 |
< |
add_stat(LONG_LONG, &swap->used, "swap", "used", NULL); |
252 |
< |
add_stat(LONG_LONG, &swap->free, "swap", "free", NULL); |
250 |
> |
add_stat(BYTES, &swap->total, "swap", "total", NULL); |
251 |
> |
add_stat(BYTES, &swap->used, "swap", "used", NULL); |
252 |
> |
add_stat(BYTES, &swap->free, "swap", "free", NULL); |
253 |
|
} |
254 |
|
} |
255 |
|
|
301 |
|
"fs", name, "fs_type", NULL); |
302 |
|
add_stat(STRING, &disk[i].mnt_point, |
303 |
|
"fs", name, "mnt_point", NULL); |
304 |
< |
add_stat(LONG_LONG, &disk[i].size, |
304 |
> |
add_stat(BYTES, &disk[i].size, |
305 |
|
"fs", name, "size", NULL); |
306 |
< |
add_stat(LONG_LONG, &disk[i].used, |
306 |
> |
add_stat(BYTES, &disk[i].used, |
307 |
|
"fs", name, "used", NULL); |
308 |
< |
add_stat(LONG_LONG, &disk[i].avail, |
308 |
> |
add_stat(BYTES, &disk[i].avail, |
309 |
|
"fs", name, "avail", NULL); |
310 |
|
add_stat(LONG_LONG, &disk[i].total_inodes, |
311 |
|
"fs", name, "total_inodes", NULL); |
331 |
|
|
332 |
|
add_stat(STRING, &diskio[i].disk_name, |
333 |
|
"disk", name, "disk_name", NULL); |
334 |
< |
add_stat(LONG_LONG, &diskio[i].read_bytes, |
334 |
> |
add_stat(BYTES, &diskio[i].read_bytes, |
335 |
|
"disk", name, "read_bytes", NULL); |
336 |
< |
add_stat(LONG_LONG, &diskio[i].write_bytes, |
336 |
> |
add_stat(BYTES, &diskio[i].write_bytes, |
337 |
|
"disk", name, "write_bytes", NULL); |
338 |
|
add_stat(TIME_T, &diskio[i].systime, |
339 |
|
"disk", name, "systime", NULL); |
355 |
|
} |
356 |
|
|
357 |
|
void populate_net() { |
358 |
< |
int n, i; |
358 |
> |
int num_io, num_iface, i; |
359 |
|
sg_network_io_stats *io; |
360 |
|
sg_network_iface_stats *iface; |
361 |
|
|
362 |
< |
io = use_diffs ? sg_get_network_io_stats_diff(&n) |
363 |
< |
: sg_get_network_io_stats(&n); |
362 |
> |
io = use_diffs ? sg_get_network_io_stats_diff(&num_io) |
363 |
> |
: sg_get_network_io_stats(&num_io); |
364 |
|
if (io != NULL) { |
365 |
< |
for (i = 0; i < n; i++) { |
365 |
> |
for (i = 0; i < num_io; i++) { |
366 |
|
const char *name = io[i].interface_name; |
367 |
|
|
368 |
|
add_stat(STRING, &io[i].interface_name, |
369 |
|
"net", name, "interface_name", NULL); |
370 |
< |
add_stat(LONG_LONG, &io[i].tx, |
370 |
> |
add_stat(BYTES, &io[i].tx, |
371 |
|
"net", name, "tx", NULL); |
372 |
< |
add_stat(LONG_LONG, &io[i].rx, |
372 |
> |
add_stat(BYTES, &io[i].rx, |
373 |
|
"net", name, "rx", NULL); |
374 |
|
add_stat(LONG_LONG, &io[i].ipackets, |
375 |
|
"net", name, "ipackets", NULL); |
386 |
|
} |
387 |
|
} |
388 |
|
|
389 |
< |
iface = sg_get_network_iface_stats(&n); |
389 |
> |
iface = sg_get_network_iface_stats(&num_iface); |
390 |
|
if (iface != NULL) { |
391 |
< |
for (i = 0; i < n; i++) { |
391 |
> |
for (i = 0; i < num_iface; i++) { |
392 |
|
const char *name = iface[i].interface_name; |
393 |
+ |
int had_io = 0, j; |
394 |
|
|
395 |
+ |
/* If there wasn't a corresponding io stat, |
396 |
+ |
add interface_name from here. */ |
397 |
+ |
if (io != NULL) { |
398 |
+ |
for (j = 0; j < num_io; j++) { |
399 |
+ |
if (strcmp(io[j].interface_name, |
400 |
+ |
name) == 0) { |
401 |
+ |
had_io = 1; |
402 |
+ |
break; |
403 |
+ |
} |
404 |
+ |
} |
405 |
+ |
} |
406 |
+ |
if (!had_io) { |
407 |
+ |
add_stat(STRING, &iface[i].interface_name, |
408 |
+ |
"net", name, "interface_name", NULL); |
409 |
+ |
} |
410 |
+ |
|
411 |
|
add_stat(INT, &iface[i].speed, |
412 |
|
"net", name, "speed", NULL); |
413 |
|
add_stat(BOOL, &iface[i].up, |
414 |
|
"net", name, "up", NULL); |
415 |
< |
add_stat(DUPLEX, &iface[i].dup, |
415 |
> |
add_stat(DUPLEX, &iface[i].duplex, |
416 |
|
"net", name, "duplex", NULL); |
417 |
|
} |
418 |
|
} |
491 |
|
/* Print the value of a stat. */ |
492 |
|
void print_stat_value(const stat *s) { |
493 |
|
void *v = s->stat; |
494 |
< |
long l; |
494 |
> |
double fv; |
495 |
> |
long lv; |
496 |
> |
long long llv; |
497 |
|
|
498 |
|
switch (s->type) { |
499 |
|
case LONG_LONG: |
500 |
|
printf("%lld", *(long long *)v); |
501 |
|
break; |
502 |
+ |
case BYTES: |
503 |
+ |
llv = *(long long *)v; |
504 |
+ |
if (bytes_scale_factor != 0) { |
505 |
+ |
llv /= bytes_scale_factor; |
506 |
+ |
} |
507 |
+ |
printf("%lld", llv); |
508 |
+ |
break; |
509 |
|
case TIME_T: |
510 |
|
/* FIXME option for formatted time? */ |
511 |
< |
l = *(time_t *)v; |
512 |
< |
printf("%ld", l); |
511 |
> |
lv = *(time_t *)v; |
512 |
> |
printf("%ld", lv); |
513 |
|
break; |
514 |
|
case FLOAT: |
486 |
– |
printf("%f", *(float *)v); |
487 |
– |
break; |
515 |
|
case DOUBLE: |
516 |
< |
printf("%f", *(double *)v); |
516 |
> |
if (s->type == FLOAT) { |
517 |
> |
fv = *(float *)v; |
518 |
> |
} else { |
519 |
> |
fv = *(double *)v; |
520 |
> |
} |
521 |
> |
if (float_scale_factor != 0) { |
522 |
> |
printf("%ld", (long)(float_scale_factor * fv)); |
523 |
> |
} else { |
524 |
> |
printf("%f", fv); |
525 |
> |
} |
526 |
|
break; |
527 |
|
case STRING: |
528 |
|
/* FIXME escaping? */ |
629 |
|
"If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n" |
630 |
|
"statistics starting with that prefix.\n" |
631 |
|
"\n"); |
632 |
< |
printf(" -l Linux sysctl-style output (default)\n" |
633 |
< |
" -b BSD sysctl-style output\n" |
634 |
< |
" -m MRTG-compatible output\n" |
635 |
< |
" -u Plain output (only show values)\n" |
636 |
< |
" -n Display cumulative stats once (default)\n" |
637 |
< |
" -s Display stat differences repeatedly\n" |
638 |
< |
" -o Display stat differences once\n" |
632 |
> |
printf(" -l Linux sysctl-style output (default)\n" |
633 |
> |
" -b BSD sysctl-style output\n" |
634 |
> |
" -m MRTG-compatible output\n" |
635 |
> |
" -u Plain output (only show values)\n" |
636 |
> |
" -n Display cumulative stats once (default)\n" |
637 |
> |
" -s Display stat differences repeatedly\n" |
638 |
> |
" -o Display stat differences once\n" |
639 |
|
" -t DELAY When repeating, wait DELAY seconds between updates (default 1)\n" |
640 |
< |
" -p Display CPU usage differences as percentages rather than\n" |
641 |
< |
" absolute values\n" |
640 |
> |
" -p Display CPU usage differences as percentages rather than\n" |
641 |
> |
" absolute values\n" |
642 |
> |
" -f FACTOR Display floating-point values as integers scaled by FACTOR\n" |
643 |
> |
" -K Display byte counts in kibibytes\n" |
644 |
> |
" -M Display byte counts in mebibytes\n" |
645 |
> |
" -G Display byte counts in gibibytes\n" |
646 |
|
"\n"); |
647 |
|
printf("Version %s - report bugs to <%s>.\n", |
648 |
|
PACKAGE_VERSION, PACKAGE_BUGREPORT); |
652 |
|
int main(int argc, char **argv) { |
653 |
|
opterr = 0; |
654 |
|
while (1) { |
655 |
< |
int c = getopt(argc, argv, "lbmunsot:p"); |
655 |
> |
int c = getopt(argc, argv, "lbmunsot:pf:KMG"); |
656 |
|
if (c == -1) |
657 |
|
break; |
658 |
|
switch (c) { |
683 |
|
case 'p': |
684 |
|
use_cpu_percent = 1; |
685 |
|
break; |
686 |
+ |
case 'f': |
687 |
+ |
float_scale_factor = atol(optarg); |
688 |
+ |
break; |
689 |
+ |
case 'K': |
690 |
+ |
bytes_scale_factor = 1024; |
691 |
+ |
break; |
692 |
+ |
case 'M': |
693 |
+ |
bytes_scale_factor = 1024 * 1024; |
694 |
+ |
break; |
695 |
+ |
case 'G': |
696 |
+ |
bytes_scale_factor = 1024 * 1024 * 1024; |
697 |
+ |
break; |
698 |
|
default: |
699 |
|
usage(); |
700 |
|
} |
717 |
|
|
718 |
|
select_interesting(argc - optind, &argv[optind]); |
719 |
|
|
720 |
< |
/* We don't care if statgrab_init fails, because we can just display |
720 |
> |
/* We don't care if sg_init fails, because we can just display |
721 |
|
the statistics that can be read as non-root. */ |
722 |
|
sg_init(); |
723 |
|
if (sg_drop_privileges() != 0) |