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.11 by ats, Mon Oct 20 22:35:14 2003 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-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
# Line 16 | Line 16
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
# Line 34 | 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>
40 < #else
41 < #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 <        cpu_percent_t *cpu_percents;
64 <        mem_stat_t *mem_stats;
65 <        swap_stat_t *swap_stats;
66 <        load_stat_t *load_stats;
67 <        process_stat_t *process_stats;
68 <        page_stat_t *page_stats;
63 >        sg_cpu_percents *cpu_percents;
64 >        sg_mem_stats *mem_stats;
65 >        sg_swap_stats *swap_stats;
66 >        sg_load_stats *load_stats;
67 >        sg_process_count *process_count;
68 >        sg_page_stats *page_stats;
69  
70 <        network_stat_t *network_stats;
71 <        int network_entries;
70 >        sg_network_io_stats *network_io_stats;
71 >        int network_io_entries;
72  
73 <        diskio_stat_t *diskio_stats;
74 <        int diskio_entries;
73 >        sg_disk_io_stats *disk_io_stats;
74 >        int disk_io_entries;
75  
76 <        disk_stat_t *disk_stats;
77 <        int disk_entries;
76 >        sg_fs_stats *fs_stats;
77 >        int fs_entries;
78  
79 <        general_stat_t *general_stats;
80 <        user_stat_t *user_stats;
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 76 | 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 150 | 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 177 | 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_stats != NULL) {
209 >        if (stats.network_io_stats != NULL) {
210                  /* Network IO */
211                  move(line, 42);
212                  printw("Network Interface");
# Line 188 | Line 214 | void display_headings(){
214                  printw("rx");
215                  move(line, 77);
216                  printw("tx");
217 <                line += 2 + stats.network_entries;
217 >                line += 2 + stats.network_io_entries;
218          }
219  
220          move(line, 42);
# Line 201 | 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;
234          int counter, line;
235          long long r,w;
236          long long rt, wt;
237 <        diskio_stat_t *diskio_stat_ptr;
238 <        network_stat_t *network_stat_ptr;
239 <        disk_stat_t *disk_stat_ptr;
237 >        sg_disk_io_stats *disk_io_stat_ptr;
238 >        sg_network_io_stats *network_stat_ptr;
239 >        sg_fs_stats *disk_stat_ptr;
240          /* Size before it will start overwriting "uptime" */
241          char hostname[15];
242          char *ptr;
243  
244 <        if (stats.general_stats != NULL) {
244 >        if (stats.host_info != NULL) {
245                  move(0,12);
246 <                strncpy(hostname, stats.general_stats->hostname, (sizeof(hostname) - 1));
246 >                strncpy(hostname, stats.host_info->hostname, (sizeof(hostname) - 1));
247                  /* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */
248                  hostname[14] = '\0';
249                  ptr=strchr(hostname, '.');
# Line 226 | 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.general_stats->uptime));
261 >                printw("%s", hr_uptime(stats.host_info->uptime));
262                  epoc_time=time(NULL);
263                  tm_time = localtime(&epoc_time);
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 254 | 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_stats != NULL) {
325 >        if (stats.process_count != NULL) {
326                  /* Process */
327                  move(2, 54);
328 <                printw("%5d", stats.process_stats->running);
328 >                printw("%5d", stats.process_count->running);
329                  move(2,74);
330 <                printw("%5d", stats.process_stats->zombie);
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_stats->sleeping);
340 >                printw("%5d", stats.process_count->sleeping);
341                  move(3, 74);
342 <                printw("%5d", stats.process_stats->total);
342 >                printw("%5d", stats.process_count->total);
343                  move(4, 54);
344 <                printw("%5d", stats.process_stats->stopped);
344 >                printw("%5d", stats.process_count->stopped);
345          }
346          if (stats.user_stats != NULL) {
347                  move(4,74);
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 295 | Line 372 | void display_data(){
372                  printw("%8s", size_conv(stats.swap_stats->free));
373          }
374  
375 <        if (stats.mem_stats != NULL && stats.swap_stats != NULL) {      
376 <                /* VM */
375 >        /* VM */
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) {
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) {
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.diskio_stats != NULL) {      
428 >        if (stats.disk_io_stats != NULL) {
429                  /* Disk IO */
430 <                diskio_stat_ptr = stats.diskio_stats;
430 >                disk_io_stat_ptr = stats.disk_io_stats;
431                  r=0;
432                  w=0;
433 <                for(counter=0;counter<stats.diskio_entries;counter++){
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", diskio_stat_ptr->disk_name);
438 >                        printw("%s", name);
439                          move(line, 12);
440 <                        rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes;
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);
447 <                        wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes;
447 >                        wt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->write_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->write_bytes;
448                          printw("%7s", size_conv(wt));
449                          w+=wt;
450 <                        diskio_stat_ptr++;
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_stats != NULL) {      
472 >        if (stats.network_io_stats != NULL) {
473                  /* Network */
474 <                network_stat_ptr = stats.network_stats;
475 <                for(counter=0;counter<stats.network_entries;counter++){
476 <                        move(line, 42);
477 <                        printw("%s", network_stat_ptr->interface_name);
474 >                network_stat_ptr = stats.network_io_stats;
475 >                for(counter=0;counter<stats.network_io_entries;counter++){
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.disk_stats != NULL) {
499 >        if (stats.fs_stats != NULL) {
500                  /* Disk */
501 <                disk_stat_ptr = stats.disk_stats;
502 <                for(counter=0;counter<stats.disk_entries;counter++){
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(){
542 <        stats.cpu_percents = cpu_percent_usage();
543 <        stats.mem_stats = get_memory_stats();
544 <        stats.swap_stats = get_swap_stats();
545 <        stats.load_stats = get_load_stats();
546 <        stats.process_stats = get_process_stats();
547 <        stats.page_stats = get_page_stats_diff();
548 <        stats.network_stats = get_network_stats_diff(&(stats.network_entries));
549 <        stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries));
550 <        stats.disk_stats = get_disk_stats(&(stats.disk_entries));
551 <        stats.general_stats = get_general_stats();
552 <        stats.user_stats = get_user_stats();
542 >        stats.cpu_percents = sg_get_cpu_percents();
543 >        stats.mem_stats = sg_get_mem_stats();
544 >        stats.swap_stats = sg_get_swap_stats();
545 >        stats.load_stats = sg_get_load_stats();
546 >        stats.process_count = sg_get_process_count();
547 >        stats.page_stats = sg_get_page_stats_diff();
548 >        stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries));
549 >        stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries));
550 >        stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries));
551 >        stats.host_info = sg_get_host_info();
552 >        stats.user_stats = sg_get_user_stats();
553  
554          return 1;
555   }
# Line 409 | 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  
584 <        extern char *optarg;
423 <        extern int optind;
424 <        int c;
584 >        time_t last_update = 0;
585  
586          WINDOW *window;
587  
428        int stdin_fileno;
429        fd_set infds;
430        struct timeval timeout;
431
588          extern int errno;
433        char ch;
589  
590          int delay=2;
591 < #ifdef ALLBSD
592 <        gid_t gid;
593 < #endif
594 <        statgrab_init();
440 < #ifdef ALLBSD
441 <        if((setegid(getgid())) != 0){
442 <                fprintf(stderr, "Failed to lose setgid'ness\n");
591 >
592 >        sg_init();
593 >        if(sg_drop_privileges() != 0){
594 >                fprintf(stderr, "Failed to drop setuid/setgid privileges\n");
595                  return 1;
596          }
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 <                
604 <        while ((c = getopt(argc, argv, "vhd:")) != EOF){
605 <                switch (c){
606 <                        case 'd':
450 <                                delay = atoi(optarg);
451 <                                if (delay == 0){
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 <                                delay--;
611 <                                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 <                }
467 <        }
624 >                }
625 >        }
626  
627          signal(SIGWINCH, sig_winch_handler);
628 <        initscr();
629 <        nonl();
630 <        cbreak();
631 <        noecho();
632 <        window=newwin(0, 0, 0, 0);
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);
653          clear();
654  
655          if(!get_stats()){
# Line 481 | Line 659 | int main(int argc, char **argv){
659          }
660  
661          display_headings();
484        stdin_fileno=fileno(stdin);
662  
663          for(;;){
664 +                time_t now;
665 +                int ch = getch();
666  
667 <                FD_ZERO(&infds);
668 <                FD_SET(stdin_fileno, &infds);
490 <                timeout.tv_sec = delay;
491 <                timeout.tv_usec = 0;
492 <                
493 <                if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){
494 <                        if(errno!=EINTR){
495 <                                perror("select failed");
496 <                                exit(1);
497 <                        }
667 >                if (ch == 'q'){
668 >                        break;
669                  }
670  
671 <                if(FD_ISSET(stdin_fileno, &infds)){
672 <                        ch=getch();
673 <                        if (ch == 'q'){
674 <                                endwin();
675 <                                return 0;
676 <                        }
671 >                /* To keep the numbers slightly accurate we do not want them
672 >                 * updating more frequently than once a second.
673 >                 */
674 >                now = time(NULL);
675 >                if ((now - last_update) >= 1) {
676 >                        get_stats();
677                  }
678 +                last_update = now;
679  
680 <                get_stats();
680 >                display_data(colouron);
681 >        }
682  
510                display_data();
511
512                /* To keep the numbers slightly accurate we do not want them updating more
513                 * frequently than once a second.
514                 */
515                sleep(1);
516        }      
517
683          endwin();
684          return 0;
685 < }      
685 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines