--- projects/libstatgrab/src/saidar/saidar.c 2003/10/22 13:07:22 1.13 +++ projects/libstatgrab/src/saidar/saidar.c 2004/04/07 15:50:26 1.29 @@ -1,7 +1,7 @@ /* - * i-scream central monitoring system + * i-scream libstatgrab * 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.29 2004/04/07 15:50:26 tdb Exp $ */ #ifdef HAVE_CONFIG_H @@ -42,24 +44,24 @@ #endif typedef struct{ - cpu_percent_t *cpu_percents; - mem_stat_t *mem_stats; - swap_stat_t *swap_stats; - load_stat_t *load_stats; - process_stat_t *process_stats; - page_stat_t *page_stats; + sg_cpu_percents *cpu_percents; + sg_mem_stats *mem_stats; + sg_swap_stats *swap_stats; + sg_load_stats *load_stats; + sg_process_count *process_count; + sg_page_stats *page_stats; - network_stat_t *network_stats; - int network_entries; + sg_network_io_stats *network_io_stats; + int network_io_entries; - diskio_stat_t *diskio_stats; - int diskio_entries; + sg_disk_io_stats *disk_io_stats; + int disk_io_entries; - disk_stat_t *disk_stats; - int disk_entries; + sg_fs_stats *fs_stats; + int fs_entries; - general_stat_t *general_stats; - user_stat_t *user_stats; + sg_host_info *host_info; + sg_user_stats *user_stats; }stats_t; stats_t stats; @@ -150,12 +152,12 @@ void display_headings(){ printw("Mem Free :"); /* Swap */ - move(6, 21); - printw("Swap Total:"); - move(7, 21); - printw("Swap Used :"); - move(8, 21); - printw("Swap Free :"); + move(6, 21); + printw("Swap Total:"); + move(7, 21); + printw("Swap Used :"); + move(8, 21); + printw("Swap Free :"); /* VM */ move(6, 42); @@ -180,7 +182,7 @@ void display_headings(){ printw("Write"); line = 10; - if (stats.network_stats != NULL) { + if (stats.network_io_stats != NULL) { /* Network IO */ move(line, 42); printw("Network Interface"); @@ -188,7 +190,7 @@ void display_headings(){ printw("rx"); move(line, 77); printw("tx"); - line += 2 + stats.network_entries; + line += 2 + stats.network_io_entries; } move(line, 42); @@ -208,16 +210,16 @@ void display_data(){ int counter, line; long long r,w; long long rt, wt; - diskio_stat_t *diskio_stat_ptr; - network_stat_t *network_stat_ptr; - disk_stat_t *disk_stat_ptr; + sg_disk_io_stats *disk_io_stat_ptr; + sg_network_io_stats *network_stat_ptr; + sg_fs_stats *disk_stat_ptr; /* Size before it will start overwriting "uptime" */ char hostname[15]; char *ptr; - if (stats.general_stats != NULL) { + if (stats.host_info != NULL) { move(0,12); - strncpy(hostname, stats.general_stats->hostname, (sizeof(hostname) - 1)); + strncpy(hostname, stats.host_info->hostname, (sizeof(hostname) - 1)); /* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */ hostname[14] = '\0'; ptr=strchr(hostname, '.'); @@ -229,7 +231,7 @@ void display_data(){ } printw("%s", hostname); move(0,36); - printw("%s", hr_uptime(stats.general_stats->uptime)); + printw("%s", hr_uptime(stats.host_info->uptime)); epoc_time=time(NULL); tm_time = localtime(&epoc_time); strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); @@ -257,18 +259,18 @@ void display_data(){ printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); } - if (stats.process_stats != NULL) { + if (stats.process_count != NULL) { /* Process */ move(2, 54); - printw("%5d", stats.process_stats->running); + printw("%5d", stats.process_count->running); move(2,74); - printw("%5d", stats.process_stats->zombie); + printw("%5d", stats.process_count->zombie); move(3, 54); - printw("%5d", stats.process_stats->sleeping); + printw("%5d", stats.process_count->sleeping); move(3, 74); - printw("%5d", stats.process_stats->total); + printw("%5d", stats.process_count->total); move(4, 54); - printw("%5d", stats.process_stats->stopped); + printw("%5d", stats.process_count->stopped); } if (stats.user_stats != NULL) { move(4,74); @@ -319,23 +321,23 @@ void display_data(){ } line = 11; - if (stats.diskio_stats != NULL) { + if (stats.disk_io_stats != NULL) { /* Disk IO */ - diskio_stat_ptr = stats.diskio_stats; + disk_io_stat_ptr = stats.disk_io_stats; r=0; w=0; - for(counter=0;counterdisk_name); + printw("%s", disk_io_stat_ptr->disk_name); move(line, 12); - rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes; + rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; printw("%7s", size_conv(rt)); r+=rt; move(line, 26); - wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes; + wt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->write_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->write_bytes; printw("%7s", size_conv(wt)); w+=wt; - diskio_stat_ptr++; + disk_io_stat_ptr++; line++; } line++; @@ -348,11 +350,11 @@ void display_data(){ } line = 11; - if (stats.network_stats != NULL) { + if (stats.network_io_stats != NULL) { /* Network */ - network_stat_ptr = stats.network_stats; - for(counter=0;counterinterface_name); move(line, 62); rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; @@ -366,10 +368,10 @@ void display_data(){ line += 2; } - if (stats.disk_stats != NULL) { + if (stats.fs_stats != NULL) { /* Disk */ - disk_stat_ptr = stats.disk_stats; - for(counter=0;countermnt_point); move(line, 62); @@ -388,21 +390,21 @@ void sig_winch_handler(int sig){ clear(); display_headings(); display_data(); - signal(SIGWINCH, sig_winch_handler); + signal(SIGWINCH, sig_winch_handler); } int get_stats(){ - stats.cpu_percents = cpu_percent_usage(); - stats.mem_stats = get_memory_stats(); - stats.swap_stats = get_swap_stats(); - stats.load_stats = get_load_stats(); - stats.process_stats = get_process_stats(); - stats.page_stats = get_page_stats_diff(); - stats.network_stats = get_network_stats_diff(&(stats.network_entries)); - stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries)); - stats.disk_stats = get_disk_stats(&(stats.disk_entries)); - stats.general_stats = get_general_stats(); - stats.user_stats = get_user_stats(); + stats.cpu_percents = sg_get_cpu_percents(); + stats.mem_stats = sg_get_mem_stats(); + stats.swap_stats = sg_get_swap_stats(); + stats.load_stats = sg_get_load_stats(); + stats.process_count = sg_get_process_count(); + stats.page_stats = sg_get_page_stats_diff(); + stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries)); + stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries)); + stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries)); + stats.host_info = sg_get_host_info(); + stats.user_stats = sg_get_user_stats(); return 1; } @@ -414,51 +416,42 @@ void version_num(char *progname){ } void usage(char *progname){ - fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); - fprintf(stderr, " -d Sets the update time in seconds\n"); + fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); + fprintf(stderr, " -d Sets the update time in seconds\n"); fprintf(stderr, " -v Prints version number\n"); - fprintf(stderr, " -h Displays this help information.\n"); - fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); - exit(1); + fprintf(stderr, " -h Displays this help information.\n"); + fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); + exit(1); } int main(int argc, char **argv){ - extern char *optarg; - extern int optind; - int c; + extern char *optarg; + 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"); + + sg_init(); + if(sg_drop_privileges() != 0){ + fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); return 1; } -#endif - while ((c = getopt(argc, argv, "vhd:")) != EOF){ - switch (c){ - case 'd': - delay = atoi(optarg); - if (delay == 0){ + while ((c = getopt(argc, argv, "vhd:")) != -1){ + switch (c){ + case 'd': + delay = atoi(optarg); + if (delay < 1){ fprintf(stderr, "Time must be 1 second or greater\n"); exit(1); } - delay--; - break; + break; case 'v': version_num(argv[0]); break; @@ -467,16 +460,16 @@ int main(int argc, char **argv){ usage(argv[0]); return 1; break; - - } - } + } + } signal(SIGWINCH, sig_winch_handler); - initscr(); - nonl(); - cbreak(); - noecho(); - window=newwin(0, 0, 0, 0); + initscr(); + nonl(); + cbreak(); + noecho(); + timeout(delay * 1000); + window=newwin(0, 0, 0, 0); clear(); if(!get_stats()){ @@ -486,38 +479,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();