/* * Copyright (c) 2004 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "tlog.h" #include "connection.h" #include "tmalloc.h" #include "top.h" void usage() { fputs( "Copyright (c) 2004-2007 Teodor Sigaev . 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=NULL; int port=TOPD_SERVER_DEFAULT_PORT; int i; TCMsg *pmsg; TC_Connection cs; opentlog( TL_OPEN_STDERR, TL_INFO, NULL); while( (ch=getopt(argc, argv, "Dh:p:"))!=-1) { switch(ch) { case 'D': debug=1; break; case 'h': host=strdup(optarg); break; case 'p': port=atoi(optarg); break; default: usage(); return 1; } } if (!host) usage(); opentlog( TL_OPEN_STDERR, (debug) ? TL_DEBUG : TL_INFO, NULL); TC_fillConnection(&cs, host, port); 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; 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; inumber; 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.physmem)/( 1024*1024.0 ), ctime( &(mu->units[i].stamp) ) ); } TC_FreeConnection(&cs); return 0; }