33 |
|
#include <libxml/xmlmemory.h> |
34 |
|
#include <libxml/parser.h> |
35 |
|
#include "genmergesort.h" |
36 |
+ |
|
37 |
+ |
#ifdef HAVE_NCURSES_H |
38 |
+ |
#include <ncurses.h> |
39 |
+ |
#else |
40 |
|
#include <curses.h> |
41 |
+ |
#endif |
42 |
|
|
43 |
|
struct host_line_t{ |
44 |
|
char *hostname; |
122 |
|
network_data_list_t *network_data_list; |
123 |
|
long long network_io_total_tx; |
124 |
|
long long network_io_total_rx; |
125 |
+ |
long long network_io_total; |
126 |
|
|
127 |
|
diskio_data_list_t *diskio_data_list; |
128 |
|
long long disk_io_total_write; |
129 |
|
long long disk_io_total_read; |
130 |
+ |
long long disk_io_total; |
131 |
|
|
132 |
|
/* Maybe in the future */ |
133 |
|
/* |
140 |
|
|
141 |
|
typedef struct machine_data_t machine_data_list_t; |
142 |
|
|
143 |
+ |
#define SORTBYMAXNAME 128 |
144 |
|
typedef struct{ |
145 |
|
int cpu_user; |
146 |
|
int cpu_idle; |
182 |
|
|
183 |
|
int disk_total_used; |
184 |
|
int disk_all_stats; |
185 |
+ |
|
186 |
+ |
char sortby[SORTBYMAXNAME]; |
187 |
|
}display_config_t; |
188 |
|
|
189 |
|
GENERIC_MERGE_SORT(static, sort_machine_stats, machine_data_list_t, next) |
195 |
|
|
196 |
|
MKCMP(cpu_used) |
197 |
|
MKCMP(load_1) |
198 |
+ |
MKCMP(network_io_total) |
199 |
|
MKCMP(network_io_total_tx) |
200 |
|
MKCMP(network_io_total_rx) |
201 |
+ |
MKCMP(disk_io_total) |
202 |
|
MKCMP(disk_io_total_write) |
203 |
|
MKCMP(disk_io_total_read) |
204 |
|
MKCMP(memory_used_pecent) |
205 |
|
MKCMP(swap_used_pecent) |
206 |
|
|
207 |
+ |
#define CPU_USED "CPU used" |
208 |
+ |
#define LOAD "Load (1)" |
209 |
+ |
#define NETIORX "total Network RX for all interfaces" |
210 |
+ |
#define NETIOTX "total Network TX for all interfaces" |
211 |
+ |
#define NETIO "total Network IO for all interfaces (rx+tx)" |
212 |
+ |
#define MEM "Memory usage" |
213 |
+ |
#define SWAP "Swap usage" |
214 |
+ |
#define DISKIOR "DiskIO reads" |
215 |
+ |
#define DISKIOW "DiskIO writes" |
216 |
+ |
#define DISKIO "Total DiskIO (reads+writes)" |
217 |
+ |
|
218 |
+ |
|
219 |
|
/* |
220 |
|
int cmp_cpu(machine_data_list_t *a, machine_data_list_t *b){ |
221 |
|
|
235 |
|
} |
236 |
|
} |
237 |
|
*/ |
238 |
+ |
|
239 |
+ |
#ifndef HAVE_ATOLL |
240 |
+ |
long long int atoll (const char *nptr){ |
241 |
+ |
return strtoll (nptr, (char **) NULL, 10); |
242 |
+ |
} |
243 |
+ |
#endif |
244 |
+ |
|
245 |
+ |
#ifndef HAVE_STRLCPY |
246 |
+ |
/* |
247 |
+ |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
248 |
+ |
* All rights reserved. |
249 |
+ |
* |
250 |
+ |
* Redistribution and use in source and binary forms, with or without |
251 |
+ |
* modification, are permitted provided that the following conditions |
252 |
+ |
* are met: |
253 |
+ |
* 1. Redistributions of source code must retain the above copyright |
254 |
+ |
* notice, this list of conditions and the following disclaimer. |
255 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
256 |
+ |
* notice, this list of conditions and the following disclaimer in the |
257 |
+ |
* documentation and/or other materials provided with the distribution. |
258 |
+ |
* 3. The name of the author may not be used to endorse or promote products |
259 |
+ |
* derived from this software without specific prior written permission. |
260 |
+ |
* |
261 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
262 |
+ |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
263 |
+ |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
264 |
+ |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
265 |
+ |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
266 |
+ |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
267 |
+ |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
268 |
+ |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
269 |
+ |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
270 |
+ |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
271 |
+ |
*/ |
272 |
+ |
|
273 |
+ |
/* |
274 |
+ |
* Copy src to string dst of size siz. At most siz-1 characters |
275 |
+ |
* will be copied. Always NUL terminates (unless siz == 0). |
276 |
+ |
* Returns strlen(src); if retval >= siz, truncation occurred. |
277 |
+ |
*/ |
278 |
+ |
size_t |
279 |
+ |
strlcpy(dst, src, siz) |
280 |
+ |
char *dst; |
281 |
+ |
const char *src; |
282 |
+ |
size_t siz; |
283 |
+ |
{ |
284 |
+ |
register char *d = dst; |
285 |
+ |
register const char *s = src; |
286 |
+ |
register size_t n = siz; |
287 |
+ |
|
288 |
+ |
/* Copy as many bytes as will fit */ |
289 |
+ |
if (n != 0 && --n != 0) { |
290 |
+ |
do { |
291 |
+ |
if ((*d++ = *s++) == 0) |
292 |
+ |
break; |
293 |
+ |
} while (--n != 0); |
294 |
+ |
} |
295 |
+ |
|
296 |
+ |
/* Not enough room in dst, add NUL and traverse rest of src */ |
297 |
+ |
if (n == 0) { |
298 |
+ |
if (siz != 0) |
299 |
+ |
*d = '\0'; /* NUL-terminate dst */ |
300 |
+ |
while (*s++) |
301 |
+ |
; |
302 |
+ |
} |
303 |
+ |
|
304 |
+ |
return(s - src - 1); /* count does not include NUL */ |
305 |
+ |
} |
306 |
+ |
|
307 |
+ |
#endif |
308 |
+ |
|
309 |
|
|
310 |
|
FILE *create_tcp_connection(char *hostname, int port){ |
311 |
|
int sock; |
654 |
|
machine_data_list->network_io_total_rx+=ndl_ptr->rx; |
655 |
|
ndl_ptr=ndl_ptr->next; |
656 |
|
} |
657 |
+ |
machine_data_list->network_io_total=machine_data_list->network_io_total_rx+machine_data_list->network_io_total_tx; |
658 |
|
|
659 |
|
didl_ptr=machine_data_list->diskio_data_list; |
660 |
|
machine_data_list->disk_io_total_read=0; |
664 |
|
machine_data_list->disk_io_total_write+=didl_ptr->write; |
665 |
|
didl_ptr=didl_ptr->next; |
666 |
|
} |
667 |
+ |
machine_data_list->disk_io_total=machine_data_list->disk_io_total_read+machine_data_list->disk_io_total_write; |
668 |
|
|
669 |
|
xmlFreeDoc(doc); |
670 |
|
|
675 |
|
|
676 |
|
void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int num_lines, int *title){ |
677 |
|
int line_num=4; |
678 |
+ |
int counter; |
679 |
|
int x=1; |
680 |
|
|
681 |
|
if(*title){ |
682 |
< |
// printf("\033[2J"); |
683 |
< |
// printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
684 |
< |
// printw("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
682 |
> |
clear(); |
683 |
> |
move (num_lines-3, 1); |
684 |
> |
printw("Sorting by %-64s", display_config->sortby); |
685 |
> |
|
686 |
|
move(1,1); |
687 |
|
printw("%-11s", "Hostname"); |
688 |
|
x=x+11+1; |
760 |
|
*title=0; |
761 |
|
} |
762 |
|
|
763 |
< |
for(;num_lines;num_lines--){ |
763 |
> |
/* Counter starts at 8, for padding (eg, headers, borders etc) */ |
764 |
> |
for(counter=8;counter<num_lines;counter++){ |
765 |
|
if(machine_data_list==NULL) break; |
766 |
|
move(line_num++, 1); |
767 |
|
printw("%-11s", machine_data_list->sysname); |
787 |
|
|
788 |
|
int main(int argc, char **argv){ |
789 |
|
WINDOW *window; |
790 |
+ |
fd_set infds; |
791 |
+ |
int maxx, maxy; |
792 |
|
|
793 |
|
FILE *control; |
794 |
|
FILE *data; |
804 |
|
|
805 |
|
int num_hosts; |
806 |
|
int max_display=0; |
807 |
< |
int title; |
807 |
> |
int title=1; |
808 |
|
|
809 |
|
int cmdopt; |
810 |
|
extern int optind; |
813 |
|
display_config_t display_config; |
814 |
|
char ch; |
815 |
|
|
816 |
+ |
int data_fileno, stdin_fileno, biggest_fileno; |
817 |
+ |
|
818 |
|
sortby_ptr=NULL; |
819 |
|
|
820 |
|
/* What to display defaults */ |
850 |
|
|
851 |
|
display_config.network_io_total_tx=1; |
852 |
|
display_config.network_io_total_rx=1; |
853 |
< |
display_config.network_all_stats=1; |
853 |
> |
display_config.network_all_stats=0; |
854 |
|
|
855 |
< |
display_config.disk_io_total_write=0; |
856 |
< |
display_config.disk_io_total_read=0; |
855 |
> |
display_config.disk_io_total_write=1; |
856 |
> |
display_config.disk_io_total_read=1; |
857 |
|
display_config.disk_io_all_stats=0; |
858 |
|
|
859 |
|
display_config.disk_total_used=0; |
867 |
|
case 's': |
868 |
|
if(!strcmp(optarg, "cpu")){ |
869 |
|
sortby_ptr=cmp_cpu_used; |
870 |
+ |
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
871 |
|
} |
872 |
|
if(!strcmp(optarg, "load")){ |
873 |
|
sortby_ptr=cmp_load_1; |
874 |
+ |
strlcpy(display_config.sortby, LOAD, SORTBYMAXNAME); |
875 |
|
} |
876 |
|
if(!strcmp(optarg, "mem")){ |
877 |
|
sortby_ptr=cmp_memory_used_pecent; |
878 |
+ |
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
879 |
|
} |
880 |
+ |
if(!strcmp(optarg, "swap")){ |
881 |
+ |
sortby_ptr=cmp_swap_used_pecent; |
882 |
+ |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
883 |
+ |
} |
884 |
|
if(sortby_ptr==NULL){ |
885 |
|
errf("Invalid sort type"); |
886 |
|
exit(1); |
889 |
|
} |
890 |
|
} |
891 |
|
|
892 |
< |
if(sortby_ptr==NULL) sortby_ptr=cmp_cpu_used; |
892 |
> |
if(sortby_ptr==NULL){ |
893 |
> |
sortby_ptr=cmp_cpu_used; |
894 |
> |
strlcpy(display_config.sortby, "CPU Used", SORTBYMAXNAME); |
895 |
> |
} |
896 |
|
|
897 |
|
if(argc<(optind+2)){ |
898 |
|
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
961 |
|
cbreak(); |
962 |
|
echo(); |
963 |
|
window=newwin(0, 0, 0, 0); |
964 |
+ |
getmaxyx(window, maxy, maxx); |
965 |
|
|
966 |
+ |
stdin_fileno=fileno(stdin); |
967 |
+ |
data_fileno=fileno(data); |
968 |
+ |
biggest_fileno=(data_fileno>stdin_fileno) ? (data_fileno+1) : (stdin_fileno+1); |
969 |
+ |
|
970 |
|
for(;;){ |
971 |
< |
response=fpgetline(data); |
972 |
< |
if (response==NULL){ |
973 |
< |
errf("Failed to read data (%m)"); |
974 |
< |
exit(1); |
975 |
< |
} |
971 |
> |
FD_ZERO(&infds); |
972 |
> |
FD_SET(stdin_fileno, &infds); |
973 |
> |
FD_SET(data_fileno, &infds); |
974 |
> |
select(biggest_fileno, &infds, NULL, NULL, NULL); |
975 |
> |
|
976 |
> |
if(FD_ISSET(stdin_fileno, &infds)){ |
977 |
> |
|
978 |
> |
ch=getc(stdin); |
979 |
> |
switch(ch){ |
980 |
|
|
981 |
< |
/* ch=getc(stdin); |
981 |
> |
/* Quit */ |
982 |
> |
case 'Q': |
983 |
> |
case 'q': |
984 |
> |
endwin(); |
985 |
> |
exit(0); |
986 |
> |
break; |
987 |
|
|
988 |
< |
if(ch=='q'){ |
989 |
< |
endwin(); |
990 |
< |
exit(0); |
988 |
> |
/* Sort by */ |
989 |
> |
case 'C': |
990 |
> |
sortby_ptr=cmp_cpu_used; |
991 |
> |
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
992 |
> |
break; |
993 |
> |
|
994 |
> |
case 'M': |
995 |
> |
sortby_ptr=cmp_memory_used_pecent; |
996 |
> |
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
997 |
> |
break; |
998 |
> |
|
999 |
> |
case 'L': |
1000 |
> |
sortby_ptr=cmp_load_1; |
1001 |
> |
strlcpy(display_config.sortby, LOAD, SORTBYMAXNAME); |
1002 |
> |
break; |
1003 |
> |
|
1004 |
> |
case 'S': |
1005 |
> |
sortby_ptr=cmp_swap_used_pecent; |
1006 |
> |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
1007 |
> |
break; |
1008 |
> |
|
1009 |
> |
case 'N': |
1010 |
> |
if(sortby_ptr==cmp_network_io_total){ |
1011 |
> |
strlcpy(display_config.sortby, NETIORX, SORTBYMAXNAME); |
1012 |
> |
sortby_ptr=cmp_network_io_total_rx; |
1013 |
> |
}else if(sortby_ptr==cmp_network_io_total_rx){ |
1014 |
> |
strlcpy(display_config.sortby, NETIOTX, SORTBYMAXNAME); |
1015 |
> |
sortby_ptr=cmp_network_io_total_tx; |
1016 |
> |
}else{ |
1017 |
> |
strlcpy(display_config.sortby, NETIO, SORTBYMAXNAME); |
1018 |
> |
sortby_ptr=cmp_network_io_total; |
1019 |
> |
} |
1020 |
> |
break; |
1021 |
> |
case 'D': |
1022 |
> |
if(sortby_ptr==cmp_disk_io_total){ |
1023 |
> |
strlcpy(display_config.sortby, DISKIOR, SORTBYMAXNAME); |
1024 |
> |
sortby_ptr=cmp_disk_io_total_read; |
1025 |
> |
}else if(sortby_ptr==cmp_disk_io_total_read){ |
1026 |
> |
strlcpy(display_config.sortby, DISKIOW, SORTBYMAXNAME); |
1027 |
> |
sortby_ptr=cmp_disk_io_total_write; |
1028 |
> |
}else{ |
1029 |
> |
strlcpy(display_config.sortby, DISKIO, SORTBYMAXNAME); |
1030 |
> |
sortby_ptr=cmp_disk_io_total; |
1031 |
> |
} |
1032 |
> |
break; |
1033 |
> |
|
1034 |
> |
/* Display */ |
1035 |
> |
|
1036 |
> |
case 'd': |
1037 |
> |
if(display_config.disk_io_total_read){ |
1038 |
> |
display_config.disk_io_total_read=0; |
1039 |
> |
display_config.disk_io_total_write=0; |
1040 |
> |
}else{ |
1041 |
> |
display_config.disk_io_total_read=1; |
1042 |
> |
display_config.disk_io_total_write=1; |
1043 |
> |
} |
1044 |
> |
break; |
1045 |
> |
case 'n': |
1046 |
> |
if(display_config.network_io_total_rx){ |
1047 |
> |
display_config.network_io_total_rx=0; |
1048 |
> |
display_config.network_io_total_tx=0; |
1049 |
> |
}else{ |
1050 |
> |
display_config.network_io_total_rx=1; |
1051 |
> |
display_config.network_io_total_tx=1; |
1052 |
> |
} |
1053 |
> |
break; |
1054 |
> |
case 'm': |
1055 |
> |
if(display_config.memory_used_pecent){ |
1056 |
> |
display_config.memory_used_pecent=0; |
1057 |
> |
}else{ |
1058 |
> |
display_config.memory_used_pecent=1; |
1059 |
> |
} |
1060 |
> |
break; |
1061 |
> |
|
1062 |
> |
case 's': |
1063 |
> |
if(display_config.swap_used_pecent){ |
1064 |
> |
display_config.swap_used_pecent=0; |
1065 |
> |
}else{ |
1066 |
> |
display_config.swap_used_pecent=1; |
1067 |
> |
} |
1068 |
> |
break; |
1069 |
> |
case 'l': |
1070 |
> |
if(display_config.load_1){ |
1071 |
> |
display_config.load_1=0; |
1072 |
> |
}else{ |
1073 |
> |
display_config.load_1=1; |
1074 |
> |
} |
1075 |
> |
break; |
1076 |
> |
case 'p': |
1077 |
> |
if(display_config.pages_in){ |
1078 |
> |
display_config.pages_in=0; |
1079 |
> |
display_config.pages_out=0; |
1080 |
> |
}else{ |
1081 |
> |
display_config.pages_in=1; |
1082 |
> |
display_config.pages_out=1; |
1083 |
> |
} |
1084 |
> |
break; |
1085 |
> |
case 'c': |
1086 |
> |
if(display_config.cpu_used){ |
1087 |
> |
display_config.cpu_used=0; |
1088 |
> |
}else{ |
1089 |
> |
display_config.cpu_used=1; |
1090 |
> |
} |
1091 |
> |
break; |
1092 |
> |
|
1093 |
> |
default: |
1094 |
> |
/* Invalid key.. Ignore.. Set Title to -1, as the |
1095 |
> |
* title++ will then make that "0" (false) so a |
1096 |
> |
* screen redraw will not happen */ |
1097 |
> |
title=-1; |
1098 |
> |
break; |
1099 |
> |
} |
1100 |
> |
|
1101 |
> |
/* Increment title so it becomes true (and making the screen update */ |
1102 |
> |
title++; |
1103 |
> |
|
1104 |
|
} |
1105 |
< |
*/ |
1105 |
> |
if(FD_ISSET(data_fileno, &infds)){ |
1106 |
> |
response=fpgetline(data); |
1107 |
> |
if (response==NULL){ |
1108 |
> |
errf("Failed to read data (%m)"); |
1109 |
> |
exit(1); |
1110 |
> |
} |
1111 |
> |
} |
1112 |
> |
|
1113 |
> |
|
1114 |
|
num_hosts=parse_xml(response, &machine_data_list); |
1115 |
|
if(num_hosts==-1) continue; |
1116 |
|
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
1117 |
|
if(max_display==0){ |
1118 |
< |
display(machine_data_list, &display_config, num_hosts, &title); |
1118 |
> |
display(machine_data_list, &display_config, maxy, &title); |
1119 |
|
}else{ |
1120 |
|
display(machine_data_list, &display_config, max_display, &title); |
1121 |
|
} |