| 36 |
|
#include <sys/times.h> |
| 37 |
|
#include <limits.h> |
| 38 |
|
#include <time.h> |
| 39 |
+ |
#include <math.h> |
| 40 |
|
|
| 41 |
|
#ifdef HAVE_NCURSES_H |
| 42 |
|
#include <ncurses.h> |
| 43 |
+ |
#define COLOR_SUPPORT |
| 44 |
|
#else |
| 45 |
|
#include <curses.h> |
| 46 |
|
#endif |
| 47 |
|
|
| 48 |
+ |
#define THRESHOLD_LOAD 1.0 |
| 49 |
+ |
|
| 50 |
+ |
#define THRESHOLD_WARN_ZMB 0 |
| 51 |
+ |
|
| 52 |
+ |
#define THRESHOLD_WARN_CPU 60.0 |
| 53 |
+ |
#define THRESHOLD_ALERT_CPU 90.0 |
| 54 |
+ |
|
| 55 |
+ |
#define THRESHOLD_WARN_SWAP 75.0 |
| 56 |
+ |
#define THRESHOLD_ALERT_SWAP 90.0 |
| 57 |
+ |
|
| 58 |
+ |
#define THRESHOLD_WARN_MEM 75.0 |
| 59 |
+ |
#define THRESHOLD_ALERT_MEM 90.0 |
| 60 |
+ |
|
| 61 |
+ |
#define THRESHOLD_WARN_DISK 75.0 |
| 62 |
+ |
#define THRESHOLD_ALERT_DISK 90.0 |
| 63 |
+ |
|
| 64 |
|
typedef struct{ |
| 65 |
|
sg_cpu_percents *cpu_percents; |
| 66 |
|
sg_mem_stats *mem_stats; |
| 229 |
|
refresh(); |
| 230 |
|
} |
| 231 |
|
|
| 232 |
< |
void display_data(){ |
| 232 |
> |
void display_data(int colors){ |
| 233 |
|
char cur_time[20]; |
| 234 |
|
struct tm *tm_time; |
| 235 |
|
time_t epoc_time; |
| 255 |
|
if (ptr != NULL){ |
| 256 |
|
*ptr = '\0'; |
| 257 |
|
} |
| 258 |
+ |
if (colors) { |
| 259 |
+ |
attron(COLOR_PAIR(1)); |
| 260 |
+ |
} |
| 261 |
|
printw("%s", hostname); |
| 262 |
|
move(0,36); |
| 263 |
|
printw("%s", hr_uptime(stats.host_info->uptime)); |
| 266 |
|
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
| 267 |
|
move(0,61); |
| 268 |
|
printw("%s", cur_time); |
| 269 |
+ |
if (colors) { |
| 270 |
+ |
attroff(COLOR_PAIR(1)); |
| 271 |
+ |
} |
| 272 |
|
} |
| 273 |
|
|
| 274 |
|
if (stats.load_stats != NULL) { |
| 275 |
|
/* Load */ |
| 276 |
+ |
if (colors) { |
| 277 |
+ |
attron(COLOR_PAIR(6)); |
| 278 |
+ |
} |
| 279 |
|
move(2,12); |
| 280 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min5) > THRESHOLD_LOAD) { |
| 281 |
+ |
attron(A_BOLD); |
| 282 |
+ |
} |
| 283 |
|
printw("%6.2f", stats.load_stats->min1); |
| 284 |
+ |
if (colors) { |
| 285 |
+ |
attroff(A_BOLD); |
| 286 |
+ |
} |
| 287 |
|
move(3,12); |
| 288 |
+ |
if (colors && fabs(stats.load_stats->min5 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
| 289 |
+ |
attron(A_BOLD); |
| 290 |
+ |
} |
| 291 |
|
printw("%6.2f", stats.load_stats->min5); |
| 292 |
+ |
if (colors) { |
| 293 |
+ |
attroff(A_BOLD); |
| 294 |
+ |
} |
| 295 |
|
move(4,12); |
| 296 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
| 297 |
+ |
attron(A_BOLD); |
| 298 |
+ |
} |
| 299 |
|
printw("%6.2f", stats.load_stats->min15); |
| 300 |
+ |
if (colors) { |
| 301 |
+ |
attroff(A_BOLD); |
| 302 |
+ |
} |
| 303 |
|
} |
| 304 |
|
|
| 305 |
|
if (stats.cpu_percents != NULL) { |
| 309 |
|
move(3,33); |
| 310 |
|
printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap)); |
| 311 |
|
move(4,33); |
| 312 |
+ |
if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_ALERT_CPU) { |
| 313 |
+ |
attron(A_STANDOUT); |
| 314 |
+ |
attron(A_BOLD); |
| 315 |
+ |
} |
| 316 |
+ |
else if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_WARN_CPU) { |
| 317 |
+ |
attron(A_BOLD); |
| 318 |
+ |
} |
| 319 |
|
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
| 320 |
+ |
if(colors) { |
| 321 |
+ |
attroff(A_BOLD); |
| 322 |
+ |
attroff(A_STANDOUT); |
| 323 |
+ |
attron(COLOR_PAIR(6)); |
| 324 |
+ |
} |
| 325 |
|
} |
| 326 |
|
|
| 327 |
|
if (stats.process_count != NULL) { |
| 329 |
|
move(2, 54); |
| 330 |
|
printw("%5d", stats.process_count->running); |
| 331 |
|
move(2,74); |
| 332 |
+ |
if (colors && stats.process_count->zombie > THRESHOLD_WARN_ZMB) { |
| 333 |
+ |
attron(A_STANDOUT); |
| 334 |
+ |
attron(A_BOLD); |
| 335 |
+ |
} |
| 336 |
|
printw("%5d", stats.process_count->zombie); |
| 337 |
+ |
if(colors) { |
| 338 |
+ |
attroff(A_STANDOUT); |
| 339 |
+ |
attroff(A_BOLD); |
| 340 |
+ |
} |
| 341 |
|
move(3, 54); |
| 342 |
|
printw("%5d", stats.process_count->sleeping); |
| 343 |
|
move(3, 74); |
| 350 |
|
printw("%5d", stats.user_stats->num_entries); |
| 351 |
|
} |
| 352 |
|
|
| 353 |
+ |
if(colors) { |
| 354 |
+ |
attroff(COLOR_PAIR(6)); |
| 355 |
+ |
attron(COLOR_PAIR(5)); |
| 356 |
+ |
} |
| 357 |
|
if (stats.mem_stats != NULL) { |
| 358 |
|
/* Mem */ |
| 359 |
|
move(6, 12); |
| 376 |
|
|
| 377 |
|
/* VM */ |
| 378 |
|
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
| 379 |
+ |
float f = 100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total; |
| 380 |
+ |
if (colors && f > THRESHOLD_ALERT_MEM) { |
| 381 |
+ |
attron(A_STANDOUT); |
| 382 |
+ |
attron(A_BOLD); |
| 383 |
+ |
} |
| 384 |
+ |
else if (colors && f > THRESHOLD_WARN_MEM) { |
| 385 |
+ |
attron(A_BOLD); |
| 386 |
+ |
} |
| 387 |
|
move(6, 54); |
| 388 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
| 388 |
> |
printw("%5.2f%%", f); |
| 389 |
> |
if (colors) { |
| 390 |
> |
attroff(A_STANDOUT); |
| 391 |
> |
attroff(A_BOLD); |
| 392 |
> |
attron(COLOR_PAIR(5)); |
| 393 |
> |
} |
| 394 |
|
} |
| 395 |
|
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
| 396 |
+ |
float f = 100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total; |
| 397 |
+ |
if (colors && f > THRESHOLD_ALERT_SWAP) { |
| 398 |
+ |
attron(A_STANDOUT); |
| 399 |
+ |
attron(A_BOLD); |
| 400 |
+ |
} |
| 401 |
+ |
else if (colors && f > THRESHOLD_WARN_SWAP) { |
| 402 |
+ |
attron(A_BOLD); |
| 403 |
+ |
} |
| 404 |
|
move(7, 54); |
| 405 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
| 405 |
> |
printw("%5.2f%%", f); |
| 406 |
> |
if (colors) { |
| 407 |
> |
attroff(A_STANDOUT); |
| 408 |
> |
attroff(A_BOLD); |
| 409 |
> |
attron(COLOR_PAIR(5)); |
| 410 |
> |
} |
| 411 |
|
} |
| 412 |
|
if (stats.mem_stats != NULL && stats.swap_stats != NULL && |
| 413 |
|
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
| 422 |
|
move(7, 74); |
| 423 |
|
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout); |
| 424 |
|
} |
| 425 |
+ |
if (colors) { |
| 426 |
+ |
attroff(COLOR_PAIR(5)); |
| 427 |
+ |
} |
| 428 |
|
|
| 429 |
|
line = 11; |
| 430 |
|
if (stats.disk_io_stats != NULL) { |
| 440 |
|
printw("%s", name); |
| 441 |
|
move(line, 12); |
| 442 |
|
rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; |
| 443 |
+ |
if(colors) { |
| 444 |
+ |
attron(COLOR_PAIR(4)); |
| 445 |
+ |
} |
| 446 |
|
printw("%7s", size_conv(rt)); |
| 447 |
|
r+=rt; |
| 448 |
|
move(line, 26); |
| 451 |
|
w+=wt; |
| 452 |
|
disk_io_stat_ptr++; |
| 453 |
|
line++; |
| 454 |
+ |
if(colors) { |
| 455 |
+ |
attroff(COLOR_PAIR(4)); |
| 456 |
+ |
} |
| 457 |
|
} |
| 458 |
|
line++; |
| 459 |
|
move(line, 0); |
| 460 |
|
printw("Total"); |
| 461 |
|
move(line, 12); |
| 462 |
+ |
if(colors) { |
| 463 |
+ |
attron(COLOR_PAIR(4)); |
| 464 |
+ |
} |
| 465 |
|
printw("%7s", size_conv(r)); |
| 466 |
|
move(line, 26); |
| 467 |
|
printw("%7s", size_conv(w)); |
| 468 |
+ |
if(colors) { |
| 469 |
+ |
attroff(COLOR_PAIR(4)); |
| 470 |
+ |
} |
| 471 |
|
} |
| 472 |
|
|
| 473 |
|
line = 11; |
| 482 |
|
printw("%s", name); |
| 483 |
|
move(line, 62); |
| 484 |
|
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
| 485 |
+ |
if(colors) { |
| 486 |
+ |
attron(COLOR_PAIR(4)); |
| 487 |
+ |
} |
| 488 |
|
printw("%7s", size_conv(rt)); |
| 489 |
|
move(line, 72); |
| 490 |
|
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
| 491 |
|
printw("%7s", size_conv(wt)); |
| 492 |
|
network_stat_ptr++; |
| 493 |
|
line++; |
| 494 |
+ |
if(colors) { |
| 495 |
+ |
attroff(COLOR_PAIR(4)); |
| 496 |
+ |
} |
| 497 |
|
} |
| 498 |
|
line += 2; |
| 499 |
|
} |
| 508 |
|
move(line, 42); |
| 509 |
|
printw("%s", name); |
| 510 |
|
move(line, 62); |
| 511 |
+ |
if(colors) { |
| 512 |
+ |
attron(COLOR_PAIR(2)); |
| 513 |
+ |
} |
| 514 |
|
printw("%7s", size_conv(disk_stat_ptr->avail)); |
| 515 |
|
move(line, 73); |
| 516 |
+ |
if(colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_ALERT_DISK) { |
| 517 |
+ |
attron(A_STANDOUT); |
| 518 |
+ |
attron(A_BOLD); |
| 519 |
+ |
} else if (colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_WARN_DISK) { |
| 520 |
+ |
attron(A_BOLD); |
| 521 |
+ |
} |
| 522 |
|
printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
| 523 |
|
disk_stat_ptr++; |
| 524 |
|
line++; |
| 525 |
+ |
if(colors) { |
| 526 |
+ |
attroff(COLOR_PAIR(2)); |
| 527 |
+ |
attroff(A_STANDOUT); |
| 528 |
+ |
attroff(A_BOLD); |
| 529 |
+ |
} |
| 530 |
|
} |
| 531 |
|
} |
| 532 |
|
|
| 536 |
|
void sig_winch_handler(int dummy){ |
| 537 |
|
clear(); |
| 538 |
|
display_headings(); |
| 539 |
< |
display_data(); |
| 539 |
> |
display_data(0); |
| 540 |
|
signal(SIGWINCH, sig_winch_handler); |
| 541 |
|
} |
| 542 |
|
|
| 563 |
|
} |
| 564 |
|
|
| 565 |
|
void usage(char *progname){ |
| 566 |
+ |
#ifdef COLOR_SUPPORT |
| 567 |
+ |
fprintf(stderr, "Usage: %s [-d delay] [-c] [-v] [-h]\n\n", progname); |
| 568 |
+ |
#else |
| 569 |
|
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
| 570 |
+ |
#endif |
| 571 |
|
fprintf(stderr, " -d Sets the update time in seconds\n"); |
| 572 |
+ |
#ifdef COLOR_SUPPORT |
| 573 |
+ |
fprintf(stderr, " -c Enables coloured output\n"); |
| 574 |
+ |
#endif |
| 575 |
|
fprintf(stderr, " -v Prints version number\n"); |
| 576 |
|
fprintf(stderr, " -h Displays this help information.\n"); |
| 577 |
|
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
| 579 |
|
} |
| 580 |
|
|
| 581 |
|
int main(int argc, char **argv){ |
| 445 |
– |
|
| 582 |
|
extern char *optarg; |
| 583 |
|
int c; |
| 584 |
+ |
int colouron = 0; |
| 585 |
|
|
| 586 |
|
time_t last_update = 0; |
| 587 |
|
|
| 597 |
|
return 1; |
| 598 |
|
} |
| 599 |
|
|
| 600 |
< |
while ((c = getopt(argc, argv, "vhd:")) != -1){ |
| 600 |
> |
#ifdef COLOR_SUPPORT |
| 601 |
> |
while ((c = getopt(argc, argv, "d:cvh")) != -1){ |
| 602 |
> |
#else |
| 603 |
> |
while ((c = getopt(argc, argv, "d:vh")) != -1){ |
| 604 |
> |
#endif |
| 605 |
|
switch (c){ |
| 606 |
|
case 'd': |
| 607 |
|
delay = atoi(optarg); |
| 610 |
|
exit(1); |
| 611 |
|
} |
| 612 |
|
break; |
| 613 |
+ |
#ifdef COLOR_SUPPORT |
| 614 |
+ |
case 'c': |
| 615 |
+ |
colouron = 1; |
| 616 |
+ |
break; |
| 617 |
+ |
#endif |
| 618 |
|
case 'v': |
| 619 |
|
version_num(argv[0]); |
| 620 |
|
break; |
| 628 |
|
|
| 629 |
|
signal(SIGWINCH, sig_winch_handler); |
| 630 |
|
initscr(); |
| 631 |
+ |
#ifdef COLOR_SUPPORT |
| 632 |
+ |
/* turn on colour */ |
| 633 |
+ |
if (colouron) { |
| 634 |
+ |
if (has_colors()) { |
| 635 |
+ |
start_color(); |
| 636 |
+ |
use_default_colors(); |
| 637 |
+ |
init_pair(1,COLOR_RED,-1); |
| 638 |
+ |
init_pair(2,COLOR_GREEN,-1); |
| 639 |
+ |
init_pair(3,COLOR_YELLOW,-1); |
| 640 |
+ |
init_pair(4,COLOR_BLUE,-1); |
| 641 |
+ |
init_pair(5,COLOR_MAGENTA,-1); |
| 642 |
+ |
init_pair(6,COLOR_CYAN,-1); |
| 643 |
+ |
} else { |
| 644 |
+ |
fprintf(stderr, "Colour support disabled: your terminal does not support colour."); |
| 645 |
+ |
colouron = 0; |
| 646 |
+ |
} |
| 647 |
+ |
} |
| 648 |
+ |
#endif |
| 649 |
|
nonl(); |
| 650 |
|
cbreak(); |
| 651 |
|
noecho(); |
| 678 |
|
} |
| 679 |
|
last_update = now; |
| 680 |
|
|
| 681 |
< |
display_data(); |
| 681 |
> |
display_data(colouron); |
| 682 |
|
} |
| 683 |
|
|
| 684 |
|
endwin(); |