1 |
|
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
3 |
> |
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2002 i-scream |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or |
26 |
|
#include <string.h> |
27 |
|
#include <sys/types.h> |
28 |
|
#include <sys/socket.h> |
29 |
+ |
#include <sys/ioctl.h> |
30 |
|
#include <unistd.h> |
31 |
|
#include <stdlib.h> |
31 |
– |
#include <ukcprog.h> |
32 |
|
#include <netinet/in.h> |
33 |
|
#include <netdb.h> |
34 |
|
#include <sys/termios.h> |
35 |
+ |
#include <signal.h> |
36 |
+ |
#include <errno.h> |
37 |
+ |
#include <ukcprog.h> |
38 |
|
|
39 |
|
#include <libxml/xmlmemory.h> |
40 |
|
#include <libxml/parser.h> |
177 |
|
int pages_in; |
178 |
|
int pages_out; |
179 |
|
|
180 |
< |
int processes_total; |
178 |
< |
int processes_sleeping; |
179 |
< |
int processes_cpu; |
180 |
< |
int processes_zombie; |
181 |
< |
int processes_stopped; |
180 |
> |
int processes; |
181 |
|
|
182 |
|
int network_io_total_tx; |
183 |
|
int network_io_total_rx; |
222 |
|
#define DISKIOW "DiskIO writes" |
223 |
|
#define DISKIO "Total DiskIO (reads+writes)" |
224 |
|
|
225 |
+ |
int sig_winch=0; |
226 |
|
|
227 |
– |
/* |
228 |
– |
int cmp_cpu(machine_data_list_t *a, machine_data_list_t *b){ |
229 |
– |
|
230 |
– |
if(a->cpu_used == b->cpu_used){ |
231 |
– |
if(a->load_1 == b->load_1) return 0; |
232 |
– |
if(a->load_1 > b->load_1){ |
233 |
– |
return -1; |
234 |
– |
}else{ |
235 |
– |
return 1; |
236 |
– |
} |
237 |
– |
} |
238 |
– |
|
239 |
– |
if((a->cpu_used) > (b->cpu_used)){ |
240 |
– |
return -1; |
241 |
– |
}else{ |
242 |
– |
return 1; |
243 |
– |
} |
244 |
– |
} |
245 |
– |
*/ |
246 |
– |
|
227 |
|
#ifndef HAVE_ATOLL |
228 |
|
long long int atoll (const char *nptr){ |
229 |
|
return strtoll (nptr, (char **) NULL, 10); |
414 |
|
machine_data_list->diskio_data_list=NULL; |
415 |
|
*md=machine_data_list; |
416 |
|
num_hosts++; |
417 |
+ |
} else { |
418 |
+ |
xmlFree(hostname); |
419 |
|
} |
420 |
|
|
421 |
|
/* Now we want to pull out the data */ |
700 |
|
printw("%5s", "(1m)"); |
701 |
|
x+=6; |
702 |
|
} |
703 |
+ |
|
704 |
|
if(display_config->pages_in && (display_config->maxx > x+6)){ |
705 |
|
move(1,x); |
706 |
|
printw("%5s", "Page"); |
708 |
|
printw("%5s", "ins"); |
709 |
|
x+=6; |
710 |
|
} |
711 |
+ |
|
712 |
|
if(display_config->pages_out && (display_config->maxx > x+6)){ |
713 |
|
move(1,x); |
714 |
|
printw("%5s", "Page"); |
716 |
|
printw("%5s", "outs"); |
717 |
|
x+=6; |
718 |
|
} |
719 |
+ |
|
720 |
|
if(display_config->memory_used_pecent && (display_config->maxx > x+6)){ |
721 |
|
move(1,x); |
722 |
|
printw("%5s", "Mem"); |
724 |
|
printw("%5s", "used"); |
725 |
|
x+=6; |
726 |
|
} |
727 |
+ |
|
728 |
|
if(display_config->swap_used_pecent && (display_config->maxx > x+6)){ |
729 |
|
move(1,x); |
730 |
|
printw("%5s", "Swap"); |
732 |
|
printw("%5s", "used"); |
733 |
|
x+=6; |
734 |
|
} |
735 |
+ |
|
736 |
|
if(display_config->network_io_total_rx){ |
737 |
|
if(display_config->units=='b' && (display_config->maxx > x+9)){ |
738 |
|
move(1,x); |
741 |
|
printw("%8s", "rx"); |
742 |
|
x+=9; |
743 |
|
} |
744 |
+ |
|
745 |
|
if(display_config->units=='k' && (display_config->maxx > x+6)){ |
746 |
|
move(1,x); |
747 |
|
printw("%5s", "Net"); |
749 |
|
printw("%5s", "rx"); |
750 |
|
x+=6; |
751 |
|
} |
752 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
752 |
> |
|
753 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
754 |
|
move(1,x); |
755 |
< |
printw("%4s", "Net"); |
755 |
> |
printw("%5s", "Net"); |
756 |
|
move(2,x); |
757 |
< |
printw("%4s", "rx"); |
758 |
< |
x+=5; |
757 |
> |
printw("%5s", "rx"); |
758 |
> |
x+=6; |
759 |
|
} |
760 |
+ |
|
761 |
|
} |
762 |
+ |
|
763 |
|
if(display_config->network_io_total_tx){ |
764 |
|
if(display_config->units=='b' && (display_config->maxx > x+9)){ |
765 |
|
move(1,x); |
768 |
|
printw("%8s", "tx"); |
769 |
|
x+=9; |
770 |
|
} |
771 |
+ |
|
772 |
|
if(display_config->units=='k' && (display_config->maxx > x+6)){ |
773 |
|
move(1,x); |
774 |
|
printw("%5s", "Net"); |
776 |
|
printw("%5s", "tx"); |
777 |
|
x+=6; |
778 |
|
} |
779 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
779 |
> |
|
780 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
781 |
|
move(1,x); |
782 |
< |
printw("%4s", "Net"); |
782 |
> |
printw("%5s", "Net"); |
783 |
|
move(2,x); |
784 |
< |
printw("%4s", "tx"); |
785 |
< |
x+=5; |
784 |
> |
printw("%5s", "tx"); |
785 |
> |
x+=6; |
786 |
|
} |
787 |
+ |
|
788 |
|
} |
789 |
|
|
790 |
|
if(display_config->disk_io_total_read){ |
795 |
|
printw("%9s", "read"); |
796 |
|
x+=10; |
797 |
|
} |
798 |
+ |
|
799 |
|
if(display_config->units=='k' && (display_config->maxx > x+7)){ |
800 |
|
move(1,x); |
801 |
|
printw("%6s", "Disk"); |
803 |
|
printw("%6s", "read"); |
804 |
|
x+=7; |
805 |
|
} |
806 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
806 |
> |
|
807 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
808 |
|
move(1,x); |
809 |
< |
printw("%5s", "Disk"); |
809 |
> |
printw("%6s", "Disk"); |
810 |
|
move(2,x); |
811 |
< |
printw("%5s", "read"); |
812 |
< |
x+=6; |
811 |
> |
printw("%6s", "read"); |
812 |
> |
x+=7; |
813 |
|
} |
814 |
+ |
|
815 |
|
} |
816 |
|
|
817 |
|
if(display_config->disk_io_total_read){ |
822 |
|
printw("%9s", "write"); |
823 |
|
x+=10; |
824 |
|
} |
825 |
+ |
|
826 |
|
if(display_config->units=='k' && (display_config->maxx > x+7)){ |
827 |
|
move(1,x); |
828 |
|
printw("%6s", "Disk"); |
830 |
|
printw("%6s", "write"); |
831 |
|
x+=7; |
832 |
|
} |
833 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
833 |
> |
|
834 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
835 |
|
move(1,x); |
836 |
< |
printw("%5s", "Disk"); |
836 |
> |
printw("%6s", "Disk"); |
837 |
|
move(2,x); |
838 |
< |
printw("%5s", "write"); |
839 |
< |
x+=6; |
838 |
> |
printw("%6s", "write"); |
839 |
> |
x+=7; |
840 |
|
} |
841 |
+ |
|
842 |
|
} |
843 |
< |
|
843 |
> |
|
844 |
> |
if(display_config->processes && (display_config->maxx > x+25)){ |
845 |
> |
move(1,x); |
846 |
> |
printw("%-24s", " Number of Process"); |
847 |
> |
move(2,x); |
848 |
> |
printw("%-24s", " Run Slep Zomb Stop Tot"); |
849 |
> |
x+=25; |
850 |
> |
} |
851 |
> |
|
852 |
|
*title=0; |
853 |
|
} |
854 |
|
|
893 |
|
printw(" %5lld", machine_data_list->network_io_total_rx/1024); |
894 |
|
x+=6; |
895 |
|
} |
896 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
897 |
< |
printw(" %4.1f", (double)(machine_data_list->network_io_total_rx/(1024.00*1024.00))); |
898 |
< |
x+=5; |
896 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
897 |
> |
printw(" %5.2f", (double)(machine_data_list->network_io_total_rx/(1024.00*1024.00))); |
898 |
> |
x+=6; |
899 |
|
} |
900 |
|
} |
901 |
|
|
908 |
|
printw(" %5lld", machine_data_list->network_io_total_tx/1024); |
909 |
|
x+=6; |
910 |
|
} |
911 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
912 |
< |
printw(" %4.1f", (double)(machine_data_list->network_io_total_tx/(1024.00*1024.00))); |
913 |
< |
x+=5; |
911 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
912 |
> |
printw(" %5.2f", (double)(machine_data_list->network_io_total_tx/(1024.00*1024.00))); |
913 |
> |
x+=6; |
914 |
|
} |
915 |
|
} |
916 |
|
|
923 |
|
printw(" %6lld", machine_data_list->disk_io_total_read/1024); |
924 |
|
x+=7; |
925 |
|
} |
926 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
927 |
< |
printw(" %5.1f", (double)(machine_data_list->disk_io_total_read/(1024.00*1024.00))); |
928 |
< |
x+=6; |
926 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
927 |
> |
printw(" %6.2f", (double)(machine_data_list->disk_io_total_read/(1024.00*1024.00))); |
928 |
> |
x+=7; |
929 |
|
} |
930 |
|
} |
931 |
|
|
938 |
|
printw(" %6lld", machine_data_list->disk_io_total_write/1024); |
939 |
|
x+=7; |
940 |
|
} |
941 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
942 |
< |
printw(" %5.1f", (double)(machine_data_list->disk_io_total_write/(1024.00*1024.00))); |
943 |
< |
x+=6; |
941 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
942 |
> |
printw(" %6.2f", (double)(machine_data_list->disk_io_total_write/(1024.00*1024.00))); |
943 |
> |
x+=7; |
944 |
|
} |
945 |
|
} |
946 |
+ |
if(display_config->processes && display_config->maxx > x+25){ |
947 |
+ |
printw(" %4d %4d %4d %4d %4d", machine_data_list->processes_cpu, \ |
948 |
+ |
machine_data_list->processes_sleeping, \ |
949 |
+ |
machine_data_list->processes_zombie, \ |
950 |
+ |
machine_data_list->processes_stopped, \ |
951 |
+ |
machine_data_list->processes_total); |
952 |
+ |
x+=25; |
953 |
+ |
} |
954 |
|
|
955 |
|
machine_data_list=machine_data_list->next; |
956 |
|
} |
960 |
|
|
961 |
|
} |
962 |
|
|
963 |
+ |
void sig_winch_handler(int sig){ |
964 |
+ |
|
965 |
+ |
sig_winch=1; |
966 |
+ |
signal(SIGWINCH, sig_winch_handler); |
967 |
+ |
} |
968 |
+ |
|
969 |
+ |
void usage() { |
970 |
+ |
printf("Usage: idar [-o order] [-s server] [-p port] [-l list] [-h]\n\n"); |
971 |
+ |
printf(" -o Sets the initial sort order. Accepted arguments are one of:\n"); |
972 |
+ |
printf(" cpu load mem swap net disk\n"); |
973 |
+ |
printf(" -s Specifies the i-scream server to connect to.\n"); |
974 |
+ |
printf(" default: %s\n", DEF_SERVER_NAME); |
975 |
+ |
printf(" -p Specifies the i-scream server port.\n"); |
976 |
+ |
printf(" default: %d\n", DEF_SERVER_PORT); |
977 |
+ |
printf(" -l Sets the list of hosts to monitor in a semi-colon separated list.\n"); |
978 |
+ |
printf(" -h Displays this help information.\n"); |
979 |
+ |
printf("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
980 |
+ |
exit(1); |
981 |
+ |
} |
982 |
+ |
|
983 |
|
int main(int argc, char **argv){ |
984 |
|
WINDOW *window; |
985 |
|
fd_set infds; |
991 |
|
char *machine_list=NULL; |
992 |
|
char *response=NULL; |
993 |
|
|
994 |
< |
char *servername; |
994 |
> |
char *server_name; |
995 |
|
int server_control_port; |
996 |
|
int server_data_port; |
997 |
|
|
1003 |
|
int cmdopt; |
1004 |
|
extern int optind; |
1005 |
|
extern char *optarg; |
1006 |
+ |
|
1007 |
+ |
extern int errno; |
1008 |
|
|
1009 |
|
display_config_t display_config; |
1010 |
< |
char ch; |
1010 |
> |
int ch; |
1011 |
|
|
1012 |
|
int data_fileno, stdin_fileno, biggest_fileno; |
1013 |
|
|
1040 |
|
display_config.pages_in=1; |
1041 |
|
display_config.pages_out=1; |
1042 |
|
|
1043 |
< |
display_config.processes_total=0; |
1006 |
< |
display_config.processes_sleeping=0; |
1007 |
< |
display_config.processes_cpu=0; |
1008 |
< |
display_config.processes_zombie=0; |
1009 |
< |
display_config.processes_stopped=0; |
1043 |
> |
display_config.processes=1; |
1044 |
|
|
1045 |
|
display_config.network_io_total_tx=1; |
1046 |
|
display_config.network_io_total_rx=1; |
1051 |
|
display_config.disk_io_all_stats=0; |
1052 |
|
|
1053 |
|
display_config.disk_total_used=0; |
1054 |
< |
display_config.disk_all_stats=0; |
1054 |
> |
display_config.disk_all_stats=0; |
1055 |
|
|
1056 |
< |
while((cmdopt=getopt(argc, argv, "s:")) != -1){ |
1056 |
> |
signal(SIGWINCH, sig_winch_handler); |
1057 |
> |
|
1058 |
> |
server_name=DEF_SERVER_NAME; |
1059 |
> |
server_control_port=DEF_SERVER_PORT; |
1060 |
> |
|
1061 |
> |
while((cmdopt=getopt(argc, argv, "o:s:p:l:h")) != -1){ |
1062 |
|
switch(cmdopt){ |
1063 |
< |
case 's': |
1063 |
> |
case 'o': |
1064 |
|
if(!strcmp(optarg, "cpu")){ |
1065 |
|
sortby_ptr=cmp_cpu_used; |
1066 |
|
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
1074 |
|
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
1075 |
|
} |
1076 |
|
if(!strcmp(optarg, "swap")){ |
1077 |
< |
sortby_ptr=cmp_swap_used_pecent; |
1078 |
< |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
1079 |
< |
} |
1077 |
> |
sortby_ptr=cmp_swap_used_pecent; |
1078 |
> |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
1079 |
> |
} |
1080 |
> |
if(!strcmp(optarg, "net")){ |
1081 |
> |
sortby_ptr=cmp_network_io_total; |
1082 |
> |
strlcpy(display_config.sortby, NETIO, SORTBYMAXNAME); |
1083 |
> |
} |
1084 |
> |
if(!strcmp(optarg, "disk")){ |
1085 |
> |
sortby_ptr=cmp_disk_io_total; |
1086 |
> |
strlcpy(display_config.sortby, DISKIO, SORTBYMAXNAME); |
1087 |
> |
} |
1088 |
|
if(sortby_ptr==NULL){ |
1089 |
< |
errf("Invalid sort type"); |
1090 |
< |
exit(1); |
1089 |
> |
errf("Invalid order given."); |
1090 |
> |
usage(); |
1091 |
|
} |
1092 |
< |
break; |
1092 |
> |
break; |
1093 |
> |
case 's': |
1094 |
> |
server_name=optarg; |
1095 |
> |
break; |
1096 |
> |
case 'p': |
1097 |
> |
server_control_port=atoi(optarg); |
1098 |
> |
break; |
1099 |
> |
case 'l': |
1100 |
> |
/* We've been passed a machine list */ |
1101 |
> |
/* list currently needs to be ; seperated */ |
1102 |
> |
machine_list=strdup(optarg); |
1103 |
> |
break; |
1104 |
> |
case 'h': |
1105 |
> |
default: |
1106 |
> |
usage(); |
1107 |
> |
break; |
1108 |
|
} |
1109 |
|
} |
1110 |
|
|
1111 |
+ |
/* Don't take any other arguments */ |
1112 |
+ |
if(argc>optind){ |
1113 |
+ |
usage(); |
1114 |
+ |
} |
1115 |
+ |
|
1116 |
|
if(sortby_ptr==NULL){ |
1117 |
|
sortby_ptr=cmp_cpu_used; |
1118 |
|
strlcpy(display_config.sortby, "CPU Used", SORTBYMAXNAME); |
1119 |
|
} |
1120 |
|
|
1121 |
< |
if(argc<(optind+2)){ |
1055 |
< |
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
1056 |
< |
exit(1); |
1057 |
< |
} |
1058 |
< |
|
1059 |
< |
servername=argv[optind]; |
1060 |
< |
server_control_port=atoi(argv[optind+1]); |
1061 |
< |
|
1062 |
< |
control=create_tcp_connection(servername, server_control_port); |
1121 |
> |
control=create_tcp_connection(server_name, server_control_port); |
1122 |
|
if(control==NULL){ |
1123 |
|
errf("Failed to connect (%m)"); |
1124 |
|
exit(1); |
1125 |
|
} |
1126 |
|
|
1068 |
– |
if(argc==4){ |
1069 |
– |
/* We've been passed a machine list */ |
1070 |
– |
/* list currently needs to be ; seperated */ |
1071 |
– |
machine_list=strdup(argv[3]); |
1072 |
– |
} |
1073 |
– |
|
1127 |
|
if((tcp_comm(control, NULL, &response, "PROTOCOL 1.1"))!=0){ |
1128 |
|
errf("Incorrect version number (%s)", response); |
1129 |
|
exit(1); |
1156 |
|
exit(1); |
1157 |
|
} |
1158 |
|
|
1159 |
< |
data=create_tcp_connection(servername, server_data_port); |
1159 |
> |
data=create_tcp_connection(server_name, server_data_port); |
1160 |
|
if(data==NULL){ |
1161 |
< |
errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); |
1161 |
> |
errf("Failed to connect to host %s on port %d (%m)",server_name, server_data_port); |
1162 |
|
exit(1); |
1163 |
|
} |
1164 |
|
|
1112 |
– |
/* |
1113 |
– |
printf("\033[2J"); |
1114 |
– |
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
1115 |
– |
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
1116 |
– |
*/ |
1117 |
– |
|
1165 |
|
initscr(); |
1166 |
|
nonl(); |
1167 |
|
cbreak(); |
1168 |
< |
echo(); |
1168 |
> |
noecho(); |
1169 |
|
window=newwin(0, 0, 0, 0); |
1170 |
|
getmaxyx(window, display_config.maxy, display_config.maxx); |
1171 |
|
|
1178 |
|
FD_SET(stdin_fileno, &infds); |
1179 |
|
FD_SET(data_fileno, &infds); |
1180 |
|
if((select(biggest_fileno, &infds, NULL, NULL, NULL))==-1){ |
1181 |
+ |
if(errno!=EINTR){ |
1182 |
+ |
errf("select failed with (%m)"); |
1183 |
+ |
exit(1); |
1184 |
+ |
} |
1185 |
+ |
} |
1186 |
+ |
|
1187 |
+ |
if(sig_winch){ |
1188 |
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) { |
1189 |
|
resizeterm(size.ws_row, size.ws_col); |
1190 |
|
wrefresh(curscr); |
1193 |
|
title=1; |
1194 |
|
display(machine_data_list, &display_config, &title); |
1195 |
|
refresh(); |
1196 |
+ |
sig_winch=0; |
1197 |
|
continue; |
1198 |
|
} |
1199 |
|
|
1200 |
|
if(FD_ISSET(stdin_fileno, &infds)){ |
1201 |
|
|
1202 |
< |
ch=getc(stdin); |
1202 |
> |
ch=getch(); |
1203 |
|
switch(ch){ |
1204 |
+ |
case KEY_RESIZE: |
1205 |
+ |
sig_winch=1; |
1206 |
+ |
break; |
1207 |
|
|
1208 |
|
/* Quit */ |
1209 |
|
case 'Q': |
1327 |
|
display_config.cpu_used=1; |
1328 |
|
} |
1329 |
|
break; |
1330 |
+ |
case 'r': |
1331 |
+ |
if(display_config.processes){ |
1332 |
+ |
display_config.processes=0; |
1333 |
+ |
}else{ |
1334 |
+ |
display_config.processes=1; |
1335 |
+ |
} |
1336 |
+ |
break; |
1337 |
|
|
1338 |
|
default: |
1339 |
< |
/* Invalid key.. Ignore.. Set Title to -1, as the |
1275 |
< |
* title++ will then make that "0" (false) so a |
1276 |
< |
* screen redraw will not happen */ |
1277 |
< |
title=-1; |
1278 |
< |
break; |
1339 |
> |
continue; |
1340 |
|
} |
1341 |
|
|
1342 |
|
/* Increment title so it becomes true (and making the screen update */ |