Add usage(), minor code cleanups
[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 void
48 usage() {
49         fputs(
50                  "Copyright (c) 2004-2007 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
51                  "rtop - read collected load of remote servers\n"
52                  "./rtop [-D] -h HOST [-p PORT]\n"
53                  "  -D      - debug mode\n"
54                  "  -h HOST - server\n"
55                  "  -p PRT  - server's port\n",
56                  stdout
57         );
58         exit(1);
59 }
60
61 int
62 main( int argc, char *argv[] ) {
63         int ch;
64         int debug=0;
65         char *host=NULL;
66         int port=TOPD_SERVER_DEFAULT_PORT;
67         int i;
68
69         MassiveUnit *mu;
70         TCMsg   *pmsg = (TCMsg*)malloc( TCMSGHDRSZ );
71         TC_Connection  cs;
72
73         pmsg->type = TOPGETTYPE;
74         pmsg->len = TCMSGHDRSZ;
75
76         while( (ch=getopt(argc, argv, "Dh:p:"))!=-1) {
77                 switch(ch) {
78                         case 'D':
79                                 debug=1;
80                                 break;
81                         case 'h':
82                                 host=strdup(optarg);
83                                 break;
84                         case 'p':
85                                 port=atoi(optarg);
86                                 break;
87                         default:
88                                 usage();
89                                 return 1;
90                 }
91         }
92
93         if (!host)
94                 usage();
95
96         opentlog( TL_OPEN_STDERR,  (debug) ? TL_DEBUG : TL_INFO, NULL);
97
98         TC_fillConnection(&cs, host, port);
99         cs.buf = (char*)pmsg;
100         cs.len = pmsg->len;
101         if ( TC_Talk(&cs) == CS_OK ) {  
102                 pmsg = (TCMsg*) cs.buf;
103                 mu = (MassiveUnit*)pmsg->data;
104
105                 printf("\tIP\tLoad\tFree\tTotal\tData\n");
106                 for(i=0; i<mu->number; i++) 
107                         printf("%s\t%.2f\t%.1fM\t%.1fM\t%s",
108                                 inet_ntoa(mu->units[i].ip),
109                                 mu->units[i].top.load,
110                                 ((float) mu->units[i].top.freemem)/( 1024*1024.0 ),
111                                 ((float) mu->units[i].top.usermem)/( 1024*1024.0 ),
112                                 ctime( &(mu->units[i].stamp) )
113                         );
114         }
115         
116         TC_FreeConnection(&cs);
117                                 
118         return 0;
119 }
120