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.2 by tdb, Thu Oct 9 16:00:31 2003 UTC vs.
Revision 1.39 by tdb, Fri Dec 1 14:21:17 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 102 | Line 128 | char *hr_uptime(time_t time){
128   }
129  
130   void display_headings(){
131 +        int line;
132 +
133          move(0,0);
134          printw("Hostname  :");
135          move(0,27);
# Line 148 | 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 175 | Line 203 | void display_headings(){
203          move(10,15);
204          printw("Read");
205          move(10,28);
206 <        printw("Write");        
206 >        printw("Write");
207  
208 <        /* Network IO */
209 <        move(10, 42);
210 <        printw("Network Interface");
211 <        move(10, 67);
212 <        printw("rx");
213 <        move(10,77);
214 <        printw("tx");
208 >        line = 10;
209 >        if (stats.network_io_stats != NULL) {
210 >                /* Network IO */
211 >                move(line, 42);
212 >                printw("Network Interface");
213 >                move(line, 67);
214 >                printw("rx");
215 >                move(line, 77);
216 >                printw("tx");
217 >                line += 2 + stats.network_io_entries;
218 >        }
219  
220 <        move(12+stats.network_entries, 42);
220 >        move(line, 42);
221          printw("Mount Point");
222 <        move(12+stats.network_entries, 65);
222 >        move(line, 65);
223          printw("Free");
224 <        move(12+stats.network_entries, 75);
224 >        move(line, 75);
225          printw("Used");
226  
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;
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 <        move(0,12);
245 <        printw("%s", stats.general_stats->hostname);
246 <        move(0,36);
247 <        printw("%s", hr_uptime(stats.general_stats->uptime));
248 <        epoc_time=time(NULL);
249 <        tm_time = localtime(&epoc_time);
250 <        strftime(cur_time, 20, "%Y-%m-%d %T", tm_time);
251 <        move(0,61);
252 <        printw("%s", cur_time);
244 >        if (stats.host_info != NULL) {
245 >                move(0,12);
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, '.');
250 >                /* Some hosts give back a FQDN for hostname. To avoid this, we'll
251 >                 * just blank out everything after the first "."
252 >                 */
253 >                if (ptr != NULL){
254 >                        *ptr = '\0';
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));
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 <        /* Load */
273 <        move(2,12);
274 <        printw("%6.2f", stats.load_stats->min1);
275 <        move(3,12);
276 <        printw("%6.2f", stats.load_stats->min5);
277 <        move(4,12);
278 <        printw("%6.2f", stats.load_stats->min15);
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 <        /* CPU */
304 <        move(2,33);
305 <        printw("%6.2f%%", stats.cpu_percents->idle);
306 <        move(3,33);
307 <        printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
308 <        move(4,33);
309 <        printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice));
303 >        if (stats.cpu_percents != NULL) {
304 >                /* CPU */
305 >                move(2,33);
306 >                printw("%6.2f%%", stats.cpu_percents->idle);
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 <        /* Process */
326 <        move(2, 54);
327 <        printw("%5d", stats.process_stats->running);
328 <        move(2,74);
329 <        printw("%5d", stats.process_stats->zombie);
330 <        move(3, 54);
331 <        printw("%5d", stats.process_stats->sleeping);
332 <        move(3, 74);
333 <        printw("%5d", stats.process_stats->total);
334 <        move(4, 54);
335 <        printw("%5d", stats.process_stats->stopped);
336 <        move(4,74);
337 <        printw("%5d", stats.user_stats->num_entries);
325 >        if (stats.process_count != NULL) {
326 >                /* Process */
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);
342 >                printw("%5d", stats.process_count->total);
343 >                move(4, 54);
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 <        /* Mem */
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));
359 >                move(7, 12);
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 <        move(6, 12);
366 <        printw("%7s", size_conv(stats.mem_stats->total));      
367 <        move(7, 12);
368 <        printw("%7s", size_conv(stats.mem_stats->used));        
369 <        move(8, 12);
370 <        printw("%7s", size_conv(stats.mem_stats->free));
371 <        
372 <        /* Swap */
373 <        move(6, 32);
260 <        printw("%8s", size_conv(stats.swap_stats->total));
261 <        move(7, 32);
262 <        printw("%8s", size_conv(stats.swap_stats->used));
263 <        move(8, 32);
264 <        printw("%8s", size_conv(stats.swap_stats->free));
365 >        if (stats.swap_stats != NULL) {
366 >                /* Swap */
367 >                move(6, 32);
368 >                printw("%8s", size_conv(stats.swap_stats->total));
369 >                move(7, 32);
370 >                printw("%8s", size_conv(stats.swap_stats->used));
371 >                move(8, 32);
372 >                printw("%8s", size_conv(stats.swap_stats->free));
373 >        }
374  
375          /* VM */
376 <        move(6, 54);
377 <        printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total));
378 <        move(7, 54);
379 <        printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total));
380 <        move(8, 54);
381 <        printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
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%%", 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%%", 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 <        /* Paging */
417 <        move(6, 74);
418 <        printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
419 <        move(7, 74);
420 <        printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
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 <        /* Disk IO */
428 <        diskio_stat_ptr = stats.diskio_stats;
429 <        r=0;
430 <        w=0;
431 <        for(counter=0;counter<stats.diskio_entries;counter++){
432 <                move(11+counter, 0);
433 <                printw("%s", diskio_stat_ptr->disk_name);
434 <                move(11+counter, 12);
435 <                rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes;
436 <                printw("%7s", size_conv(rt));
437 <                r+=rt;
438 <                move(11+counter, 26);
439 <                wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes;
440 <                printw("%7s", size_conv(wt));
441 <                w+=wt;
442 <                diskio_stat_ptr++;
427 >        line = 11;
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", 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);
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 >                        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          }
297        move(12+counter, 0);
298        printw("Total");
299        move(12+counter, 12);
300        printw("%7s", size_conv(r));
301        move(12+counter, 26);
302        printw("%7s", size_conv(w));
470  
471 <        /* Network */
472 <        network_stat_ptr = stats.network_stats;
473 <        for(counter=0;counter<stats.network_entries;counter++){
474 <                move(11+counter, 42);
475 <                printw("%s", network_stat_ptr->interface_name);
476 <                move(11+counter, 62);
477 <                rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
478 <                printw("%7s", size_conv(rt));
479 <                move(11+counter, 72);
480 <                wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
481 <                printw("%7s", size_conv(wt));
482 <                network_stat_ptr++;
471 >        line = 11;
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 >                        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;
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 <        disk_stat_ptr = stats.disk_stats;
500 <        for(counter=0;counter<stats.disk_entries;counter++){
501 <                move(13+stats.network_entries+counter, 42);
502 <                printw("%s", disk_stat_ptr->mnt_point);
503 <                move(13+stats.network_entries+counter, 62);
504 <                printw("%7s", size_conv(disk_stat_ptr->avail));
505 <                move(13+stats.network_entries+counter, 73);
506 <                printw("%5.2f%%", 100.00 * ((float)disk_stat_ptr->used / (float)disk_stat_ptr->size));
507 <                disk_stat_ptr++;
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", 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 >                        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 <                if((stats.cpu_percents = cpu_percent_usage()) == NULL) return 0;
543 <                if((stats.mem_stats = get_memory_stats()) == NULL) return 0;
544 <                if((stats.swap_stats = get_swap_stats()) == NULL) return 0;
545 <                if((stats.load_stats = get_load_stats()) == NULL) return 0;
546 <                if((stats.process_stats = get_process_stats()) == NULL) return 0;
547 <                if((stats.page_stats = get_page_stats_diff()) == NULL) return 0;
548 <                if((stats.network_stats = get_network_stats_diff(&(stats.network_entries))) == NULL) return 0;
549 <                if((stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries))) == NULL) return 0;
550 <                if((stats.disk_stats = get_disk_stats(&(stats.disk_entries))) == NULL) return 0;
551 <                if((stats.general_stats = get_general_stats()) == NULL) return 0;
552 <                if((stats.user_stats = get_user_stats()) == NULL) return 0;
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;
554 >        return 1;
555   }
556  
557 + void version_num(char *progname){
558 +        fprintf(stderr, "%s version %s\n", progname, PACKAGE_VERSION);
559 +        fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
560 +        exit(1);
561 + }
562 +
563 + void usage(char *progname){
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);
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;
358 <        extern int optind;
359 <        int c;
584 >        time_t last_update = 0;
585  
586          WINDOW *window;
587  
363        int stdin_fileno;
364        fd_set infds;
365        struct timeval timeout;
366
588          extern int errno;
368        char ch;
589  
590          int delay=2;
591  
592 <        while ((c = getopt(argc, argv, "d:")) != EOF){
593 <                switch (c){
594 <                        case 'd':
595 <                                delay = atoi(optarg);
596 <                                if (delay == 0){
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 >                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;
612 <                }
613 <        }
610 >                                break;
611 > #ifdef COLOR_SUPPORT
612 >                        case 'c':
613 >                                colouron = 1;
614 >                                break;
615 > #endif
616 >                        case 'v':
617 >                                version_num(argv[0]);
618 >                                break;
619 >                        case 'h':
620 >                        default:
621 >                                usage(argv[0]);
622 >                                return 1;
623 >                                break;
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 >        cbreak();
649 >        noecho();
650 >        timeout(delay * 1000);
651 >        window=newwin(0, 0, 0, 0);
652          clear();
653  
654          if(!get_stats()){
# Line 397 | Line 658 | int main(int argc, char **argv){
658          }
659  
660          display_headings();
400        stdin_fileno=fileno(stdin);
661  
662          for(;;){
663 +                time_t now;
664 +                int ch = getch();
665  
666 <                FD_ZERO(&infds);
667 <                FD_SET(stdin_fileno, &infds);
406 <                timeout.tv_sec = delay;
407 <                timeout.tv_usec = 0;
408 <                
409 <                if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){
410 <                        if(errno!=EINTR){
411 <                                perror("select failed");
412 <                                exit(1);
413 <                        }
666 >                if (ch == 'q'){
667 >                        break;
668                  }
669  
670 <                if(FD_ISSET(stdin_fileno, &infds)){
671 <                        ch=getch();
672 <                        if (ch == 'q'){
673 <                                endwin();
674 <                                return 0;
675 <                        }
670 >                /* To keep the numbers slightly accurate we do not want them
671 >                 * updating more frequently than once a second.
672 >                 */
673 >                now = time(NULL);
674 >                if ((now - last_update) >= 1) {
675 >                        get_stats();
676                  }
677 +                last_update = now;
678  
679 <                get_stats();
679 >                display_data(colouron);
680 >        }
681  
426                display_data();
427
428                /* To keep the numbers slightly accurate we do not want them updating more
429                 * frequently than once a second.
430                 */
431                sleep(1);
432
433        }      
434
435
436
682          endwin();
683          return 0;
684 < }      
684 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines