| 153 |
|
|
| 154 |
|
GENERIC_MERGE_SORT(static, sort_machine_stats, machine_data_list_t, next) |
| 155 |
|
|
| 156 |
+ |
#define MKCMP(x) int cmp_##x(machine_data_list_t *a, machine_data_list_t *b){return ((a->x) == (b->x)? 0 : (((a->x) > (b->x))? -1 : 1));} |
| 157 |
+ |
|
| 158 |
+ |
|
| 159 |
+ |
int (*sortby_ptr)(machine_data_list_t *a, machine_data_list_t *b); |
| 160 |
+ |
|
| 161 |
+ |
MKCMP(cpu_used) |
| 162 |
+ |
MKCMP(load_1) |
| 163 |
+ |
MKCMP(network_io_total_tx) |
| 164 |
+ |
MKCMP(network_io_total_rx) |
| 165 |
+ |
MKCMP(disk_io_total_write) |
| 166 |
+ |
MKCMP(disk_io_total_read) |
| 167 |
+ |
MKCMP(memory_used_pecent) |
| 168 |
+ |
MKCMP(swap_used_pecent) |
| 169 |
+ |
|
| 170 |
+ |
/* |
| 171 |
|
int cmp_cpu(machine_data_list_t *a, machine_data_list_t *b){ |
| 172 |
|
|
| 173 |
|
if(a->cpu_used == b->cpu_used){ |
| 185 |
|
return 1; |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
+ |
*/ |
| 189 |
|
|
| 190 |
|
FILE *create_tcp_connection(char *hostname, int port){ |
| 191 |
|
int sock; |
| 551 |
|
|
| 552 |
|
} |
| 553 |
|
|
| 554 |
< |
void display(machine_data_list_t *machine_data_list, int num_lines){ |
| 554 |
> |
void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int num_lines, int *title){ |
| 555 |
|
int line_num=4; |
| 556 |
+ |
int x=1; |
| 557 |
|
|
| 558 |
+ |
if(*title){ |
| 559 |
+ |
printf("\033[2J"); |
| 560 |
+ |
// printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
| 561 |
+ |
// printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
| 562 |
+ |
printf("\033[1;1H%-11s", "Hostname"); |
| 563 |
+ |
x=x+11+1; |
| 564 |
+ |
if(display_config->cpu_used){ |
| 565 |
+ |
printf("\033[1;%dH%5s", x, "CPU"); |
| 566 |
+ |
printf("\033[2;%dH%5s", x, "used%"); |
| 567 |
+ |
x+=6; |
| 568 |
+ |
} |
| 569 |
+ |
if(display_config->load_1){ |
| 570 |
+ |
printf("\033[1;%dH%5s", x, "Load"); |
| 571 |
+ |
printf("\033[2;%dH%5s", x, "(1m)"); |
| 572 |
+ |
x+=6; |
| 573 |
+ |
} |
| 574 |
+ |
if(display_config->pages_in){ |
| 575 |
+ |
printf("\033[1;%dH%5s", x, "Page"); |
| 576 |
+ |
printf("\033[2;%dH%5s", x, "ins"); |
| 577 |
+ |
x+=6; |
| 578 |
+ |
} |
| 579 |
+ |
if(display_config->pages_out){ |
| 580 |
+ |
printf("\033[1;%dH%5s", x, "Page"); |
| 581 |
+ |
printf("\033[2;%dH%5s", x, "outs"); |
| 582 |
+ |
x+=6; |
| 583 |
+ |
} |
| 584 |
+ |
if(display_config->memory_used_pecent){ |
| 585 |
+ |
printf("\033[1;%dH%5s", x, "Mem"); |
| 586 |
+ |
printf("\033[2;%dH%5s", x, "used"); |
| 587 |
+ |
x+=6; |
| 588 |
+ |
} |
| 589 |
+ |
if(display_config->swap_used_pecent){ |
| 590 |
+ |
printf("\033[1;%dH%5s", x, "Swap"); |
| 591 |
+ |
printf("\033[2;%dH%5s", x, "used"); |
| 592 |
+ |
x+=6; |
| 593 |
+ |
} |
| 594 |
+ |
if(display_config->network_io_total_rx){ |
| 595 |
+ |
printf("\033[1;%dH%8s", x, "Net"); |
| 596 |
+ |
printf("\033[2;%dH%8s", x, "rx"); |
| 597 |
+ |
x+=9; |
| 598 |
+ |
} |
| 599 |
+ |
if(display_config->network_io_total_tx){ |
| 600 |
+ |
printf("\033[1;%dH%8s", x, "Net"); |
| 601 |
+ |
printf("\033[2;%dH%8s", x, "tx"); |
| 602 |
+ |
x+=9; |
| 603 |
+ |
} |
| 604 |
+ |
if(display_config->disk_io_total_read){ |
| 605 |
+ |
printf("\033[1;%dH%9s", x, "Disk"); |
| 606 |
+ |
printf("\033[2;%dH%9s", x, "read"); |
| 607 |
+ |
x+=10; |
| 608 |
+ |
} |
| 609 |
+ |
if(display_config->disk_io_total_read){ |
| 610 |
+ |
printf("\033[1;%dH%9s", x, "Disk"); |
| 611 |
+ |
printf("\033[2;%dH%9s", x, "write"); |
| 612 |
+ |
x+=10; |
| 613 |
+ |
} |
| 614 |
+ |
|
| 615 |
+ |
*title=0; |
| 616 |
+ |
} |
| 617 |
+ |
|
| 618 |
|
for(;num_lines;num_lines--){ |
| 619 |
|
if(machine_data_list==NULL) break; |
| 620 |
< |
printf("\033[%d;%dH%-11s %5.1f %5.1f %5d %5d %5.1f %5.1f %8lld %8lld %9lld %9lld", \ |
| 621 |
< |
line_num++, 1, \ |
| 622 |
< |
machine_data_list->sysname, |
| 623 |
< |
machine_data_list->cpu_used, |
| 624 |
< |
machine_data_list->load_1, |
| 625 |
< |
machine_data_list->pages_in, |
| 626 |
< |
machine_data_list->pages_out, |
| 627 |
< |
machine_data_list->memory_used_pecent, |
| 628 |
< |
machine_data_list->swap_used_pecent, |
| 629 |
< |
machine_data_list->network_io_total_rx, |
| 630 |
< |
machine_data_list->network_io_total_tx, |
| 631 |
< |
machine_data_list->disk_io_total_read, |
| 632 |
< |
machine_data_list->disk_io_total_write); |
| 620 |
> |
|
| 621 |
> |
printf("\033[%d;%dH%-11s", line_num++, 1, machine_data_list->sysname); |
| 622 |
> |
|
| 623 |
> |
if(display_config->cpu_used) printf(" %5.1f", machine_data_list->cpu_used); |
| 624 |
> |
if(display_config->load_1) printf(" %5.1f", machine_data_list->load_1); |
| 625 |
> |
if(display_config->pages_in) printf(" %5d", machine_data_list->pages_in); |
| 626 |
> |
if(display_config->pages_out) printf(" %5d", machine_data_list->pages_out); |
| 627 |
> |
if(display_config->memory_used_pecent) printf(" %5.1f", machine_data_list->memory_used_pecent); |
| 628 |
> |
if(display_config->swap_used_pecent) printf(" %5.1f", machine_data_list->swap_used_pecent); |
| 629 |
> |
if(display_config->network_io_total_rx) printf(" %8lld", machine_data_list->network_io_total_rx); |
| 630 |
> |
if(display_config->network_io_total_tx) printf(" %8lld", machine_data_list->network_io_total_tx); |
| 631 |
> |
if(display_config->disk_io_total_read) printf(" %9lld", machine_data_list->disk_io_total_read); |
| 632 |
> |
if(display_config->disk_io_total_write) printf(" %9lld", machine_data_list->disk_io_total_write); |
| 633 |
> |
|
| 634 |
|
machine_data_list=machine_data_list->next; |
| 635 |
|
} |
| 636 |
|
|
| 637 |
+ |
|
| 638 |
|
fflush(stdout); |
| 639 |
|
|
| 640 |
|
} |
| 654 |
|
|
| 655 |
|
int num_hosts; |
| 656 |
|
int max_display=0; |
| 657 |
+ |
int title; |
| 658 |
|
|
| 659 |
|
int cmdopt; |
| 660 |
|
extern int optind; |
| 661 |
|
extern char *optarg; |
| 662 |
+ |
sortby_ptr=NULL; |
| 663 |
|
|
| 664 |
|
display_config_t display_config; |
| 665 |
|
|
| 694 |
|
display_config.processes_zombie=0; |
| 695 |
|
display_config.processes_stopped=0; |
| 696 |
|
|
| 697 |
< |
display_config.network_io_total_tx=0; |
| 698 |
< |
display_config.network_io_total_rx=0; |
| 697 |
> |
display_config.network_io_total_tx=1; |
| 698 |
> |
display_config.network_io_total_rx=1; |
| 699 |
|
display_config.network_all_stats=1; |
| 700 |
|
|
| 701 |
< |
display_config.disk_io_total_write=0; |
| 702 |
< |
display_config.disk_io_total_read=0; |
| 703 |
< |
display_config.disk_io_all_stats=1; |
| 701 |
> |
display_config.disk_io_total_write=1; |
| 702 |
> |
display_config.disk_io_total_read=1; |
| 703 |
> |
display_config.disk_io_all_stats=0; |
| 704 |
|
|
| 705 |
|
display_config.disk_total_used=0; |
| 706 |
|
display_config.disk_all_stats=0; |
| 707 |
|
|
| 708 |
< |
while((cmdopt=getopt(argc, argv, "d:")) != -1){ |
| 708 |
> |
|
| 709 |
> |
while((cmdopt=getopt(argc, argv, "d:s:")) != -1){ |
| 710 |
|
switch(cmdopt){ |
| 711 |
|
case 'd': |
| 712 |
|
max_display=atoi(optarg); |
| 713 |
|
break; |
| 714 |
+ |
case 's': |
| 715 |
+ |
if(!strcmp(optarg, "cpu")){ |
| 716 |
+ |
sortby_ptr=cmp_cpu_used; |
| 717 |
+ |
} |
| 718 |
+ |
if(!strcmp(optarg, "load")){ |
| 719 |
+ |
sortby_ptr=cmp_load_1; |
| 720 |
+ |
} |
| 721 |
+ |
if(!strcmp(optarg, "mem")){ |
| 722 |
+ |
sortby_ptr=cmp_memory_used_pecent; |
| 723 |
+ |
} |
| 724 |
+ |
if(sortby_ptr==NULL){ |
| 725 |
+ |
errf("Invalid sort type"); |
| 726 |
+ |
exit(1); |
| 727 |
+ |
} |
| 728 |
+ |
break; |
| 729 |
|
} |
| 730 |
|
} |
| 731 |
|
|
| 732 |
+ |
if(sortby_ptr==NULL) sortby_ptr=cmp_cpu_used; |
| 733 |
|
|
| 734 |
|
if(argc<(optind+2)){ |
| 735 |
|
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
| 787 |
|
errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); |
| 788 |
|
} |
| 789 |
|
|
| 790 |
< |
|
| 790 |
> |
/* |
| 791 |
|
printf("\033[2J"); |
| 792 |
|
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
| 793 |
|
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
| 794 |
+ |
*/ |
| 795 |
|
fflush(stdout); |
| 796 |
|
for(;;){ |
| 797 |
|
response=fpgetline(data); |
| 802 |
|
|
| 803 |
|
num_hosts=parse_xml(response, &machine_data_list); |
| 804 |
|
if(num_hosts==-1) continue; |
| 805 |
< |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, cmp_cpu); |
| 805 |
> |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
| 806 |
|
if(max_display==0){ |
| 807 |
< |
display(machine_data_list, num_hosts); |
| 807 |
> |
display(machine_data_list, &display_config, num_hosts, &title); |
| 808 |
|
}else{ |
| 809 |
< |
display(machine_data_list, max_display); |
| 809 |
> |
display(machine_data_list, &display_config, max_display, &title); |
| 810 |
|
} |
| 811 |
|
|
| 812 |
|
} |