From 58c5d4eefdf1cdee0651f7c74ffd1501adbdc9c3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 8 Oct 2008 23:08:21 +0200 Subject: fsinfo-Befehl implementiert --- src/Client/CommandManager.cpp | 83 ++++++++++++++++++++++++++--- src/Client/CommandManager.h | 6 ++- src/Client/CommandParser.cpp | 21 +++++++- src/Client/CommandParser.h | 1 + src/Client/Requests/CoreStatusRequest.cpp | 42 --------------- src/Client/Requests/CoreStatusRequest.h | 43 --------------- src/Client/Requests/DaemonFSInfoRequest.cpp | 47 ++++++++++++++++ src/Client/Requests/DaemonFSInfoRequest.h | 48 +++++++++++++++++ src/Client/Requests/Makefile.am | 4 +- src/Client/Requests/Makefile.in | 10 ++-- 10 files changed, 204 insertions(+), 101 deletions(-) delete mode 100644 src/Client/Requests/CoreStatusRequest.cpp delete mode 100644 src/Client/Requests/CoreStatusRequest.h create mode 100644 src/Client/Requests/DaemonFSInfoRequest.cpp create mode 100644 src/Client/Requests/DaemonFSInfoRequest.h (limited to 'src/Client') diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 2a20b29..08a2780 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -20,9 +20,11 @@ #include "CommandManager.h" #include #include +#include #include #include +#include #include #include @@ -30,6 +32,48 @@ namespace Mad { namespace Client { +void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { + const std::string units[] = { + "kB", "MB", "GB", "TB", "" + }; + + const std::vector& fsList = packet.getFSInfo(); + + for(std::vector::const_iterator fs = fsList.begin(); fs != fsList.end(); ++fs) { + unsigned usedUnit = 0, totalUnit = 0; + + float used = fs->used; + float total = fs->total; + float available = fs->available; + + while(used >= 1024 && !units[usedUnit+1].empty()) { + ++usedUnit; + used /= 1024; + available /= 1024; + } + + while(total >= 1024 && !units[totalUnit+1].empty()) { + ++totalUnit; + total /= 1024; + } + + std::string nameString = fs->mountedOn + " (" + fs->fsName + ")"; + + if(nameString.length() < 32) { + nameString.resize(32, ' '); + } + else { + nameString += '\n'; + nameString.resize(nameString.length() + 32, ' '); + } + + std::printf("\t%s%.*f%s", nameString.c_str(), (used < 10) ? 2 : 1, used, (usedUnit == totalUnit) ? "" : (" " + units[usedUnit]).c_str()); + std::printf("/%.*f %s (%.1f%%)\n", (total < 10) ? 2 : 1, total, units[totalUnit].c_str(), std::min(100*used/(used+available), 100.0f)); + } + + std::printf("\n"); +} + void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packet) { if(packet.getUptime()) { unsigned long days = packet.getUptime()/86400; @@ -82,11 +126,10 @@ void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packe } } -void CommandManager::coreStatusRequestFinished(const Common::Request &request) { + +void CommandManager::daemonCommandRequestFinished(const Common::Request<> &request) { try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); - std::cout << "Server status:" << std::endl; - printHostStatus(packet); + request.getResult(); } catch(Common::Exception &exception) { Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); @@ -95,9 +138,11 @@ void CommandManager::coreStatusRequestFinished(const Common::Request &request) { +void CommandManager::daemonFSInfoRequestFinished(const Common::Request &request) { try { - request.getResult(); + const Net::Packets::FSInfoPacket &packet = request.getResult(); + std::cout << "Host file system usage:" << std::endl; + printFSInfo(packet); } catch(Common::Exception &exception) { Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); @@ -131,5 +176,31 @@ void CommandManager::disconnectRequestFinished(const Common::Request<> &request) requestFinished(); } +void CommandManager::fsInfoRequestFinished(const Common::Request &request) { + try { + const Net::Packets::FSInfoPacket &packet = request.getResult(); + std::cout << "Server file system usage:" << std::endl; + printFSInfo(packet); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +void CommandManager::statusRequestFinished(const Common::Request &request) { + try { + const Net::Packets::HostStatusPacket &packet = request.getResult(); + std::cout << "Server status:" << std::endl; + printHostStatus(packet); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + } } diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index f93d07f..1937c0f 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -26,6 +26,7 @@ namespace Mad { namespace Net { namespace Packets { +class FSInfoPacket; class HostStatusPacket; class HostListPacket; } @@ -49,12 +50,15 @@ class CommandManager { finished(); } + void printFSInfo(const Net::Packets::FSInfoPacket &packet); void printHostStatus(const Net::Packets::HostStatusPacket &packet); - void coreStatusRequestFinished(const Common::Request &request); void daemonCommandRequestFinished(const Common::Request<> &request); + void daemonFSInfoRequestFinished(const Common::Request &request); void daemonStatusRequestFinished(const Common::Request &request); void disconnectRequestFinished(const Common::Request<> &request); + void fsInfoRequestFinished(const Common::Request &request); + void statusRequestFinished(const Common::Request &request); public: CommandManager() : activeRequests(0), disconnect(false) {} diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 135f771..ab3dd50 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -19,13 +19,15 @@ #include "CommandParser.h" #include "InformationManager.h" -#include "Requests/CoreStatusRequest.h" +#include "Requests/DaemonFSInfoRequest.h" #include "Requests/DaemonCommandRequest.h" #include "Requests/DaemonStatusRequest.h" #include #include #include +#include #include +#include #include #include @@ -37,6 +39,7 @@ namespace Mad { namespace Client { const CommandParser::Command CommandParser::commands[] = { + {{"fsinfo", 0}, "fsinfo [host]", "Displays file system usage information", "Displays file system usage information of a host or the server (if no host is given).", &CommandParser::fsinfoCommand}, {{"help", "?", 0}, "help [command]", "Displays usage information about commands", "Displays usage information about a command. If no command is given, a list of all available commands is displayed.", &CommandParser::helpCommand}, {{"list_hosts", "hosts", 0}, "list_hosts [-a]", "Lists the currently active hosts", "Lists the currently active hosts.\n\n -a\tAlso list inactive hosts", &CommandParser::listHostsCommand}, {{"reboot", 0}, "reboot *|host...", "Reboots host", "Reboots hosts. * will reboot all hosts.", &CommandParser::rebootCommand}, @@ -102,6 +105,20 @@ std::map CommandParser::parseHostList(const std:: } +void CommandParser::fsinfoCommand(const std::vector &args) { + if(args.size() == 1) + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Common::Requests::FSInfoRequest(sigc::mem_fun(commandManager, &CommandManager::fsInfoRequestFinished)))); + else if(args.size() == 2) + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonFSInfoRequestFinished)))); + else { + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + printUsage("fsinfo"); + return; + } + + commandManager.activeRequests++; +} + void CommandParser::helpCommand(const std::vector &args) { if(args.size() == 1) { std::cout << "Available commands:" << std::endl << std::endl; @@ -221,7 +238,7 @@ void CommandParser::shutdownCommand(const std::vector &args) { void CommandParser::statusCommand(const std::vector &args) { if(args.size() == 1) - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Requests::CoreStatusRequest(sigc::mem_fun(commandManager, &CommandManager::coreStatusRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Common::Requests::StatusRequest(sigc::mem_fun(commandManager, &CommandManager::statusRequestFinished)))); else if(args.size() == 2) Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonStatusRequestFinished)))); else { diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index daca0b9..660f548 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -60,6 +60,7 @@ class CommandParser { std::map parseHostList(const std::vector &args, bool mustBeActive = false); + void fsinfoCommand(const std::vector &args); void helpCommand(const std::vector &args); void listHostsCommand(const std::vector &args); void rebootCommand(const std::vector &args); diff --git a/src/Client/Requests/CoreStatusRequest.cpp b/src/Client/Requests/CoreStatusRequest.cpp deleted file mode 100644 index 69e3ecf..0000000 --- a/src/Client/Requests/CoreStatusRequest.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * CoreStatusRequest.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "CoreStatusRequest.h" -#include - -namespace Mad { -namespace Client { -namespace Requests { - -void CoreStatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::STATUS, requestId)); -} - -void CoreStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { - finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); - return; // TODO Logging - } - - finish(Net::Packets::HostStatusPacket(packet)); -} - -} -} -} diff --git a/src/Client/Requests/CoreStatusRequest.h b/src/Client/Requests/CoreStatusRequest.h deleted file mode 100644 index f57f4bf..0000000 --- a/src/Client/Requests/CoreStatusRequest.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * CoreStatusRequest.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_CLIENT_REQUESTS_CORESTATUSREQUEST_H_ -#define MAD_CLIENT_REQUESTS_CORESTATUSREQUEST_H_ - -#include -#include - -namespace Mad { -namespace Client { -namespace Requests { - -class CoreStatusRequest : public Common::Request { - protected: - virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); - - public: - CoreStatusRequest(slot_type slot) : Common::Request(slot) {} -}; - -} -} -} - -#endif /* MAD_CLIENT_REQUESTS_CORESTATUSREQUEST_H_ */ diff --git a/src/Client/Requests/DaemonFSInfoRequest.cpp b/src/Client/Requests/DaemonFSInfoRequest.cpp new file mode 100644 index 0000000..eb26cf4 --- /dev/null +++ b/src/Client/Requests/DaemonFSInfoRequest.cpp @@ -0,0 +1,47 @@ +/* + * DaemonFSInfoRequest.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "DaemonFSInfoRequest.h" +#include +#include + +namespace Mad { +namespace Client { +namespace Requests { + +void DaemonFSInfoRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { + connection->send(Net::Packet(Net::Packet::DAEMON_FS_INFO, requestId, daemon.c_str(), daemon.length())); +} + +void DaemonFSInfoRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() == Net::Packet::ERROR) { + finishWithError(Net::Packets::ErrorPacket(packet).getException()); + return; + } + else if(packet.getType() != Net::Packet::OK) { + finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); + return; // TODO Logging + } + + finish(Net::Packets::FSInfoPacket(packet)); +} + +} +} +} diff --git a/src/Client/Requests/DaemonFSInfoRequest.h b/src/Client/Requests/DaemonFSInfoRequest.h new file mode 100644 index 0000000..d966b8d --- /dev/null +++ b/src/Client/Requests/DaemonFSInfoRequest.h @@ -0,0 +1,48 @@ +/* + * DaemonFSInfoRequest.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ +#define MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ + +#include +#include + +#include + +namespace Mad { +namespace Client { +namespace Requests { + +class DaemonFSInfoRequest : public Common::Request { + private: + std::string daemon; + + protected: + virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + DaemonFSInfoRequest(const std::string &daemon0, slot_type slot) : Common::Request(slot), daemon(daemon0) {} +}; + +} +} +} + +#endif /* MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ */ diff --git a/src/Client/Requests/Makefile.am b/src/Client/Requests/Makefile.am index 8c4f542..a96b265 100644 --- a/src/Client/Requests/Makefile.am +++ b/src/Client/Requests/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CoreStatusRequest.cpp DaemonCommandRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp +librequests_la_SOURCES = DaemonCommandRequest.cpp DaemonFSInfoRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CoreStatusRequest.h DaemonCommandRequest.h DaemonListRequest.h DaemonStatusRequest.h +noinst_HEADERS = DaemonCommandRequest.h DaemonFSInfoRequest.h DaemonListRequest.h DaemonStatusRequest.h diff --git a/src/Client/Requests/Makefile.in b/src/Client/Requests/Makefile.in index 9336dbb..a074523 100644 --- a/src/Client/Requests/Makefile.in +++ b/src/Client/Requests/Makefile.in @@ -48,8 +48,8 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequests_la_LIBADD = -am_librequests_la_OBJECTS = CoreStatusRequest.lo \ - DaemonCommandRequest.lo DaemonListRequest.lo \ +am_librequests_la_OBJECTS = DaemonCommandRequest.lo \ + DaemonFSInfoRequest.lo DaemonListRequest.lo \ DaemonStatusRequest.lo librequests_la_OBJECTS = $(am_librequests_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src @@ -189,8 +189,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CoreStatusRequest.cpp DaemonCommandRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CoreStatusRequest.h DaemonCommandRequest.h DaemonListRequest.h DaemonStatusRequest.h +librequests_la_SOURCES = DaemonCommandRequest.cpp DaemonFSInfoRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp +noinst_HEADERS = DaemonCommandRequest.h DaemonFSInfoRequest.h DaemonListRequest.h DaemonStatusRequest.h all: all-am .SUFFIXES: @@ -242,8 +242,8 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoreStatusRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonCommandRequest.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonFSInfoRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonListRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStatusRequest.Plo@am__quote@ -- cgit v1.2.3