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> |
32 |
|
#include <ukcprog.h> |
33 |
|
#include <netinet/in.h> |
34 |
|
#include <netdb.h> |
35 |
|
#include <sys/termios.h> |
36 |
+ |
#include <signal.h> |
37 |
+ |
#include <errno.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); |
698 |
|
printw("%5s", "(1m)"); |
699 |
|
x+=6; |
700 |
|
} |
701 |
+ |
|
702 |
|
if(display_config->pages_in && (display_config->maxx > x+6)){ |
703 |
|
move(1,x); |
704 |
|
printw("%5s", "Page"); |
706 |
|
printw("%5s", "ins"); |
707 |
|
x+=6; |
708 |
|
} |
709 |
+ |
|
710 |
|
if(display_config->pages_out && (display_config->maxx > x+6)){ |
711 |
|
move(1,x); |
712 |
|
printw("%5s", "Page"); |
714 |
|
printw("%5s", "outs"); |
715 |
|
x+=6; |
716 |
|
} |
717 |
+ |
|
718 |
|
if(display_config->memory_used_pecent && (display_config->maxx > x+6)){ |
719 |
|
move(1,x); |
720 |
|
printw("%5s", "Mem"); |
722 |
|
printw("%5s", "used"); |
723 |
|
x+=6; |
724 |
|
} |
725 |
+ |
|
726 |
|
if(display_config->swap_used_pecent && (display_config->maxx > x+6)){ |
727 |
|
move(1,x); |
728 |
|
printw("%5s", "Swap"); |
730 |
|
printw("%5s", "used"); |
731 |
|
x+=6; |
732 |
|
} |
733 |
+ |
|
734 |
|
if(display_config->network_io_total_rx){ |
735 |
|
if(display_config->units=='b' && (display_config->maxx > x+9)){ |
736 |
|
move(1,x); |
739 |
|
printw("%8s", "rx"); |
740 |
|
x+=9; |
741 |
|
} |
742 |
+ |
|
743 |
|
if(display_config->units=='k' && (display_config->maxx > x+6)){ |
744 |
|
move(1,x); |
745 |
|
printw("%5s", "Net"); |
747 |
|
printw("%5s", "rx"); |
748 |
|
x+=6; |
749 |
|
} |
750 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
750 |
> |
|
751 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
752 |
|
move(1,x); |
753 |
< |
printw("%4s", "Net"); |
753 |
> |
printw("%5s", "Net"); |
754 |
|
move(2,x); |
755 |
< |
printw("%4s", "rx"); |
756 |
< |
x+=5; |
755 |
> |
printw("%5s", "rx"); |
756 |
> |
x+=6; |
757 |
|
} |
758 |
+ |
|
759 |
|
} |
760 |
+ |
|
761 |
|
if(display_config->network_io_total_tx){ |
762 |
|
if(display_config->units=='b' && (display_config->maxx > x+9)){ |
763 |
|
move(1,x); |
766 |
|
printw("%8s", "tx"); |
767 |
|
x+=9; |
768 |
|
} |
769 |
+ |
|
770 |
|
if(display_config->units=='k' && (display_config->maxx > x+6)){ |
771 |
|
move(1,x); |
772 |
|
printw("%5s", "Net"); |
774 |
|
printw("%5s", "tx"); |
775 |
|
x+=6; |
776 |
|
} |
777 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
777 |
> |
|
778 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
779 |
|
move(1,x); |
780 |
< |
printw("%4s", "Net"); |
780 |
> |
printw("%5s", "Net"); |
781 |
|
move(2,x); |
782 |
< |
printw("%4s", "tx"); |
783 |
< |
x+=5; |
782 |
> |
printw("%5s", "tx"); |
783 |
> |
x+=6; |
784 |
|
} |
785 |
+ |
|
786 |
|
} |
787 |
|
|
788 |
|
if(display_config->disk_io_total_read){ |
793 |
|
printw("%9s", "read"); |
794 |
|
x+=10; |
795 |
|
} |
796 |
+ |
|
797 |
|
if(display_config->units=='k' && (display_config->maxx > x+7)){ |
798 |
|
move(1,x); |
799 |
|
printw("%6s", "Disk"); |
801 |
|
printw("%6s", "read"); |
802 |
|
x+=7; |
803 |
|
} |
804 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
804 |
> |
|
805 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
806 |
|
move(1,x); |
807 |
< |
printw("%5s", "Disk"); |
807 |
> |
printw("%6s", "Disk"); |
808 |
|
move(2,x); |
809 |
< |
printw("%5s", "read"); |
810 |
< |
x+=6; |
809 |
> |
printw("%6s", "read"); |
810 |
> |
x+=7; |
811 |
|
} |
812 |
+ |
|
813 |
|
} |
814 |
|
|
815 |
|
if(display_config->disk_io_total_read){ |
820 |
|
printw("%9s", "write"); |
821 |
|
x+=10; |
822 |
|
} |
823 |
+ |
|
824 |
|
if(display_config->units=='k' && (display_config->maxx > x+7)){ |
825 |
|
move(1,x); |
826 |
|
printw("%6s", "Disk"); |
828 |
|
printw("%6s", "write"); |
829 |
|
x+=7; |
830 |
|
} |
831 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
831 |
> |
|
832 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
833 |
|
move(1,x); |
834 |
< |
printw("%5s", "Disk"); |
834 |
> |
printw("%6s", "Disk"); |
835 |
|
move(2,x); |
836 |
< |
printw("%5s", "write"); |
837 |
< |
x+=6; |
836 |
> |
printw("%6s", "write"); |
837 |
> |
x+=7; |
838 |
|
} |
839 |
+ |
|
840 |
|
} |
841 |
< |
|
841 |
> |
|
842 |
> |
if(display_config->processes && (display_config->maxx > x+25)){ |
843 |
> |
move(1,x); |
844 |
> |
printw("%-24s", " Number of Process"); |
845 |
> |
move(2,x); |
846 |
> |
printw("%-24s", " Run Slep Zomb Stop Tot"); |
847 |
> |
x+=25; |
848 |
> |
} |
849 |
> |
|
850 |
|
*title=0; |
851 |
|
} |
852 |
|
|
891 |
|
printw(" %5lld", machine_data_list->network_io_total_rx/1024); |
892 |
|
x+=6; |
893 |
|
} |
894 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
895 |
< |
printw(" %4.1f", (double)(machine_data_list->network_io_total_rx/(1024.00*1024.00))); |
896 |
< |
x+=5; |
894 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
895 |
> |
printw(" %5.2f", (double)(machine_data_list->network_io_total_rx/(1024.00*1024.00))); |
896 |
> |
x+=6; |
897 |
|
} |
898 |
|
} |
899 |
|
|
906 |
|
printw(" %5lld", machine_data_list->network_io_total_tx/1024); |
907 |
|
x+=6; |
908 |
|
} |
909 |
< |
if(display_config->units=='m' && (display_config->maxx > x+5)){ |
910 |
< |
printw(" %4.1f", (double)(machine_data_list->network_io_total_tx/(1024.00*1024.00))); |
911 |
< |
x+=5; |
909 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
910 |
> |
printw(" %5.2f", (double)(machine_data_list->network_io_total_tx/(1024.00*1024.00))); |
911 |
> |
x+=6; |
912 |
|
} |
913 |
|
} |
914 |
|
|
921 |
|
printw(" %6lld", machine_data_list->disk_io_total_read/1024); |
922 |
|
x+=7; |
923 |
|
} |
924 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
925 |
< |
printw(" %5.1f", (double)(machine_data_list->disk_io_total_read/(1024.00*1024.00))); |
926 |
< |
x+=6; |
924 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
925 |
> |
printw(" %6.2f", (double)(machine_data_list->disk_io_total_read/(1024.00*1024.00))); |
926 |
> |
x+=7; |
927 |
|
} |
928 |
|
} |
929 |
|
|
936 |
|
printw(" %6lld", machine_data_list->disk_io_total_write/1024); |
937 |
|
x+=7; |
938 |
|
} |
939 |
< |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
940 |
< |
printw(" %5.1f", (double)(machine_data_list->disk_io_total_write/(1024.00*1024.00))); |
941 |
< |
x+=6; |
939 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
940 |
> |
printw(" %6.2f", (double)(machine_data_list->disk_io_total_write/(1024.00*1024.00))); |
941 |
> |
x+=7; |
942 |
|
} |
943 |
|
} |
944 |
+ |
if(display_config->processes && display_config->maxx > x+25){ |
945 |
+ |
printw(" %4d %4d %4d %4d %4d", machine_data_list->processes_cpu, \ |
946 |
+ |
machine_data_list->processes_sleeping, \ |
947 |
+ |
machine_data_list->processes_zombie, \ |
948 |
+ |
machine_data_list->processes_stopped, \ |
949 |
+ |
machine_data_list->processes_total); |
950 |
+ |
x+=25; |
951 |
+ |
} |
952 |
|
|
953 |
|
machine_data_list=machine_data_list->next; |
954 |
|
} |
958 |
|
|
959 |
|
} |
960 |
|
|
961 |
+ |
void sig_winch_handler(int sig){ |
962 |
+ |
|
963 |
+ |
sig_winch=1; |
964 |
+ |
signal(SIGWINCH, sig_winch_handler); |
965 |
+ |
} |
966 |
+ |
|
967 |
+ |
void usage() { |
968 |
+ |
printf("Usage: idar [-o order] [-s server] [-p port] [-l list] [-h]\n\n"); |
969 |
+ |
printf(" -o Sets the initial sort order. Accepted arguments are one of:\n"); |
970 |
+ |
printf(" cpu load mem swap net disk\n"); |
971 |
+ |
printf(" -s Specifies the i-scream server to connect to.\n"); |
972 |
+ |
printf(" default: %s\n", DEF_SERVER_NAME); |
973 |
+ |
printf(" -p Specifies the i-scream server port.\n"); |
974 |
+ |
printf(" default: %d\n", DEF_SERVER_PORT); |
975 |
+ |
printf(" -l Sets the list of hosts to monitor in a semi-colon separated list.\n"); |
976 |
+ |
printf(" -h Displays this help information.\n"); |
977 |
+ |
printf("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
978 |
+ |
exit(1); |
979 |
+ |
} |
980 |
+ |
|
981 |
|
int main(int argc, char **argv){ |
982 |
|
WINDOW *window; |
983 |
|
fd_set infds; |
989 |
|
char *machine_list=NULL; |
990 |
|
char *response=NULL; |
991 |
|
|
992 |
< |
char *servername; |
992 |
> |
char *server_name; |
993 |
|
int server_control_port; |
994 |
|
int server_data_port; |
995 |
|
|
1001 |
|
int cmdopt; |
1002 |
|
extern int optind; |
1003 |
|
extern char *optarg; |
1004 |
+ |
|
1005 |
+ |
extern int errno; |
1006 |
|
|
1007 |
|
display_config_t display_config; |
1008 |
|
char ch; |
1038 |
|
display_config.pages_in=1; |
1039 |
|
display_config.pages_out=1; |
1040 |
|
|
1041 |
< |
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; |
1041 |
> |
display_config.processes=1; |
1042 |
|
|
1043 |
|
display_config.network_io_total_tx=1; |
1044 |
|
display_config.network_io_total_rx=1; |
1049 |
|
display_config.disk_io_all_stats=0; |
1050 |
|
|
1051 |
|
display_config.disk_total_used=0; |
1052 |
< |
display_config.disk_all_stats=0; |
1052 |
> |
display_config.disk_all_stats=0; |
1053 |
|
|
1054 |
< |
while((cmdopt=getopt(argc, argv, "s:")) != -1){ |
1054 |
> |
signal(SIGWINCH, sig_winch_handler); |
1055 |
> |
|
1056 |
> |
server_name=DEF_SERVER_NAME; |
1057 |
> |
server_control_port=DEF_SERVER_PORT; |
1058 |
> |
|
1059 |
> |
while((cmdopt=getopt(argc, argv, "o:s:p:l:h")) != -1){ |
1060 |
|
switch(cmdopt){ |
1061 |
< |
case 's': |
1061 |
> |
case 'o': |
1062 |
|
if(!strcmp(optarg, "cpu")){ |
1063 |
|
sortby_ptr=cmp_cpu_used; |
1064 |
|
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
1072 |
|
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
1073 |
|
} |
1074 |
|
if(!strcmp(optarg, "swap")){ |
1075 |
< |
sortby_ptr=cmp_swap_used_pecent; |
1076 |
< |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
1077 |
< |
} |
1075 |
> |
sortby_ptr=cmp_swap_used_pecent; |
1076 |
> |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
1077 |
> |
} |
1078 |
> |
if(!strcmp(optarg, "net")){ |
1079 |
> |
sortby_ptr=cmp_network_io_total; |
1080 |
> |
strlcpy(display_config.sortby, NETIO, SORTBYMAXNAME); |
1081 |
> |
} |
1082 |
> |
if(!strcmp(optarg, "disk")){ |
1083 |
> |
sortby_ptr=cmp_disk_io_total; |
1084 |
> |
strlcpy(display_config.sortby, DISKIO, SORTBYMAXNAME); |
1085 |
> |
} |
1086 |
|
if(sortby_ptr==NULL){ |
1087 |
< |
errf("Invalid sort type"); |
1088 |
< |
exit(1); |
1087 |
> |
errf("Invalid order given."); |
1088 |
> |
usage(); |
1089 |
|
} |
1090 |
< |
break; |
1090 |
> |
break; |
1091 |
> |
case 's': |
1092 |
> |
server_name=optarg; |
1093 |
> |
break; |
1094 |
> |
case 'p': |
1095 |
> |
server_control_port=atoi(optarg); |
1096 |
> |
break; |
1097 |
> |
case 'l': |
1098 |
> |
/* We've been passed a machine list */ |
1099 |
> |
/* list currently needs to be ; seperated */ |
1100 |
> |
machine_list=strdup(optarg); |
1101 |
> |
break; |
1102 |
> |
case 'h': |
1103 |
> |
default: |
1104 |
> |
usage(); |
1105 |
> |
break; |
1106 |
|
} |
1107 |
|
} |
1108 |
|
|
1109 |
+ |
/* Don't take any other arguments */ |
1110 |
+ |
if(argc>optind){ |
1111 |
+ |
usage(); |
1112 |
+ |
} |
1113 |
+ |
|
1114 |
|
if(sortby_ptr==NULL){ |
1115 |
|
sortby_ptr=cmp_cpu_used; |
1116 |
|
strlcpy(display_config.sortby, "CPU Used", SORTBYMAXNAME); |
1117 |
|
} |
1118 |
|
|
1119 |
< |
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); |
1119 |
> |
control=create_tcp_connection(server_name, server_control_port); |
1120 |
|
if(control==NULL){ |
1121 |
|
errf("Failed to connect (%m)"); |
1122 |
|
exit(1); |
1123 |
|
} |
1124 |
|
|
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 |
– |
|
1125 |
|
if((tcp_comm(control, NULL, &response, "PROTOCOL 1.1"))!=0){ |
1126 |
|
errf("Incorrect version number (%s)", response); |
1127 |
|
exit(1); |
1154 |
|
exit(1); |
1155 |
|
} |
1156 |
|
|
1157 |
< |
data=create_tcp_connection(servername, server_data_port); |
1157 |
> |
data=create_tcp_connection(server_name, server_data_port); |
1158 |
|
if(data==NULL){ |
1159 |
< |
errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); |
1159 |
> |
errf("Failed to connect to host %s on port %d (%m)",server_name, server_data_port); |
1160 |
|
exit(1); |
1161 |
|
} |
1162 |
|
|
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 |
– |
|
1163 |
|
initscr(); |
1164 |
|
nonl(); |
1165 |
|
cbreak(); |
1166 |
< |
echo(); |
1166 |
> |
noecho(); |
1167 |
|
window=newwin(0, 0, 0, 0); |
1168 |
|
getmaxyx(window, display_config.maxy, display_config.maxx); |
1169 |
|
|
1176 |
|
FD_SET(stdin_fileno, &infds); |
1177 |
|
FD_SET(data_fileno, &infds); |
1178 |
|
if((select(biggest_fileno, &infds, NULL, NULL, NULL))==-1){ |
1179 |
+ |
if(errno!=EINTR){ |
1180 |
+ |
errf("select failed with (%m)"); |
1181 |
+ |
exit(1); |
1182 |
+ |
} |
1183 |
+ |
} |
1184 |
+ |
|
1185 |
+ |
if(sig_winch){ |
1186 |
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) { |
1187 |
|
resizeterm(size.ws_row, size.ws_col); |
1188 |
|
wrefresh(curscr); |
1191 |
|
title=1; |
1192 |
|
display(machine_data_list, &display_config, &title); |
1193 |
|
refresh(); |
1194 |
+ |
sig_winch=0; |
1195 |
|
continue; |
1196 |
|
} |
1197 |
|
|
1198 |
|
if(FD_ISSET(stdin_fileno, &infds)){ |
1199 |
|
|
1200 |
< |
ch=getc(stdin); |
1200 |
> |
ch=getch(); |
1201 |
|
switch(ch){ |
1202 |
+ |
case KEY_RESIZE: |
1203 |
+ |
sig_winch=1; |
1204 |
+ |
break; |
1205 |
|
|
1206 |
|
/* Quit */ |
1207 |
|
case 'Q': |
1325 |
|
display_config.cpu_used=1; |
1326 |
|
} |
1327 |
|
break; |
1328 |
+ |
case 'r': |
1329 |
+ |
if(display_config.processes){ |
1330 |
+ |
display_config.processes=0; |
1331 |
+ |
}else{ |
1332 |
+ |
display_config.processes=1; |
1333 |
+ |
} |
1334 |
+ |
break; |
1335 |
|
|
1336 |
|
default: |
1337 |
< |
/* 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; |
1337 |
> |
continue; |
1338 |
|
} |
1339 |
|
|
1340 |
|
/* Increment title so it becomes true (and making the screen update */ |