--- projects/libstatgrab/src/saidar/saidar.c 2004/04/05 15:40:17 1.27 +++ projects/libstatgrab/src/saidar/saidar.c 2006/11/30 23:03:19 1.35 @@ -1,5 +1,5 @@ /* - * i-scream central monitoring system + * i-scream libstatgrab * http://www.i-scream.org * Copyright (C) 2000-2004 i-scream * @@ -17,7 +17,7 @@ * 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.27 2004/04/05 15:40:17 ats Exp $ + * $Id: saidar.c,v 1.35 2006/11/30 23:03:19 tdb Exp $ */ #ifdef HAVE_CONFIG_H @@ -63,14 +63,20 @@ typedef struct{ sg_host_info *host_info; sg_user_stats *user_stats; }stats_t; - + stats_t stats; char *size_conv(long long number){ char type[] = {'B', 'K', 'M', 'G', 'T'}; int x=0; + int sign=1; static char string[10]; + if(number < 0){ + sign=-1; + number=-number; + } + for(;x<5;x++){ if( (number/1024) < (100)) { break; @@ -78,16 +84,18 @@ char *size_conv(long long number){ number = (number/1024); } - snprintf(string, 10, "%lld%c", number, type[x]); + number = number*sign; + + snprintf(string, 10, "%lld%c", number, type[x]); return string; - + } char *hr_uptime(time_t time){ int day = 0, hour = 0, min = 0; static char uptime_str[25]; int sec = (int) time; - + day = sec / (24*60*60); sec = sec % (24*60*60); hour = sec / (60*60); @@ -152,12 +160,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); @@ -179,7 +187,7 @@ void display_headings(){ move(10,15); printw("Read"); move(10,28); - printw("Write"); + printw("Write"); line = 10; if (stats.network_io_stats != NULL) { @@ -228,7 +236,7 @@ void display_data(){ */ if (ptr != NULL){ *ptr = '\0'; - } + } printw("%s", hostname); move(0,36); printw("%s", hr_uptime(stats.host_info->uptime)); @@ -280,14 +288,14 @@ void display_data(){ if (stats.mem_stats != NULL) { /* Mem */ move(6, 12); - printw("%7s", size_conv(stats.mem_stats->total)); + printw("%7s", size_conv(stats.mem_stats->total)); move(7, 12); - printw("%7s", size_conv(stats.mem_stats->used)); + printw("%7s", size_conv(stats.mem_stats->used)); move(8, 12); printw("%7s", size_conv(stats.mem_stats->free)); } - - if (stats.swap_stats != NULL) { + + if (stats.swap_stats != NULL) { /* Swap */ move(6, 32); printw("%8s", size_conv(stats.swap_stats->total)); @@ -298,21 +306,21 @@ void display_data(){ } /* VM */ - if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { + 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 && stats.swap_stats->total != 0) { + 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 && - stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { + 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))); } - if (stats.page_stats != NULL) { + if (stats.page_stats != NULL) { /* Paging */ move(6, 74); printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein); @@ -321,14 +329,17 @@ void display_data(){ } line = 11; - if (stats.disk_io_stats != NULL) { + if (stats.disk_io_stats != NULL) { /* Disk IO */ disk_io_stat_ptr = stats.disk_io_stats; r=0; w=0; for(counter=0;counterdisk_name, sizeof(name)); + name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ move(line, 0); - printw("%s", disk_io_stat_ptr->disk_name); + printw("%s", name); move(line, 12); 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)); @@ -350,17 +361,20 @@ void display_data(){ } line = 11; - if (stats.network_io_stats != NULL) { + if (stats.network_io_stats != NULL) { /* Network */ network_stat_ptr = stats.network_io_stats; for(counter=0;counterinterface_name); + char name[20]; + strncpy(name, network_stat_ptr->interface_name, sizeof(name)); + name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ + move(line, 42); + printw("%s", name); move(line, 62); rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; printw("%7s", size_conv(rt)); move(line, 72); - wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; + wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; printw("%7s", size_conv(wt)); network_stat_ptr++; line++; @@ -368,16 +382,19 @@ void display_data(){ line += 2; } - if (stats.fs_stats != NULL) { + if (stats.fs_stats != NULL) { /* Disk */ disk_stat_ptr = stats.fs_stats; for(counter=0;countermnt_point, sizeof(name)); + name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ move(line, 42); - printw("%s", disk_stat_ptr->mnt_point); + printw("%s", name); move(line, 62); printw("%7s", size_conv(disk_stat_ptr->avail)); move(line, 73); - printw("%5.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); + printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); disk_stat_ptr++; line++; } @@ -386,11 +403,11 @@ void display_data(){ refresh(); } -void sig_winch_handler(int sig){ +void sig_winch_handler(int dummy){ clear(); display_headings(); display_data(); - signal(SIGWINCH, sig_winch_handler); + signal(SIGWINCH, sig_winch_handler); } int get_stats(){ @@ -416,18 +433,18 @@ 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; - int c; + extern char *optarg; + int c; time_t last_update = 0; @@ -442,34 +459,34 @@ int main(int argc, char **argv){ fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); return 1; } - - while ((c = getopt(argc, argv, "vhd:")) != -1){ - switch (c){ - case 'd': - delay = atoi(optarg); + + 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); } - break; + break; case 'v': - version_num(argv[0]); + version_num(argv[0]); break; case 'h': default: usage(argv[0]); return 1; break; - } - } + } + } signal(SIGWINCH, sig_winch_handler); - initscr(); - nonl(); - cbreak(); - noecho(); + initscr(); + nonl(); + cbreak(); + noecho(); timeout(delay * 1000); - window=newwin(0, 0, 0, 0); + window=newwin(0, 0, 0, 0); clear(); if(!get_stats()){ @@ -498,8 +515,8 @@ int main(int argc, char **argv){ last_update = now; display_data(); - } + } endwin(); return 0; -} +}