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> |
44 |
|
#include <curses.h> |
45 |
|
#endif |
46 |
|
|
47 |
+ |
#define THRESHOLD_LOAD 1.0 |
48 |
+ |
|
49 |
+ |
#define THRESHOLD_WARN_ZMB 0 |
50 |
+ |
|
51 |
+ |
#define THRESHOLD_WARN_CPU 60.0 |
52 |
+ |
#define THRESHOLD_ALERT_CPU 90.0 |
53 |
+ |
|
54 |
+ |
#define THRESHOLD_WARN_SWAP 75.0 |
55 |
+ |
#define THRESHOLD_ALERT_SWAP 90.0 |
56 |
+ |
|
57 |
+ |
#define THRESHOLD_WARN_MEM 75.0 |
58 |
+ |
#define THRESHOLD_ALERT_MEM 90.0 |
59 |
+ |
|
60 |
+ |
#define THRESHOLD_WARN_DISK 75.0 |
61 |
+ |
#define THRESHOLD_ALERT_DISK 90.0 |
62 |
+ |
|
63 |
|
typedef struct{ |
64 |
|
sg_cpu_percents *cpu_percents; |
65 |
|
sg_mem_stats *mem_stats; |
228 |
|
refresh(); |
229 |
|
} |
230 |
|
|
231 |
< |
void display_data(){ |
231 |
> |
void display_data(int colors){ |
232 |
|
char cur_time[20]; |
233 |
|
struct tm *tm_time; |
234 |
|
time_t epoc_time; |
254 |
|
if (ptr != NULL){ |
255 |
|
*ptr = '\0'; |
256 |
|
} |
257 |
+ |
if (colors) { |
258 |
+ |
attron(COLOR_PAIR(1)); |
259 |
+ |
} |
260 |
|
printw("%s", hostname); |
261 |
|
move(0,36); |
262 |
|
printw("%s", hr_uptime(stats.host_info->uptime)); |
265 |
|
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
266 |
|
move(0,61); |
267 |
|
printw("%s", cur_time); |
268 |
+ |
if (colors) { |
269 |
+ |
attroff(COLOR_PAIR(1)); |
270 |
+ |
} |
271 |
|
} |
272 |
|
|
273 |
|
if (stats.load_stats != NULL) { |
274 |
|
/* Load */ |
275 |
+ |
if (colors) { |
276 |
+ |
attron(COLOR_PAIR(6)); |
277 |
+ |
} |
278 |
|
move(2,12); |
279 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min5) > THRESHOLD_LOAD) { |
280 |
+ |
attron(A_BOLD); |
281 |
+ |
} |
282 |
|
printw("%6.2f", stats.load_stats->min1); |
283 |
+ |
if (colors) { |
284 |
+ |
attroff(A_BOLD); |
285 |
+ |
} |
286 |
|
move(3,12); |
287 |
+ |
if (colors && fabs(stats.load_stats->min5 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
288 |
+ |
attron(A_BOLD); |
289 |
+ |
} |
290 |
|
printw("%6.2f", stats.load_stats->min5); |
291 |
+ |
if (colors) { |
292 |
+ |
attroff(A_BOLD); |
293 |
+ |
} |
294 |
|
move(4,12); |
295 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
296 |
+ |
attron(A_BOLD); |
297 |
+ |
} |
298 |
|
printw("%6.2f", stats.load_stats->min15); |
299 |
+ |
if (colors) { |
300 |
+ |
attroff(A_BOLD); |
301 |
+ |
} |
302 |
|
} |
303 |
|
|
304 |
|
if (stats.cpu_percents != NULL) { |
308 |
|
move(3,33); |
309 |
|
printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap)); |
310 |
|
move(4,33); |
311 |
+ |
if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_ALERT_CPU) { |
312 |
+ |
attron(A_STANDOUT); |
313 |
+ |
} |
314 |
+ |
else if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_WARN_CPU) { |
315 |
+ |
attron(A_BOLD); |
316 |
+ |
} |
317 |
|
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
318 |
+ |
if(colors) { |
319 |
+ |
attroff(A_BOLD); |
320 |
+ |
attroff(A_STANDOUT); |
321 |
+ |
attron(COLOR_PAIR(6)); |
322 |
+ |
} |
323 |
|
} |
324 |
|
|
325 |
|
if (stats.process_count != NULL) { |
327 |
|
move(2, 54); |
328 |
|
printw("%5d", stats.process_count->running); |
329 |
|
move(2,74); |
330 |
+ |
if (colors && stats.process_count->zombie > THRESHOLD_WARN_ZMB) { |
331 |
+ |
attron(A_STANDOUT); |
332 |
+ |
} |
333 |
|
printw("%5d", stats.process_count->zombie); |
334 |
+ |
if(colors) { |
335 |
+ |
attroff(A_STANDOUT); |
336 |
+ |
attron(COLOR_PAIR(6)); |
337 |
+ |
} |
338 |
|
move(3, 54); |
339 |
|
printw("%5d", stats.process_count->sleeping); |
340 |
|
move(3, 74); |
347 |
|
printw("%5d", stats.user_stats->num_entries); |
348 |
|
} |
349 |
|
|
350 |
+ |
if(colors) { |
351 |
+ |
attroff(COLOR_PAIR(6)); |
352 |
+ |
attron(COLOR_PAIR(5)); |
353 |
+ |
} |
354 |
|
if (stats.mem_stats != NULL) { |
355 |
|
/* Mem */ |
356 |
|
move(6, 12); |
373 |
|
|
374 |
|
/* VM */ |
375 |
|
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
376 |
+ |
float f = 100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total; |
377 |
+ |
if (colors && f > THRESHOLD_ALERT_MEM) { |
378 |
+ |
attron(A_STANDOUT); |
379 |
+ |
} |
380 |
+ |
else if (colors && f > THRESHOLD_WARN_MEM) { |
381 |
+ |
attron(A_BOLD); |
382 |
+ |
} |
383 |
|
move(6, 54); |
384 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
384 |
> |
printw("%5.2f%%", f); |
385 |
> |
if (colors) { |
386 |
> |
attroff(A_STANDOUT); |
387 |
> |
attroff(A_BOLD); |
388 |
> |
attron(COLOR_PAIR(5)); |
389 |
> |
} |
390 |
|
} |
391 |
|
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
392 |
+ |
float f = 100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total; |
393 |
+ |
if (colors && f > THRESHOLD_ALERT_SWAP) { |
394 |
+ |
attron(A_STANDOUT); |
395 |
+ |
} |
396 |
+ |
else if (colors && f > THRESHOLD_WARN_SWAP) { |
397 |
+ |
attron(A_BOLD); |
398 |
+ |
} |
399 |
|
move(7, 54); |
400 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
400 |
> |
printw("%5.2f%%", f); |
401 |
> |
if (colors) { |
402 |
> |
attroff(A_STANDOUT); |
403 |
> |
attroff(A_BOLD); |
404 |
> |
attron(COLOR_PAIR(5)); |
405 |
> |
} |
406 |
|
} |
407 |
|
if (stats.mem_stats != NULL && stats.swap_stats != NULL && |
408 |
|
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
417 |
|
move(7, 74); |
418 |
|
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout); |
419 |
|
} |
420 |
+ |
if (colors) { |
421 |
+ |
attroff(COLOR_PAIR(5)); |
422 |
+ |
} |
423 |
|
|
424 |
|
line = 11; |
425 |
|
if (stats.disk_io_stats != NULL) { |
435 |
|
printw("%s", name); |
436 |
|
move(line, 12); |
437 |
|
rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; |
438 |
+ |
if(colors) { |
439 |
+ |
attron(COLOR_PAIR(4)); |
440 |
+ |
} |
441 |
|
printw("%7s", size_conv(rt)); |
442 |
|
r+=rt; |
443 |
|
move(line, 26); |
446 |
|
w+=wt; |
447 |
|
disk_io_stat_ptr++; |
448 |
|
line++; |
449 |
+ |
if(colors) { |
450 |
+ |
attroff(COLOR_PAIR(4)); |
451 |
+ |
} |
452 |
|
} |
453 |
|
line++; |
454 |
|
move(line, 0); |
455 |
|
printw("Total"); |
456 |
|
move(line, 12); |
457 |
+ |
if(colors) { |
458 |
+ |
attron(COLOR_PAIR(4)); |
459 |
+ |
} |
460 |
|
printw("%7s", size_conv(r)); |
461 |
|
move(line, 26); |
462 |
|
printw("%7s", size_conv(w)); |
463 |
+ |
if(colors) { |
464 |
+ |
attroff(COLOR_PAIR(4)); |
465 |
+ |
} |
466 |
|
} |
467 |
|
|
468 |
|
line = 11; |
477 |
|
printw("%s", name); |
478 |
|
move(line, 62); |
479 |
|
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
480 |
+ |
if(colors) { |
481 |
+ |
attron(COLOR_PAIR(4)); |
482 |
+ |
} |
483 |
|
printw("%7s", size_conv(rt)); |
484 |
|
move(line, 72); |
485 |
|
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
486 |
|
printw("%7s", size_conv(wt)); |
487 |
|
network_stat_ptr++; |
488 |
|
line++; |
489 |
+ |
if(colors) { |
490 |
+ |
attroff(COLOR_PAIR(4)); |
491 |
+ |
} |
492 |
|
} |
493 |
|
line += 2; |
494 |
|
} |
503 |
|
move(line, 42); |
504 |
|
printw("%s", name); |
505 |
|
move(line, 62); |
506 |
+ |
if(colors) { |
507 |
+ |
attron(COLOR_PAIR(2)); |
508 |
+ |
} |
509 |
|
printw("%7s", size_conv(disk_stat_ptr->avail)); |
510 |
|
move(line, 73); |
511 |
+ |
if(colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_ALERT_DISK) { |
512 |
+ |
attron(A_STANDOUT); |
513 |
+ |
} else if (colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_WARN_DISK) { |
514 |
+ |
attron(A_BOLD); |
515 |
+ |
} |
516 |
|
printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
517 |
|
disk_stat_ptr++; |
518 |
|
line++; |
519 |
+ |
if(colors) { |
520 |
+ |
attroff(COLOR_PAIR(2)); |
521 |
+ |
attroff(A_STANDOUT); |
522 |
+ |
attroff(A_BOLD); |
523 |
+ |
} |
524 |
|
} |
525 |
|
} |
526 |
|
|
530 |
|
void sig_winch_handler(int dummy){ |
531 |
|
clear(); |
532 |
|
display_headings(); |
533 |
< |
display_data(); |
533 |
> |
display_data(0); |
534 |
|
signal(SIGWINCH, sig_winch_handler); |
535 |
|
} |
536 |
|
|
557 |
|
} |
558 |
|
|
559 |
|
void usage(char *progname){ |
560 |
< |
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
560 |
> |
fprintf(stderr, "Usage: %s [-d delay] [-c] [-v] [-h]\n\n", progname); |
561 |
|
fprintf(stderr, " -d Sets the update time in seconds\n"); |
562 |
+ |
fprintf(stderr, " -c Enables coloured output\n"); |
563 |
|
fprintf(stderr, " -v Prints version number\n"); |
564 |
|
fprintf(stderr, " -h Displays this help information.\n"); |
565 |
|
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
567 |
|
} |
568 |
|
|
569 |
|
int main(int argc, char **argv){ |
445 |
– |
|
570 |
|
extern char *optarg; |
571 |
|
int c; |
572 |
+ |
int colouron = 0; |
573 |
|
|
574 |
|
time_t last_update = 0; |
575 |
|
|
585 |
|
return 1; |
586 |
|
} |
587 |
|
|
588 |
< |
while ((c = getopt(argc, argv, "vhd:")) != -1){ |
588 |
> |
while ((c = getopt(argc, argv, "d:cvh")) != -1){ |
589 |
|
switch (c){ |
590 |
|
case 'd': |
591 |
|
delay = atoi(optarg); |
594 |
|
exit(1); |
595 |
|
} |
596 |
|
break; |
597 |
+ |
case 'c': |
598 |
+ |
colouron = 1; |
599 |
+ |
break; |
600 |
|
case 'v': |
601 |
|
version_num(argv[0]); |
602 |
|
break; |
610 |
|
|
611 |
|
signal(SIGWINCH, sig_winch_handler); |
612 |
|
initscr(); |
613 |
+ |
/* turn on colour */ |
614 |
+ |
if (colouron) { |
615 |
+ |
if (has_colors()) { |
616 |
+ |
start_color(); |
617 |
+ |
use_default_colors(); |
618 |
+ |
init_pair(1,COLOR_RED,-1); |
619 |
+ |
init_pair(2,COLOR_GREEN,-1); |
620 |
+ |
init_pair(3,COLOR_YELLOW,-1); |
621 |
+ |
init_pair(4,COLOR_BLUE,-1); |
622 |
+ |
init_pair(5,COLOR_MAGENTA,-1); |
623 |
+ |
init_pair(6,COLOR_CYAN,-1); |
624 |
+ |
} else { |
625 |
+ |
fprintf(stderr, "Colour support disabled: your terminal does not support colour."); |
626 |
+ |
colouron = 0; |
627 |
+ |
} |
628 |
+ |
} |
629 |
|
nonl(); |
630 |
|
cbreak(); |
631 |
|
noecho(); |
658 |
|
} |
659 |
|
last_update = now; |
660 |
|
|
661 |
< |
display_data(); |
661 |
> |
display_data(colouron); |
662 |
|
} |
663 |
|
|
664 |
|
endwin(); |