ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/idar/idar.c
Revision: 1.20
Committed: Thu Aug 21 21:10:34 2003 UTC (21 years, 3 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.19: +1 -0 lines
Log Message:
Fix bug #7 using patch given by Adam Sampson.
Added include to compile on Linux.

File Contents

# Content
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;
52
53 struct host_line_t *next;
54 };
55 typedef struct host_line_t host_line_list_t;
56
57 struct diskio_data_t{
58 char *name;
59
60 long long read;
61 long long write;
62
63 struct diskio_data_t *next;
64 };
65 typedef struct diskio_data_t diskio_data_list_t;
66
67 struct network_data_t{
68 char *name;
69
70 long long rx;
71 long long tx;
72
73 struct network_data_t *next;
74 };
75 typedef struct network_data_t network_data_list_t;
76
77 /*
78 struct disk_data_t{
79 char *name;
80 char *mount_pnt;
81
82 long long total_space;
83 long long total_used;
84 long long total_avail;
85 // Other data we are less intrested in are not stored
86
87 struct disk_data_t *next;
88 };
89 typedef struct disk_data_t disk_data_list_t;
90 */
91 #define MAXHOSTNAMESIZE 10
92 struct machine_data_t{
93
94 char *name;
95
96 char sysname[MAXHOSTNAMESIZE+1];
97
98 double cpu_user;
99 double cpu_idle;
100 double cpu_iowait;
101 double cpu_kernel;
102 double cpu_swap;
103 double cpu_used; /* 100 - idle */
104
105 long long memory_total;
106 long long memory_free;
107 long long memory_used;
108 double memory_used_pecent;
109
110 long long swap_total;
111 long long swap_free;
112 long long swap_used;
113 double swap_used_pecent;
114
115 int pages_in;
116 int pages_out;
117
118 double load_1;
119 double load_5;
120 double load_15;
121
122 int processes_total;
123 int processes_sleeping;
124 int processes_cpu;
125 int processes_zombie;
126 int processes_stopped;
127
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 /*
140 disk_data_list_t disk_data_list;
141 double disk_total_used;
142 */
143
144 struct machine_data_t *next;
145 };
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;
159 int cpu_kernel;
160 int cpu_swap;
161 int cpu_used;
162
163 int memory_total;
164 int memory_free;
165 int memory_used;
166 int memory_used_pecent;
167
168 int swap_total;
169 int swap_free;
170 int swap_used;
171 int swap_used_pecent;
172
173 int load_1;
174 int load_5;
175 int load_15;
176
177 int pages_in;
178 int pages_out;
179
180 int processes;
181
182 int network_io_total_tx;
183 int network_io_total_rx;
184 int network_all_stats;
185
186 int disk_io_total_write;
187 int disk_io_total_read;
188 int disk_io_all_stats;
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 #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
200
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;
300 struct sockaddr_in addr;
301 struct in_addr haddr;
302 FILE *f;
303
304 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){
305 return NULL;
306 }
307
308 if((get_host_addr(hostname, &haddr))!=0){
309 close(sock);
310 return NULL;
311 }
312
313 memset(&addr, 0, sizeof(addr));
314 addr.sin_family = AF_INET;
315 memcpy(&addr.sin_addr, &haddr, sizeof(haddr));
316 addr.sin_port = htons(port);
317
318 if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){
319 close(sock);
320 return NULL;
321 }
322
323 if((f=fdopen(sock, "r+"))==NULL){
324 close(sock);
325 return NULL;
326 }
327
328 return f;
329 }
330
331 int tcp_comm(FILE *f, char *send, char **response, char *expected){
332
333 if(send!=NULL){
334 fprintf(f, "%s\n", send);
335 }
336 fflush(f);
337 *response=fpgetline(f);
338 fseek(f, 0, SEEK_CUR);
339
340 if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1;
341
342 if(expected==NULL) return 0;
343
344 if((strcmp(expected, *response))==0) return 0;
345
346 return -1;
347 }
348
349 /* Takes a xml char * and a machine_data_list_t. This will parse
350 * the xml and put it into the correct entry of the machine_data
351 * linked list. This will return the number of entries in the linked
352 * list, or -1 on a error.
353 */
354 int parse_xml(char *xml, machine_data_list_t **md){
355 xmlDocPtr doc;
356 xmlNodePtr cur;
357 xmlNodePtr ele;
358 xmlAttr *list;
359
360 static int num_hosts=0;
361
362 machine_data_list_t *machine_data_list=*md;
363
364 int found_host=0;
365
366 char *hostname=NULL;
367 network_data_list_t *network_data_list=NULL;
368 network_data_list_t *ndl_ptr=NULL;
369 diskio_data_list_t *diskio_data_list=NULL;
370 diskio_data_list_t *didl_ptr=NULL;
371 char *tmp;
372
373 doc=xmlParseDoc(xml);
374 if(doc==NULL) return -1;
375
376 cur = xmlDocGetRootElement(doc);
377 if(cur==NULL){
378 xmlFreeDoc(doc);
379 return -1;
380 }
381
382
383 /* Get the hostname */
384 list=cur->properties;
385 while(list!=NULL){
386 if((xmlStrcmp(list->name, (const xmlChar *) "machine_name"))==0){
387 hostname = xmlNodeGetContent(list->children);
388 if(hostname==NULL){
389 return -1;
390 }
391 }
392 list=list->next;
393 }
394 if(hostname==NULL){
395 return -1;
396 }
397
398 /* Get machine_data for host.. Or create if it doesn't have one */
399 while(machine_data_list!=NULL){
400 if((strncmp(machine_data_list->name, hostname, strlen(hostname)))==0){
401 found_host=1;
402 break;
403 }
404 machine_data_list=machine_data_list->next;
405 }
406
407 if(!found_host){
408 /* We didn't find this host, but we should be at the end of the list */
409 machine_data_list=malloc(sizeof(machine_data_list_t));
410 if(machine_data_list==NULL) return -1;
411 machine_data_list->next=(*md);
412 machine_data_list->name=hostname;
413 machine_data_list->network_data_list=NULL;
414 machine_data_list->diskio_data_list=NULL;
415 *md=machine_data_list;
416 num_hosts++;
417 }
418
419 /* Now we want to pull out the data */
420
421 cur = cur->xmlChildrenNode;
422 while(cur != NULL) {
423 ele=cur->xmlChildrenNode;
424 while(ele != NULL){
425
426 /* CPU Stats */
427 if(!xmlStrcmp(cur->name, (const xmlChar *) "cpu")){
428 tmp=xmlNodeGetContent(ele);
429 if(!xmlStrcmp(ele->name, (const xmlChar *) "user")){
430 machine_data_list->cpu_user=atof(tmp);
431 }
432 if(!xmlStrcmp(ele->name, (const xmlChar *) "kernel")){
433 machine_data_list->cpu_kernel=atof(tmp);
434 }
435 if(!xmlStrcmp(ele->name, (const xmlChar *) "idle")){
436 machine_data_list->cpu_idle=atof(tmp);
437 }
438 if(!xmlStrcmp(ele->name, (const xmlChar *) "iowait")){
439 machine_data_list->cpu_iowait=atof(tmp);
440 }
441 if(!xmlStrcmp(ele->name, (const xmlChar *) "swap")){
442 machine_data_list->cpu_iowait=atof(tmp);
443 }
444
445 if(tmp!=NULL) xmlFree(tmp);
446 }
447
448 /* Memory Stats */
449 if(!xmlStrcmp(cur->name, (const xmlChar *) "memory")){
450 tmp=xmlNodeGetContent(ele);
451 if(!xmlStrcmp(ele->name, (const xmlChar *) "total")){
452 machine_data_list->memory_total=atoll(tmp);
453 }
454 if(!xmlStrcmp(ele->name, (const xmlChar *) "free")){
455 machine_data_list->memory_free=atoll(tmp);
456 }
457 if(!xmlStrcmp(ele->name, (const xmlChar *) "used")){
458 machine_data_list->memory_used=atoll(tmp);
459 }
460
461 if(tmp!=NULL) xmlFree(tmp);
462 }
463
464 /* Load Stats */
465 if(!xmlStrcmp(cur->name, (const xmlChar *) "load")){
466 tmp=xmlNodeGetContent(ele);
467 if(!xmlStrcmp(ele->name, (const xmlChar *) "load1")){
468 machine_data_list->load_1=atof(tmp);
469 }
470 if(!xmlStrcmp(ele->name, (const xmlChar *) "load5")){
471 machine_data_list->load_5=atof(tmp);
472 }
473 if(!xmlStrcmp(ele->name, (const xmlChar *) "load15")){
474 machine_data_list->load_15=atof(tmp);
475 }
476
477 if(tmp!=NULL) xmlFree(tmp);
478 }
479
480 /* swap stats */
481 if(!xmlStrcmp(cur->name, (const xmlChar *) "swap")){
482 tmp=xmlNodeGetContent(ele);
483 if(!xmlStrcmp(ele->name, (const xmlChar *) "total")){
484 machine_data_list->swap_total=atoll(tmp);
485 }
486 if(!xmlStrcmp(ele->name, (const xmlChar *) "free")){
487 machine_data_list->swap_free=atoll(tmp);
488 }
489 if(!xmlStrcmp(ele->name, (const xmlChar *) "used")){
490 machine_data_list->swap_used=atoll(tmp);
491 }
492
493 if(tmp!=NULL) xmlFree(tmp);
494 }
495
496 /* Process stat */
497 if(!xmlStrcmp(cur->name, (const xmlChar *) "processes")){
498 tmp=xmlNodeGetContent(ele);
499 if(!xmlStrcmp(ele->name, (const xmlChar *) "sleeping")){
500 machine_data_list->processes_sleeping=atoi(tmp);
501 }
502 if(!xmlStrcmp(ele->name, (const xmlChar *) "cpu")){
503 machine_data_list->processes_cpu=atoi(tmp);
504 }
505 if(!xmlStrcmp(ele->name, (const xmlChar *) "zombie")){
506 machine_data_list->processes_zombie=atoi(tmp);
507 }
508 if(!xmlStrcmp(ele->name, (const xmlChar *) "total")){
509 machine_data_list->processes_total=atoi(tmp);
510 }
511
512 if(tmp!=NULL) xmlFree(tmp);
513 }
514
515 /* paging stats */
516 if(!xmlStrcmp(cur->name, (const xmlChar *) "pages")){
517 tmp=xmlNodeGetContent(ele);
518 if(!xmlStrcmp(ele->name, (const xmlChar *) "pageins")){
519 machine_data_list->pages_in=atoi(tmp);
520 }
521 if(!xmlStrcmp(ele->name, (const xmlChar *) "pageouts")){
522 machine_data_list->pages_out=atoi(tmp);
523 }
524
525 if(tmp!=NULL) xmlFree(tmp);
526 }
527
528 /* OS stats */
529 if(!xmlStrcmp(cur->name, (const xmlChar *) "os")){
530 tmp=xmlNodeGetContent(ele);
531 if(!xmlStrcmp(ele->name, (const xmlChar *) "sysname")){
532 strlcpy(machine_data_list->sysname, tmp, MAXHOSTNAMESIZE+1);
533 }
534 if(tmp!=NULL) xmlFree(tmp);
535 }
536
537 /* Network stats */
538 /* Needs to connect current stat to a previous one. Or create it if new */
539 /* Get name.. Walk list. If match, copy rx/tx values. Else malloc, add to list */
540 if(!xmlStrcmp(cur->name, (const xmlChar *) "net")){
541
542 list=ele->properties;
543 if(list==NULL) continue;
544 network_data_list=malloc(sizeof(network_data_list_t));
545 if(network_data_list==NULL) return -1;
546 while(list!=NULL){
547 tmp=xmlNodeGetContent(list->children);
548 if(!xmlStrcmp(list->name, (const xmlChar *) "name")){
549 network_data_list->name=strdup(tmp);
550 }
551 if(!xmlStrcmp(list->name, (const xmlChar *) "rx")){
552 network_data_list->rx=atoll(tmp);
553 }
554 if(!xmlStrcmp(list->name, (const xmlChar *) "tx")){
555 network_data_list->tx=atoll(tmp);
556 }
557
558 xmlFree(tmp);
559 list=list->next;
560 }
561 if(network_data_list->name==NULL) continue;
562 found_host=0;
563 ndl_ptr=machine_data_list->network_data_list;
564 while(ndl_ptr!=NULL){
565 if(!strcmp(ndl_ptr->name, network_data_list->name)){
566 found_host=1;
567 break;
568 }
569 ndl_ptr=ndl_ptr->next;
570 }
571 if(found_host){
572 ndl_ptr->rx=network_data_list->rx;
573 ndl_ptr->tx=network_data_list->tx;
574 free(network_data_list->name);
575 free(network_data_list);
576 }else{
577 network_data_list->next=machine_data_list->network_data_list;
578 machine_data_list->network_data_list=network_data_list;
579 }
580 }
581
582 /* Disk IO stats */
583 if(!xmlStrcmp(cur->name, (const xmlChar *) "diskio")){
584
585 list=ele->properties;
586 if(list==NULL) continue;
587 diskio_data_list=malloc(sizeof(diskio_data_list_t));
588 if(diskio_data_list==NULL) return -1;
589 while(list!=NULL){
590 tmp=xmlNodeGetContent(list->children);
591 if(!xmlStrcmp(list->name, (const xmlChar *) "name")){
592 diskio_data_list->name=strdup(tmp);
593 }
594 if(!xmlStrcmp(list->name, (const xmlChar *) "rbytes")){
595 diskio_data_list->read=atoll(tmp);
596 }
597 if(!xmlStrcmp(list->name, (const xmlChar *) "wbytes")){
598 diskio_data_list->write=atoll(tmp);
599 }
600
601 xmlFree(tmp);
602 list=list->next;
603 }
604 if(diskio_data_list->name==NULL) continue;
605 found_host=0;
606 didl_ptr=machine_data_list->diskio_data_list;
607 while(didl_ptr!=NULL){
608 if(!strcmp(didl_ptr->name, diskio_data_list->name)){
609 found_host=1;
610 break;
611 }
612 didl_ptr=didl_ptr->next;
613 }
614 if(found_host){
615 didl_ptr->read=diskio_data_list->read;
616 didl_ptr->write=diskio_data_list->write;
617 free(diskio_data_list->name);
618 free(diskio_data_list);
619 }else{
620 diskio_data_list->next=machine_data_list->diskio_data_list;
621 machine_data_list->diskio_data_list=diskio_data_list;
622 }
623 }
624
625
626
627 ele=ele->next;
628 }
629 cur=cur->next;
630 }
631
632 /* Append data we want thats not stored in the server */
633 machine_data_list->cpu_used=100.00-machine_data_list->cpu_idle;
634 machine_data_list->memory_used_pecent=((double)machine_data_list->memory_used / (double)machine_data_list->memory_total) * 100.00;
635 machine_data_list->swap_used_pecent=((double)machine_data_list->swap_used / (double)machine_data_list->swap_total) * 100.00;
636
637 ndl_ptr=machine_data_list->network_data_list;
638 machine_data_list->network_io_total_tx=0;
639 machine_data_list->network_io_total_rx=0;
640 while(ndl_ptr!=NULL){
641 machine_data_list->network_io_total_tx+=ndl_ptr->tx;
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;
649 machine_data_list->disk_io_total_write=0;
650 while(didl_ptr!=NULL){
651 machine_data_list->disk_io_total_read+=didl_ptr->read;
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
659 return num_hosts;
660
661
662 }
663
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 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 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
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 *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 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;
1020 display_config.cpu_kernel=0;
1021 display_config.cpu_swap=0;
1022 display_config.cpu_used=1;
1023
1024 display_config.memory_total=0;
1025 display_config.memory_free=0;
1026 display_config.memory_used=0;
1027 display_config.memory_used_pecent=1;
1028
1029 display_config.swap_total=0;
1030 display_config.swap_free=0;
1031 display_config.swap_used=0;
1032 display_config.swap_used_pecent=1;
1033
1034 display_config.load_1=1;
1035 display_config.load_5=0;
1036 display_config.load_15=0;
1037
1038 display_config.pages_in=1;
1039 display_config.pages_out=1;
1040
1041 display_config.processes=1;
1042
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=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;
1053
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 '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(sortby_ptr==NULL){
1115 sortby_ptr=cmp_cpu_used;
1116 strlcpy(display_config.sortby, "CPU Used", SORTBYMAXNAME);
1117 }
1118
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
1125 if((tcp_comm(control, NULL, &response, "PROTOCOL 1.1"))!=0){
1126 errf("Incorrect version number (%s)", response);
1127 exit(1);
1128 }
1129
1130 if((tcp_comm(control, "stattop", &response, "OK"))!=0){
1131 errf("Unexpected response %s", response);
1132 exit(1);
1133 }
1134
1135 if(machine_list!=NULL){
1136 if((tcp_comm(control, "SETHOSTLIST", &response, "OK"))!=0){
1137 errf("Unexpected response %s", response);
1138 exit(1);
1139 }
1140 if((tcp_comm(control, machine_list, &response, "OK"))!=0){
1141 errf("Unexpected response %s", response);
1142 exit(1);
1143 }
1144 }
1145
1146 if((tcp_comm(control, "STARTDATA", &response, NULL))!=0){
1147 errf("Unexpected response %s", response);
1148 exit(1);
1149 }
1150
1151 server_data_port=atoi(response);
1152 if(server_data_port==0){
1153 errf("Unexpected response %s", response);
1154 exit(1);
1155 }
1156
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)",server_name, server_data_port);
1160 exit(1);
1161 }
1162
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 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, sortby_ptr);
1356 display(machine_data_list, &display_config, &title);
1357
1358 }
1359 exit(0);
1360 }