--- projects/libstatgrab/src/saidar/saidar.c 2003/10/21 18:01:15 1.12 +++ projects/libstatgrab/src/saidar/saidar.c 2004/01/19 16:49:23 1.24 @@ -1,7 +1,7 @@ /* * i-scream central monitoring system * http://www.i-scream.org - * Copyright (C) 2000-2002 i-scream + * Copyright (C) 2000-2004 i-scream * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -16,6 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id: saidar.c,v 1.24 2004/01/19 16:49:23 tdb Exp $ */ #ifdef HAVE_CONFIG_H @@ -296,15 +298,16 @@ void display_data(){ } /* VM */ - if (stats.mem_stats != NULL) { + if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { move(6, 54); printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); } - if (stats.swap_stats != NULL) { + if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { move(7, 54); printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); } - if (stats.mem_stats != NULL && stats.swap_stats != NULL) { + if (stats.mem_stats != NULL && stats.swap_stats != NULL && + stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { move(8, 54); printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total))); } @@ -427,28 +430,21 @@ int main(int argc, char **argv){ extern int optind; int c; + time_t last_update = 0; + WINDOW *window; - int stdin_fileno; - fd_set infds; - struct timeval timeout; - extern int errno; - char ch; int delay=2; -#ifdef ALLBSD - gid_t gid; -#endif + statgrab_init(); -#ifdef ALLBSD - if((setegid(getgid())) != 0){ - fprintf(stderr, "Failed to lose setgid'ness\n"); + if(statgrab_drop_privileges() != 0){ + fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); return 1; } -#endif - while ((c = getopt(argc, argv, "vhd:")) != EOF){ + while ((c = getopt(argc, argv, "vhd:")) != -1){ switch (c){ case 'd': delay = atoi(optarg); @@ -456,7 +452,6 @@ int main(int argc, char **argv){ fprintf(stderr, "Time must be 1 second or greater\n"); exit(1); } - delay--; break; case 'v': version_num(argv[0]); @@ -466,7 +461,6 @@ int main(int argc, char **argv){ usage(argv[0]); return 1; break; - } } @@ -475,6 +469,7 @@ int main(int argc, char **argv){ nonl(); cbreak(); noecho(); + timeout(delay * 1000); window=newwin(0, 0, 0, 0); clear(); @@ -485,38 +480,25 @@ int main(int argc, char **argv){ } display_headings(); - stdin_fileno=fileno(stdin); for(;;){ + time_t now; + int ch = getch(); - FD_ZERO(&infds); - FD_SET(stdin_fileno, &infds); - timeout.tv_sec = delay; - timeout.tv_usec = 0; - - if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){ - if(errno!=EINTR){ - perror("select failed"); - exit(1); - } + if (ch == 'q'){ + break; } - if(FD_ISSET(stdin_fileno, &infds)){ - ch=getch(); - if (ch == 'q'){ - endwin(); - return 0; - } + /* To keep the numbers slightly accurate we do not want them + * updating more frequently than once a second. + */ + now = time(NULL); + if ((now - last_update) >= 1) { + get_stats(); } + last_update = now; - get_stats(); - display_data(); - - /* To keep the numbers slightly accurate we do not want them updating more - * frequently than once a second. - */ - sleep(1); } endwin();