29 |
|
#include <sys/ioctl.h> |
30 |
|
#include <unistd.h> |
31 |
|
#include <stdlib.h> |
32 |
– |
#include <sys/termios.h> |
32 |
|
#include <signal.h> |
33 |
|
#include <errno.h> |
34 |
|
#include <statgrab.h> |
35 |
|
#include <sys/times.h> |
36 |
|
#include <limits.h> |
37 |
|
#include <time.h> |
38 |
+ |
#include <math.h> |
39 |
+ |
#ifdef AIX |
40 |
+ |
#include <termios.h> |
41 |
+ |
#else |
42 |
+ |
#include <sys/termios.h> |
43 |
+ |
#endif |
44 |
|
|
45 |
|
#ifdef HAVE_NCURSES_H |
46 |
< |
#include <ncurses.h> |
42 |
< |
#else |
43 |
< |
#include <curses.h> |
46 |
> |
#define COLOR_SUPPORT |
47 |
|
#endif |
48 |
+ |
#include CURSES_HEADER_FILE |
49 |
|
|
50 |
+ |
#define THRESHOLD_LOAD 1.0 |
51 |
+ |
|
52 |
+ |
#define THRESHOLD_WARN_ZMB 0 |
53 |
+ |
|
54 |
+ |
#define THRESHOLD_WARN_CPU 60.0 |
55 |
+ |
#define THRESHOLD_ALERT_CPU 90.0 |
56 |
+ |
|
57 |
+ |
#define THRESHOLD_WARN_SWAP 75.0 |
58 |
+ |
#define THRESHOLD_ALERT_SWAP 90.0 |
59 |
+ |
|
60 |
+ |
#define THRESHOLD_WARN_MEM 75.0 |
61 |
+ |
#define THRESHOLD_ALERT_MEM 90.0 |
62 |
+ |
|
63 |
+ |
#define THRESHOLD_WARN_DISK 75.0 |
64 |
+ |
#define THRESHOLD_ALERT_DISK 90.0 |
65 |
+ |
|
66 |
+ |
int sig_winch_flag = 0; |
67 |
+ |
|
68 |
|
typedef struct{ |
69 |
|
sg_cpu_percents *cpu_percents; |
70 |
|
sg_mem_stats *mem_stats; |
233 |
|
refresh(); |
234 |
|
} |
235 |
|
|
236 |
< |
void display_data(){ |
236 |
> |
void display_data(int colors){ |
237 |
|
char cur_time[20]; |
238 |
|
struct tm *tm_time; |
239 |
|
time_t epoc_time; |
259 |
|
if (ptr != NULL){ |
260 |
|
*ptr = '\0'; |
261 |
|
} |
262 |
+ |
if (colors) { |
263 |
+ |
attron(COLOR_PAIR(1)); |
264 |
+ |
} |
265 |
|
printw("%s", hostname); |
266 |
|
move(0,36); |
267 |
|
printw("%s", hr_uptime(stats.host_info->uptime)); |
270 |
|
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
271 |
|
move(0,61); |
272 |
|
printw("%s", cur_time); |
273 |
+ |
if (colors) { |
274 |
+ |
attroff(COLOR_PAIR(1)); |
275 |
+ |
} |
276 |
|
} |
277 |
|
|
278 |
|
if (stats.load_stats != NULL) { |
279 |
|
/* Load */ |
280 |
+ |
if (colors) { |
281 |
+ |
attron(COLOR_PAIR(6)); |
282 |
+ |
} |
283 |
|
move(2,12); |
284 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min5) > THRESHOLD_LOAD) { |
285 |
+ |
attron(A_BOLD); |
286 |
+ |
} |
287 |
|
printw("%6.2f", stats.load_stats->min1); |
288 |
+ |
if (colors) { |
289 |
+ |
attroff(A_BOLD); |
290 |
+ |
} |
291 |
|
move(3,12); |
292 |
+ |
if (colors && fabs(stats.load_stats->min5 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
293 |
+ |
attron(A_BOLD); |
294 |
+ |
} |
295 |
|
printw("%6.2f", stats.load_stats->min5); |
296 |
+ |
if (colors) { |
297 |
+ |
attroff(A_BOLD); |
298 |
+ |
} |
299 |
|
move(4,12); |
300 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
301 |
+ |
attron(A_BOLD); |
302 |
+ |
} |
303 |
|
printw("%6.2f", stats.load_stats->min15); |
304 |
+ |
if (colors) { |
305 |
+ |
attroff(A_BOLD); |
306 |
+ |
} |
307 |
|
} |
308 |
|
|
309 |
|
if (stats.cpu_percents != NULL) { |
313 |
|
move(3,33); |
314 |
|
printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap)); |
315 |
|
move(4,33); |
316 |
+ |
if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_ALERT_CPU) { |
317 |
+ |
attron(A_STANDOUT); |
318 |
+ |
attron(A_BOLD); |
319 |
+ |
} |
320 |
+ |
else if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_WARN_CPU) { |
321 |
+ |
attron(A_BOLD); |
322 |
+ |
} |
323 |
|
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
324 |
+ |
if(colors) { |
325 |
+ |
attroff(A_BOLD); |
326 |
+ |
attroff(A_STANDOUT); |
327 |
+ |
attron(COLOR_PAIR(6)); |
328 |
+ |
} |
329 |
|
} |
330 |
|
|
331 |
|
if (stats.process_count != NULL) { |
333 |
|
move(2, 54); |
334 |
|
printw("%5d", stats.process_count->running); |
335 |
|
move(2,74); |
336 |
+ |
if (colors && stats.process_count->zombie > THRESHOLD_WARN_ZMB) { |
337 |
+ |
attron(A_STANDOUT); |
338 |
+ |
attron(A_BOLD); |
339 |
+ |
} |
340 |
|
printw("%5d", stats.process_count->zombie); |
341 |
+ |
if(colors) { |
342 |
+ |
attroff(A_STANDOUT); |
343 |
+ |
attroff(A_BOLD); |
344 |
+ |
} |
345 |
|
move(3, 54); |
346 |
|
printw("%5d", stats.process_count->sleeping); |
347 |
|
move(3, 74); |
354 |
|
printw("%5d", stats.user_stats->num_entries); |
355 |
|
} |
356 |
|
|
357 |
+ |
if(colors) { |
358 |
+ |
attroff(COLOR_PAIR(6)); |
359 |
+ |
attron(COLOR_PAIR(5)); |
360 |
+ |
} |
361 |
|
if (stats.mem_stats != NULL) { |
362 |
|
/* Mem */ |
363 |
|
move(6, 12); |
380 |
|
|
381 |
|
/* VM */ |
382 |
|
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
383 |
+ |
float f = 100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total; |
384 |
+ |
if (colors && f > THRESHOLD_ALERT_MEM) { |
385 |
+ |
attron(A_STANDOUT); |
386 |
+ |
attron(A_BOLD); |
387 |
+ |
} |
388 |
+ |
else if (colors && f > THRESHOLD_WARN_MEM) { |
389 |
+ |
attron(A_BOLD); |
390 |
+ |
} |
391 |
|
move(6, 54); |
392 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
392 |
> |
printw("%5.2f%%", f); |
393 |
> |
if (colors) { |
394 |
> |
attroff(A_STANDOUT); |
395 |
> |
attroff(A_BOLD); |
396 |
> |
attron(COLOR_PAIR(5)); |
397 |
> |
} |
398 |
|
} |
399 |
|
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
400 |
+ |
float f = 100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total; |
401 |
+ |
if (colors && f > THRESHOLD_ALERT_SWAP) { |
402 |
+ |
attron(A_STANDOUT); |
403 |
+ |
attron(A_BOLD); |
404 |
+ |
} |
405 |
+ |
else if (colors && f > THRESHOLD_WARN_SWAP) { |
406 |
+ |
attron(A_BOLD); |
407 |
+ |
} |
408 |
|
move(7, 54); |
409 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
409 |
> |
printw("%5.2f%%", f); |
410 |
> |
if (colors) { |
411 |
> |
attroff(A_STANDOUT); |
412 |
> |
attroff(A_BOLD); |
413 |
> |
attron(COLOR_PAIR(5)); |
414 |
> |
} |
415 |
|
} |
416 |
|
if (stats.mem_stats != NULL && stats.swap_stats != NULL && |
417 |
|
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
426 |
|
move(7, 74); |
427 |
|
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout); |
428 |
|
} |
429 |
+ |
if (colors) { |
430 |
+ |
attroff(COLOR_PAIR(5)); |
431 |
+ |
} |
432 |
|
|
433 |
|
line = 11; |
434 |
|
if (stats.disk_io_stats != NULL) { |
444 |
|
printw("%s", name); |
445 |
|
move(line, 12); |
446 |
|
rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; |
447 |
+ |
if(colors) { |
448 |
+ |
attron(COLOR_PAIR(4)); |
449 |
+ |
} |
450 |
|
printw("%7s", size_conv(rt)); |
451 |
|
r+=rt; |
452 |
|
move(line, 26); |
455 |
|
w+=wt; |
456 |
|
disk_io_stat_ptr++; |
457 |
|
line++; |
458 |
+ |
if(colors) { |
459 |
+ |
attroff(COLOR_PAIR(4)); |
460 |
+ |
} |
461 |
|
} |
462 |
|
line++; |
463 |
|
move(line, 0); |
464 |
|
printw("Total"); |
465 |
|
move(line, 12); |
466 |
+ |
if(colors) { |
467 |
+ |
attron(COLOR_PAIR(4)); |
468 |
+ |
} |
469 |
|
printw("%7s", size_conv(r)); |
470 |
|
move(line, 26); |
471 |
|
printw("%7s", size_conv(w)); |
472 |
+ |
if(colors) { |
473 |
+ |
attroff(COLOR_PAIR(4)); |
474 |
+ |
} |
475 |
|
} |
476 |
|
|
477 |
|
line = 11; |
486 |
|
printw("%s", name); |
487 |
|
move(line, 62); |
488 |
|
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
489 |
+ |
if(colors) { |
490 |
+ |
attron(COLOR_PAIR(4)); |
491 |
+ |
} |
492 |
|
printw("%7s", size_conv(rt)); |
493 |
|
move(line, 72); |
494 |
|
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
495 |
|
printw("%7s", size_conv(wt)); |
496 |
|
network_stat_ptr++; |
497 |
|
line++; |
498 |
+ |
if(colors) { |
499 |
+ |
attroff(COLOR_PAIR(4)); |
500 |
+ |
} |
501 |
|
} |
502 |
|
line += 2; |
503 |
|
} |
512 |
|
move(line, 42); |
513 |
|
printw("%s", name); |
514 |
|
move(line, 62); |
515 |
+ |
if(colors) { |
516 |
+ |
attron(COLOR_PAIR(2)); |
517 |
+ |
} |
518 |
|
printw("%7s", size_conv(disk_stat_ptr->avail)); |
519 |
|
move(line, 73); |
520 |
+ |
if(colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_ALERT_DISK) { |
521 |
+ |
attron(A_STANDOUT); |
522 |
+ |
attron(A_BOLD); |
523 |
+ |
} else if (colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_WARN_DISK) { |
524 |
+ |
attron(A_BOLD); |
525 |
+ |
} |
526 |
|
printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
527 |
|
disk_stat_ptr++; |
528 |
|
line++; |
529 |
+ |
if(colors) { |
530 |
+ |
attroff(COLOR_PAIR(2)); |
531 |
+ |
attroff(A_STANDOUT); |
532 |
+ |
attroff(A_BOLD); |
533 |
+ |
} |
534 |
|
} |
535 |
|
} |
536 |
|
|
538 |
|
} |
539 |
|
|
540 |
|
void sig_winch_handler(int dummy){ |
541 |
< |
clear(); |
408 |
< |
display_headings(); |
409 |
< |
display_data(); |
541 |
> |
sig_winch_flag = 1; |
542 |
|
signal(SIGWINCH, sig_winch_handler); |
543 |
|
} |
544 |
|
|
565 |
|
} |
566 |
|
|
567 |
|
void usage(char *progname){ |
568 |
+ |
#ifdef COLOR_SUPPORT |
569 |
+ |
fprintf(stderr, "Usage: %s [-d delay] [-c] [-v] [-h]\n\n", progname); |
570 |
+ |
#else |
571 |
|
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
572 |
+ |
#endif |
573 |
|
fprintf(stderr, " -d Sets the update time in seconds\n"); |
574 |
+ |
#ifdef COLOR_SUPPORT |
575 |
+ |
fprintf(stderr, " -c Enables coloured output\n"); |
576 |
+ |
#endif |
577 |
|
fprintf(stderr, " -v Prints version number\n"); |
578 |
|
fprintf(stderr, " -h Displays this help information.\n"); |
579 |
|
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
581 |
|
} |
582 |
|
|
583 |
|
int main(int argc, char **argv){ |
445 |
– |
|
584 |
|
extern char *optarg; |
585 |
|
int c; |
586 |
+ |
int colouron = 0; |
587 |
|
|
588 |
|
time_t last_update = 0; |
589 |
|
|
599 |
|
return 1; |
600 |
|
} |
601 |
|
|
602 |
< |
while ((c = getopt(argc, argv, "vhd:")) != -1){ |
602 |
> |
#ifdef COLOR_SUPPORT |
603 |
> |
while ((c = getopt(argc, argv, "d:cvh")) != -1){ |
604 |
> |
#else |
605 |
> |
while ((c = getopt(argc, argv, "d:vh")) != -1){ |
606 |
> |
#endif |
607 |
|
switch (c){ |
608 |
|
case 'd': |
609 |
|
delay = atoi(optarg); |
612 |
|
exit(1); |
613 |
|
} |
614 |
|
break; |
615 |
+ |
#ifdef COLOR_SUPPORT |
616 |
+ |
case 'c': |
617 |
+ |
colouron = 1; |
618 |
+ |
break; |
619 |
+ |
#endif |
620 |
|
case 'v': |
621 |
|
version_num(argv[0]); |
622 |
|
break; |
630 |
|
|
631 |
|
signal(SIGWINCH, sig_winch_handler); |
632 |
|
initscr(); |
633 |
+ |
#ifdef COLOR_SUPPORT |
634 |
+ |
/* turn on colour */ |
635 |
+ |
if (colouron) { |
636 |
+ |
if (has_colors()) { |
637 |
+ |
start_color(); |
638 |
+ |
use_default_colors(); |
639 |
+ |
init_pair(1,COLOR_RED,-1); |
640 |
+ |
init_pair(2,COLOR_GREEN,-1); |
641 |
+ |
init_pair(3,COLOR_YELLOW,-1); |
642 |
+ |
init_pair(4,COLOR_BLUE,-1); |
643 |
+ |
init_pair(5,COLOR_MAGENTA,-1); |
644 |
+ |
init_pair(6,COLOR_CYAN,-1); |
645 |
+ |
} else { |
646 |
+ |
fprintf(stderr, "Colour support disabled: your terminal does not support colour."); |
647 |
+ |
colouron = 0; |
648 |
+ |
} |
649 |
+ |
} |
650 |
+ |
#endif |
651 |
|
nonl(); |
652 |
+ |
curs_set(0); |
653 |
|
cbreak(); |
654 |
|
noecho(); |
655 |
|
timeout(delay * 1000); |
681 |
|
} |
682 |
|
last_update = now; |
683 |
|
|
684 |
< |
display_data(); |
684 |
> |
if(sig_winch_flag) { |
685 |
> |
clear(); |
686 |
> |
display_headings(); |
687 |
> |
sig_winch_flag = 0; |
688 |
> |
} |
689 |
> |
|
690 |
> |
display_data(colouron); |
691 |
|
} |
692 |
|
|
693 |
|
endwin(); |