1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
< |
* Copyright (C) 2000-2002 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 |
44 |
|
#endif |
45 |
|
|
46 |
|
typedef struct{ |
47 |
< |
cpu_percent_t *cpu_percents; |
48 |
< |
mem_stat_t *mem_stats; |
49 |
< |
swap_stat_t *swap_stats; |
50 |
< |
load_stat_t *load_stats; |
51 |
< |
process_stat_t *process_stats; |
52 |
< |
page_stat_t *page_stats; |
47 |
> |
sg_cpu_percents *cpu_percents; |
48 |
> |
sg_mem_stats *mem_stats; |
49 |
> |
sg_swap_stats *swap_stats; |
50 |
> |
sg_load_stats *load_stats; |
51 |
> |
sg_process_count *process_count; |
52 |
> |
sg_page_stats *page_stats; |
53 |
|
|
54 |
< |
network_stat_t *network_stats; |
55 |
< |
int network_entries; |
54 |
> |
sg_network_io_stats *network_io_stats; |
55 |
> |
int network_io_entries; |
56 |
|
|
57 |
< |
diskio_stat_t *diskio_stats; |
58 |
< |
int diskio_entries; |
57 |
> |
sg_disk_io_stats *disk_io_stats; |
58 |
> |
int disk_io_entries; |
59 |
|
|
60 |
< |
disk_stat_t *disk_stats; |
61 |
< |
int disk_entries; |
60 |
> |
sg_fs_stats *fs_stats; |
61 |
> |
int fs_entries; |
62 |
|
|
63 |
< |
general_stat_t *general_stats; |
64 |
< |
user_stat_t *user_stats; |
63 |
> |
sg_host_info *host_info; |
64 |
> |
sg_user_stats *user_stats; |
65 |
|
}stats_t; |
66 |
|
|
67 |
|
stats_t stats; |
69 |
|
char *size_conv(long long number){ |
70 |
|
char type[] = {'B', 'K', 'M', 'G', 'T'}; |
71 |
|
int x=0; |
72 |
+ |
int sign=1; |
73 |
|
static char string[10]; |
74 |
|
|
75 |
+ |
if(number < 0){ |
76 |
+ |
sign=-1; |
77 |
+ |
number=-number; |
78 |
+ |
} |
79 |
+ |
|
80 |
|
for(;x<5;x++){ |
81 |
|
if( (number/1024) < (100)) { |
82 |
|
break; |
84 |
|
number = (number/1024); |
85 |
|
} |
86 |
|
|
87 |
< |
snprintf(string, 10, "%lld%c", number, type[x]); |
87 |
> |
number = number*sign; |
88 |
> |
|
89 |
> |
snprintf(string, 10, "%lld%c", number, type[x]); |
90 |
|
return string; |
91 |
|
|
92 |
|
} |
160 |
|
printw("Mem Free :"); |
161 |
|
|
162 |
|
/* Swap */ |
163 |
< |
move(6, 21); |
164 |
< |
printw("Swap Total:"); |
165 |
< |
move(7, 21); |
166 |
< |
printw("Swap Used :"); |
167 |
< |
move(8, 21); |
168 |
< |
printw("Swap Free :"); |
163 |
> |
move(6, 21); |
164 |
> |
printw("Swap Total:"); |
165 |
> |
move(7, 21); |
166 |
> |
printw("Swap Used :"); |
167 |
> |
move(8, 21); |
168 |
> |
printw("Swap Free :"); |
169 |
|
|
170 |
|
/* VM */ |
171 |
|
move(6, 42); |
190 |
|
printw("Write"); |
191 |
|
|
192 |
|
line = 10; |
193 |
< |
if (stats.network_stats != NULL) { |
193 |
> |
if (stats.network_io_stats != NULL) { |
194 |
|
/* Network IO */ |
195 |
|
move(line, 42); |
196 |
|
printw("Network Interface"); |
198 |
|
printw("rx"); |
199 |
|
move(line, 77); |
200 |
|
printw("tx"); |
201 |
< |
line += 2 + stats.network_entries; |
201 |
> |
line += 2 + stats.network_io_entries; |
202 |
|
} |
203 |
|
|
204 |
|
move(line, 42); |
218 |
|
int counter, line; |
219 |
|
long long r,w; |
220 |
|
long long rt, wt; |
221 |
< |
diskio_stat_t *diskio_stat_ptr; |
222 |
< |
network_stat_t *network_stat_ptr; |
223 |
< |
disk_stat_t *disk_stat_ptr; |
221 |
> |
sg_disk_io_stats *disk_io_stat_ptr; |
222 |
> |
sg_network_io_stats *network_stat_ptr; |
223 |
> |
sg_fs_stats *disk_stat_ptr; |
224 |
|
/* Size before it will start overwriting "uptime" */ |
225 |
|
char hostname[15]; |
226 |
|
char *ptr; |
227 |
|
|
228 |
< |
if (stats.general_stats != NULL) { |
228 |
> |
if (stats.host_info != NULL) { |
229 |
|
move(0,12); |
230 |
< |
strncpy(hostname, stats.general_stats->hostname, (sizeof(hostname) - 1)); |
230 |
> |
strncpy(hostname, stats.host_info->hostname, (sizeof(hostname) - 1)); |
231 |
|
/* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */ |
232 |
|
hostname[14] = '\0'; |
233 |
|
ptr=strchr(hostname, '.'); |
239 |
|
} |
240 |
|
printw("%s", hostname); |
241 |
|
move(0,36); |
242 |
< |
printw("%s", hr_uptime(stats.general_stats->uptime)); |
242 |
> |
printw("%s", hr_uptime(stats.host_info->uptime)); |
243 |
|
epoc_time=time(NULL); |
244 |
|
tm_time = localtime(&epoc_time); |
245 |
|
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
267 |
|
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
268 |
|
} |
269 |
|
|
270 |
< |
if (stats.process_stats != NULL) { |
270 |
> |
if (stats.process_count != NULL) { |
271 |
|
/* Process */ |
272 |
|
move(2, 54); |
273 |
< |
printw("%5d", stats.process_stats->running); |
273 |
> |
printw("%5d", stats.process_count->running); |
274 |
|
move(2,74); |
275 |
< |
printw("%5d", stats.process_stats->zombie); |
275 |
> |
printw("%5d", stats.process_count->zombie); |
276 |
|
move(3, 54); |
277 |
< |
printw("%5d", stats.process_stats->sleeping); |
277 |
> |
printw("%5d", stats.process_count->sleeping); |
278 |
|
move(3, 74); |
279 |
< |
printw("%5d", stats.process_stats->total); |
279 |
> |
printw("%5d", stats.process_count->total); |
280 |
|
move(4, 54); |
281 |
< |
printw("%5d", stats.process_stats->stopped); |
281 |
> |
printw("%5d", stats.process_count->stopped); |
282 |
|
} |
283 |
|
if (stats.user_stats != NULL) { |
284 |
|
move(4,74); |
329 |
|
} |
330 |
|
|
331 |
|
line = 11; |
332 |
< |
if (stats.diskio_stats != NULL) { |
332 |
> |
if (stats.disk_io_stats != NULL) { |
333 |
|
/* Disk IO */ |
334 |
< |
diskio_stat_ptr = stats.diskio_stats; |
334 |
> |
disk_io_stat_ptr = stats.disk_io_stats; |
335 |
|
r=0; |
336 |
|
w=0; |
337 |
< |
for(counter=0;counter<stats.diskio_entries;counter++){ |
337 |
> |
for(counter=0;counter<stats.disk_io_entries;counter++){ |
338 |
> |
char name[12]; |
339 |
> |
strncpy(name, disk_io_stat_ptr->disk_name, sizeof(name)); |
340 |
> |
name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ |
341 |
|
move(line, 0); |
342 |
< |
printw("%s", diskio_stat_ptr->disk_name); |
342 |
> |
printw("%s", name); |
343 |
|
move(line, 12); |
344 |
< |
rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes; |
344 |
> |
rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; |
345 |
|
printw("%7s", size_conv(rt)); |
346 |
|
r+=rt; |
347 |
|
move(line, 26); |
348 |
< |
wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes; |
348 |
> |
wt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->write_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->write_bytes; |
349 |
|
printw("%7s", size_conv(wt)); |
350 |
|
w+=wt; |
351 |
< |
diskio_stat_ptr++; |
351 |
> |
disk_io_stat_ptr++; |
352 |
|
line++; |
353 |
|
} |
354 |
|
line++; |
361 |
|
} |
362 |
|
|
363 |
|
line = 11; |
364 |
< |
if (stats.network_stats != NULL) { |
364 |
> |
if (stats.network_io_stats != NULL) { |
365 |
|
/* Network */ |
366 |
< |
network_stat_ptr = stats.network_stats; |
367 |
< |
for(counter=0;counter<stats.network_entries;counter++){ |
368 |
< |
move(line, 42); |
369 |
< |
printw("%s", network_stat_ptr->interface_name); |
366 |
> |
network_stat_ptr = stats.network_io_stats; |
367 |
> |
for(counter=0;counter<stats.network_io_entries;counter++){ |
368 |
> |
char name[20]; |
369 |
> |
strncpy(name, network_stat_ptr->interface_name, sizeof(name)); |
370 |
> |
name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ |
371 |
> |
move(line, 42); |
372 |
> |
printw("%s", name); |
373 |
|
move(line, 62); |
374 |
|
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
375 |
|
printw("%7s", size_conv(rt)); |
382 |
|
line += 2; |
383 |
|
} |
384 |
|
|
385 |
< |
if (stats.disk_stats != NULL) { |
385 |
> |
if (stats.fs_stats != NULL) { |
386 |
|
/* Disk */ |
387 |
< |
disk_stat_ptr = stats.disk_stats; |
388 |
< |
for(counter=0;counter<stats.disk_entries;counter++){ |
387 |
> |
disk_stat_ptr = stats.fs_stats; |
388 |
> |
for(counter=0;counter<stats.fs_entries;counter++){ |
389 |
> |
char name[20]; |
390 |
> |
strncpy(name, disk_stat_ptr->mnt_point, sizeof(name)); |
391 |
> |
name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ |
392 |
|
move(line, 42); |
393 |
< |
printw("%s", disk_stat_ptr->mnt_point); |
393 |
> |
printw("%s", name); |
394 |
|
move(line, 62); |
395 |
|
printw("%7s", size_conv(disk_stat_ptr->avail)); |
396 |
|
move(line, 73); |
397 |
< |
printw("%5.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
397 |
> |
printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
398 |
|
disk_stat_ptr++; |
399 |
|
line++; |
400 |
|
} |
403 |
|
refresh(); |
404 |
|
} |
405 |
|
|
406 |
< |
void sig_winch_handler(int sig){ |
406 |
> |
void sig_winch_handler(int dummy){ |
407 |
|
clear(); |
408 |
|
display_headings(); |
409 |
|
display_data(); |
410 |
< |
signal(SIGWINCH, sig_winch_handler); |
410 |
> |
signal(SIGWINCH, sig_winch_handler); |
411 |
|
} |
412 |
|
|
413 |
|
int get_stats(){ |
414 |
< |
stats.cpu_percents = cpu_percent_usage(); |
415 |
< |
stats.mem_stats = get_memory_stats(); |
416 |
< |
stats.swap_stats = get_swap_stats(); |
417 |
< |
stats.load_stats = get_load_stats(); |
418 |
< |
stats.process_stats = get_process_stats(); |
419 |
< |
stats.page_stats = get_page_stats_diff(); |
420 |
< |
stats.network_stats = get_network_stats_diff(&(stats.network_entries)); |
421 |
< |
stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries)); |
422 |
< |
stats.disk_stats = get_disk_stats(&(stats.disk_entries)); |
423 |
< |
stats.general_stats = get_general_stats(); |
424 |
< |
stats.user_stats = get_user_stats(); |
414 |
> |
stats.cpu_percents = sg_get_cpu_percents(); |
415 |
> |
stats.mem_stats = sg_get_mem_stats(); |
416 |
> |
stats.swap_stats = sg_get_swap_stats(); |
417 |
> |
stats.load_stats = sg_get_load_stats(); |
418 |
> |
stats.process_count = sg_get_process_count(); |
419 |
> |
stats.page_stats = sg_get_page_stats_diff(); |
420 |
> |
stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries)); |
421 |
> |
stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries)); |
422 |
> |
stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries)); |
423 |
> |
stats.host_info = sg_get_host_info(); |
424 |
> |
stats.user_stats = sg_get_user_stats(); |
425 |
|
|
426 |
|
return 1; |
427 |
|
} |
433 |
|
} |
434 |
|
|
435 |
|
void usage(char *progname){ |
436 |
< |
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
437 |
< |
fprintf(stderr, " -d Sets the update time in seconds\n"); |
436 |
> |
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
437 |
> |
fprintf(stderr, " -d Sets the update time in seconds\n"); |
438 |
|
fprintf(stderr, " -v Prints version number\n"); |
439 |
< |
fprintf(stderr, " -h Displays this help information.\n"); |
440 |
< |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
441 |
< |
exit(1); |
439 |
> |
fprintf(stderr, " -h Displays this help information.\n"); |
440 |
> |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
441 |
> |
exit(1); |
442 |
|
} |
443 |
|
|
444 |
|
int main(int argc, char **argv){ |
445 |
|
|
446 |
< |
extern char *optarg; |
447 |
< |
extern int optind; |
429 |
< |
int c; |
446 |
> |
extern char *optarg; |
447 |
> |
int c; |
448 |
|
|
449 |
+ |
time_t last_update = 0; |
450 |
+ |
|
451 |
|
WINDOW *window; |
452 |
|
|
433 |
– |
int stdin_fileno; |
434 |
– |
fd_set infds; |
435 |
– |
struct timeval timeout; |
436 |
– |
|
453 |
|
extern int errno; |
438 |
– |
char ch; |
454 |
|
|
455 |
|
int delay=2; |
456 |
< |
#ifdef ALLBSD |
457 |
< |
gid_t gid; |
458 |
< |
#endif |
459 |
< |
statgrab_init(); |
445 |
< |
#ifdef ALLBSD |
446 |
< |
if((setegid(getgid())) != 0){ |
447 |
< |
fprintf(stderr, "Failed to lose setgid'ness\n"); |
456 |
> |
|
457 |
> |
sg_init(); |
458 |
> |
if(sg_drop_privileges() != 0){ |
459 |
> |
fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); |
460 |
|
return 1; |
461 |
|
} |
450 |
– |
#endif |
462 |
|
|
463 |
< |
while ((c = getopt(argc, argv, "vhd:")) != EOF){ |
464 |
< |
switch (c){ |
465 |
< |
case 'd': |
466 |
< |
delay = atoi(optarg); |
467 |
< |
if (delay == 0){ |
463 |
> |
while ((c = getopt(argc, argv, "vhd:")) != -1){ |
464 |
> |
switch (c){ |
465 |
> |
case 'd': |
466 |
> |
delay = atoi(optarg); |
467 |
> |
if (delay < 1){ |
468 |
|
fprintf(stderr, "Time must be 1 second or greater\n"); |
469 |
|
exit(1); |
470 |
|
} |
471 |
< |
delay--; |
461 |
< |
break; |
471 |
> |
break; |
472 |
|
case 'v': |
473 |
|
version_num(argv[0]); |
474 |
|
break; |
477 |
|
usage(argv[0]); |
478 |
|
return 1; |
479 |
|
break; |
480 |
< |
|
481 |
< |
} |
472 |
< |
} |
480 |
> |
} |
481 |
> |
} |
482 |
|
|
483 |
|
signal(SIGWINCH, sig_winch_handler); |
484 |
< |
initscr(); |
485 |
< |
nonl(); |
486 |
< |
cbreak(); |
487 |
< |
noecho(); |
488 |
< |
window=newwin(0, 0, 0, 0); |
484 |
> |
initscr(); |
485 |
> |
nonl(); |
486 |
> |
cbreak(); |
487 |
> |
noecho(); |
488 |
> |
timeout(delay * 1000); |
489 |
> |
window=newwin(0, 0, 0, 0); |
490 |
|
clear(); |
491 |
|
|
492 |
|
if(!get_stats()){ |
496 |
|
} |
497 |
|
|
498 |
|
display_headings(); |
489 |
– |
stdin_fileno=fileno(stdin); |
499 |
|
|
500 |
|
for(;;){ |
501 |
+ |
time_t now; |
502 |
+ |
int ch = getch(); |
503 |
|
|
504 |
< |
FD_ZERO(&infds); |
505 |
< |
FD_SET(stdin_fileno, &infds); |
495 |
< |
timeout.tv_sec = delay; |
496 |
< |
timeout.tv_usec = 0; |
497 |
< |
|
498 |
< |
if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){ |
499 |
< |
if(errno!=EINTR){ |
500 |
< |
perror("select failed"); |
501 |
< |
exit(1); |
502 |
< |
} |
504 |
> |
if (ch == 'q'){ |
505 |
> |
break; |
506 |
|
} |
507 |
|
|
508 |
< |
if(FD_ISSET(stdin_fileno, &infds)){ |
509 |
< |
ch=getch(); |
510 |
< |
if (ch == 'q'){ |
511 |
< |
endwin(); |
512 |
< |
return 0; |
513 |
< |
} |
508 |
> |
/* To keep the numbers slightly accurate we do not want them |
509 |
> |
* updating more frequently than once a second. |
510 |
> |
*/ |
511 |
> |
now = time(NULL); |
512 |
> |
if ((now - last_update) >= 1) { |
513 |
> |
get_stats(); |
514 |
|
} |
515 |
+ |
last_update = now; |
516 |
|
|
513 |
– |
get_stats(); |
514 |
– |
|
517 |
|
display_data(); |
516 |
– |
|
517 |
– |
/* To keep the numbers slightly accurate we do not want them updating more |
518 |
– |
* frequently than once a second. |
519 |
– |
*/ |
520 |
– |
sleep(1); |
518 |
|
} |
519 |
|
|
520 |
|
endwin(); |