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; |
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 |
< |
// printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
614 |
< |
printf("\033[1;1H%-11s", "Hostname"); |
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; |
617 |
|
if(display_config->cpu_used){ |
618 |
< |
printf("\033[1;%dH%5s", x, "CPU"); |
619 |
< |
printf("\033[2;%dH%5s", x, "used%"); |
618 |
> |
move(1,x); |
619 |
> |
printw("%5s", "CPU"); |
620 |
> |
move(2,x); |
621 |
> |
printw("%5s", "used%"); |
622 |
|
x+=6; |
623 |
|
} |
624 |
|
if(display_config->load_1){ |
625 |
< |
printf("\033[1;%dH%5s", x, "Load"); |
626 |
< |
printf("\033[2;%dH%5s", x, "(1m)"); |
625 |
> |
move(1,x); |
626 |
> |
printw("%5s", "Load"); |
627 |
> |
move(2,x); |
628 |
> |
printw("%5s", "(1m)"); |
629 |
|
x+=6; |
630 |
|
} |
631 |
|
if(display_config->pages_in){ |
632 |
< |
printf("\033[1;%dH%5s", x, "Page"); |
633 |
< |
printf("\033[2;%dH%5s", x, "ins"); |
632 |
> |
move(1,x); |
633 |
> |
printw("%5s", "Page"); |
634 |
> |
move(2,x); |
635 |
> |
printw("%5s", "ins"); |
636 |
|
x+=6; |
637 |
|
} |
638 |
|
if(display_config->pages_out){ |
639 |
< |
printf("\033[1;%dH%5s", x, "Page"); |
640 |
< |
printf("\033[2;%dH%5s", x, "outs"); |
639 |
> |
move(1,x); |
640 |
> |
printw("%5s", "Page"); |
641 |
> |
move(2,x); |
642 |
> |
printw("%5s", "outs"); |
643 |
|
x+=6; |
644 |
|
} |
645 |
|
if(display_config->memory_used_pecent){ |
646 |
< |
printf("\033[1;%dH%5s", x, "Mem"); |
647 |
< |
printf("\033[2;%dH%5s", x, "used"); |
646 |
> |
move(1,x); |
647 |
> |
printw("%5s", "Mem"); |
648 |
> |
move(2,x); |
649 |
> |
printw("%5s", "used"); |
650 |
|
x+=6; |
651 |
|
} |
652 |
|
if(display_config->swap_used_pecent){ |
653 |
< |
printf("\033[1;%dH%5s", x, "Swap"); |
654 |
< |
printf("\033[2;%dH%5s", x, "used"); |
653 |
> |
move(1,x); |
654 |
> |
printw("%5s", "Swap"); |
655 |
> |
move(2,x); |
656 |
> |
printw("%5s", "used"); |
657 |
|
x+=6; |
658 |
|
} |
659 |
|
if(display_config->network_io_total_rx){ |
660 |
< |
printf("\033[1;%dH%8s", x, "Net"); |
661 |
< |
printf("\033[2;%dH%8s", x, "rx"); |
660 |
> |
move(1,x); |
661 |
> |
printw("%8s", "Net"); |
662 |
> |
move(2,x); |
663 |
> |
printw("%8s", "rx"); |
664 |
|
x+=9; |
665 |
|
} |
666 |
|
if(display_config->network_io_total_tx){ |
667 |
< |
printf("\033[1;%dH%8s", x, "Net"); |
668 |
< |
printf("\033[2;%dH%8s", x, "tx"); |
667 |
> |
move(1,x); |
668 |
> |
printw("%8s", "Net"); |
669 |
> |
move(2,x); |
670 |
> |
printw("%8s", "tx"); |
671 |
|
x+=9; |
672 |
|
} |
673 |
|
if(display_config->disk_io_total_read){ |
674 |
< |
printf("\033[1;%dH%9s", x, "Disk"); |
675 |
< |
printf("\033[2;%dH%9s", x, "read"); |
674 |
> |
move(1,x); |
675 |
> |
printw("%9s", "Disk"); |
676 |
> |
move(2,x); |
677 |
> |
printw("%9s", "read"); |
678 |
|
x+=10; |
679 |
|
} |
680 |
|
if(display_config->disk_io_total_read){ |
681 |
< |
printf("\033[1;%dH%9s", x, "Disk"); |
682 |
< |
printf("\033[2;%dH%9s", x, "write"); |
681 |
> |
move(1,x); |
682 |
> |
printw("%9s", "Disk"); |
683 |
> |
move(2,x); |
684 |
> |
printw("%9s", "write"); |
685 |
|
x+=10; |
686 |
|
} |
687 |
|
|
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); |
696 |
|
|
697 |
< |
printf("\033[%d;%dH%-11s", line_num++, 1, machine_data_list->sysname); |
697 |
> |
if(display_config->cpu_used) printw(" %5.1f", machine_data_list->cpu_used); |
698 |
> |
if(display_config->load_1) printw(" %5.1f", machine_data_list->load_1); |
699 |
> |
if(display_config->pages_in) printw(" %5d", machine_data_list->pages_in); |
700 |
> |
if(display_config->pages_out) printw(" %5d", machine_data_list->pages_out); |
701 |
> |
if(display_config->memory_used_pecent) printw(" %5.1f", machine_data_list->memory_used_pecent); |
702 |
> |
if(display_config->swap_used_pecent) printw(" %5.1f", machine_data_list->swap_used_pecent); |
703 |
> |
if(display_config->network_io_total_rx) printw(" %8lld", machine_data_list->network_io_total_rx); |
704 |
> |
if(display_config->network_io_total_tx) printw(" %8lld", machine_data_list->network_io_total_tx); |
705 |
> |
if(display_config->disk_io_total_read) printw(" %9lld", machine_data_list->disk_io_total_read); |
706 |
> |
if(display_config->disk_io_total_write) printw(" %9lld", machine_data_list->disk_io_total_write); |
707 |
|
|
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 |
– |
|
708 |
|
machine_data_list=machine_data_list->next; |
709 |
|
} |
710 |
|
|
711 |
|
|
712 |
< |
fflush(stdout); |
712 |
> |
refresh(); |
713 |
|
|
714 |
|
} |
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; |
723 |
|
|
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; |
739 |
|
extern char *optarg; |
662 |
– |
sortby_ptr=NULL; |
740 |
|
|
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 */ |
749 |
|
display_config.cpu_user=0; |
750 |
|
display_config.cpu_idle=0; |
780 |
|
display_config.network_io_total_rx=1; |
781 |
|
display_config.network_all_stats=1; |
782 |
|
|
783 |
< |
display_config.disk_io_total_write=1; |
784 |
< |
display_config.disk_io_total_read=1; |
783 |
> |
display_config.disk_io_total_write=0; |
784 |
> |
display_config.disk_io_total_read=0; |
785 |
|
display_config.disk_io_all_stats=0; |
786 |
|
|
787 |
|
display_config.disk_total_used=0; |
788 |
|
display_config.disk_all_stats=0; |
789 |
|
|
708 |
– |
|
790 |
|
while((cmdopt=getopt(argc, argv, "d:s:")) != -1){ |
791 |
|
switch(cmdopt){ |
792 |
|
case 'd': |
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]); |
883 |
|
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
884 |
|
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
885 |
|
*/ |
886 |
< |
fflush(stdout); |
886 |
> |
|
887 |
> |
initscr(); |
888 |
> |
nonl(); |
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); |
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 |
> |
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 |
> |
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 |
+ |
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 |
|
} |