ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/saidar/saidar.c
(Generate patch)

Comparing projects/libstatgrab/src/saidar/saidar.c (file contents):
Revision 1.27 by ats, Mon Apr 5 15:40:17 2004 UTC vs.
Revision 1.40 by tdb, Sun Dec 17 01:06:16 2006 UTC

# Line 1 | Line 1
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   *
# Line 36 | Line 36
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>
42 < #else
43 < #include <curses.h>
42 > #define COLOR_SUPPORT
43   #endif
44 + #include CURSES_HEADER_FILE
45  
46 + #define THRESHOLD_LOAD 1.0
47 +
48 + #define THRESHOLD_WARN_ZMB 0
49 +
50 + #define THRESHOLD_WARN_CPU 60.0
51 + #define THRESHOLD_ALERT_CPU 90.0
52 +
53 + #define THRESHOLD_WARN_SWAP 75.0
54 + #define THRESHOLD_ALERT_SWAP 90.0
55 +
56 + #define THRESHOLD_WARN_MEM 75.0
57 + #define THRESHOLD_ALERT_MEM 90.0
58 +
59 + #define THRESHOLD_WARN_DISK 75.0
60 + #define THRESHOLD_ALERT_DISK 90.0
61 +
62   typedef struct{
63          sg_cpu_percents *cpu_percents;
64          sg_mem_stats *mem_stats;
# Line 63 | Line 79 | typedef struct{
79          sg_host_info *host_info;
80          sg_user_stats *user_stats;
81   }stats_t;
82 <        
82 >
83   stats_t stats;
84  
85   char *size_conv(long long number){
86          char type[] = {'B', 'K', 'M', 'G', 'T'};
87          int x=0;
88 +        int sign=1;
89          static char string[10];
90  
91 +        if(number < 0){
92 +                sign=-1;
93 +                number=-number;
94 +        }
95 +
96          for(;x<5;x++){
97                  if( (number/1024) < (100)) {
98                          break;
# Line 78 | Line 100 | char *size_conv(long long number){
100                  number = (number/1024);
101          }
102  
103 <        snprintf(string, 10, "%lld%c", number, type[x]);        
103 >        number = number*sign;
104 >
105 >        snprintf(string, 10, "%lld%c", number, type[x]);
106          return string;
107 <        
107 >
108   }
109  
110   char *hr_uptime(time_t time){
111          int day = 0, hour = 0, min = 0;
112          static char uptime_str[25];
113          int sec = (int) time;
114 <        
114 >
115          day = sec / (24*60*60);
116          sec = sec % (24*60*60);
117          hour = sec / (60*60);
# Line 152 | Line 176 | void display_headings(){
176          printw("Mem Free  :");
177  
178          /* Swap */
179 <        move(6, 21);
180 <        printw("Swap Total:");
181 <        move(7, 21);
182 <        printw("Swap Used :");
183 <        move(8, 21);
184 <        printw("Swap Free :");
179 >        move(6, 21);
180 >        printw("Swap Total:");
181 >        move(7, 21);
182 >        printw("Swap Used :");
183 >        move(8, 21);
184 >        printw("Swap Free :");
185  
186          /* VM */
187          move(6, 42);
# Line 179 | Line 203 | void display_headings(){
203          move(10,15);
204          printw("Read");
205          move(10,28);
206 <        printw("Write");        
206 >        printw("Write");
207  
208          line = 10;
209          if (stats.network_io_stats != NULL) {
# Line 203 | Line 227 | void display_headings(){
227          refresh();
228   }
229  
230 < void display_data(){
230 > void display_data(int colors){
231          char cur_time[20];
232          struct tm *tm_time;
233          time_t epoc_time;
# Line 228 | Line 252 | void display_data(){
252                   */
253                  if (ptr != NULL){
254                          *ptr = '\0';
255 <                }      
255 >                }
256 >                if (colors) {
257 >                        attron(COLOR_PAIR(1));
258 >                }
259                  printw("%s", hostname);
260                  move(0,36);
261                  printw("%s", hr_uptime(stats.host_info->uptime));
# Line 237 | Line 264 | void display_data(){
264                  strftime(cur_time, 20, "%Y-%m-%d %T", tm_time);
265                  move(0,61);
266                  printw("%s", cur_time);
267 +                if (colors) {
268 +                        attroff(COLOR_PAIR(1));
269 +                }
270          }
271  
272          if (stats.load_stats != NULL) {
273                  /* Load */
274 +                if (colors) {
275 +                        attron(COLOR_PAIR(6));
276 +                }
277                  move(2,12);
278 +                if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min5) > THRESHOLD_LOAD) {
279 +                        attron(A_BOLD);
280 +                }
281                  printw("%6.2f", stats.load_stats->min1);
282 +                if (colors) {
283 +                        attroff(A_BOLD);
284 +                }
285                  move(3,12);
286 +                if (colors && fabs(stats.load_stats->min5 - stats.load_stats->min15) > THRESHOLD_LOAD) {
287 +                        attron(A_BOLD);
288 +                }
289                  printw("%6.2f", stats.load_stats->min5);
290 +                if (colors) {
291 +                        attroff(A_BOLD);
292 +                }
293                  move(4,12);
294 +                if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min15) > THRESHOLD_LOAD) {
295 +                        attron(A_BOLD);
296 +                }
297                  printw("%6.2f", stats.load_stats->min15);
298 +                if (colors) {
299 +                        attroff(A_BOLD);
300 +                }
301          }
302  
303          if (stats.cpu_percents != NULL) {
# Line 256 | Line 307 | void display_data(){
307                  move(3,33);
308                  printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
309                  move(4,33);
310 +                if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_ALERT_CPU) {
311 +                        attron(A_STANDOUT);
312 +                        attron(A_BOLD);
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) {
# Line 264 | Line 327 | void display_data(){
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 +                        attron(A_BOLD);
333 +                }
334                  printw("%5d", stats.process_count->zombie);
335 +                if(colors) {
336 +                        attroff(A_STANDOUT);
337 +                        attroff(A_BOLD);
338 +                }
339                  move(3, 54);
340                  printw("%5d", stats.process_count->sleeping);
341                  move(3, 74);
# Line 277 | Line 348 | void display_data(){
348                  printw("%5d", stats.user_stats->num_entries);
349          }
350  
351 +        if(colors) {
352 +                attroff(COLOR_PAIR(6));
353 +                attron(COLOR_PAIR(5));
354 +        }
355          if (stats.mem_stats != NULL) {
356                  /* Mem */
357                  move(6, 12);
358 <                printw("%7s", size_conv(stats.mem_stats->total));      
358 >                printw("%7s", size_conv(stats.mem_stats->total));
359                  move(7, 12);
360 <                printw("%7s", size_conv(stats.mem_stats->used));        
360 >                printw("%7s", size_conv(stats.mem_stats->used));
361                  move(8, 12);
362                  printw("%7s", size_conv(stats.mem_stats->free));
363          }
364 <        
365 <        if (stats.swap_stats != NULL) {
364 >
365 >        if (stats.swap_stats != NULL) {
366                  /* Swap */
367                  move(6, 32);
368                  printw("%8s", size_conv(stats.swap_stats->total));
# Line 298 | Line 373 | void display_data(){
373          }
374  
375          /* VM */
376 <        if (stats.mem_stats != NULL && stats.mem_stats->total != 0) {  
376 >        if (stats.mem_stats != NULL && stats.mem_stats->total != 0) {
377 >                float f = 100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total;
378 >                if (colors && f > THRESHOLD_ALERT_MEM) {
379 >                        attron(A_STANDOUT);
380 >                        attron(A_BOLD);
381 >                }
382 >                else if (colors && f > THRESHOLD_WARN_MEM) {
383 >                        attron(A_BOLD);
384 >                }
385                  move(6, 54);
386 <                printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total));
386 >                printw("%5.2f%%", f);
387 >                if (colors) {
388 >                        attroff(A_STANDOUT);
389 >                        attroff(A_BOLD);
390 >                        attron(COLOR_PAIR(5));
391 >                }
392          }
393 <        if (stats.swap_stats != NULL && stats.swap_stats->total != 0) {
393 >        if (stats.swap_stats != NULL && stats.swap_stats->total != 0) {
394 >                float f = 100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total;
395 >                if (colors && f > THRESHOLD_ALERT_SWAP) {
396 >                        attron(A_STANDOUT);
397 >                        attron(A_BOLD);
398 >                }
399 >                else if (colors && f > THRESHOLD_WARN_SWAP) {
400 >                        attron(A_BOLD);
401 >                }
402                  move(7, 54);
403 <                printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total));
403 >                printw("%5.2f%%", f);
404 >                if (colors) {
405 >                        attroff(A_STANDOUT);
406 >                        attroff(A_BOLD);
407 >                        attron(COLOR_PAIR(5));
408 >                }
409          }
410          if (stats.mem_stats != NULL && stats.swap_stats != NULL &&
411 <            stats.mem_stats->total != 0 && stats.swap_stats->total != 0) {      
411 >            stats.mem_stats->total != 0 && stats.swap_stats->total != 0) {
412                  move(8, 54);
413                  printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
414          }
415  
416 <        if (stats.page_stats != NULL) {
416 >        if (stats.page_stats != NULL) {
417                  /* Paging */
418                  move(6, 74);
419                  printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
420                  move(7, 74);
421                  printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
422          }
423 +        if (colors) {
424 +                attroff(COLOR_PAIR(5));
425 +        }
426  
427          line = 11;
428 <        if (stats.disk_io_stats != NULL) {      
428 >        if (stats.disk_io_stats != NULL) {
429                  /* Disk IO */
430                  disk_io_stat_ptr = stats.disk_io_stats;
431                  r=0;
432                  w=0;
433                  for(counter=0;counter<stats.disk_io_entries;counter++){
434 +                        char name[12];
435 +                        strncpy(name, disk_io_stat_ptr->disk_name, sizeof(name));
436 +                        name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
437                          move(line, 0);
438 <                        printw("%s", disk_io_stat_ptr->disk_name);
438 >                        printw("%s", name);
439                          move(line, 12);
440                          rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes;
441 +                        if(colors) {
442 +                                attron(COLOR_PAIR(4));
443 +                        }
444                          printw("%7s", size_conv(rt));
445                          r+=rt;
446                          move(line, 26);
# Line 339 | Line 449 | void display_data(){
449                          w+=wt;
450                          disk_io_stat_ptr++;
451                          line++;
452 +                        if(colors) {
453 +                                attroff(COLOR_PAIR(4));
454 +                        }
455                  }
456                  line++;
457                  move(line, 0);
458                  printw("Total");
459                  move(line, 12);
460 +                if(colors) {
461 +                        attron(COLOR_PAIR(4));
462 +                }
463                  printw("%7s", size_conv(r));
464                  move(line, 26);
465                  printw("%7s", size_conv(w));
466 +                if(colors) {
467 +                        attroff(COLOR_PAIR(4));
468 +                }
469          }
470  
471          line = 11;
472 <        if (stats.network_io_stats != NULL) {  
472 >        if (stats.network_io_stats != NULL) {
473                  /* Network */
474                  network_stat_ptr = stats.network_io_stats;
475                  for(counter=0;counter<stats.network_io_entries;counter++){
476 <                        move(line, 42);
477 <                        printw("%s", network_stat_ptr->interface_name);
476 >                        char name[20];
477 >                        strncpy(name, network_stat_ptr->interface_name, sizeof(name));
478 >                        name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
479 >                        move(line, 42);
480 >                        printw("%s", name);
481                          move(line, 62);
482                          rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
483 +                        if(colors) {
484 +                                attron(COLOR_PAIR(4));
485 +                        }
486                          printw("%7s", size_conv(rt));
487                          move(line, 72);
488 <                        wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
488 >                        wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
489                          printw("%7s", size_conv(wt));
490                          network_stat_ptr++;
491                          line++;
492 +                        if(colors) {
493 +                                attroff(COLOR_PAIR(4));
494 +                        }
495                  }
496                  line += 2;
497          }
498  
499 <        if (stats.fs_stats != NULL) {  
499 >        if (stats.fs_stats != NULL) {
500                  /* Disk */
501                  disk_stat_ptr = stats.fs_stats;
502                  for(counter=0;counter<stats.fs_entries;counter++){
503 +                        char name[20];
504 +                        strncpy(name, disk_stat_ptr->mnt_point, sizeof(name));
505 +                        name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
506                          move(line, 42);
507 <                        printw("%s", disk_stat_ptr->mnt_point);
507 >                        printw("%s", name);
508                          move(line, 62);
509 +                        if(colors) {
510 +                                attron(COLOR_PAIR(2));
511 +                        }
512                          printw("%7s", size_conv(disk_stat_ptr->avail));
513                          move(line, 73);
514 <                        printw("%5.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)));
514 >                        if(colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_ALERT_DISK) {
515 >                                attron(A_STANDOUT);
516 >                                attron(A_BOLD);
517 >                        } else if (colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_WARN_DISK) {
518 >                                attron(A_BOLD);
519 >                        }
520 >                        printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)));
521                          disk_stat_ptr++;
522                          line++;
523 +                        if(colors) {
524 +                                attroff(COLOR_PAIR(2));
525 +                                attroff(A_STANDOUT);
526 +                                attroff(A_BOLD);
527 +                        }
528                  }
529          }
530  
531          refresh();
532   }
533  
534 < void sig_winch_handler(int sig){
534 > void sig_winch_handler(int dummy){
535          clear();
536          display_headings();
537 <        display_data();
538 <        signal(SIGWINCH, sig_winch_handler);
537 >        display_data(0);
538 >        signal(SIGWINCH, sig_winch_handler);
539   }
540  
541   int get_stats(){
# Line 416 | Line 561 | void version_num(char *progname){
561   }
562  
563   void usage(char *progname){
564 <        fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname);
565 <        fprintf(stderr, "  -d    Sets the update time in seconds\n");
564 > #ifdef COLOR_SUPPORT
565 >        fprintf(stderr, "Usage: %s [-d delay] [-c] [-v] [-h]\n\n", progname);
566 > #else
567 >        fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname);
568 > #endif
569 >        fprintf(stderr, "  -d    Sets the update time in seconds\n");
570 > #ifdef COLOR_SUPPORT
571 >        fprintf(stderr, "  -c    Enables coloured output\n");
572 > #endif
573          fprintf(stderr, "  -v    Prints version number\n");
574 <        fprintf(stderr, "  -h    Displays this help information.\n");
575 <        fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
576 <        exit(1);
574 >        fprintf(stderr, "  -h    Displays this help information.\n");
575 >        fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
576 >        exit(1);
577   }
578  
579   int main(int argc, char **argv){
580 +        extern char *optarg;
581 +        int c;
582 +        int colouron = 0;
583  
429        extern char *optarg;
430        int c;
431
584          time_t last_update = 0;
585  
586          WINDOW *window;
# Line 442 | Line 594 | int main(int argc, char **argv){
594                  fprintf(stderr, "Failed to drop setuid/setgid privileges\n");
595                  return 1;
596          }
597 <                
598 <        while ((c = getopt(argc, argv, "vhd:")) != -1){
599 <                switch (c){
600 <                        case 'd':
601 <                                delay = atoi(optarg);
597 >
598 > #ifdef COLOR_SUPPORT
599 >        while ((c = getopt(argc, argv, "d:cvh")) != -1){
600 > #else
601 >        while ((c = getopt(argc, argv, "d:vh")) != -1){
602 > #endif
603 >                switch (c){
604 >                        case 'd':
605 >                                delay = atoi(optarg);
606                                  if (delay < 1){
607                                          fprintf(stderr, "Time must be 1 second or greater\n");
608                                          exit(1);
609                                  }
610 <                                break;
610 >                                break;
611 > #ifdef COLOR_SUPPORT
612 >                        case 'c':
613 >                                colouron = 1;
614 >                                break;
615 > #endif
616                          case 'v':
617 <                                version_num(argv[0]);  
617 >                                version_num(argv[0]);
618                                  break;
619                          case 'h':
620                          default:
621                                  usage(argv[0]);
622                                  return 1;
623                                  break;
624 <                }
625 <        }
624 >                }
625 >        }
626  
627          signal(SIGWINCH, sig_winch_handler);
628 <        initscr();
629 <        nonl();
630 <        cbreak();
631 <        noecho();
628 >        initscr();
629 > #ifdef COLOR_SUPPORT
630 >        /* turn on colour */
631 >        if (colouron) {
632 >                if (has_colors()) {
633 >                        start_color();
634 >                        use_default_colors();
635 >                        init_pair(1,COLOR_RED,-1);
636 >                        init_pair(2,COLOR_GREEN,-1);
637 >                        init_pair(3,COLOR_YELLOW,-1);
638 >                        init_pair(4,COLOR_BLUE,-1);
639 >                        init_pair(5,COLOR_MAGENTA,-1);
640 >                        init_pair(6,COLOR_CYAN,-1);
641 >                } else {
642 >                        fprintf(stderr, "Colour support disabled: your terminal does not support colour.");
643 >                        colouron = 0;
644 >                }
645 >        }
646 > #endif
647 >        nonl();
648 >        curs_set(0);
649 >        cbreak();
650 >        noecho();
651          timeout(delay * 1000);
652 <        window=newwin(0, 0, 0, 0);
652 >        window=newwin(0, 0, 0, 0);
653          clear();
654  
655          if(!get_stats()){
# Line 497 | Line 677 | int main(int argc, char **argv){
677                  }
678                  last_update = now;
679  
680 <                display_data();
681 <        }      
680 >                display_data(colouron);
681 >        }
682  
683          endwin();
684          return 0;
685 < }      
685 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines