ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/saidar/saidar.c
Revision: 1.38
Committed: Fri Dec 1 01:03:59 2006 UTC (17 years, 11 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.37: +16 -1 lines
Log Message:
Colour is only supported if ncurses is being used.

File Contents

# User Rev Content
1 tdb 1.2 /*
2 tdb 1.28 * i-scream libstatgrab
3 tdb 1.2 * http://www.i-scream.org
4 tdb 1.23 * Copyright (C) 2000-2004 i-scream
5 tdb 1.2 *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
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 tdb 1.24 *
20 tdb 1.38 * $Id: saidar.c,v 1.37 2006/12/01 00:38:39 tdb Exp $
21 tdb 1.2 */
22    
23     #ifdef HAVE_CONFIG_H
24     #include "config.h"
25     #endif
26    
27 pajs 1.1 #include <stdio.h>
28     #include <string.h>
29     #include <sys/ioctl.h>
30     #include <unistd.h>
31     #include <stdlib.h>
32     #include <sys/termios.h>
33     #include <signal.h>
34     #include <errno.h>
35     #include <statgrab.h>
36     #include <sys/times.h>
37     #include <limits.h>
38     #include <time.h>
39 tdb 1.36 #include <math.h>
40 tdb 1.2
41     #ifdef HAVE_NCURSES_H
42     #include <ncurses.h>
43 tdb 1.38 #define COLOR_SUPPORT
44 tdb 1.2 #else
45 pajs 1.1 #include <curses.h>
46 tdb 1.2 #endif
47 pajs 1.1
48 tdb 1.36 #define THRESHOLD_LOAD 1.0
49    
50     #define THRESHOLD_WARN_ZMB 0
51    
52     #define THRESHOLD_WARN_CPU 60.0
53     #define THRESHOLD_ALERT_CPU 90.0
54    
55     #define THRESHOLD_WARN_SWAP 75.0
56     #define THRESHOLD_ALERT_SWAP 90.0
57    
58     #define THRESHOLD_WARN_MEM 75.0
59     #define THRESHOLD_ALERT_MEM 90.0
60    
61     #define THRESHOLD_WARN_DISK 75.0
62     #define THRESHOLD_ALERT_DISK 90.0
63    
64 pajs 1.1 typedef struct{
65 ats 1.27 sg_cpu_percents *cpu_percents;
66     sg_mem_stats *mem_stats;
67     sg_swap_stats *swap_stats;
68     sg_load_stats *load_stats;
69     sg_process_count *process_count;
70     sg_page_stats *page_stats;
71    
72     sg_network_io_stats *network_io_stats;
73     int network_io_entries;
74 pajs 1.1
75 ats 1.27 sg_disk_io_stats *disk_io_stats;
76     int disk_io_entries;
77 pajs 1.1
78 ats 1.27 sg_fs_stats *fs_stats;
79     int fs_entries;
80 pajs 1.1
81 ats 1.27 sg_host_info *host_info;
82     sg_user_stats *user_stats;
83 pajs 1.1 }stats_t;
84 tdb 1.35
85 pajs 1.1 stats_t stats;
86    
87     char *size_conv(long long number){
88     char type[] = {'B', 'K', 'M', 'G', 'T'};
89     int x=0;
90 tdb 1.33 int sign=1;
91 pajs 1.1 static char string[10];
92    
93 tdb 1.33 if(number < 0){
94     sign=-1;
95     number=-number;
96     }
97    
98 pajs 1.1 for(;x<5;x++){
99     if( (number/1024) < (100)) {
100     break;
101     }
102     number = (number/1024);
103     }
104    
105 tdb 1.33 number = number*sign;
106    
107     snprintf(string, 10, "%lld%c", number, type[x]);
108 pajs 1.1 return string;
109 tdb 1.35
110 pajs 1.1 }
111    
112     char *hr_uptime(time_t time){
113     int day = 0, hour = 0, min = 0;
114     static char uptime_str[25];
115     int sec = (int) time;
116 tdb 1.35
117 pajs 1.1 day = sec / (24*60*60);
118     sec = sec % (24*60*60);
119     hour = sec / (60*60);
120     sec = sec % (60*60);
121     min = sec / 60;
122     sec = sec % 60;
123    
124     if(day){
125     snprintf(uptime_str, 25, "%dd %02d:%02d:%02d", day, hour, min, sec);
126     }else{
127     snprintf(uptime_str, 25, "%02d:%02d:%02d", hour, min, sec);
128     }
129     return uptime_str;
130     }
131    
132     void display_headings(){
133 ats 1.11 int line;
134    
135 pajs 1.1 move(0,0);
136     printw("Hostname :");
137     move(0,27);
138     printw("Uptime : ");
139     move(0,54);
140     printw("Date : ");
141    
142     /* Load */
143     move(2,0);
144     printw("Load 1 :");
145     move(3,0);
146     printw("Load 5 :");
147     move(4,0);
148     printw("Load 15 :");
149    
150     /* CPU */
151     move(2,21);
152     printw("CPU Idle :");
153     move(3,21);
154     printw("CPU System:");
155     move(4,21);
156     printw("CPU User :");
157    
158     /* Process */
159     move(2, 42);
160     printw("Running :");
161     move(3, 42);
162     printw("Sleeping :");
163     move(4, 42);
164     printw("Stopped :");
165     move(2, 62);
166     printw("Zombie :");
167     move(3, 62);
168     printw("Total :");
169     move(4, 62);
170     printw("No. Users :");
171    
172     /* Mem */
173     move(6, 0);
174     printw("Mem Total :");
175     move(7, 0);
176     printw("Mem Used :");
177     move(8, 0);
178     printw("Mem Free :");
179    
180     /* Swap */
181 tdb 1.29 move(6, 21);
182     printw("Swap Total:");
183     move(7, 21);
184     printw("Swap Used :");
185     move(8, 21);
186     printw("Swap Free :");
187 pajs 1.1
188     /* VM */
189     move(6, 42);
190     printw("Mem Used :");
191     move(7, 42);
192     printw("Swap Used :");
193     move(8, 42);
194     printw("Total Used:");
195    
196     /* Paging */
197     move(6, 62);
198     printw("Paging in :");
199     move(7, 62);
200     printw("Paging out:");
201    
202     /* Disk IO */
203     move(10,0);
204     printw("Disk Name");
205     move(10,15);
206     printw("Read");
207     move(10,28);
208 tdb 1.35 printw("Write");
209 pajs 1.1
210 ats 1.11 line = 10;
211 ats 1.27 if (stats.network_io_stats != NULL) {
212 ats 1.11 /* Network IO */
213     move(line, 42);
214     printw("Network Interface");
215     move(line, 67);
216     printw("rx");
217     move(line, 77);
218     printw("tx");
219 ats 1.27 line += 2 + stats.network_io_entries;
220 ats 1.11 }
221 pajs 1.1
222 ats 1.11 move(line, 42);
223 pajs 1.1 printw("Mount Point");
224 ats 1.11 move(line, 65);
225 pajs 1.1 printw("Free");
226 ats 1.11 move(line, 75);
227 pajs 1.1 printw("Used");
228    
229     refresh();
230     }
231    
232 tdb 1.36 void display_data(int colors){
233 pajs 1.1 char cur_time[20];
234     struct tm *tm_time;
235     time_t epoc_time;
236 ats 1.11 int counter, line;
237 pajs 1.1 long long r,w;
238     long long rt, wt;
239 ats 1.27 sg_disk_io_stats *disk_io_stat_ptr;
240     sg_network_io_stats *network_stat_ptr;
241     sg_fs_stats *disk_stat_ptr;
242 pajs 1.7 /* Size before it will start overwriting "uptime" */
243     char hostname[15];
244     char *ptr;
245 pajs 1.1
246 ats 1.27 if (stats.host_info != NULL) {
247 ats 1.11 move(0,12);
248 ats 1.27 strncpy(hostname, stats.host_info->hostname, (sizeof(hostname) - 1));
249 ats 1.11 /* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */
250     hostname[14] = '\0';
251     ptr=strchr(hostname, '.');
252     /* Some hosts give back a FQDN for hostname. To avoid this, we'll
253     * just blank out everything after the first "."
254     */
255     if (ptr != NULL){
256     *ptr = '\0';
257 tdb 1.35 }
258 tdb 1.36 if (colors) {
259     attron(COLOR_PAIR(1));
260     }
261 ats 1.11 printw("%s", hostname);
262     move(0,36);
263 ats 1.27 printw("%s", hr_uptime(stats.host_info->uptime));
264 ats 1.11 epoc_time=time(NULL);
265     tm_time = localtime(&epoc_time);
266     strftime(cur_time, 20, "%Y-%m-%d %T", tm_time);
267     move(0,61);
268     printw("%s", cur_time);
269 tdb 1.36 if (colors) {
270     attroff(COLOR_PAIR(1));
271     }
272 ats 1.11 }
273 pajs 1.1
274 ats 1.11 if (stats.load_stats != NULL) {
275     /* Load */
276 tdb 1.36 if (colors) {
277     attron(COLOR_PAIR(6));
278     }
279 ats 1.11 move(2,12);
280 tdb 1.36 if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min5) > THRESHOLD_LOAD) {
281     attron(A_BOLD);
282     }
283 ats 1.11 printw("%6.2f", stats.load_stats->min1);
284 tdb 1.36 if (colors) {
285     attroff(A_BOLD);
286     }
287 ats 1.11 move(3,12);
288 tdb 1.36 if (colors && fabs(stats.load_stats->min5 - stats.load_stats->min15) > THRESHOLD_LOAD) {
289     attron(A_BOLD);
290     }
291 ats 1.11 printw("%6.2f", stats.load_stats->min5);
292 tdb 1.36 if (colors) {
293     attroff(A_BOLD);
294     }
295 ats 1.11 move(4,12);
296 tdb 1.36 if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min15) > THRESHOLD_LOAD) {
297     attron(A_BOLD);
298     }
299 ats 1.11 printw("%6.2f", stats.load_stats->min15);
300 tdb 1.36 if (colors) {
301     attroff(A_BOLD);
302     }
303 ats 1.11 }
304 pajs 1.1
305 ats 1.11 if (stats.cpu_percents != NULL) {
306     /* CPU */
307     move(2,33);
308     printw("%6.2f%%", stats.cpu_percents->idle);
309     move(3,33);
310     printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
311     move(4,33);
312 tdb 1.36 if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_ALERT_CPU) {
313     attron(A_STANDOUT);
314 tdb 1.37 attron(A_BOLD);
315 tdb 1.36 }
316     else if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_WARN_CPU) {
317     attron(A_BOLD);
318     }
319 ats 1.11 printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice));
320 tdb 1.36 if(colors) {
321     attroff(A_BOLD);
322     attroff(A_STANDOUT);
323     attron(COLOR_PAIR(6));
324     }
325 ats 1.11 }
326 pajs 1.1
327 ats 1.27 if (stats.process_count != NULL) {
328 ats 1.11 /* Process */
329     move(2, 54);
330 ats 1.27 printw("%5d", stats.process_count->running);
331 ats 1.11 move(2,74);
332 tdb 1.36 if (colors && stats.process_count->zombie > THRESHOLD_WARN_ZMB) {
333     attron(A_STANDOUT);
334 tdb 1.37 attron(A_BOLD);
335 tdb 1.36 }
336 ats 1.27 printw("%5d", stats.process_count->zombie);
337 tdb 1.36 if(colors) {
338     attroff(A_STANDOUT);
339 tdb 1.37 attroff(A_BOLD);
340 tdb 1.36 }
341 ats 1.11 move(3, 54);
342 ats 1.27 printw("%5d", stats.process_count->sleeping);
343 ats 1.11 move(3, 74);
344 ats 1.27 printw("%5d", stats.process_count->total);
345 ats 1.11 move(4, 54);
346 ats 1.27 printw("%5d", stats.process_count->stopped);
347 ats 1.11 }
348     if (stats.user_stats != NULL) {
349     move(4,74);
350     printw("%5d", stats.user_stats->num_entries);
351     }
352 pajs 1.1
353 tdb 1.36 if(colors) {
354     attroff(COLOR_PAIR(6));
355     attron(COLOR_PAIR(5));
356     }
357 ats 1.11 if (stats.mem_stats != NULL) {
358     /* Mem */
359     move(6, 12);
360 tdb 1.35 printw("%7s", size_conv(stats.mem_stats->total));
361 ats 1.11 move(7, 12);
362 tdb 1.35 printw("%7s", size_conv(stats.mem_stats->used));
363 ats 1.11 move(8, 12);
364     printw("%7s", size_conv(stats.mem_stats->free));
365     }
366 tdb 1.35
367     if (stats.swap_stats != NULL) {
368 ats 1.11 /* Swap */
369     move(6, 32);
370     printw("%8s", size_conv(stats.swap_stats->total));
371     move(7, 32);
372     printw("%8s", size_conv(stats.swap_stats->used));
373     move(8, 32);
374     printw("%8s", size_conv(stats.swap_stats->free));
375     }
376    
377 tdb 1.12 /* VM */
378 tdb 1.35 if (stats.mem_stats != NULL && stats.mem_stats->total != 0) {
379 tdb 1.36 float f = 100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total;
380     if (colors && f > THRESHOLD_ALERT_MEM) {
381     attron(A_STANDOUT);
382 tdb 1.37 attron(A_BOLD);
383 tdb 1.36 }
384     else if (colors && f > THRESHOLD_WARN_MEM) {
385     attron(A_BOLD);
386     }
387 ats 1.11 move(6, 54);
388 tdb 1.36 printw("%5.2f%%", f);
389     if (colors) {
390     attroff(A_STANDOUT);
391     attroff(A_BOLD);
392     attron(COLOR_PAIR(5));
393     }
394 tdb 1.12 }
395 tdb 1.35 if (stats.swap_stats != NULL && stats.swap_stats->total != 0) {
396 tdb 1.36 float f = 100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total;
397     if (colors && f > THRESHOLD_ALERT_SWAP) {
398     attron(A_STANDOUT);
399 tdb 1.37 attron(A_BOLD);
400 tdb 1.36 }
401     else if (colors && f > THRESHOLD_WARN_SWAP) {
402     attron(A_BOLD);
403     }
404 ats 1.11 move(7, 54);
405 tdb 1.36 printw("%5.2f%%", f);
406     if (colors) {
407     attroff(A_STANDOUT);
408     attroff(A_BOLD);
409     attron(COLOR_PAIR(5));
410     }
411 tdb 1.12 }
412 tdb 1.13 if (stats.mem_stats != NULL && stats.swap_stats != NULL &&
413 tdb 1.35 stats.mem_stats->total != 0 && stats.swap_stats->total != 0) {
414 ats 1.11 move(8, 54);
415     printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
416     }
417    
418 tdb 1.35 if (stats.page_stats != NULL) {
419 ats 1.11 /* Paging */
420     move(6, 74);
421     printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
422     move(7, 74);
423     printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
424     }
425 tdb 1.36 if (colors) {
426     attroff(COLOR_PAIR(5));
427     }
428 pajs 1.1
429 ats 1.11 line = 11;
430 tdb 1.35 if (stats.disk_io_stats != NULL) {
431 ats 1.11 /* Disk IO */
432 ats 1.27 disk_io_stat_ptr = stats.disk_io_stats;
433 ats 1.11 r=0;
434     w=0;
435 ats 1.27 for(counter=0;counter<stats.disk_io_entries;counter++){
436 tdb 1.34 char name[12];
437     strncpy(name, disk_io_stat_ptr->disk_name, sizeof(name));
438     name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
439 ats 1.11 move(line, 0);
440 tdb 1.34 printw("%s", name);
441 ats 1.11 move(line, 12);
442 ats 1.27 rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes;
443 tdb 1.36 if(colors) {
444     attron(COLOR_PAIR(4));
445     }
446 ats 1.11 printw("%7s", size_conv(rt));
447     r+=rt;
448     move(line, 26);
449 ats 1.27 wt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->write_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->write_bytes;
450 ats 1.11 printw("%7s", size_conv(wt));
451     w+=wt;
452 ats 1.27 disk_io_stat_ptr++;
453 ats 1.11 line++;
454 tdb 1.36 if(colors) {
455     attroff(COLOR_PAIR(4));
456     }
457 ats 1.11 }
458     line++;
459     move(line, 0);
460     printw("Total");
461     move(line, 12);
462 tdb 1.36 if(colors) {
463     attron(COLOR_PAIR(4));
464     }
465 ats 1.11 printw("%7s", size_conv(r));
466     move(line, 26);
467     printw("%7s", size_conv(w));
468 tdb 1.36 if(colors) {
469     attroff(COLOR_PAIR(4));
470     }
471 ats 1.11 }
472 pajs 1.1
473 ats 1.11 line = 11;
474 tdb 1.35 if (stats.network_io_stats != NULL) {
475 ats 1.11 /* Network */
476 ats 1.27 network_stat_ptr = stats.network_io_stats;
477     for(counter=0;counter<stats.network_io_entries;counter++){
478 tdb 1.34 char name[20];
479     strncpy(name, network_stat_ptr->interface_name, sizeof(name));
480     name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
481 tdb 1.29 move(line, 42);
482 tdb 1.34 printw("%s", name);
483 ats 1.11 move(line, 62);
484     rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
485 tdb 1.36 if(colors) {
486     attron(COLOR_PAIR(4));
487     }
488 ats 1.11 printw("%7s", size_conv(rt));
489     move(line, 72);
490 tdb 1.35 wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
491 ats 1.11 printw("%7s", size_conv(wt));
492     network_stat_ptr++;
493     line++;
494 tdb 1.36 if(colors) {
495     attroff(COLOR_PAIR(4));
496     }
497 ats 1.11 }
498     line += 2;
499     }
500 pajs 1.1
501 tdb 1.35 if (stats.fs_stats != NULL) {
502 ats 1.11 /* Disk */
503 ats 1.27 disk_stat_ptr = stats.fs_stats;
504     for(counter=0;counter<stats.fs_entries;counter++){
505 tdb 1.34 char name[20];
506     strncpy(name, disk_stat_ptr->mnt_point, sizeof(name));
507     name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
508 ats 1.11 move(line, 42);
509 tdb 1.34 printw("%s", name);
510 ats 1.11 move(line, 62);
511 tdb 1.36 if(colors) {
512     attron(COLOR_PAIR(2));
513     }
514 ats 1.11 printw("%7s", size_conv(disk_stat_ptr->avail));
515     move(line, 73);
516 tdb 1.36 if(colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_ALERT_DISK) {
517     attron(A_STANDOUT);
518 tdb 1.37 attron(A_BOLD);
519 tdb 1.36 } else if (colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_WARN_DISK) {
520     attron(A_BOLD);
521     }
522 tdb 1.30 printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)));
523 ats 1.11 disk_stat_ptr++;
524     line++;
525 tdb 1.36 if(colors) {
526     attroff(COLOR_PAIR(2));
527     attroff(A_STANDOUT);
528     attroff(A_BOLD);
529     }
530 ats 1.11 }
531 pajs 1.1 }
532    
533     refresh();
534     }
535    
536 ats 1.32 void sig_winch_handler(int dummy){
537 pajs 1.1 clear();
538     display_headings();
539 tdb 1.36 display_data(0);
540 tdb 1.29 signal(SIGWINCH, sig_winch_handler);
541 pajs 1.1 }
542    
543     int get_stats(){
544 ats 1.27 stats.cpu_percents = sg_get_cpu_percents();
545     stats.mem_stats = sg_get_mem_stats();
546     stats.swap_stats = sg_get_swap_stats();
547     stats.load_stats = sg_get_load_stats();
548     stats.process_count = sg_get_process_count();
549     stats.page_stats = sg_get_page_stats_diff();
550     stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries));
551     stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries));
552     stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries));
553     stats.host_info = sg_get_host_info();
554     stats.user_stats = sg_get_user_stats();
555 pajs 1.1
556 ats 1.11 return 1;
557 pajs 1.1 }
558    
559 pajs 1.3 void version_num(char *progname){
560 pajs 1.5 fprintf(stderr, "%s version %s\n", progname, PACKAGE_VERSION);
561 pajs 1.3 fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
562     exit(1);
563     }
564    
565     void usage(char *progname){
566 tdb 1.38 #ifdef COLOR_SUPPORT
567 tdb 1.36 fprintf(stderr, "Usage: %s [-d delay] [-c] [-v] [-h]\n\n", progname);
568 tdb 1.38 #else
569     fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname);
570     #endif
571 tdb 1.29 fprintf(stderr, " -d Sets the update time in seconds\n");
572 tdb 1.38 #ifdef COLOR_SUPPORT
573 tdb 1.36 fprintf(stderr, " -c Enables coloured output\n");
574 tdb 1.38 #endif
575 pajs 1.4 fprintf(stderr, " -v Prints version number\n");
576 tdb 1.29 fprintf(stderr, " -h Displays this help information.\n");
577     fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
578     exit(1);
579 pajs 1.3 }
580    
581 pajs 1.1 int main(int argc, char **argv){
582 tdb 1.29 extern char *optarg;
583     int c;
584 tdb 1.36 int colouron = 0;
585 pajs 1.1
586 ats 1.17 time_t last_update = 0;
587    
588 pajs 1.1 WINDOW *window;
589    
590     extern int errno;
591    
592     int delay=2;
593 ats 1.21
594 ats 1.27 sg_init();
595     if(sg_drop_privileges() != 0){
596 ats 1.22 fprintf(stderr, "Failed to drop setuid/setgid privileges\n");
597 pajs 1.10 return 1;
598     }
599 tdb 1.35
600 tdb 1.38 #ifdef COLOR_SUPPORT
601 tdb 1.36 while ((c = getopt(argc, argv, "d:cvh")) != -1){
602 tdb 1.38 #else
603     while ((c = getopt(argc, argv, "d:vh")) != -1){
604     #endif
605 tdb 1.29 switch (c){
606     case 'd':
607     delay = atoi(optarg);
608 tdb 1.25 if (delay < 1){
609 pajs 1.1 fprintf(stderr, "Time must be 1 second or greater\n");
610     exit(1);
611     }
612 tdb 1.29 break;
613 tdb 1.38 #ifdef COLOR_SUPPORT
614 tdb 1.36 case 'c':
615     colouron = 1;
616     break;
617 tdb 1.38 #endif
618 pajs 1.3 case 'v':
619 tdb 1.35 version_num(argv[0]);
620 pajs 1.3 break;
621     case 'h':
622     default:
623     usage(argv[0]);
624     return 1;
625     break;
626 tdb 1.29 }
627     }
628 pajs 1.1
629     signal(SIGWINCH, sig_winch_handler);
630 tdb 1.29 initscr();
631 tdb 1.38 #ifdef COLOR_SUPPORT
632 tdb 1.36 /* turn on colour */
633     if (colouron) {
634     if (has_colors()) {
635     start_color();
636     use_default_colors();
637     init_pair(1,COLOR_RED,-1);
638     init_pair(2,COLOR_GREEN,-1);
639     init_pair(3,COLOR_YELLOW,-1);
640     init_pair(4,COLOR_BLUE,-1);
641     init_pair(5,COLOR_MAGENTA,-1);
642     init_pair(6,COLOR_CYAN,-1);
643     } else {
644     fprintf(stderr, "Colour support disabled: your terminal does not support colour.");
645     colouron = 0;
646     }
647     }
648 tdb 1.38 #endif
649 tdb 1.29 nonl();
650     cbreak();
651     noecho();
652 ats 1.14 timeout(delay * 1000);
653 tdb 1.29 window=newwin(0, 0, 0, 0);
654 pajs 1.1 clear();
655    
656     if(!get_stats()){
657     fprintf(stderr, "Failed to get all the stats. Please check correct permissions\n");
658     endwin();
659     return 1;
660     }
661    
662     display_headings();
663    
664     for(;;){
665 ats 1.17 time_t now;
666 ats 1.15 int ch = getch();
667 ats 1.17
668 ats 1.14 if (ch == 'q'){
669 ats 1.18 break;
670 pajs 1.1 }
671    
672 ats 1.17 /* To keep the numbers slightly accurate we do not want them
673     * updating more frequently than once a second.
674     */
675     now = time(NULL);
676     if ((now - last_update) >= 1) {
677     get_stats();
678     }
679     last_update = now;
680 pajs 1.1
681 tdb 1.36 display_data(colouron);
682 tdb 1.35 }
683 pajs 1.1
684     endwin();
685     return 0;
686 tdb 1.35 }