1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
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) { |
173 |
|
|
174 |
|
if (cpu_p != NULL) { |
175 |
|
add_stat(FLOAT, &cpu_p->user, |
176 |
< |
"cpu", "user", NULL); |
176 |
> |
"cpu", "user", NULL); |
177 |
|
add_stat(FLOAT, &cpu_p->kernel, |
178 |
< |
"cpu", "kernel", NULL); |
178 |
> |
"cpu", "kernel", NULL); |
179 |
|
add_stat(FLOAT, &cpu_p->idle, |
180 |
< |
"cpu", "idle", NULL); |
180 |
> |
"cpu", "idle", NULL); |
181 |
|
add_stat(FLOAT, &cpu_p->iowait, |
182 |
< |
"cpu", "iowait", NULL); |
182 |
> |
"cpu", "iowait", NULL); |
183 |
|
add_stat(FLOAT, &cpu_p->swap, |
184 |
< |
"cpu", "swap", NULL); |
184 |
> |
"cpu", "swap", NULL); |
185 |
|
add_stat(FLOAT, &cpu_p->nice, |
186 |
< |
"cpu", "nice", NULL); |
186 |
> |
"cpu", "nice", NULL); |
187 |
|
add_stat(TIME_T, &cpu_p->time_taken, |
188 |
< |
"cpu", "time_taken", NULL); |
188 |
> |
"cpu", "time_taken", NULL); |
189 |
|
} |
190 |
|
} else { |
191 |
|
sg_cpu_stats *cpu_s; |
192 |
|
|
193 |
|
cpu_s = use_diffs ? sg_get_cpu_stats_diff() |
194 |
< |
: sg_get_cpu_stats(); |
194 |
> |
: sg_get_cpu_stats(); |
195 |
|
if (cpu_s != NULL) { |
196 |
|
add_stat(LONG_LONG, &cpu_s->user, |
197 |
< |
"cpu", "user", NULL); |
197 |
> |
"cpu", "user", NULL); |
198 |
|
add_stat(LONG_LONG, &cpu_s->kernel, |
199 |
< |
"cpu", "kernel", NULL); |
199 |
> |
"cpu", "kernel", NULL); |
200 |
|
add_stat(LONG_LONG, &cpu_s->idle, |
201 |
< |
"cpu", "idle", NULL); |
201 |
> |
"cpu", "idle", NULL); |
202 |
|
add_stat(LONG_LONG, &cpu_s->iowait, |
203 |
< |
"cpu", "iowait", NULL); |
203 |
> |
"cpu", "iowait", NULL); |
204 |
|
add_stat(LONG_LONG, &cpu_s->swap, |
205 |
< |
"cpu", "swap", NULL); |
205 |
> |
"cpu", "swap", NULL); |
206 |
|
add_stat(LONG_LONG, &cpu_s->nice, |
207 |
< |
"cpu", "nice", NULL); |
207 |
> |
"cpu", "nice", NULL); |
208 |
|
add_stat(LONG_LONG, &cpu_s->total, |
209 |
< |
"cpu", "total", NULL); |
209 |
> |
"cpu", "total", NULL); |
210 |
|
add_stat(TIME_T, &cpu_s->systime, |
211 |
< |
"cpu", "systime", NULL); |
211 |
> |
"cpu", "systime", NULL); |
212 |
|
} |
213 |
|
} |
214 |
|
} |
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 |
|
|
259 |
|
|
260 |
|
if (host != NULL) { |
261 |
|
add_stat(STRING, &host->os_name, |
262 |
< |
"general", "os_name", NULL); |
262 |
> |
"general", "os_name", NULL); |
263 |
|
add_stat(STRING, &host->os_release, |
264 |
< |
"general", "os_release", NULL); |
264 |
> |
"general", "os_release", NULL); |
265 |
|
add_stat(STRING, &host->os_version, |
266 |
< |
"general", "os_version", NULL); |
266 |
> |
"general", "os_version", NULL); |
267 |
|
add_stat(STRING, &host->platform, "general", "platform", NULL); |
268 |
|
add_stat(STRING, &host->hostname, "general", "hostname", NULL); |
269 |
|
add_stat(TIME_T, &host->uptime, "general", "uptime", NULL); |
296 |
|
*p = '_'; |
297 |
|
|
298 |
|
add_stat(STRING, &disk[i].device_name, |
299 |
< |
"fs", name, "device_name", NULL); |
299 |
> |
"fs", name, "device_name", NULL); |
300 |
|
add_stat(STRING, &disk[i].fs_type, |
301 |
< |
"fs", name, "fs_type", NULL); |
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, |
305 |
< |
"fs", name, "size", NULL); |
306 |
< |
add_stat(LONG_LONG, &disk[i].used, |
307 |
< |
"fs", name, "used", NULL); |
308 |
< |
add_stat(LONG_LONG, &disk[i].avail, |
309 |
< |
"fs", name, "avail", NULL); |
303 |
> |
"fs", name, "mnt_point", NULL); |
304 |
> |
add_stat(BYTES, &disk[i].size, |
305 |
> |
"fs", name, "size", NULL); |
306 |
> |
add_stat(BYTES, &disk[i].used, |
307 |
> |
"fs", name, "used", NULL); |
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); |
311 |
> |
"fs", name, "total_inodes", NULL); |
312 |
|
add_stat(LONG_LONG, &disk[i].used_inodes, |
313 |
< |
"fs", name, "used_inodes", NULL); |
313 |
> |
"fs", name, "used_inodes", NULL); |
314 |
|
add_stat(LONG_LONG, &disk[i].free_inodes, |
315 |
< |
"fs", name, "free_inodes", NULL); |
315 |
> |
"fs", name, "free_inodes", NULL); |
316 |
> |
add_stat(LONG_LONG, &disk[i].avail_inodes, |
317 |
> |
"fs", name, "avail_inodes", NULL); |
318 |
> |
add_stat(LONG_LONG, &disk[i].io_size, |
319 |
> |
"fs", name, "io_size", NULL); |
320 |
> |
add_stat(LONG_LONG, &disk[i].block_size, |
321 |
> |
"fs", name, "block_size", NULL); |
322 |
> |
add_stat(LONG_LONG, &disk[i].total_blocks, |
323 |
> |
"fs", name, "total_blocks", NULL); |
324 |
> |
add_stat(LONG_LONG, &disk[i].free_blocks, |
325 |
> |
"fs", name, "free_blocks", NULL); |
326 |
> |
add_stat(LONG_LONG, &disk[i].avail_blocks, |
327 |
> |
"fs", name, "avail_blocks", NULL); |
328 |
> |
add_stat(LONG_LONG, &disk[i].used_blocks, |
329 |
> |
"fs", name, "used_blocks", NULL); |
330 |
|
|
331 |
|
free(buf); |
332 |
|
} |
338 |
|
sg_disk_io_stats *diskio; |
339 |
|
|
340 |
|
diskio = use_diffs ? sg_get_disk_io_stats_diff(&n) |
341 |
< |
: sg_get_disk_io_stats(&n); |
341 |
> |
: sg_get_disk_io_stats(&n); |
342 |
|
if (diskio != NULL) { |
343 |
|
for (i = 0; i < n; i++) { |
344 |
|
const char *name = diskio[i].disk_name; |
345 |
|
|
346 |
|
add_stat(STRING, &diskio[i].disk_name, |
347 |
< |
"disk", name, "disk_name", NULL); |
348 |
< |
add_stat(LONG_LONG, &diskio[i].read_bytes, |
349 |
< |
"disk", name, "read_bytes", NULL); |
350 |
< |
add_stat(LONG_LONG, &diskio[i].write_bytes, |
351 |
< |
"disk", name, "write_bytes", NULL); |
347 |
> |
"disk", name, "disk_name", NULL); |
348 |
> |
add_stat(BYTES, &diskio[i].read_bytes, |
349 |
> |
"disk", name, "read_bytes", NULL); |
350 |
> |
add_stat(BYTES, &diskio[i].write_bytes, |
351 |
> |
"disk", name, "write_bytes", NULL); |
352 |
|
add_stat(TIME_T, &diskio[i].systime, |
353 |
< |
"disk", name, "systime", NULL); |
353 |
> |
"disk", name, "systime", NULL); |
354 |
|
} |
355 |
|
} |
356 |
|
} |
369 |
|
} |
370 |
|
|
371 |
|
void populate_net() { |
372 |
< |
int n, i; |
372 |
> |
int num_io, num_iface, i; |
373 |
|
sg_network_io_stats *io; |
374 |
|
sg_network_iface_stats *iface; |
375 |
|
|
376 |
< |
io = use_diffs ? sg_get_network_io_stats_diff(&n) |
377 |
< |
: sg_get_network_io_stats(&n); |
376 |
> |
io = use_diffs ? sg_get_network_io_stats_diff(&num_io) |
377 |
> |
: sg_get_network_io_stats(&num_io); |
378 |
|
if (io != NULL) { |
379 |
< |
for (i = 0; i < n; i++) { |
379 |
> |
for (i = 0; i < num_io; i++) { |
380 |
|
const char *name = io[i].interface_name; |
381 |
|
|
382 |
|
add_stat(STRING, &io[i].interface_name, |
383 |
< |
"net", name, "interface_name", NULL); |
384 |
< |
add_stat(LONG_LONG, &io[i].tx, |
385 |
< |
"net", name, "tx", NULL); |
386 |
< |
add_stat(LONG_LONG, &io[i].rx, |
387 |
< |
"net", name, "rx", NULL); |
383 |
> |
"net", name, "interface_name", NULL); |
384 |
> |
add_stat(BYTES, &io[i].tx, |
385 |
> |
"net", name, "tx", NULL); |
386 |
> |
add_stat(BYTES, &io[i].rx, |
387 |
> |
"net", name, "rx", NULL); |
388 |
|
add_stat(LONG_LONG, &io[i].ipackets, |
389 |
< |
"net", name, "ipackets", NULL); |
389 |
> |
"net", name, "ipackets", NULL); |
390 |
|
add_stat(LONG_LONG, &io[i].opackets, |
391 |
< |
"net", name, "opackets", NULL); |
391 |
> |
"net", name, "opackets", NULL); |
392 |
|
add_stat(LONG_LONG, &io[i].ierrors, |
393 |
< |
"net", name, "ierrors", NULL); |
393 |
> |
"net", name, "ierrors", NULL); |
394 |
|
add_stat(LONG_LONG, &io[i].oerrors, |
395 |
< |
"net", name, "oerrors", NULL); |
395 |
> |
"net", name, "oerrors", NULL); |
396 |
|
add_stat(LONG_LONG, &io[i].collisions, |
397 |
< |
"net", name, "collisions", NULL); |
397 |
> |
"net", name, "collisions", NULL); |
398 |
|
add_stat(TIME_T, &io[i].systime, |
399 |
< |
"net", name, "systime", NULL); |
399 |
> |
"net", name, "systime", NULL); |
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
< |
iface = sg_get_network_iface_stats(&n); |
403 |
> |
iface = sg_get_network_iface_stats(&num_iface); |
404 |
|
if (iface != NULL) { |
405 |
< |
for (i = 0; i < n; i++) { |
405 |
> |
for (i = 0; i < num_iface; i++) { |
406 |
|
const char *name = iface[i].interface_name; |
407 |
+ |
int had_io = 0, j; |
408 |
|
|
409 |
+ |
/* If there wasn't a corresponding io stat, |
410 |
+ |
add interface_name from here. */ |
411 |
+ |
if (io != NULL) { |
412 |
+ |
for (j = 0; j < num_io; j++) { |
413 |
+ |
if (strcmp(io[j].interface_name, |
414 |
+ |
name) == 0) { |
415 |
+ |
had_io = 1; |
416 |
+ |
break; |
417 |
+ |
} |
418 |
+ |
} |
419 |
+ |
} |
420 |
+ |
if (!had_io) { |
421 |
+ |
add_stat(STRING, &iface[i].interface_name, |
422 |
+ |
"net", name, "interface_name", NULL); |
423 |
+ |
} |
424 |
+ |
|
425 |
|
add_stat(INT, &iface[i].speed, |
426 |
< |
"net", name, "speed", NULL); |
426 |
> |
"net", name, "speed", NULL); |
427 |
|
add_stat(BOOL, &iface[i].up, |
428 |
< |
"net", name, "up", NULL); |
429 |
< |
add_stat(DUPLEX, &iface[i].dup, |
430 |
< |
"net", name, "duplex", NULL); |
428 |
> |
"net", name, "up", NULL); |
429 |
> |
add_stat(DUPLEX, &iface[i].duplex, |
430 |
> |
"net", name, "duplex", NULL); |
431 |
|
} |
432 |
|
} |
433 |
|
} |
478 |
|
for (i = 0; i < argc; i++) { |
479 |
|
for (t = &toplevels[0]; t->name != NULL; t++) { |
480 |
|
if (strncmp(argv[i], t->name, |
481 |
< |
strlen(t->name)) == 0) { |
481 |
> |
strlen(t->name)) == 0) { |
482 |
|
t->interesting = 1; |
483 |
|
break; |
484 |
|
} |
505 |
|
/* Print the value of a stat. */ |
506 |
|
void print_stat_value(const stat *s) { |
507 |
|
void *v = s->stat; |
508 |
< |
long l; |
508 |
> |
double fv; |
509 |
> |
long lv; |
510 |
> |
long long llv; |
511 |
|
|
512 |
|
switch (s->type) { |
513 |
|
case LONG_LONG: |
514 |
+ |
#ifdef WIN32 /* Windows printf does not understand %lld, so use %I64d instead */ |
515 |
+ |
printf("%I64d", *(long long *)v); |
516 |
+ |
#else |
517 |
|
printf("%lld", *(long long *)v); |
518 |
+ |
#endif |
519 |
|
break; |
520 |
+ |
case BYTES: |
521 |
+ |
llv = *(long long *)v; |
522 |
+ |
if (bytes_scale_factor != 0) { |
523 |
+ |
llv /= bytes_scale_factor; |
524 |
+ |
} |
525 |
+ |
#ifdef WIN32 |
526 |
+ |
printf("%I64d", llv); |
527 |
+ |
#else |
528 |
+ |
printf("%lld", llv); |
529 |
+ |
#endif |
530 |
+ |
break; |
531 |
|
case TIME_T: |
532 |
|
/* FIXME option for formatted time? */ |
533 |
< |
l = *(time_t *)v; |
534 |
< |
printf("%ld", l); |
533 |
> |
lv = *(time_t *)v; |
534 |
> |
printf("%ld", lv); |
535 |
|
break; |
536 |
|
case FLOAT: |
486 |
– |
printf("%f", *(float *)v); |
487 |
– |
break; |
537 |
|
case DOUBLE: |
538 |
< |
printf("%f", *(double *)v); |
538 |
> |
if (s->type == FLOAT) { |
539 |
> |
fv = *(float *)v; |
540 |
> |
} else { |
541 |
> |
fv = *(double *)v; |
542 |
> |
} |
543 |
> |
if (float_scale_factor != 0) { |
544 |
> |
printf("%ld", (long)(float_scale_factor * fv)); |
545 |
> |
} else { |
546 |
> |
printf("%f", fv); |
547 |
> |
} |
548 |
|
break; |
549 |
|
case STRING: |
550 |
|
/* FIXME escaping? */ |
615 |
|
s = NULL; |
616 |
|
} else { |
617 |
|
s = (const stat *)bsearch(&key, stats, |
618 |
< |
num_stats, |
619 |
< |
sizeof *stats, |
620 |
< |
compare); |
618 |
> |
num_stats, |
619 |
> |
sizeof *stats, |
620 |
> |
compare); |
621 |
|
} |
622 |
|
|
623 |
|
if (s == NULL) { |
661 |
|
" -t DELAY When repeating, wait DELAY seconds between updates (default 1)\n" |
662 |
|
" -p Display CPU usage differences as percentages rather than\n" |
663 |
|
" absolute values\n" |
664 |
+ |
" -f FACTOR Display floating-point values as integers scaled by FACTOR\n" |
665 |
+ |
" -K Display byte counts in kibibytes\n" |
666 |
+ |
" -M Display byte counts in mebibytes\n" |
667 |
+ |
" -G Display byte counts in gibibytes\n" |
668 |
|
"\n"); |
669 |
|
printf("Version %s - report bugs to <%s>.\n", |
670 |
|
PACKAGE_VERSION, PACKAGE_BUGREPORT); |
674 |
|
int main(int argc, char **argv) { |
675 |
|
opterr = 0; |
676 |
|
while (1) { |
677 |
< |
int c = getopt(argc, argv, "lbmunsot:p"); |
677 |
> |
int c = getopt(argc, argv, "lbmunsot:pf:KMG"); |
678 |
|
if (c == -1) |
679 |
|
break; |
680 |
|
switch (c) { |
705 |
|
case 'p': |
706 |
|
use_cpu_percent = 1; |
707 |
|
break; |
708 |
+ |
case 'f': |
709 |
+ |
float_scale_factor = atol(optarg); |
710 |
+ |
break; |
711 |
+ |
case 'K': |
712 |
+ |
bytes_scale_factor = 1024; |
713 |
+ |
break; |
714 |
+ |
case 'M': |
715 |
+ |
bytes_scale_factor = 1024 * 1024; |
716 |
+ |
break; |
717 |
+ |
case 'G': |
718 |
+ |
bytes_scale_factor = 1024 * 1024 * 1024; |
719 |
+ |
break; |
720 |
|
default: |
721 |
|
usage(); |
722 |
|
} |
739 |
|
|
740 |
|
select_interesting(argc - optind, &argv[optind]); |
741 |
|
|
742 |
< |
/* We don't care if statgrab_init fails, because we can just display |
742 |
> |
/* We don't care if sg_init fails, because we can just display |
743 |
|
the statistics that can be read as non-root. */ |
744 |
|
sg_init(); |
745 |
+ |
sg_snapshot(); |
746 |
|
if (sg_drop_privileges() != 0) |
747 |
|
die("Failed to drop setuid/setgid privileges"); |
748 |
|
|
754 |
|
case REPEAT_ONCE: |
755 |
|
get_stats(); |
756 |
|
sleep(repeat_time); |
757 |
+ |
sg_snapshot(); |
758 |
|
get_stats(); |
759 |
|
print_stats(argc, argv); |
760 |
|
break; |
764 |
|
print_stats(argc, argv); |
765 |
|
printf("\n"); |
766 |
|
sleep(repeat_time); |
767 |
+ |
sg_snapshot(); |
768 |
|
} |
769 |
|
} |
770 |
|
|
772 |
|
printf("\n"); |
773 |
|
printf("statgrab\n"); |
774 |
|
} |
775 |
+ |
|
776 |
+ |
sg_shutdown(); |
777 |
|
|
778 |
|
return 0; |
779 |
|
} |