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> |
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> |
41 |
|
#include "genmergesort.h" |
42 |
|
|
43 |
+ |
#ifdef HAVE_NCURSES_H |
44 |
+ |
#include <ncurses.h> |
45 |
+ |
#else |
46 |
+ |
#include <curses.h> |
47 |
+ |
#endif |
48 |
+ |
|
49 |
|
struct host_line_t{ |
50 |
|
char *hostname; |
51 |
|
int line; |
128 |
|
network_data_list_t *network_data_list; |
129 |
|
long long network_io_total_tx; |
130 |
|
long long network_io_total_rx; |
131 |
+ |
long long network_io_total; |
132 |
|
|
133 |
|
diskio_data_list_t *diskio_data_list; |
134 |
|
long long disk_io_total_write; |
135 |
|
long long disk_io_total_read; |
136 |
+ |
long long disk_io_total; |
137 |
|
|
138 |
|
/* Maybe in the future */ |
139 |
|
/* |
146 |
|
|
147 |
|
typedef struct machine_data_t machine_data_list_t; |
148 |
|
|
149 |
+ |
#define SORTBYMAXNAME 128 |
150 |
|
typedef struct{ |
151 |
+ |
int maxx; |
152 |
+ |
int maxy; |
153 |
+ |
|
154 |
+ |
char units; |
155 |
+ |
|
156 |
|
int cpu_user; |
157 |
|
int cpu_idle; |
158 |
|
int cpu_iowait; |
177 |
|
int pages_in; |
178 |
|
int pages_out; |
179 |
|
|
180 |
< |
int processes_total; |
137 |
< |
int processes_sleeping; |
138 |
< |
int processes_cpu; |
139 |
< |
int processes_zombie; |
140 |
< |
int processes_stopped; |
180 |
> |
int processes; |
181 |
|
|
182 |
|
int network_io_total_tx; |
183 |
|
int network_io_total_rx; |
189 |
|
|
190 |
|
int disk_total_used; |
191 |
|
int disk_all_stats; |
192 |
+ |
|
193 |
+ |
char sortby[SORTBYMAXNAME]; |
194 |
|
}display_config_t; |
195 |
|
|
196 |
|
GENERIC_MERGE_SORT(static, sort_machine_stats, machine_data_list_t, next) |
197 |
|
|
198 |
< |
int cmp_cpu(machine_data_list_t *a, machine_data_list_t *b){ |
198 |
> |
#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));} |
199 |
|
|
158 |
– |
if(a->cpu_used == b->cpu_used){ |
159 |
– |
if(a->load_1 == b->load_1) return 0; |
160 |
– |
if(a->load_1 > b->load_1){ |
161 |
– |
return -1; |
162 |
– |
}else{ |
163 |
– |
return 1; |
164 |
– |
} |
165 |
– |
} |
200 |
|
|
201 |
< |
if((a->cpu_used) > (b->cpu_used)){ |
202 |
< |
return -1; |
203 |
< |
}else{ |
204 |
< |
return 1; |
205 |
< |
} |
201 |
> |
int (*sortby_ptr)(machine_data_list_t *a, machine_data_list_t *b); |
202 |
> |
|
203 |
> |
MKCMP(cpu_used) |
204 |
> |
MKCMP(load_1) |
205 |
> |
MKCMP(network_io_total) |
206 |
> |
MKCMP(network_io_total_tx) |
207 |
> |
MKCMP(network_io_total_rx) |
208 |
> |
MKCMP(disk_io_total) |
209 |
> |
MKCMP(disk_io_total_write) |
210 |
> |
MKCMP(disk_io_total_read) |
211 |
> |
MKCMP(memory_used_pecent) |
212 |
> |
MKCMP(swap_used_pecent) |
213 |
> |
|
214 |
> |
#define CPU_USED "CPU used" |
215 |
> |
#define LOAD "Load (1)" |
216 |
> |
#define NETIORX "total Network RX for all interfaces" |
217 |
> |
#define NETIOTX "total Network TX for all interfaces" |
218 |
> |
#define NETIO "total Network IO for all interfaces (rx+tx)" |
219 |
> |
#define MEM "Memory usage" |
220 |
> |
#define SWAP "Swap usage" |
221 |
> |
#define DISKIOR "DiskIO reads" |
222 |
> |
#define DISKIOW "DiskIO writes" |
223 |
> |
#define DISKIO "Total DiskIO (reads+writes)" |
224 |
> |
|
225 |
> |
int sig_winch=0; |
226 |
> |
|
227 |
> |
#ifndef HAVE_ATOLL |
228 |
> |
long long int atoll (const char *nptr){ |
229 |
> |
return strtoll (nptr, (char **) NULL, 10); |
230 |
|
} |
231 |
+ |
#endif |
232 |
+ |
|
233 |
+ |
#ifndef HAVE_STRLCPY |
234 |
+ |
/* |
235 |
+ |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
236 |
+ |
* All rights reserved. |
237 |
+ |
* |
238 |
+ |
* Redistribution and use in source and binary forms, with or without |
239 |
+ |
* modification, are permitted provided that the following conditions |
240 |
+ |
* are met: |
241 |
+ |
* 1. Redistributions of source code must retain the above copyright |
242 |
+ |
* notice, this list of conditions and the following disclaimer. |
243 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
244 |
+ |
* notice, this list of conditions and the following disclaimer in the |
245 |
+ |
* documentation and/or other materials provided with the distribution. |
246 |
+ |
* 3. The name of the author may not be used to endorse or promote products |
247 |
+ |
* derived from this software without specific prior written permission. |
248 |
+ |
* |
249 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
250 |
+ |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
251 |
+ |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
252 |
+ |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
253 |
+ |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
254 |
+ |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
255 |
+ |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
256 |
+ |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
257 |
+ |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
258 |
+ |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
259 |
+ |
*/ |
260 |
+ |
|
261 |
+ |
/* |
262 |
+ |
* Copy src to string dst of size siz. At most siz-1 characters |
263 |
+ |
* will be copied. Always NUL terminates (unless siz == 0). |
264 |
+ |
* Returns strlen(src); if retval >= siz, truncation occurred. |
265 |
+ |
*/ |
266 |
+ |
size_t |
267 |
+ |
strlcpy(dst, src, siz) |
268 |
+ |
char *dst; |
269 |
+ |
const char *src; |
270 |
+ |
size_t siz; |
271 |
+ |
{ |
272 |
+ |
register char *d = dst; |
273 |
+ |
register const char *s = src; |
274 |
+ |
register size_t n = siz; |
275 |
+ |
|
276 |
+ |
/* Copy as many bytes as will fit */ |
277 |
+ |
if (n != 0 && --n != 0) { |
278 |
+ |
do { |
279 |
+ |
if ((*d++ = *s++) == 0) |
280 |
+ |
break; |
281 |
+ |
} while (--n != 0); |
282 |
+ |
} |
283 |
+ |
|
284 |
+ |
/* Not enough room in dst, add NUL and traverse rest of src */ |
285 |
+ |
if (n == 0) { |
286 |
+ |
if (siz != 0) |
287 |
+ |
*d = '\0'; /* NUL-terminate dst */ |
288 |
+ |
while (*s++) |
289 |
+ |
; |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
return(s - src - 1); /* count does not include NUL */ |
293 |
+ |
} |
294 |
+ |
|
295 |
+ |
#endif |
296 |
+ |
|
297 |
|
|
298 |
|
FILE *create_tcp_connection(char *hostname, int port){ |
299 |
|
int sock; |
642 |
|
machine_data_list->network_io_total_rx+=ndl_ptr->rx; |
643 |
|
ndl_ptr=ndl_ptr->next; |
644 |
|
} |
645 |
+ |
machine_data_list->network_io_total=machine_data_list->network_io_total_rx+machine_data_list->network_io_total_tx; |
646 |
|
|
647 |
|
didl_ptr=machine_data_list->diskio_data_list; |
648 |
|
machine_data_list->disk_io_total_read=0; |
652 |
|
machine_data_list->disk_io_total_write+=didl_ptr->write; |
653 |
|
didl_ptr=didl_ptr->next; |
654 |
|
} |
655 |
+ |
machine_data_list->disk_io_total=machine_data_list->disk_io_total_read+machine_data_list->disk_io_total_write; |
656 |
|
|
657 |
|
xmlFreeDoc(doc); |
658 |
|
|
661 |
|
|
662 |
|
} |
663 |
|
|
664 |
< |
void display(machine_data_list_t *machine_data_list, int num_lines){ |
664 |
> |
void display(machine_data_list_t *machine_data_list, display_config_t *display_config, int *title){ |
665 |
|
int line_num=4; |
666 |
+ |
int counter; |
667 |
+ |
int x=1; |
668 |
|
|
669 |
< |
for(;num_lines;num_lines--){ |
669 |
> |
if(*title){ |
670 |
> |
clear(); |
671 |
> |
move (display_config->maxy-3, 1); |
672 |
> |
printw("Sorting by %-64s", display_config->sortby); |
673 |
> |
move (display_config->maxy-2, 1); |
674 |
> |
if(display_config->units == 'b'){ |
675 |
> |
printw("Units are measured in bytes/sec"); |
676 |
> |
} |
677 |
> |
if(display_config->units == 'k'){ |
678 |
> |
printw("Units are measured in kilobytes/sec"); |
679 |
> |
} |
680 |
> |
if(display_config->units == 'm'){ |
681 |
> |
printw("Units are measured in megabytes/sec"); |
682 |
> |
} |
683 |
> |
|
684 |
> |
move(1,1); |
685 |
> |
printw("%-11s", "Hostname"); |
686 |
> |
x=x+11+1; |
687 |
> |
if(display_config->cpu_used && (display_config->maxx > x+6)){ |
688 |
> |
move(1,x); |
689 |
> |
printw("%5s", "CPU"); |
690 |
> |
move(2,x); |
691 |
> |
printw("%5s", "used%"); |
692 |
> |
x+=6; |
693 |
> |
} |
694 |
> |
if(display_config->load_1 && (display_config->maxx > x+6)){ |
695 |
> |
move(1,x); |
696 |
> |
printw("%5s", "Load"); |
697 |
> |
move(2,x); |
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"); |
705 |
> |
move(2,x); |
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"); |
713 |
> |
move(2,x); |
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"); |
721 |
> |
move(2,x); |
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"); |
729 |
> |
move(2,x); |
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); |
737 |
> |
printw("%8s", "Net"); |
738 |
> |
move(2,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"); |
746 |
> |
move(2,x); |
747 |
> |
printw("%5s", "rx"); |
748 |
> |
x+=6; |
749 |
> |
} |
750 |
> |
|
751 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
752 |
> |
move(1,x); |
753 |
> |
printw("%5s", "Net"); |
754 |
> |
move(2,x); |
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); |
764 |
> |
printw("%8s", "Net"); |
765 |
> |
move(2,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"); |
773 |
> |
move(2,x); |
774 |
> |
printw("%5s", "tx"); |
775 |
> |
x+=6; |
776 |
> |
} |
777 |
> |
|
778 |
> |
if(display_config->units=='m' && (display_config->maxx > x+6)){ |
779 |
> |
move(1,x); |
780 |
> |
printw("%5s", "Net"); |
781 |
> |
move(2,x); |
782 |
> |
printw("%5s", "tx"); |
783 |
> |
x+=6; |
784 |
> |
} |
785 |
> |
|
786 |
> |
} |
787 |
> |
|
788 |
> |
if(display_config->disk_io_total_read){ |
789 |
> |
if(display_config->units=='b' && (display_config->maxx > x+10)){ |
790 |
> |
move(1,x); |
791 |
> |
printw("%9s", "Disk"); |
792 |
> |
move(2,x); |
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"); |
800 |
> |
move(2,x); |
801 |
> |
printw("%6s", "read"); |
802 |
> |
x+=7; |
803 |
> |
} |
804 |
> |
|
805 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
806 |
> |
move(1,x); |
807 |
> |
printw("%6s", "Disk"); |
808 |
> |
move(2,x); |
809 |
> |
printw("%6s", "read"); |
810 |
> |
x+=7; |
811 |
> |
} |
812 |
> |
|
813 |
> |
} |
814 |
> |
|
815 |
> |
if(display_config->disk_io_total_read){ |
816 |
> |
if(display_config->units=='b' && (display_config->maxx > x+10)){ |
817 |
> |
move(1,x); |
818 |
> |
printw("%9s", "Disk"); |
819 |
> |
move(2,x); |
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"); |
827 |
> |
move(2,x); |
828 |
> |
printw("%6s", "write"); |
829 |
> |
x+=7; |
830 |
> |
} |
831 |
> |
|
832 |
> |
if(display_config->units=='m' && (display_config->maxx > x+7)){ |
833 |
> |
move(1,x); |
834 |
> |
printw("%6s", "Disk"); |
835 |
> |
move(2,x); |
836 |
> |
printw("%6s", "write"); |
837 |
> |
x+=7; |
838 |
> |
} |
839 |
> |
|
840 |
> |
} |
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 |
> |
|
853 |
> |
/* Counter starts at 8, for padding (eg, headers, borders etc) */ |
854 |
> |
for(counter=8;counter<display_config->maxy;counter++){ |
855 |
|
if(machine_data_list==NULL) break; |
856 |
< |
printf("\033[%d;%dH%-11s %5.1f %5.1f %5d %5d %5.1f %5.1f %8lld %8lld %9lld %9lld", \ |
857 |
< |
line_num++, 1, \ |
858 |
< |
machine_data_list->sysname, |
859 |
< |
machine_data_list->cpu_used, |
860 |
< |
machine_data_list->load_1, |
861 |
< |
machine_data_list->pages_in, |
862 |
< |
machine_data_list->pages_out, |
863 |
< |
machine_data_list->memory_used_pecent, |
864 |
< |
machine_data_list->swap_used_pecent, |
865 |
< |
machine_data_list->network_io_total_rx, |
866 |
< |
machine_data_list->network_io_total_tx, |
867 |
< |
machine_data_list->disk_io_total_read, |
868 |
< |
machine_data_list->disk_io_total_write); |
856 |
> |
move(line_num++, 1); |
857 |
> |
printw("%-11s", machine_data_list->sysname); |
858 |
> |
x=13; |
859 |
> |
|
860 |
> |
if(display_config->cpu_used && (display_config->maxx > x+6)){ |
861 |
> |
printw(" %5.1f", machine_data_list->cpu_used); |
862 |
> |
x+=6; |
863 |
> |
} |
864 |
> |
if(display_config->load_1 && (display_config->maxx > x+6)){ |
865 |
> |
printw(" %5.1f", machine_data_list->load_1); |
866 |
> |
x+=6; |
867 |
> |
} |
868 |
> |
if(display_config->pages_in && (display_config->maxx > x+6)){ |
869 |
> |
printw(" %5d", machine_data_list->pages_in); |
870 |
> |
x+=6; |
871 |
> |
} |
872 |
> |
if(display_config->pages_out && (display_config->maxx > x+6)){ |
873 |
> |
printw(" %5d", machine_data_list->pages_out); |
874 |
> |
x+=6; |
875 |
> |
} |
876 |
> |
if(display_config->memory_used_pecent && (display_config->maxx > x+6)){ |
877 |
> |
printw(" %5.1f", machine_data_list->memory_used_pecent); |
878 |
> |
x+=6; |
879 |
> |
} |
880 |
> |
if(display_config->swap_used_pecent && (display_config->maxx > x+6)){ |
881 |
> |
printw(" %5.1f", machine_data_list->swap_used_pecent); |
882 |
> |
x+=6; |
883 |
> |
} |
884 |
> |
|
885 |
> |
if(display_config->network_io_total_rx){ |
886 |
> |
if(display_config->units=='b' && (display_config->maxx > x+9)){ |
887 |
> |
printw(" %8lld", machine_data_list->network_io_total_rx); |
888 |
> |
x+=9; |
889 |
> |
} |
890 |
> |
if(display_config->units=='k' && (display_config->maxx > x+6)){ |
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+6)){ |
895 |
> |
printw(" %5.2f", (double)(machine_data_list->network_io_total_rx/(1024.00*1024.00))); |
896 |
> |
x+=6; |
897 |
> |
} |
898 |
> |
} |
899 |
> |
|
900 |
> |
if(display_config->network_io_total_tx){ |
901 |
> |
if(display_config->units=='b' && (display_config->maxx > x+9)){ |
902 |
> |
printw(" %8lld", machine_data_list->network_io_total_tx); |
903 |
> |
x+=9; |
904 |
> |
} |
905 |
> |
if(display_config->units=='k' && (display_config->maxx > x+6)){ |
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+6)){ |
910 |
> |
printw(" %5.2f", (double)(machine_data_list->network_io_total_tx/(1024.00*1024.00))); |
911 |
> |
x+=6; |
912 |
> |
} |
913 |
> |
} |
914 |
> |
|
915 |
> |
if(display_config->disk_io_total_read){ |
916 |
> |
if(display_config->units=='b' && (display_config->maxx > x+10)){ |
917 |
> |
printw(" %9lld", machine_data_list->disk_io_total_read); |
918 |
> |
x+=10; |
919 |
> |
} |
920 |
> |
if(display_config->units=='k' && (display_config->maxx > x+7)){ |
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+7)){ |
925 |
> |
printw(" %6.2f", (double)(machine_data_list->disk_io_total_read/(1024.00*1024.00))); |
926 |
> |
x+=7; |
927 |
> |
} |
928 |
> |
} |
929 |
> |
|
930 |
> |
if(display_config->disk_io_total_write){ |
931 |
> |
if(display_config->units=='b' && (display_config->maxx > x+10)){ |
932 |
> |
printw(" %9lld", machine_data_list->disk_io_total_write); |
933 |
> |
x+=10; |
934 |
> |
} |
935 |
> |
if(display_config->units=='k' && (display_config->maxx > x+7)){ |
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+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 |
|
} |
955 |
|
|
956 |
< |
fflush(stdout); |
956 |
> |
|
957 |
> |
refresh(); |
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; |
984 |
+ |
struct winsize size; |
985 |
+ |
|
986 |
|
FILE *control; |
987 |
|
FILE *data; |
988 |
|
|
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 |
|
|
996 |
|
machine_data_list_t *machine_data_list=NULL; |
997 |
|
|
998 |
|
int num_hosts; |
999 |
< |
int max_display=0; |
999 |
> |
int title=1; |
1000 |
|
|
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; |
1009 |
|
|
1010 |
+ |
int data_fileno, stdin_fileno, biggest_fileno; |
1011 |
+ |
|
1012 |
+ |
sortby_ptr=NULL; |
1013 |
+ |
|
1014 |
|
/* What to display defaults */ |
1015 |
+ |
display_config.units='b'; |
1016 |
+ |
|
1017 |
|
display_config.cpu_user=0; |
1018 |
|
display_config.cpu_idle=0; |
1019 |
|
display_config.cpu_iowait=0; |
1038 |
|
display_config.pages_in=1; |
1039 |
|
display_config.pages_out=1; |
1040 |
|
|
1041 |
< |
display_config.processes_total=0; |
611 |
< |
display_config.processes_sleeping=0; |
612 |
< |
display_config.processes_cpu=0; |
613 |
< |
display_config.processes_zombie=0; |
614 |
< |
display_config.processes_stopped=0; |
1041 |
> |
display_config.processes=1; |
1042 |
|
|
1043 |
< |
display_config.network_io_total_tx=0; |
1044 |
< |
display_config.network_io_total_rx=0; |
1045 |
< |
display_config.network_all_stats=1; |
1043 |
> |
display_config.network_io_total_tx=1; |
1044 |
> |
display_config.network_io_total_rx=1; |
1045 |
> |
display_config.network_all_stats=0; |
1046 |
|
|
1047 |
< |
display_config.disk_io_total_write=0; |
1048 |
< |
display_config.disk_io_total_read=0; |
1049 |
< |
display_config.disk_io_all_stats=1; |
1047 |
> |
display_config.disk_io_total_write=1; |
1048 |
> |
display_config.disk_io_total_read=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, "d:")) != -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 'd': |
1062 |
< |
max_display=atoi(optarg); |
1063 |
< |
break; |
1061 |
> |
case 'o': |
1062 |
> |
if(!strcmp(optarg, "cpu")){ |
1063 |
> |
sortby_ptr=cmp_cpu_used; |
1064 |
> |
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
1065 |
> |
} |
1066 |
> |
if(!strcmp(optarg, "load")){ |
1067 |
> |
sortby_ptr=cmp_load_1; |
1068 |
> |
strlcpy(display_config.sortby, LOAD, SORTBYMAXNAME); |
1069 |
> |
} |
1070 |
> |
if(!strcmp(optarg, "mem")){ |
1071 |
> |
sortby_ptr=cmp_memory_used_pecent; |
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 |
> |
} |
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 order given."); |
1088 |
> |
usage(); |
1089 |
> |
} |
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(argc<(optind+2)){ |
1115 |
< |
printf("Usage is %s <-d lines> hostname port <machine list>\n", argv[0]); |
1116 |
< |
exit(1); |
1114 |
> |
if(sortby_ptr==NULL){ |
1115 |
> |
sortby_ptr=cmp_cpu_used; |
1116 |
> |
strlcpy(display_config.sortby, "CPU Used", SORTBYMAXNAME); |
1117 |
|
} |
1118 |
|
|
1119 |
< |
servername=argv[optind]; |
642 |
< |
server_control_port=atoi(argv[optind+1]); |
643 |
< |
|
644 |
< |
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 |
|
|
649 |
– |
if(argc==4){ |
650 |
– |
/* We've been passed a machine list */ |
651 |
– |
/* list currently needs to be ; seperated */ |
652 |
– |
machine_list=strdup(argv[3]); |
653 |
– |
} |
654 |
– |
|
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 |
|
|
1163 |
< |
|
1164 |
< |
printf("\033[2J"); |
1165 |
< |
printf("\033[1;1HHostname CPU Load Page Page Mem Swap Net Net Disk Disk"); |
1166 |
< |
printf("\033[2;1H used%% (1m) ins outs used used rx tx read write"); |
1167 |
< |
fflush(stdout); |
1163 |
> |
initscr(); |
1164 |
> |
nonl(); |
1165 |
> |
cbreak(); |
1166 |
> |
noecho(); |
1167 |
> |
window=newwin(0, 0, 0, 0); |
1168 |
> |
getmaxyx(window, display_config.maxy, display_config.maxx); |
1169 |
> |
|
1170 |
> |
stdin_fileno=fileno(stdin); |
1171 |
> |
data_fileno=fileno(data); |
1172 |
> |
biggest_fileno=(data_fileno>stdin_fileno) ? (data_fileno+1) : (stdin_fileno+1); |
1173 |
> |
|
1174 |
|
for(;;){ |
1175 |
< |
response=fpgetline(data); |
1176 |
< |
if (response==NULL){ |
1177 |
< |
errf("Failed to read data (%m)"); |
1178 |
< |
exit(1); |
1175 |
> |
FD_ZERO(&infds); |
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); |
1189 |
+ |
} |
1190 |
+ |
getmaxyx(window, display_config.maxy, display_config.maxx); |
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=getch(); |
1201 |
+ |
switch(ch){ |
1202 |
+ |
case KEY_RESIZE: |
1203 |
+ |
sig_winch=1; |
1204 |
+ |
break; |
1205 |
+ |
|
1206 |
+ |
/* Quit */ |
1207 |
+ |
case 'Q': |
1208 |
+ |
case 'q': |
1209 |
+ |
endwin(); |
1210 |
+ |
exit(0); |
1211 |
+ |
break; |
1212 |
+ |
/* Units */ |
1213 |
+ |
case 'U': |
1214 |
+ |
case 'u': |
1215 |
+ |
if(display_config.units == 'b'){ |
1216 |
+ |
display_config.units = 'k'; |
1217 |
+ |
}else if(display_config.units == 'k'){ |
1218 |
+ |
display_config.units = 'm'; |
1219 |
+ |
}else{ |
1220 |
+ |
display_config.units = 'b'; |
1221 |
+ |
} |
1222 |
+ |
break; |
1223 |
+ |
|
1224 |
+ |
/* Sort by */ |
1225 |
+ |
case 'C': |
1226 |
+ |
sortby_ptr=cmp_cpu_used; |
1227 |
+ |
strlcpy(display_config.sortby, CPU_USED, SORTBYMAXNAME); |
1228 |
+ |
break; |
1229 |
+ |
|
1230 |
+ |
case 'M': |
1231 |
+ |
sortby_ptr=cmp_memory_used_pecent; |
1232 |
+ |
strlcpy(display_config.sortby, MEM, SORTBYMAXNAME); |
1233 |
+ |
break; |
1234 |
+ |
|
1235 |
+ |
case 'L': |
1236 |
+ |
sortby_ptr=cmp_load_1; |
1237 |
+ |
strlcpy(display_config.sortby, LOAD, SORTBYMAXNAME); |
1238 |
+ |
break; |
1239 |
+ |
|
1240 |
+ |
case 'S': |
1241 |
+ |
sortby_ptr=cmp_swap_used_pecent; |
1242 |
+ |
strlcpy(display_config.sortby, SWAP, SORTBYMAXNAME); |
1243 |
+ |
break; |
1244 |
+ |
|
1245 |
+ |
case 'N': |
1246 |
+ |
if(sortby_ptr==cmp_network_io_total){ |
1247 |
+ |
strlcpy(display_config.sortby, NETIORX, SORTBYMAXNAME); |
1248 |
+ |
sortby_ptr=cmp_network_io_total_rx; |
1249 |
+ |
}else if(sortby_ptr==cmp_network_io_total_rx){ |
1250 |
+ |
strlcpy(display_config.sortby, NETIOTX, SORTBYMAXNAME); |
1251 |
+ |
sortby_ptr=cmp_network_io_total_tx; |
1252 |
+ |
}else{ |
1253 |
+ |
strlcpy(display_config.sortby, NETIO, SORTBYMAXNAME); |
1254 |
+ |
sortby_ptr=cmp_network_io_total; |
1255 |
+ |
} |
1256 |
+ |
break; |
1257 |
+ |
case 'D': |
1258 |
+ |
if(sortby_ptr==cmp_disk_io_total){ |
1259 |
+ |
strlcpy(display_config.sortby, DISKIOR, SORTBYMAXNAME); |
1260 |
+ |
sortby_ptr=cmp_disk_io_total_read; |
1261 |
+ |
}else if(sortby_ptr==cmp_disk_io_total_read){ |
1262 |
+ |
strlcpy(display_config.sortby, DISKIOW, SORTBYMAXNAME); |
1263 |
+ |
sortby_ptr=cmp_disk_io_total_write; |
1264 |
+ |
}else{ |
1265 |
+ |
strlcpy(display_config.sortby, DISKIO, SORTBYMAXNAME); |
1266 |
+ |
sortby_ptr=cmp_disk_io_total; |
1267 |
+ |
} |
1268 |
+ |
break; |
1269 |
+ |
|
1270 |
+ |
/* Display */ |
1271 |
+ |
|
1272 |
+ |
case 'd': |
1273 |
+ |
if(display_config.disk_io_total_read){ |
1274 |
+ |
display_config.disk_io_total_read=0; |
1275 |
+ |
display_config.disk_io_total_write=0; |
1276 |
+ |
}else{ |
1277 |
+ |
display_config.disk_io_total_read=1; |
1278 |
+ |
display_config.disk_io_total_write=1; |
1279 |
+ |
} |
1280 |
+ |
break; |
1281 |
+ |
case 'n': |
1282 |
+ |
if(display_config.network_io_total_rx){ |
1283 |
+ |
display_config.network_io_total_rx=0; |
1284 |
+ |
display_config.network_io_total_tx=0; |
1285 |
+ |
}else{ |
1286 |
+ |
display_config.network_io_total_rx=1; |
1287 |
+ |
display_config.network_io_total_tx=1; |
1288 |
+ |
} |
1289 |
+ |
break; |
1290 |
+ |
case 'm': |
1291 |
+ |
if(display_config.memory_used_pecent){ |
1292 |
+ |
display_config.memory_used_pecent=0; |
1293 |
+ |
}else{ |
1294 |
+ |
display_config.memory_used_pecent=1; |
1295 |
+ |
} |
1296 |
+ |
break; |
1297 |
+ |
|
1298 |
+ |
case 's': |
1299 |
+ |
if(display_config.swap_used_pecent){ |
1300 |
+ |
display_config.swap_used_pecent=0; |
1301 |
+ |
}else{ |
1302 |
+ |
display_config.swap_used_pecent=1; |
1303 |
+ |
} |
1304 |
+ |
break; |
1305 |
+ |
case 'l': |
1306 |
+ |
if(display_config.load_1){ |
1307 |
+ |
display_config.load_1=0; |
1308 |
+ |
}else{ |
1309 |
+ |
display_config.load_1=1; |
1310 |
+ |
} |
1311 |
+ |
break; |
1312 |
+ |
case 'p': |
1313 |
+ |
if(display_config.pages_in){ |
1314 |
+ |
display_config.pages_in=0; |
1315 |
+ |
display_config.pages_out=0; |
1316 |
+ |
}else{ |
1317 |
+ |
display_config.pages_in=1; |
1318 |
+ |
display_config.pages_out=1; |
1319 |
+ |
} |
1320 |
+ |
break; |
1321 |
+ |
case 'c': |
1322 |
+ |
if(display_config.cpu_used){ |
1323 |
+ |
display_config.cpu_used=0; |
1324 |
+ |
}else{ |
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 |
+ |
continue; |
1338 |
+ |
} |
1339 |
+ |
|
1340 |
+ |
/* Increment title so it becomes true (and making the screen update */ |
1341 |
+ |
title++; |
1342 |
+ |
|
1343 |
+ |
} |
1344 |
+ |
if(FD_ISSET(data_fileno, &infds)){ |
1345 |
+ |
response=fpgetline(data); |
1346 |
+ |
if (response==NULL){ |
1347 |
+ |
errf("Failed to read data (%m)"); |
1348 |
+ |
exit(1); |
1349 |
+ |
} |
1350 |
+ |
} |
1351 |
+ |
|
1352 |
+ |
|
1353 |
|
num_hosts=parse_xml(response, &machine_data_list); |
1354 |
|
if(num_hosts==-1) continue; |
1355 |
< |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, cmp_cpu); |
1356 |
< |
if(max_display==0){ |
708 |
< |
display(machine_data_list, num_hosts); |
709 |
< |
}else{ |
710 |
< |
display(machine_data_list, max_display); |
711 |
< |
} |
1355 |
> |
machine_data_list=sort_machine_stats(machine_data_list, num_hosts, sortby_ptr); |
1356 |
> |
display(machine_data_list, &display_config, &title); |
1357 |
|
|
1358 |
|
} |
1359 |
|
exit(0); |