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