| 1 |
+ |
/* |
| 2 |
+ |
* i-scream central monitoring system |
| 3 |
+ |
* http://www.i-scream.org.uk |
| 4 |
+ |
* Copyright (C) 2000-2002 i-scream |
| 5 |
+ |
* |
| 6 |
+ |
* This program is free software; you can redistribute it and/or |
| 7 |
+ |
* modify it under the terms of the GNU General Public License |
| 8 |
+ |
* as published by the Free Software Foundation; either version 2 |
| 9 |
+ |
* of the License, or (at your option) any later version. |
| 10 |
+ |
* |
| 11 |
+ |
* This program is distributed in the hope that it will be useful, |
| 12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
+ |
* GNU General Public License for more details. |
| 15 |
+ |
* |
| 16 |
+ |
* You should have received a copy of the GNU General Public License |
| 17 |
+ |
* along with this program; if not, write to the Free Software |
| 18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
+ |
*/ |
| 20 |
+ |
|
| 21 |
+ |
#ifdef HAVE_CONFIG_H |
| 22 |
+ |
#include "config.h" |
| 23 |
+ |
#endif |
| 24 |
+ |
|
| 25 |
|
#include <stdio.h> |
| 26 |
|
#include <string.h> |
| 27 |
|
#include <sys/types.h> |
| 177 |
|
|
| 178 |
|
GENERIC_MERGE_SORT(static, sort_machine_stats, machine_data_list_t, next) |
| 179 |
|
|
| 180 |
+ |
#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));} |
| 181 |
+ |
|
| 182 |
+ |
|
| 183 |
+ |
int (*sortby_ptr)(machine_data_list_t *a, machine_data_list_t *b); |
| 184 |
+ |
|
| 185 |
+ |
MKCMP(cpu_used) |
| 186 |
+ |
MKCMP(load_1) |
| 187 |
+ |
MKCMP(network_io_total_tx) |
| 188 |
+ |
MKCMP(network_io_total_rx) |
| 189 |
+ |
MKCMP(disk_io_total_write) |
| 190 |
+ |
MKCMP(disk_io_total_read) |
| 191 |
+ |
MKCMP(memory_used_pecent) |
| 192 |
+ |
MKCMP(swap_used_pecent) |
| 193 |
+ |
|
| 194 |
+ |
/* |
| 195 |
|
int cmp_cpu(machine_data_list_t *a, machine_data_list_t *b){ |
| 196 |
|
|
| 197 |
|
if(a->cpu_used == b->cpu_used){ |
| 209 |
|
return 1; |
| 210 |
|
} |
| 211 |
|
} |
| 212 |
+ |
*/ |
| 213 |
|
|
| 214 |
|
FILE *create_tcp_connection(char *hostname, int port){ |
| 215 |
|
int sock; |
| 575 |
|
|
| 576 |
|
} |
| 577 |
|
|
| 578 |
< |
void display(machine_data_list_t *machine_data_list, int num_lines){ |
| 578 |
> |
void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int num_lines, int *title){ |
| 579 |
|
int line_num=4; |
| 580 |
+ |
int x=1; |
| 581 |
|
|
| 582 |
+ |
if(*title){ |
| 583 |
+ |
printf("\033[2J"); |
| 584 |
+ |
// printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
| 585 |
+ |
// printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
| 586 |
+ |
printf("\033[1;1H%-11s", "Hostname"); |
| 587 |
+ |
x=x+11+1; |
| 588 |
+ |
if(display_config->cpu_used){ |
| 589 |
+ |
printf("\033[1;%dH%5s", x, "CPU"); |
| 590 |
+ |
printf("\033[2;%dH%5s", x, "used%"); |
| 591 |
+ |
x+=6; |
| 592 |
+ |
} |
| 593 |
+ |
if(display_config->load_1){ |
| 594 |
+ |
printf("\033[1;%dH%5s", x, "Load"); |
| 595 |
+ |
printf("\033[2;%dH%5s", x, "(1m)"); |
| 596 |
+ |
x+=6; |
| 597 |
+ |
} |
| 598 |
+ |
if(display_config->pages_in){ |
| 599 |
+ |
printf("\033[1;%dH%5s", x, "Page"); |
| 600 |
+ |
printf("\033[2;%dH%5s", x, "ins"); |
| 601 |
+ |
x+=6; |
| 602 |
+ |
} |
| 603 |
+ |
if(display_config->pages_out){ |
| 604 |
+ |
printf("\033[1;%dH%5s", x, "Page"); |
| 605 |
+ |
printf("\033[2;%dH%5s", x, "outs"); |
| 606 |
+ |
x+=6; |
| 607 |
+ |
} |
| 608 |
+ |
if(display_config->memory_used_pecent){ |
| 609 |
+ |
printf("\033[1;%dH%5s", x, "Mem"); |
| 610 |
+ |
printf("\033[2;%dH%5s", x, "used"); |
| 611 |
+ |
x+=6; |
| 612 |
+ |
} |
| 613 |
+ |
if(display_config->swap_used_pecent){ |
| 614 |
+ |
printf("\033[1;%dH%5s", x, "Swap"); |
| 615 |
+ |
printf("\033[2;%dH%5s", x, "used"); |
| 616 |
+ |
x+=6; |
| 617 |
+ |
} |
| 618 |
+ |
if(display_config->network_io_total_rx){ |
| 619 |
+ |
printf("\033[1;%dH%8s", x, "Net"); |
| 620 |
+ |
printf("\033[2;%dH%8s", x, "rx"); |
| 621 |
+ |
x+=9; |
| 622 |
+ |
} |
| 623 |
+ |
if(display_config->network_io_total_tx){ |
| 624 |
+ |
printf("\033[1;%dH%8s", x, "Net"); |
| 625 |
+ |
printf("\033[2;%dH%8s", x, "tx"); |
| 626 |
+ |
x+=9; |
| 627 |
+ |
} |
| 628 |
+ |
if(display_config->disk_io_total_read){ |
| 629 |
+ |
printf("\033[1;%dH%9s", x, "Disk"); |
| 630 |
+ |
printf("\033[2;%dH%9s", x, "read"); |
| 631 |
+ |
x+=10; |
| 632 |
+ |
} |
| 633 |
+ |
if(display_config->disk_io_total_read){ |
| 634 |
+ |
printf("\033[1;%dH%9s", x, "Disk"); |
| 635 |
+ |
printf("\033[2;%dH%9s", x, "write"); |
| 636 |
+ |
x+=10; |
| 637 |
+ |
} |
| 638 |
+ |
|
| 639 |
+ |
*title=0; |
| 640 |
+ |
} |
| 641 |
+ |
|
| 642 |
|
for(;num_lines;num_lines--){ |
| 643 |
|
if(machine_data_list==NULL) break; |
| 644 |
< |
printf("\033[%d;%dH%-11s %5.1f %5.1f %5d %5d %5.1f %5.1f %8lld %8lld %9lld %9lld", \ |
| 645 |
< |
line_num++, 1, \ |
| 646 |
< |
machine_data_list->sysname, |
| 647 |
< |
machine_data_list->cpu_used, |
| 648 |
< |
machine_data_list->load_1, |
| 649 |
< |
machine_data_list->pages_in, |
| 650 |
< |
machine_data_list->pages_out, |
| 651 |
< |
machine_data_list->memory_used_pecent, |
| 652 |
< |
machine_data_list->swap_used_pecent, |
| 653 |
< |
machine_data_list->network_io_total_rx, |
| 654 |
< |
machine_data_list->network_io_total_tx, |
| 655 |
< |
machine_data_list->disk_io_total_read, |
| 656 |
< |
machine_data_list->disk_io_total_write); |
| 644 |
> |
|
| 645 |
> |
printf("\033[%d;%dH%-11s", line_num++, 1, machine_data_list->sysname); |
| 646 |
> |
|
| 647 |
> |
if(display_config->cpu_used) printf(" %5.1f", machine_data_list->cpu_used); |
| 648 |
> |
if(display_config->load_1) printf(" %5.1f", machine_data_list->load_1); |
| 649 |
> |
if(display_config->pages_in) printf(" %5d", machine_data_list->pages_in); |
| 650 |
> |
if(display_config->pages_out) printf(" %5d", machine_data_list->pages_out); |
| 651 |
> |
if(display_config->memory_used_pecent) printf(" %5.1f", machine_data_list->memory_used_pecent); |
| 652 |
> |
if(display_config->swap_used_pecent) printf(" %5.1f", machine_data_list->swap_used_pecent); |
| 653 |
> |
if(display_config->network_io_total_rx) printf(" %8lld", machine_data_list->network_io_total_rx); |
| 654 |
> |
if(display_config->network_io_total_tx) printf(" %8lld", machine_data_list->network_io_total_tx); |
| 655 |
> |
if(display_config->disk_io_total_read) printf(" %9lld", machine_data_list->disk_io_total_read); |
| 656 |
> |
if(display_config->disk_io_total_write) printf(" %9lld", machine_data_list->disk_io_total_write); |
| 657 |
> |
|
| 658 |
|
machine_data_list=machine_data_list->next; |
| 659 |
|
} |
| 660 |
|
|
| 661 |
+ |
|
| 662 |
|
fflush(stdout); |
| 663 |
|
|
| 664 |
|
} |
| 678 |
|
|
| 679 |
|
int num_hosts; |
| 680 |
|
int max_display=0; |
| 681 |
+ |
int title; |
| 682 |
|
|
| 683 |
|
int cmdopt; |
| 684 |
|
extern int optind; |
| 685 |
|
extern char *optarg; |
| 686 |
+ |
sortby_ptr=NULL; |
| 687 |
|
|
| 688 |
|
display_config_t display_config; |
| 689 |
|
|
| 718 |
|
display_config.processes_zombie=0; |
| 719 |
|
display_config.processes_stopped=0; |
| 720 |
|
|
| 721 |
< |
display_config.network_io_total_tx=0; |
| 722 |
< |
display_config.network_io_total_rx=0; |
| 721 |
> |
display_config.network_io_total_tx=1; |
| 722 |
> |
display_config.network_io_total_rx=1; |
| 723 |
|
display_config.network_all_stats=1; |
| 724 |
|
|
| 725 |
< |
display_config.disk_io_total_write=0; |
| 726 |
< |
display_config.disk_io_total_read=0; |
| 727 |
< |
display_config.disk_io_all_stats=1; |
| 725 |
> |
display_config.disk_io_total_write=1; |
| 726 |
> |
display_config.disk_io_total_read=1; |
| 727 |
> |
display_config.disk_io_all_stats=0; |
| 728 |
|
|
| 729 |
|
display_config.disk_total_used=0; |
| 730 |
|
display_config.disk_all_stats=0; |
| 731 |
|
|
| 732 |
< |
while((cmdopt=getopt(argc, argv, "d:")) != -1){ |
| 732 |
> |
|
| 733 |
> |
while((cmdopt=getopt(argc, argv, "d:s:")) != -1){ |
| 734 |
|
switch(cmdopt){ |
| 735 |
|
case 'd': |
| 736 |
|
max_display=atoi(optarg); |
| 737 |
|
break; |
| 738 |
+ |
case 's': |
| 739 |
+ |
if(!strcmp(optarg, "cpu")){ |
| 740 |
+ |
sortby_ptr=cmp_cpu_used; |
| 741 |
+ |
} |
| 742 |
+ |
if(!strcmp(optarg, "load")){ |
| 743 |
+ |
sortby_ptr=cmp_load_1; |
| 744 |
+ |
} |
| 745 |
+ |
if(!strcmp(optarg, "mem")){ |
| 746 |
+ |
sortby_ptr=cmp_memory_used_pecent; |
| 747 |
+ |
} |
| 748 |
+ |
if(sortby_ptr==NULL){ |
| 749 |
+ |
errf("Invalid sort type"); |
| 750 |
+ |
exit(1); |
| 751 |
+ |
} |
| 752 |
+ |
break; |
| 753 |
|
} |
| 754 |
|
} |
| 755 |
|
|
| 756 |
+ |
if(sortby_ptr==NULL) sortby_ptr=cmp_cpu_used; |
| 757 |
|
|
| 758 |
|
if(argc<(optind+2)){ |
| 759 |
|
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
| 811 |
|
errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); |
| 812 |
|
} |
| 813 |
|
|
| 814 |
< |
|
| 814 |
> |
/* |
| 815 |
|
printf("\033[2J"); |
| 816 |
|
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
| 817 |
|
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
| 818 |
+ |
*/ |
| 819 |
|
fflush(stdout); |
| 820 |
|
for(;;){ |
| 821 |
|
response=fpgetline(data); |
| 826 |
|
|
| 827 |
|
num_hosts=parse_xml(response, &machine_data_list); |
| 828 |
|
if(num_hosts==-1) continue; |
| 829 |
< |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, cmp_cpu); |
| 829 |
> |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
| 830 |
|
if(max_display==0){ |
| 831 |
< |
display(machine_data_list, num_hosts); |
| 831 |
> |
display(machine_data_list, &display_config, num_hosts, &title); |
| 832 |
|
}else{ |
| 833 |
< |
display(machine_data_list, max_display); |
| 833 |
> |
display(machine_data_list, &display_config, max_display, &title); |
| 834 |
|
} |
| 835 |
|
|
| 836 |
|
} |