Apply changes of TC_ServerConnect
[remotetop.git] / rtop.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <sys/sysctl.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41
42 #include "tlog.h"
43 #include "connection.h"
44 #include "tmalloc.h"
45 #include "top.h"
46
47 int
48 main( int argc, char *argv[] ) {
49         int ch;
50         int debug=0;
51         char *host="127.0.0.1";
52         int port=TOPD_SERVER_DEFAULT_PORT;
53         int i;
54
55         MassiveUnit *mu;
56         TCMsg   *pmsg = (TCMsg*)malloc( TCMSGHDRSZ );
57         TC_Connection  cs;
58
59         pmsg->type = TOPGETTYPE;
60         pmsg->len = TCMSGHDRSZ;
61
62         while( (ch=getopt(argc, argv, "Dh:p:"))!=-1) {
63                 switch(ch) {
64                         case 'D':
65                                 debug=1;
66                                 break;
67                         case 'h':
68                                 host=strdup(optarg);
69                                 break;
70                         case 'p':
71                                 port=atoi(optarg);
72                                 break;
73                         default:
74                                 return 1;
75                 }
76         }
77
78         if ( debug )
79                 opentlog( TL_OPEN_STDERR,  TL_DEBUG, NULL);
80         else
81                 opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
82
83         TC_fillConnection(&cs, host, port);
84         cs.buf = (char*)pmsg;
85         cs.len = pmsg->len;
86         if ( TC_Talk(&cs) == CS_OK ) {  
87                 pmsg = (TCMsg*) cs.buf;
88                 mu = (MassiveUnit*)pmsg->data;
89
90                 printf("\tIP\tLoad\tFree\tTotal\tData\n");
91                 for(i=0; i<mu->number; i++) 
92                         printf("%s\t%.2f\t%.1fM\t%.1fM\t%s",
93                                 inet_ntoa(mu->units[i].ip),
94                                 mu->units[i].top.load,
95                                 ((float) mu->units[i].top.freemem)/( 1024*1024.0 ),
96                                 ((float) mu->units[i].top.usermem)/( 1024*1024.0 ),
97                                 ctime( &(mu->units[i].stamp) )
98                         );
99         }
100         
101         TC_FreeConnection(&cs);
102                                 
103         return 0;
104 }
105