--- projects/cms/source/idar/idar.c 2003/03/30 23:21:55 1.8 +++ projects/cms/source/idar/idar.c 2003/04/09 21:55:10 1.14 @@ -29,6 +29,9 @@ #include #include #include +#include +#include +#include #include #include @@ -142,6 +145,11 @@ typedef struct machine_data_t machine_data_list_t; #define SORTBYMAXNAME 128 typedef struct{ + int maxx; + int maxy; + + char units; + int cpu_user; int cpu_idle; int cpu_iowait; @@ -235,6 +243,77 @@ int cmp_cpu(machine_data_list_t *a, machine_data_list_ } } */ + +#ifndef HAVE_ATOLL +long long int atoll (const char *nptr){ + return strtoll (nptr, (char **) NULL, 10); +} +#endif + +#ifndef HAVE_STRLCPY +/* + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} + +#endif + FILE *create_tcp_connection(char *hostname, int port){ int sock; @@ -602,110 +681,261 @@ int parse_xml(char *xml, machine_data_list_t **md){ } -void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int num_lines, int *title){ +void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int *title){ int line_num=4; int counter; int x=1; if(*title){ clear(); - move (num_lines-3, 1); + move (display_config->maxy-3, 1); printw("Sorting by %-64s", display_config->sortby); + move (display_config->maxy-2, 1); + if(display_config->units == 'b'){ + printw("Units are measured in bytes/sec"); + } + if(display_config->units == 'k'){ + printw("Units are measured in kilobytes/sec"); + } + if(display_config->units == 'm'){ + printw("Units are measured in megabytes/sec"); + } move(1,1); printw("%-11s", "Hostname"); x=x+11+1; - if(display_config->cpu_used){ + if(display_config->cpu_used && (display_config->maxx > x+6)){ move(1,x); printw("%5s", "CPU"); move(2,x); printw("%5s", "used%"); x+=6; } - if(display_config->load_1){ + if(display_config->load_1 && (display_config->maxx > x+6)){ move(1,x); printw("%5s", "Load"); move(2,x); printw("%5s", "(1m)"); x+=6; } - if(display_config->pages_in){ + if(display_config->pages_in && (display_config->maxx > x+6)){ move(1,x); printw("%5s", "Page"); move(2,x); printw("%5s", "ins"); x+=6; } - if(display_config->pages_out){ + if(display_config->pages_out && (display_config->maxx > x+6)){ move(1,x); printw("%5s", "Page"); move(2,x); printw("%5s", "outs"); x+=6; } - if(display_config->memory_used_pecent){ + if(display_config->memory_used_pecent && (display_config->maxx > x+6)){ move(1,x); printw("%5s", "Mem"); move(2,x); printw("%5s", "used"); x+=6; } - if(display_config->swap_used_pecent){ + if(display_config->swap_used_pecent && (display_config->maxx > x+6)){ move(1,x); printw("%5s", "Swap"); move(2,x); printw("%5s", "used"); x+=6; } - if(display_config->network_io_total_rx){ - move(1,x); - printw("%8s", "Net"); - move(2,x); - printw("%8s", "rx"); - x+=9; + if(display_config->network_io_total_rx){ + if(display_config->units=='b' && (display_config->maxx > x+9)){ + move(1,x); + printw("%8s", "Net"); + move(2,x); + printw("%8s", "rx"); + x+=9; + } + if(display_config->units=='k' && (display_config->maxx > x+6)){ + move(1,x); + printw("%5s", "Net"); + move(2,x); + printw("%5s", "rx"); + x+=6; + } + if(display_config->units=='m' && (display_config->maxx > x+5)){ + move(1,x); + printw("%4s", "Net"); + move(2,x); + printw("%4s", "rx"); + x+=5; + } } if(display_config->network_io_total_tx){ - move(1,x); - printw("%8s", "Net"); - move(2,x); - printw("%8s", "tx"); - x+=9; + if(display_config->units=='b' && (display_config->maxx > x+9)){ + move(1,x); + printw("%8s", "Net"); + move(2,x); + printw("%8s", "tx"); + x+=9; + } + if(display_config->units=='k' && (display_config->maxx > x+6)){ + move(1,x); + printw("%5s", "Net"); + move(2,x); + printw("%5s", "tx"); + x+=6; + } + if(display_config->units=='m' && (display_config->maxx > x+5)){ + move(1,x); + printw("%4s", "Net"); + move(2,x); + printw("%4s", "tx"); + x+=5; + } } + if(display_config->disk_io_total_read){ - move(1,x); - printw("%9s", "Disk"); - move(2,x); - printw("%9s", "read"); - x+=10; + if(display_config->units=='b' && (display_config->maxx > x+10)){ + move(1,x); + printw("%9s", "Disk"); + move(2,x); + printw("%9s", "read"); + x+=10; + } + if(display_config->units=='k' && (display_config->maxx > x+7)){ + move(1,x); + printw("%6s", "Disk"); + move(2,x); + printw("%6s", "read"); + x+=7; + } + if(display_config->units=='m' && (display_config->maxx > x+6)){ + move(1,x); + printw("%5s", "Disk"); + move(2,x); + printw("%5s", "read"); + x+=6; + } } + if(display_config->disk_io_total_read){ - move(1,x); - printw("%9s", "Disk"); - move(2,x); - printw("%9s", "write"); - x+=10; + if(display_config->units=='b' && (display_config->maxx > x+10)){ + move(1,x); + printw("%9s", "Disk"); + move(2,x); + printw("%9s", "write"); + x+=10; + } + if(display_config->units=='k' && (display_config->maxx > x+7)){ + move(1,x); + printw("%6s", "Disk"); + move(2,x); + printw("%6s", "write"); + x+=7; + } + if(display_config->units=='m' && (display_config->maxx > x+6)){ + move(1,x); + printw("%5s", "Disk"); + move(2,x); + printw("%5s", "write"); + x+=6; + } } *title=0; } /* Counter starts at 8, for padding (eg, headers, borders etc) */ - for(counter=8;countermaxy;counter++){ if(machine_data_list==NULL) break; move(line_num++, 1); printw("%-11s", machine_data_list->sysname); + x=13; - if(display_config->cpu_used) printw(" %5.1f", machine_data_list->cpu_used); - if(display_config->load_1) printw(" %5.1f", machine_data_list->load_1); - if(display_config->pages_in) printw(" %5d", machine_data_list->pages_in); - if(display_config->pages_out) printw(" %5d", machine_data_list->pages_out); - if(display_config->memory_used_pecent) printw(" %5.1f", machine_data_list->memory_used_pecent); - if(display_config->swap_used_pecent) printw(" %5.1f", machine_data_list->swap_used_pecent); - if(display_config->network_io_total_rx) printw(" %8lld", machine_data_list->network_io_total_rx); - if(display_config->network_io_total_tx) printw(" %8lld", machine_data_list->network_io_total_tx); - if(display_config->disk_io_total_read) printw(" %9lld", machine_data_list->disk_io_total_read); - if(display_config->disk_io_total_write) printw(" %9lld", machine_data_list->disk_io_total_write); + if(display_config->cpu_used && (display_config->maxx > x+6)){ + printw(" %5.1f", machine_data_list->cpu_used); + x+=6; + } + if(display_config->load_1 && (display_config->maxx > x+6)){ + printw(" %5.1f", machine_data_list->load_1); + x+=6; + } + if(display_config->pages_in && (display_config->maxx > x+6)){ + printw(" %5d", machine_data_list->pages_in); + x+=6; + } + if(display_config->pages_out && (display_config->maxx > x+6)){ + printw(" %5d", machine_data_list->pages_out); + x+=6; + } + if(display_config->memory_used_pecent && (display_config->maxx > x+6)){ + printw(" %5.1f", machine_data_list->memory_used_pecent); + x+=6; + } + if(display_config->swap_used_pecent && (display_config->maxx > x+6)){ + printw(" %5.1f", machine_data_list->swap_used_pecent); + x+=6; + } + if(display_config->network_io_total_rx){ + if(display_config->units=='b' && (display_config->maxx > x+9)){ + printw(" %8lld", machine_data_list->network_io_total_rx); + x+=9; + } + if(display_config->units=='k' && (display_config->maxx > x+6)){ + printw(" %5lld", machine_data_list->network_io_total_rx/1024); + x+=6; + } + if(display_config->units=='m' && (display_config->maxx > x+5)){ + printw(" %4.1f", (double)(machine_data_list->network_io_total_rx/(1024.00*1024.00))); + x+=5; + } + } + + if(display_config->network_io_total_tx){ + if(display_config->units=='b' && (display_config->maxx > x+9)){ + printw(" %8lld", machine_data_list->network_io_total_tx); + x+=9; + } + if(display_config->units=='k' && (display_config->maxx > x+6)){ + printw(" %5lld", machine_data_list->network_io_total_tx/1024); + x+=6; + } + if(display_config->units=='m' && (display_config->maxx > x+5)){ + printw(" %4.1f", (double)(machine_data_list->network_io_total_tx/(1024.00*1024.00))); + x+=5; + } + } + + if(display_config->disk_io_total_read){ + if(display_config->units=='b' && (display_config->maxx > x+10)){ + printw(" %9lld", machine_data_list->disk_io_total_read); + x+=10; + } + if(display_config->units=='k' && (display_config->maxx > x+7)){ + printw(" %6lld", machine_data_list->disk_io_total_read/1024); + x+=7; + } + if(display_config->units=='m' && (display_config->maxx > x+6)){ + printw(" %5.1f", (double)(machine_data_list->disk_io_total_read/(1024.00*1024.00))); + x+=6; + } + } + + if(display_config->disk_io_total_write){ + if(display_config->units=='b' && (display_config->maxx > x+10)){ + printw(" %9lld", machine_data_list->disk_io_total_write); + x+=10; + } + if(display_config->units=='k' && (display_config->maxx > x+7)){ + printw(" %6lld", machine_data_list->disk_io_total_write/1024); + x+=7; + } + if(display_config->units=='m' && (display_config->maxx > x+6)){ + printw(" %5.1f", (double)(machine_data_list->disk_io_total_write/(1024.00*1024.00))); + x+=6; + } + } + machine_data_list=machine_data_list->next; } @@ -717,7 +947,7 @@ void display(machine_data_list_t *machine_data_list, d int main(int argc, char **argv){ WINDOW *window; fd_set infds; - int maxx, maxy; + struct winsize size; FILE *control; FILE *data; @@ -732,7 +962,6 @@ int main(int argc, char **argv){ machine_data_list_t *machine_data_list=NULL; int num_hosts; - int max_display=0; int title=1; int cmdopt; @@ -747,6 +976,8 @@ int main(int argc, char **argv){ sortby_ptr=NULL; /* What to display defaults */ + display_config.units='b'; + display_config.cpu_user=0; display_config.cpu_idle=0; display_config.cpu_iowait=0; @@ -788,11 +1019,8 @@ int main(int argc, char **argv){ display_config.disk_total_used=0; display_config.disk_all_stats=0; - while((cmdopt=getopt(argc, argv, "d:s:")) != -1){ + while((cmdopt=getopt(argc, argv, "s:")) != -1){ switch(cmdopt){ - case 'd': - max_display=atoi(optarg); - break; case 's': if(!strcmp(optarg, "cpu")){ sortby_ptr=cmp_cpu_used; @@ -834,6 +1062,7 @@ int main(int argc, char **argv){ control=create_tcp_connection(servername, server_control_port); if(control==NULL){ errf("Failed to connect (%m)"); + exit(1); } if(argc==4){ @@ -877,6 +1106,7 @@ int main(int argc, char **argv){ data=create_tcp_connection(servername, server_data_port); if(data==NULL){ errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); + exit(1); } /* @@ -890,7 +1120,7 @@ int main(int argc, char **argv){ cbreak(); echo(); window=newwin(0, 0, 0, 0); - getmaxyx(window, maxy, maxx); + getmaxyx(window, display_config.maxy, display_config.maxx); stdin_fileno=fileno(stdin); data_fileno=fileno(data); @@ -900,7 +1130,17 @@ int main(int argc, char **argv){ FD_ZERO(&infds); FD_SET(stdin_fileno, &infds); FD_SET(data_fileno, &infds); - select(biggest_fileno, &infds, NULL, NULL, NULL); + if((select(biggest_fileno, &infds, NULL, NULL, NULL))==-1){ + if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) { + resizeterm(size.ws_row, size.ws_col); + wrefresh(curscr); + } + getmaxyx(window, display_config.maxy, display_config.maxx); + title=1; + display(machine_data_list, &display_config, &title); + refresh(); + continue; + } if(FD_ISSET(stdin_fileno, &infds)){ @@ -913,6 +1153,17 @@ int main(int argc, char **argv){ endwin(); exit(0); break; + /* Units */ + case 'U': + case 'u': + if(display_config.units == 'b'){ + display_config.units = 'k'; + }else if(display_config.units == 'k'){ + display_config.units = 'm'; + }else{ + display_config.units = 'b'; + } + break; /* Sort by */ case 'C': @@ -1043,11 +1294,7 @@ int main(int argc, char **argv){ num_hosts=parse_xml(response, &machine_data_list); if(num_hosts==-1) continue; machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); - if(max_display==0){ - display(machine_data_list, &display_config, maxy, &title); - }else{ - display(machine_data_list, &display_config, max_display, &title); - } + display(machine_data_list, &display_config, &title); } exit(0);