📄 kfsstats_main.cc
字号:
//---------------------------------------------------------- -*- Mode: C++ -*-// $Id: kfsstats_main.cc 106 2008-07-29 06:43:16Z sriramsrao $ //// Created 2006/07/20// Author: Sriram Rao//// Copyright 2008 Quantcast Corp.// Copyright 2006-2008 Kosmix Corp.//// This file is part of Kosmos File System (KFS).//// Licensed under the Apache License, Version 2.0// (the "License"); you may not use this file except in compliance with// the License. You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or// implied. See the License for the specific language governing// permissions and limitations under the License.//// \brief Get the stats from chunk/meta servers. Run in a loop until// the user hits Ctrl-C. Like iostat, results are refreshed every N secs//----------------------------------------------------------------------------extern "C" {#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>};#include <iostream>#include <string>using std::string;using std::cout;using std::endl;#include "libkfsIO/TcpSocket.h"#include "common/log.h"#include "MonUtils.h"using namespace KFS;using namespace KFS_MON;static voidStatsMetaServer(const ServerLocation &location, bool rpcStats, int numSecs);voidBasicStatsMetaServer(TcpSocket &metaServerSock, int numSecs);voidRpcStatsMetaServer(TcpSocket &metaServerSock, int numSecs);static voidStatsChunkServer(const ServerLocation &location, bool rpcStats, int numSecs);static voidBasicStatsChunkServer(TcpSocket &chunkServerSock, int numSecs);static voidRpcStatsChunkServer(TcpSocket &chunkServerSock, int numSecs);static voidPrintChunkBasicStatsHeader();static voidPrintMetaBasicStatsHeader();int main(int argc, char **argv){ char optchar; bool help = false, meta = false, chunk = false; bool rpcStats = false, verboseLogging = false; const char *server = NULL; int port = -1, numSecs = 10; KFS::MsgLogger::Init(NULL); while ((optchar = getopt(argc, argv, "hcmn:p:s:tv")) != -1) { switch (optchar) { case 'm': meta = true; break; case 'c': chunk = true; break; case 's': server = optarg; break; case 'p': port = atoi(optarg); break; case 'n': numSecs = atoi(optarg); break; case 't': rpcStats = true; break; case 'h': help = true; break; case 'v': verboseLogging = true; break; default: KFS_LOG_VA_ERROR("Unrecognized flag %c", optchar); help = true; break; } } help = help || (!meta && !chunk); if (help || (server == NULL) || (port < 0)) { cout << "Usage: " << argv[0] << " [-m|-c] -s <server name> -p <port>" << " [-n <secs>] [-t] {-v}" << endl; cout << "Use -m for metaserver and -c for chunk server" << endl; cout << "Use -t for RPC stats" << endl; exit(-1); } if (verboseLogging) { KFS::MsgLogger::SetLevel(log4cpp::Priority::DEBUG); } else { KFS::MsgLogger::SetLevel(log4cpp::Priority::INFO); } ServerLocation location(server, port); if (meta) StatsMetaServer(location, rpcStats, numSecs); else if (chunk) StatsChunkServer(location, rpcStats, numSecs);}static voidPrintRpcStat(const string &statName, Properties &prop){ // cout << statName << " = " << prop.getValue(statName, (long // long) 0) << endl; cout << statName << " = " << prop.getValue(statName, "0") << endl;}voidStatsMetaServer(const ServerLocation &location, bool rpcStats, int numSecs){ TcpSocket metaServerSock; if (metaServerSock.Connect(location) < 0) { KFS_LOG_VA_ERROR("Unable to connect to %s", location.ToString().c_str()); exit(0); } if (rpcStats) { RpcStatsMetaServer(metaServerSock, numSecs); } else { BasicStatsMetaServer(metaServerSock, numSecs); } metaServerSock.Close();}voidRpcStatsMetaServer(TcpSocket &metaServerSock, int numSecs){ int numIO; int cmdSeqNum = 1; while (1) { MetaStatsOp op(cmdSeqNum); ++cmdSeqNum; numIO = DoOpCommon(&op, &metaServerSock); if (numIO < 0) { KFS_LOG_ERROR("Server isn't responding to stats"); exit(0); } PrintRpcStat("Get alloc", op.stats); PrintRpcStat("Get layout", op.stats); PrintRpcStat("Lookup", op.stats); PrintRpcStat("Lookup Path", op.stats); PrintRpcStat("Allocate", op.stats); PrintRpcStat("Truncate", op.stats); PrintRpcStat("Create", op.stats); PrintRpcStat("Remove", op.stats); PrintRpcStat("Rename", op.stats); PrintRpcStat("Mkdir", op.stats); PrintRpcStat("Rmdir", op.stats); PrintRpcStat("Lease Acquire", op.stats); PrintRpcStat("Lease Renew", op.stats); PrintRpcStat("Lease Cleanup", op.stats); PrintRpcStat("Chunkserver Hello", op.stats); PrintRpcStat("Chunkserver Bye", op.stats); PrintRpcStat("Replication Checker", op.stats); PrintRpcStat("Num Replications Todo", op.stats); PrintRpcStat("Num Ongoing Replications", op.stats); PrintRpcStat("Num Failed Replications", op.stats); PrintRpcStat("Total Num Replications", op.stats); PrintRpcStat("Num Stale Chunks", op.stats); cout << "----------------------------------" << endl; if (numSecs == 0) break; sleep(numSecs); }}voidBasicStatsMetaServer(TcpSocket &metaServerSock, int numSecs){ int numIO; int cmdSeqNum = 1; PrintMetaBasicStatsHeader(); while (1) { MetaStatsOp op(cmdSeqNum); ++cmdSeqNum; numIO = DoOpCommon(&op, &metaServerSock); if (numIO < 0) { KFS_LOG_ERROR("Server isn't responding to stats"); exit(0); } // useful things to have: # of connections handled if (cmdSeqNum % 10 == 0) PrintMetaBasicStatsHeader(); cout << op.stats.getValue("Open network fds", (long long) 0) << '\t'; cout << op.stats.getValue("Bytes read from network", (long long) 0) << '\t'; cout << op.stats.getValue("Bytes written to network", (long long) 0) << endl; if (numSecs == 0) break; sleep(numSecs); }}static voidPrintMetaBasicStatsHeader(){ cout << "Net Fds" << '\t' << "N/w Bytes In" << '\t' << "N/w Bytes Out" << endl;}voidStatsChunkServer(const ServerLocation &location, bool rpcStats, int numSecs){ TcpSocket chunkServerSock; if (chunkServerSock.Connect(location) < 0) { KFS_LOG_VA_ERROR("Unable to connect to %s", location.ToString().c_str()); exit(0); } if (rpcStats) { RpcStatsChunkServer(chunkServerSock, numSecs); } else { BasicStatsChunkServer(chunkServerSock, numSecs); } chunkServerSock.Close();}voidRpcStatsChunkServer(TcpSocket &chunkServerSock, int numSecs){ int numIO; int cmdSeqNum = 1; while (1) { ChunkStatsOp op(cmdSeqNum); ++cmdSeqNum; numIO = DoOpCommon(&op, &chunkServerSock); if (numIO < 0) { KFS_LOG_ERROR("Server isn't responding to stats"); exit(0); } PrintRpcStat("Alloc", op.stats); PrintRpcStat("Size", op.stats); PrintRpcStat("Open", op.stats); PrintRpcStat("Read", op.stats); PrintRpcStat("Write (AIO)", op.stats); PrintRpcStat("Write Prepare", op.stats); PrintRpcStat("Write Sync", op.stats); PrintRpcStat("Write Duration", op.stats); PrintRpcStat("Write Master", op.stats); PrintRpcStat("Delete", op.stats); PrintRpcStat("Truncate", op.stats); PrintRpcStat("Heartbeat", op.stats); PrintRpcStat("Change Chunk Vers", op.stats); PrintRpcStat("Num aios", op.stats); PrintRpcStat("Num ops", op.stats); cout << "----------------------------------" << endl; if (numSecs == 0) break; sleep(numSecs); }}voidBasicStatsChunkServer(TcpSocket &chunkServerSock, int numSecs){ int numIO; int cmdSeqNum = 1; PrintChunkBasicStatsHeader(); while (1) { ChunkStatsOp op(cmdSeqNum); ++cmdSeqNum; numIO = DoOpCommon(&op, &chunkServerSock); if (numIO < 0) { KFS_LOG_ERROR("Server isn't responding to stats"); exit(0); } if (cmdSeqNum % 10 == 0) PrintChunkBasicStatsHeader(); cout << op.stats.getValue("Open network fds", (long long) 0) << '\t'; cout << op.stats.getValue("Bytes read from network", (long long) 0) << '\t'; cout << op.stats.getValue("Bytes written to network", (long long) 0) << '\t'; cout << op.stats.getValue("Open disk fds", (long long) 0) << '\t'; cout << op.stats.getValue("Bytes read from disk", (long long) 0) << '\t'; cout << op.stats.getValue("Bytes written to disk", (long long) 0) << endl; if (numSecs == 0) break; sleep(numSecs); }}static voidPrintChunkBasicStatsHeader(){ cout << "Net Fds" << '\t' << "N/w Bytes In" << '\t' << "N/w Bytes Out" << '\t' << "Disk Fds" << '\t' << "Disk Bytes In" << '\t' << "Disk Bytes Out" << endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -