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 |
|
|
583 |
|
machine_data_list->network_io_total_rx+=ndl_ptr->rx; |
584 |
|
ndl_ptr=ndl_ptr->next; |
585 |
|
} |
586 |
+ |
machine_data_list->network_io_total=machine_data_list->network_io_total_rx+machine_data_list->network_io_total_tx; |
587 |
|
|
588 |
|
didl_ptr=machine_data_list->diskio_data_list; |
589 |
|
machine_data_list->disk_io_total_read=0; |
593 |
|
machine_data_list->disk_io_total_write+=didl_ptr->write; |
594 |
|
didl_ptr=didl_ptr->next; |
595 |
|
} |
596 |
+ |
machine_data_list->disk_io_total=machine_data_list->disk_io_total_read+machine_data_list->disk_io_total_write; |
597 |
|
|
598 |
|
xmlFreeDoc(doc); |
599 |
|
|
604 |
|
|
605 |
|
void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int num_lines, int *title){ |
606 |
|
int line_num=4; |
607 |
+ |
int counter; |
608 |
|
int x=1; |
609 |
|
|
610 |
|
if(*title){ |
611 |
< |
// printf("\033[2J"); |
612 |
< |
// printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
613 |
< |
// printw("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
611 |
> |
move (num_lines-3, 1); |
612 |
> |
printw("Sorting by %-64s", display_config->sortby); |
613 |
> |
|
614 |
|
move(1,1); |
615 |
|
printw("%-11s", "Hostname"); |
616 |
|
x=x+11+1; |
688 |
|
*title=0; |
689 |
|
} |
690 |
|
|
691 |
< |
for(;num_lines;num_lines--){ |
691 |
> |
/* Counter starts at 8, for padding (eg, headers, borders etc) */ |
692 |
> |
for(counter=8;counter<num_lines;counter++){ |
693 |
|
if(machine_data_list==NULL) break; |
694 |
|
move(line_num++, 1); |
695 |
|
printw("%-11s", machine_data_list->sysname); |
715 |
|
|
716 |
|
int main(int argc, char **argv){ |
717 |
|
WINDOW *window; |
718 |
+ |
fd_set infds; |
719 |
+ |
int maxx, maxy; |
720 |
|
|
721 |
|
FILE *control; |
722 |
|
FILE *data; |
732 |
|
|
733 |
|
int num_hosts; |
734 |
|
int max_display=0; |
735 |
< |
int title; |
735 |
> |
int title=1; |
736 |
|
|
737 |
|
int cmdopt; |
738 |
|
extern int optind; |
741 |
|
display_config_t display_config; |
742 |
|
char ch; |
743 |
|
|
744 |
+ |
int data_fileno, stdin_fileno, biggest_fileno; |
745 |
+ |
|
746 |
|
sortby_ptr=NULL; |
747 |
|
|
748 |
|
/* What to display defaults */ |
795 |
|
case 's': |
796 |
|
if(!strcmp(optarg, "cpu")){ |
797 |
|
sortby_ptr=cmp_cpu_used; |
798 |
+ |
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
799 |
|
} |
800 |
|
if(!strcmp(optarg, "load")){ |
801 |
|
sortby_ptr=cmp_load_1; |
802 |
+ |
strlcpy(display_config.sortby, LOAD, SORTBYMAXNAME); |
803 |
|
} |
804 |
|
if(!strcmp(optarg, "mem")){ |
805 |
|
sortby_ptr=cmp_memory_used_pecent; |
806 |
+ |
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
807 |
|
} |
808 |
+ |
if(!strcmp(optarg, "swap")){ |
809 |
+ |
sortby_ptr=cmp_swap_used_pecent; |
810 |
+ |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
811 |
+ |
} |
812 |
|
if(sortby_ptr==NULL){ |
813 |
|
errf("Invalid sort type"); |
814 |
|
exit(1); |
817 |
|
} |
818 |
|
} |
819 |
|
|
820 |
< |
if(sortby_ptr==NULL) sortby_ptr=cmp_cpu_used; |
820 |
> |
if(sortby_ptr==NULL){ |
821 |
> |
sortby_ptr=cmp_cpu_used; |
822 |
> |
strlcpy(display_config.sortby, "CPU Used", SORTBYMAXNAME); |
823 |
> |
} |
824 |
|
|
825 |
|
if(argc<(optind+2)){ |
826 |
|
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
889 |
|
cbreak(); |
890 |
|
echo(); |
891 |
|
window=newwin(0, 0, 0, 0); |
892 |
+ |
getmaxyx(window, maxy, maxx); |
893 |
|
|
894 |
+ |
stdin_fileno=fileno(stdin); |
895 |
+ |
data_fileno=fileno(data); |
896 |
+ |
biggest_fileno=(data_fileno>stdin_fileno) ? (data_fileno+1) : (stdin_fileno+1); |
897 |
+ |
|
898 |
|
for(;;){ |
899 |
< |
response=fpgetline(data); |
900 |
< |
if (response==NULL){ |
901 |
< |
errf("Failed to read data (%m)"); |
902 |
< |
exit(1); |
903 |
< |
} |
899 |
> |
FD_ZERO(&infds); |
900 |
> |
FD_SET(stdin_fileno, &infds); |
901 |
> |
FD_SET(data_fileno, &infds); |
902 |
> |
select(biggest_fileno, &infds, NULL, NULL, NULL); |
903 |
> |
|
904 |
> |
if(FD_ISSET(stdin_fileno, &infds)){ |
905 |
> |
|
906 |
> |
ch=getc(stdin); |
907 |
> |
switch(ch){ |
908 |
> |
case 'Q': |
909 |
> |
case 'q': |
910 |
> |
endwin(); |
911 |
> |
exit(0); |
912 |
> |
break; |
913 |
|
|
914 |
< |
/* ch=getc(stdin); |
914 |
> |
case 'C': |
915 |
> |
sortby_ptr=cmp_cpu_used; |
916 |
> |
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
917 |
> |
break; |
918 |
> |
|
919 |
> |
case 'M': |
920 |
> |
sortby_ptr=cmp_memory_used_pecent; |
921 |
> |
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
922 |
> |
break; |
923 |
|
|
924 |
< |
if(ch=='q'){ |
925 |
< |
endwin(); |
926 |
< |
exit(0); |
924 |
> |
case 'L': |
925 |
> |
sortby_ptr=cmp_load_1; |
926 |
> |
strlcpy(display_config.sortby, LOAD, SORTBYMAXNAME); |
927 |
> |
break; |
928 |
> |
|
929 |
> |
case 'S': |
930 |
> |
sortby_ptr=cmp_swap_used_pecent; |
931 |
> |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
932 |
> |
break; |
933 |
> |
|
934 |
> |
case 'N': |
935 |
> |
if(sortby_ptr==cmp_network_io_total){ |
936 |
> |
strlcpy(display_config.sortby, NETIORX, SORTBYMAXNAME); |
937 |
> |
sortby_ptr=cmp_network_io_total_rx; |
938 |
> |
}else if(sortby_ptr==cmp_network_io_total_rx){ |
939 |
> |
strlcpy(display_config.sortby, NETIOTX, SORTBYMAXNAME); |
940 |
> |
sortby_ptr=cmp_network_io_total_tx; |
941 |
> |
}else{ |
942 |
> |
strlcpy(display_config.sortby, NETIO, SORTBYMAXNAME); |
943 |
> |
sortby_ptr=cmp_network_io_total; |
944 |
> |
} |
945 |
> |
break; |
946 |
> |
|
947 |
> |
} |
948 |
> |
|
949 |
> |
title=1; |
950 |
> |
|
951 |
|
} |
952 |
< |
*/ |
952 |
> |
if(FD_ISSET(data_fileno, &infds)){ |
953 |
> |
response=fpgetline(data); |
954 |
> |
if (response==NULL){ |
955 |
> |
errf("Failed to read data (%m)"); |
956 |
> |
exit(1); |
957 |
> |
} |
958 |
> |
} |
959 |
> |
|
960 |
> |
|
961 |
|
num_hosts=parse_xml(response, &machine_data_list); |
962 |
|
if(num_hosts==-1) continue; |
963 |
|
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
964 |
|
if(max_display==0){ |
965 |
< |
display(machine_data_list, &display_config, num_hosts, &title); |
965 |
> |
display(machine_data_list, &display_config, maxy, &title); |
966 |
|
}else{ |
967 |
|
display(machine_data_list, &display_config, max_display, &title); |
968 |
|
} |