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