| 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> |
| 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; |
| 45 |
|
int line; |
| 183 |
|
|
| 184 |
|
GENERIC_MERGE_SORT(static, sort_machine_stats, machine_data_list_t, next) |
| 185 |
|
|
| 186 |
+ |
#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));} |
| 187 |
+ |
|
| 188 |
+ |
|
| 189 |
+ |
int (*sortby_ptr)(machine_data_list_t *a, machine_data_list_t *b); |
| 190 |
+ |
|
| 191 |
+ |
MKCMP(cpu_used) |
| 192 |
+ |
MKCMP(load_1) |
| 193 |
+ |
MKCMP(network_io_total_tx) |
| 194 |
+ |
MKCMP(network_io_total_rx) |
| 195 |
+ |
MKCMP(disk_io_total_write) |
| 196 |
+ |
MKCMP(disk_io_total_read) |
| 197 |
+ |
MKCMP(memory_used_pecent) |
| 198 |
+ |
MKCMP(swap_used_pecent) |
| 199 |
+ |
|
| 200 |
+ |
/* |
| 201 |
|
int cmp_cpu(machine_data_list_t *a, machine_data_list_t *b){ |
| 202 |
|
|
| 203 |
|
if(a->cpu_used == b->cpu_used){ |
| 215 |
|
return 1; |
| 216 |
|
} |
| 217 |
|
} |
| 218 |
+ |
*/ |
| 219 |
|
|
| 220 |
|
FILE *create_tcp_connection(char *hostname, int port){ |
| 221 |
|
int sock; |
| 581 |
|
|
| 582 |
|
} |
| 583 |
|
|
| 584 |
< |
void display(machine_data_list_t *machine_data_list, int num_lines){ |
| 584 |
> |
void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int num_lines, int *title){ |
| 585 |
|
int line_num=4; |
| 586 |
+ |
int x=1; |
| 587 |
|
|
| 588 |
+ |
if(*title){ |
| 589 |
+ |
// printf("\033[2J"); |
| 590 |
+ |
// printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
| 591 |
+ |
// printw("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
| 592 |
+ |
move(1,1); |
| 593 |
+ |
printw("%-11s", "Hostname"); |
| 594 |
+ |
x=x+11+1; |
| 595 |
+ |
if(display_config->cpu_used){ |
| 596 |
+ |
move(1,x); |
| 597 |
+ |
printw("%5s", "CPU"); |
| 598 |
+ |
move(2,x); |
| 599 |
+ |
printw("%5s", "used%"); |
| 600 |
+ |
x+=6; |
| 601 |
+ |
} |
| 602 |
+ |
if(display_config->load_1){ |
| 603 |
+ |
move(1,x); |
| 604 |
+ |
printw("%5s", "Load"); |
| 605 |
+ |
move(2,x); |
| 606 |
+ |
printw("%5s", "(1m)"); |
| 607 |
+ |
x+=6; |
| 608 |
+ |
} |
| 609 |
+ |
if(display_config->pages_in){ |
| 610 |
+ |
move(1,x); |
| 611 |
+ |
printw("%5s", "Page"); |
| 612 |
+ |
move(2,x); |
| 613 |
+ |
printw("%5s", "ins"); |
| 614 |
+ |
x+=6; |
| 615 |
+ |
} |
| 616 |
+ |
if(display_config->pages_out){ |
| 617 |
+ |
move(1,x); |
| 618 |
+ |
printw("%5s", "Page"); |
| 619 |
+ |
move(2,x); |
| 620 |
+ |
printw("%5s", "outs"); |
| 621 |
+ |
x+=6; |
| 622 |
+ |
} |
| 623 |
+ |
if(display_config->memory_used_pecent){ |
| 624 |
+ |
move(1,x); |
| 625 |
+ |
printw("%5s", "Mem"); |
| 626 |
+ |
move(2,x); |
| 627 |
+ |
printw("%5s", "used"); |
| 628 |
+ |
x+=6; |
| 629 |
+ |
} |
| 630 |
+ |
if(display_config->swap_used_pecent){ |
| 631 |
+ |
move(1,x); |
| 632 |
+ |
printw("%5s", "Swap"); |
| 633 |
+ |
move(2,x); |
| 634 |
+ |
printw("%5s", "used"); |
| 635 |
+ |
x+=6; |
| 636 |
+ |
} |
| 637 |
+ |
if(display_config->network_io_total_rx){ |
| 638 |
+ |
move(1,x); |
| 639 |
+ |
printw("%8s", "Net"); |
| 640 |
+ |
move(2,x); |
| 641 |
+ |
printw("%8s", "rx"); |
| 642 |
+ |
x+=9; |
| 643 |
+ |
} |
| 644 |
+ |
if(display_config->network_io_total_tx){ |
| 645 |
+ |
move(1,x); |
| 646 |
+ |
printw("%8s", "Net"); |
| 647 |
+ |
move(2,x); |
| 648 |
+ |
printw("%8s", "tx"); |
| 649 |
+ |
x+=9; |
| 650 |
+ |
} |
| 651 |
+ |
if(display_config->disk_io_total_read){ |
| 652 |
+ |
move(1,x); |
| 653 |
+ |
printw("%9s", "Disk"); |
| 654 |
+ |
move(2,x); |
| 655 |
+ |
printw("%9s", "read"); |
| 656 |
+ |
x+=10; |
| 657 |
+ |
} |
| 658 |
+ |
if(display_config->disk_io_total_read){ |
| 659 |
+ |
move(1,x); |
| 660 |
+ |
printw("%9s", "Disk"); |
| 661 |
+ |
move(2,x); |
| 662 |
+ |
printw("%9s", "write"); |
| 663 |
+ |
x+=10; |
| 664 |
+ |
} |
| 665 |
+ |
|
| 666 |
+ |
*title=0; |
| 667 |
+ |
} |
| 668 |
+ |
|
| 669 |
|
for(;num_lines;num_lines--){ |
| 670 |
|
if(machine_data_list==NULL) break; |
| 671 |
< |
printf("\033[%d;%dH%-11s %5.1f %5.1f %5d %5d %5.1f %5.1f %8lld %8lld %9lld %9lld", \ |
| 672 |
< |
line_num++, 1, \ |
| 673 |
< |
machine_data_list->sysname, |
| 674 |
< |
machine_data_list->cpu_used, |
| 675 |
< |
machine_data_list->load_1, |
| 676 |
< |
machine_data_list->pages_in, |
| 677 |
< |
machine_data_list->pages_out, |
| 678 |
< |
machine_data_list->memory_used_pecent, |
| 679 |
< |
machine_data_list->swap_used_pecent, |
| 680 |
< |
machine_data_list->network_io_total_rx, |
| 681 |
< |
machine_data_list->network_io_total_tx, |
| 682 |
< |
machine_data_list->disk_io_total_read, |
| 683 |
< |
machine_data_list->disk_io_total_write); |
| 671 |
> |
move(line_num++, 1); |
| 672 |
> |
printw("%-11s", machine_data_list->sysname); |
| 673 |
> |
|
| 674 |
> |
if(display_config->cpu_used) printw(" %5.1f", machine_data_list->cpu_used); |
| 675 |
> |
if(display_config->load_1) printw(" %5.1f", machine_data_list->load_1); |
| 676 |
> |
if(display_config->pages_in) printw(" %5d", machine_data_list->pages_in); |
| 677 |
> |
if(display_config->pages_out) printw(" %5d", machine_data_list->pages_out); |
| 678 |
> |
if(display_config->memory_used_pecent) printw(" %5.1f", machine_data_list->memory_used_pecent); |
| 679 |
> |
if(display_config->swap_used_pecent) printw(" %5.1f", machine_data_list->swap_used_pecent); |
| 680 |
> |
if(display_config->network_io_total_rx) printw(" %8lld", machine_data_list->network_io_total_rx); |
| 681 |
> |
if(display_config->network_io_total_tx) printw(" %8lld", machine_data_list->network_io_total_tx); |
| 682 |
> |
if(display_config->disk_io_total_read) printw(" %9lld", machine_data_list->disk_io_total_read); |
| 683 |
> |
if(display_config->disk_io_total_write) printw(" %9lld", machine_data_list->disk_io_total_write); |
| 684 |
> |
|
| 685 |
|
machine_data_list=machine_data_list->next; |
| 686 |
|
} |
| 687 |
|
|
| 688 |
< |
fflush(stdout); |
| 688 |
> |
|
| 689 |
> |
refresh(); |
| 690 |
|
|
| 691 |
|
} |
| 692 |
|
|
| 693 |
|
int main(int argc, char **argv){ |
| 694 |
+ |
WINDOW *window; |
| 695 |
+ |
fd_set infds; |
| 696 |
+ |
|
| 697 |
|
FILE *control; |
| 698 |
|
FILE *data; |
| 699 |
|
|
| 708 |
|
|
| 709 |
|
int num_hosts; |
| 710 |
|
int max_display=0; |
| 711 |
+ |
int title; |
| 712 |
|
|
| 713 |
|
int cmdopt; |
| 714 |
|
extern int optind; |
| 715 |
|
extern char *optarg; |
| 716 |
|
|
| 717 |
|
display_config_t display_config; |
| 718 |
+ |
char ch; |
| 719 |
|
|
| 720 |
+ |
int data_fileno, stdin_fileno, biggest_fileno; |
| 721 |
+ |
|
| 722 |
+ |
sortby_ptr=NULL; |
| 723 |
+ |
|
| 724 |
|
/* What to display defaults */ |
| 725 |
|
display_config.cpu_user=0; |
| 726 |
|
display_config.cpu_idle=0; |
| 752 |
|
display_config.processes_zombie=0; |
| 753 |
|
display_config.processes_stopped=0; |
| 754 |
|
|
| 755 |
< |
display_config.network_io_total_tx=0; |
| 756 |
< |
display_config.network_io_total_rx=0; |
| 755 |
> |
display_config.network_io_total_tx=1; |
| 756 |
> |
display_config.network_io_total_rx=1; |
| 757 |
|
display_config.network_all_stats=1; |
| 758 |
|
|
| 759 |
|
display_config.disk_io_total_write=0; |
| 760 |
|
display_config.disk_io_total_read=0; |
| 761 |
< |
display_config.disk_io_all_stats=1; |
| 761 |
> |
display_config.disk_io_all_stats=0; |
| 762 |
|
|
| 763 |
|
display_config.disk_total_used=0; |
| 764 |
|
display_config.disk_all_stats=0; |
| 765 |
|
|
| 766 |
< |
while((cmdopt=getopt(argc, argv, "d:")) != -1){ |
| 766 |
> |
while((cmdopt=getopt(argc, argv, "d:s:")) != -1){ |
| 767 |
|
switch(cmdopt){ |
| 768 |
|
case 'd': |
| 769 |
|
max_display=atoi(optarg); |
| 770 |
|
break; |
| 771 |
+ |
case 's': |
| 772 |
+ |
if(!strcmp(optarg, "cpu")){ |
| 773 |
+ |
sortby_ptr=cmp_cpu_used; |
| 774 |
+ |
} |
| 775 |
+ |
if(!strcmp(optarg, "load")){ |
| 776 |
+ |
sortby_ptr=cmp_load_1; |
| 777 |
+ |
} |
| 778 |
+ |
if(!strcmp(optarg, "mem")){ |
| 779 |
+ |
sortby_ptr=cmp_memory_used_pecent; |
| 780 |
+ |
} |
| 781 |
+ |
if(sortby_ptr==NULL){ |
| 782 |
+ |
errf("Invalid sort type"); |
| 783 |
+ |
exit(1); |
| 784 |
+ |
} |
| 785 |
+ |
break; |
| 786 |
|
} |
| 787 |
|
} |
| 788 |
|
|
| 789 |
+ |
if(sortby_ptr==NULL) sortby_ptr=cmp_cpu_used; |
| 790 |
|
|
| 791 |
|
if(argc<(optind+2)){ |
| 792 |
|
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
| 844 |
|
errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); |
| 845 |
|
} |
| 846 |
|
|
| 847 |
< |
|
| 847 |
> |
/* |
| 848 |
|
printf("\033[2J"); |
| 849 |
|
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
| 850 |
|
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
| 851 |
< |
fflush(stdout); |
| 851 |
> |
*/ |
| 852 |
> |
|
| 853 |
> |
initscr(); |
| 854 |
> |
nonl(); |
| 855 |
> |
cbreak(); |
| 856 |
> |
echo(); |
| 857 |
> |
window=newwin(0, 0, 0, 0); |
| 858 |
> |
|
| 859 |
> |
stdin_fileno=fileno(stdin); |
| 860 |
> |
data_fileno=fileno(data); |
| 861 |
> |
biggest_fileno=(data_fileno>stdin_fileno) ? (data_fileno+1) : (stdin_fileno+1); |
| 862 |
> |
|
| 863 |
|
for(;;){ |
| 864 |
< |
response=fpgetline(data); |
| 865 |
< |
if (response==NULL){ |
| 866 |
< |
errf("Failed to read data (%m)"); |
| 867 |
< |
exit(1); |
| 864 |
> |
FD_ZERO(&infds); |
| 865 |
> |
FD_SET(stdin_fileno, &infds); |
| 866 |
> |
FD_SET(data_fileno, &infds); |
| 867 |
> |
select(biggest_fileno, &infds, NULL, NULL, NULL); |
| 868 |
> |
|
| 869 |
> |
if(FD_ISSET(stdin_fileno, &infds)){ |
| 870 |
> |
|
| 871 |
> |
ch=getc(stdin); |
| 872 |
> |
|
| 873 |
> |
if(ch=='q'){ |
| 874 |
> |
endwin(); |
| 875 |
> |
exit(0); |
| 876 |
> |
} |
| 877 |
|
} |
| 878 |
+ |
if(FD_ISSET(data_fileno, &infds)){ |
| 879 |
+ |
response=fpgetline(data); |
| 880 |
+ |
if (response==NULL){ |
| 881 |
+ |
errf("Failed to read data (%m)"); |
| 882 |
+ |
exit(1); |
| 883 |
+ |
} |
| 884 |
+ |
} |
| 885 |
|
|
| 886 |
+ |
|
| 887 |
|
num_hosts=parse_xml(response, &machine_data_list); |
| 888 |
|
if(num_hosts==-1) continue; |
| 889 |
< |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, cmp_cpu); |
| 889 |
> |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
| 890 |
|
if(max_display==0){ |
| 891 |
< |
display(machine_data_list, num_hosts); |
| 891 |
> |
display(machine_data_list, &display_config, num_hosts, &title); |
| 892 |
|
}else{ |
| 893 |
< |
display(machine_data_list, max_display); |
| 893 |
> |
display(machine_data_list, &display_config, max_display, &title); |
| 894 |
|
} |
| 895 |
|
|
| 896 |
|
} |