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 |
+ |
|
691 |
|
FILE *control; |
692 |
|
FILE *data; |
693 |
|
|
702 |
|
|
703 |
|
int num_hosts; |
704 |
|
int max_display=0; |
705 |
+ |
int title; |
706 |
|
|
707 |
|
int cmdopt; |
708 |
|
extern int optind; |
709 |
|
extern char *optarg; |
710 |
|
|
711 |
|
display_config_t display_config; |
712 |
+ |
char ch; |
713 |
|
|
714 |
+ |
sortby_ptr=NULL; |
715 |
+ |
|
716 |
|
/* What to display defaults */ |
717 |
|
display_config.cpu_user=0; |
718 |
|
display_config.cpu_idle=0; |
744 |
|
display_config.processes_zombie=0; |
745 |
|
display_config.processes_stopped=0; |
746 |
|
|
747 |
< |
display_config.network_io_total_tx=0; |
748 |
< |
display_config.network_io_total_rx=0; |
747 |
> |
display_config.network_io_total_tx=1; |
748 |
> |
display_config.network_io_total_rx=1; |
749 |
|
display_config.network_all_stats=1; |
750 |
|
|
751 |
|
display_config.disk_io_total_write=0; |
752 |
|
display_config.disk_io_total_read=0; |
753 |
< |
display_config.disk_io_all_stats=1; |
753 |
> |
display_config.disk_io_all_stats=0; |
754 |
|
|
755 |
|
display_config.disk_total_used=0; |
756 |
|
display_config.disk_all_stats=0; |
757 |
|
|
758 |
< |
while((cmdopt=getopt(argc, argv, "d:")) != -1){ |
758 |
> |
while((cmdopt=getopt(argc, argv, "d:s:")) != -1){ |
759 |
|
switch(cmdopt){ |
760 |
|
case 'd': |
761 |
|
max_display=atoi(optarg); |
762 |
|
break; |
763 |
+ |
case 's': |
764 |
+ |
if(!strcmp(optarg, "cpu")){ |
765 |
+ |
sortby_ptr=cmp_cpu_used; |
766 |
+ |
} |
767 |
+ |
if(!strcmp(optarg, "load")){ |
768 |
+ |
sortby_ptr=cmp_load_1; |
769 |
+ |
} |
770 |
+ |
if(!strcmp(optarg, "mem")){ |
771 |
+ |
sortby_ptr=cmp_memory_used_pecent; |
772 |
+ |
} |
773 |
+ |
if(sortby_ptr==NULL){ |
774 |
+ |
errf("Invalid sort type"); |
775 |
+ |
exit(1); |
776 |
+ |
} |
777 |
+ |
break; |
778 |
|
} |
779 |
|
} |
780 |
|
|
781 |
+ |
if(sortby_ptr==NULL) sortby_ptr=cmp_cpu_used; |
782 |
|
|
783 |
|
if(argc<(optind+2)){ |
784 |
|
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
836 |
|
errf("Failed to connect to host %s on port %d (%m)",servername, server_data_port); |
837 |
|
} |
838 |
|
|
839 |
< |
|
839 |
> |
/* |
840 |
|
printf("\033[2J"); |
841 |
|
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
842 |
|
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
843 |
< |
fflush(stdout); |
843 |
> |
*/ |
844 |
> |
|
845 |
> |
initscr(); |
846 |
> |
nonl(); |
847 |
> |
cbreak(); |
848 |
> |
echo(); |
849 |
> |
window=newwin(0, 0, 0, 0); |
850 |
> |
|
851 |
|
for(;;){ |
852 |
|
response=fpgetline(data); |
853 |
|
if (response==NULL){ |
855 |
|
exit(1); |
856 |
|
} |
857 |
|
|
858 |
+ |
/* ch=getc(stdin); |
859 |
+ |
|
860 |
+ |
if(ch=='q'){ |
861 |
+ |
endwin(); |
862 |
+ |
exit(0); |
863 |
+ |
} |
864 |
+ |
*/ |
865 |
|
num_hosts=parse_xml(response, &machine_data_list); |
866 |
|
if(num_hosts==-1) continue; |
867 |
< |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, cmp_cpu); |
867 |
> |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
868 |
|
if(max_display==0){ |
869 |
< |
display(machine_data_list, num_hosts); |
869 |
> |
display(machine_data_list, &display_config, num_hosts, &title); |
870 |
|
}else{ |
871 |
< |
display(machine_data_list, max_display); |
871 |
> |
display(machine_data_list, &display_config, max_display, &title); |
872 |
|
} |
873 |
|
|
874 |
|
} |