add .gitignore
[remotetop.git] / rtop.c
diff --git a/rtop.c b/rtop.c
index c204aaf..a9f7260 100644 (file)
--- a/rtop.c
+++ b/rtop.c
@@ -34,6 +34,7 @@
 #include <errno.h>
 #include <sys/sysctl.h>
 #include <sys/types.h>
+#include <time.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include "tmalloc.h"
 #include "top.h"
 
+void
+usage() {
+       fputs(
+                "Copyright (c) 2004-2007 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
+                "rtop - read collected load of remote servers\n"
+                "./rtop [-D] -h HOST [-p PORT]\n"
+                "  -D      - debug mode\n"
+                "  -h HOST - server\n"
+                "  -p PRT  - server's port\n",
+                stdout
+       );
+       exit(1);
+}
+
 int
 main( int argc, char *argv[] ) {
        int ch;
        int debug=0;
-       char *host="127.0.0.1";
+       char *host=NULL;
        int port=TOPD_SERVER_DEFAULT_PORT;
        int i;
 
-       MassiveUnit *mu;
-       TCMsg   *pmsg = (TCMsg*)malloc( TCMSGHDRSZ );
+       TCMsg   *pmsg;
        TC_Connection  cs;
 
-       pmsg->type = TOPGETTYPE;
-       pmsg->len = TCMSGHDRSZ;
+       opentlog( TL_OPEN_STDERR, TL_INFO, NULL);
 
        while( (ch=getopt(argc, argv, "Dh:p:"))!=-1) {
                switch(ch) {
@@ -71,29 +84,38 @@ main( int argc, char *argv[] ) {
                                port=atoi(optarg);
                                break;
                        default:
+                               usage();
                                return 1;
                }
        }
 
-        if ( debug )
-                opentlog( TL_OPEN_STDERR,  TL_DEBUG, NULL);
-        else
-                opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
+       if (!host)
+               usage();
+
+       opentlog( TL_OPEN_STDERR,  (debug) ? TL_DEBUG : TL_INFO, NULL);
 
        TC_fillConnection(&cs, host, port);
-       cs.buf = (char*)pmsg;
-       cs.len = pmsg->len;
-       if ( TC_Talk(&cs) == CS_OK ) {  
-               pmsg = (TCMsg*) cs.buf;
+
+       pmsg = (TCMsg*)tmalloc( TCMSGHDRSZ );
+       pmsg->type = TOPGETTYPE;
+       pmsg->len = TCMSGHDRSZ;
+       cs.buf = pmsg;
+
+       if ( TC_Talk(&cs, 0) == CS_OK ) {       
+               MassiveUnit *mu;
+
+               pmsg = cs.buf;
                mu = (MassiveUnit*)pmsg->data;
 
-               printf("\tIP\tLoad\tFree\tTotal\tData\n");
+               tlog(TL_DEBUG,"Got packet len:%db type:%08x n:%d", pmsg->len, pmsg->type, mu->number);
+
+               printf("\tIP\tLoad\tFree\tTotal\tDate\n");
                for(i=0; i<mu->number; i++) 
                        printf("%s\t%.2f\t%.1fM\t%.1fM\t%s",
                                inet_ntoa(mu->units[i].ip),
                                mu->units[i].top.load,
                                ((float) mu->units[i].top.freemem)/( 1024*1024.0 ),
-                               ((float) mu->units[i].top.usermem)/( 1024*1024.0 ),
+                               ((float) mu->units[i].top.physmem)/( 1024*1024.0 ),
                                ctime( &(mu->units[i].stamp) )
                        );
        }