diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-12 00:40:06 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-12 00:40:06 +0200 |
commit | b0ca04e1baf9a3405bfb50d7c3d95e8e425f4c3e (patch) | |
tree | adf91aa4677d508ffe3ad1a6500ec03e8bc8c11e /src | |
parent | 668437afdfb8d8a6bdaaaba6225177295e49982a (diff) | |
download | mad-b0ca04e1baf9a3405bfb50d7c3d95e8e425f4c3e.tar mad-b0ca04e1baf9a3405bfb50d7c3d95e8e425f4c3e.zip |
Serverstatus zeigt die Speicherauslastung an
Diffstat (limited to 'src')
-rw-r--r-- | src/Client/CommandParser.cpp | 52 | ||||
-rw-r--r-- | src/Client/Makefile.in | 2 | ||||
-rw-r--r-- | src/Client/Request/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/Backends/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/Backends/SystemBackendProc.cpp | 49 | ||||
-rw-r--r-- | src/Common/Backends/SystemBackendProc.h | 3 | ||||
-rw-r--r-- | src/Common/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/Request/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/SystemBackend.h | 22 | ||||
-rw-r--r-- | src/Core/Makefile.in | 2 | ||||
-rw-r--r-- | src/Core/RequestHandler/CoreStatusRequestHandler.h | 8 | ||||
-rw-r--r-- | src/Core/RequestHandler/Makefile.in | 2 | ||||
-rw-r--r-- | src/Makefile.in | 2 | ||||
-rw-r--r-- | src/Net/Makefile.in | 2 | ||||
-rw-r--r-- | src/Net/Packets/CoreStatusPacket.cpp | 10 | ||||
-rw-r--r-- | src/Net/Packets/CoreStatusPacket.h | 37 | ||||
-rw-r--r-- | src/Net/Packets/Makefile.in | 2 |
17 files changed, 164 insertions, 37 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 6f5f38b..47c06c3 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -22,6 +22,7 @@ #include <Common/Request/DisconnectRequest.h> #include <iostream> +#include <cstdio> namespace Mad { namespace Client { @@ -102,28 +103,51 @@ void CommandParser::coreStatusRequestFinished(const Net::Packets::CoreStatusPack unsigned long hours = (packet.getUptime()%86400)/3600; unsigned long minutes = (packet.getUptime()%3600)/60; - std::cout << "\tUptime: "; + std::printf("\tUptime:\t\t"); - if(days) std::cout << days << " days "; + if(days) std::printf("%lu days ", days); - std::cout << hours << ":"; + std::printf("%lu:%02lu", hours, minutes); - std::streamsize width = std::cout.width(2); - std::cout.fill('0'); - std::cout << minutes; + if(packet.getIdleTime()) + std::printf(" (load average: %.1f%%)", 100.0f-(packet.getIdleTime()*100.0f/packet.getUptime())); - std::cout.width(width); + std::printf("\n\n"); + } + + if(packet.getTotalMem() && packet.getFreeMem()) { + const std::string units[] = { + "kB", "MB", "GB", "TB", "" + }; + + unsigned unit = 0; + float totalMem = packet.getTotalMem(), usedMem = packet.getTotalMem()-packet.getFreeMem(); - if(packet.getIdleTime()) { - std::streamsize prec = std::cout.precision(3); - std::cout << " (load average: " << (100.0f-(packet.getIdleTime()*100.0f/packet.getUptime())) << "%)"; - std::cout.precision(prec); + while(totalMem >= 1024 && !units[unit+1].empty()) { + ++unit; + totalMem /= 1024; + usedMem /= 1024; } - std::cout << std::endl; - } + std::printf("\tMemory usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); + std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); + + if(packet.getTotalSwap() && packet.getFreeSwap()) { + unit = 0; + totalMem = packet.getTotalSwap(); usedMem = packet.getTotalSwap()-packet.getFreeSwap(); - std::cout << std::endl; + while(totalMem >= 1024 && !units[unit+1].empty()) { + ++unit; + totalMem /= 1024; + usedMem /= 1024; + } + + std::printf("\tSwap usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); + std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); + } + + std::printf("\n"); + } requestFinished(); } diff --git a/src/Client/Makefile.in b/src/Client/Makefile.in index 2263aff..423e2f3 100644 --- a/src/Client/Makefile.in +++ b/src/Client/Makefile.in @@ -129,6 +129,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Client/Request/Makefile.in b/src/Client/Request/Makefile.in index 804c5fb..1ac4924 100644 --- a/src/Client/Request/Makefile.in +++ b/src/Client/Request/Makefile.in @@ -100,6 +100,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Common/Backends/Makefile.in b/src/Common/Backends/Makefile.in index 431e582..3538513 100644 --- a/src/Common/Backends/Makefile.in +++ b/src/Common/Backends/Makefile.in @@ -119,6 +119,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp index d33fbf3..7e9fcbd 100644 --- a/src/Common/Backends/SystemBackendProc.cpp +++ b/src/Common/Backends/SystemBackendProc.cpp @@ -18,14 +18,18 @@ */ #include "SystemBackendProc.h" + #include <fstream> +#include <pcrecpp.h> + +#include <iostream> namespace Mad { namespace Common { namespace Backends { -SystemBackendProc::Uptime SystemBackendProc::getUptime() const { - Uptime uptime = {0, 0}; +SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() const { + UptimeInfo uptime = {0, 0}; std::ifstream file("/proc/uptime"); @@ -37,17 +41,54 @@ SystemBackendProc::Uptime SystemBackendProc::getUptime() const { if(!file.good()) return uptime; - uptime.uptime = (uint32_t)f; + uptime.uptime = (unsigned long)f; file >> f; if(!file.good()) return uptime; - uptime.idleTime = (uint32_t)f; + uptime.idleTime = (unsigned long)f; return uptime; } +SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() const { + MemoryInfo memInfo = {0, 0, 0, 0}; + + std::ifstream file("/proc/meminfo"); + + if(!file.good()) + return memInfo; + + pcrecpp::RE re("(.+):\\s*(\\d+).*"); + + while(!file.eof() && file.good()) { + std::string line; + std::getline(file, line); + + std::string name; + unsigned long value; + + if(!re.FullMatch(line, &name, &value)) + continue; + + if(name == "MemTotal") + memInfo.totalMem = value; + else if(name == "MemFree") + memInfo.freeMem = value; + else if(name == "SwapTotal") + memInfo.totalSwap = value; + else if(name == "SwapFree") + memInfo.freeSwap = value; + + if(memInfo.totalMem && memInfo.freeMem && memInfo.totalSwap && memInfo.freeSwap) + break; + } + + + return memInfo; +} + } } } diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h index af34eee..cdcc3e7 100644 --- a/src/Common/Backends/SystemBackendProc.h +++ b/src/Common/Backends/SystemBackendProc.h @@ -35,7 +35,8 @@ class SystemBackendProc : public SystemBackend { setBackend(new SystemBackendProc()); } - virtual Uptime getUptime() const; + virtual UptimeInfo getUptimeInfo() const; + virtual MemoryInfo getMemoryInfo() const; }; } diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 7c805e9..95a5c35 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -130,6 +130,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Common/Request/Makefile.in b/src/Common/Request/Makefile.in index 8d53cc0..6c3b68d 100644 --- a/src/Common/Request/Makefile.in +++ b/src/Common/Request/Makefile.in @@ -102,6 +102,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index f88ddd9..6929c8d 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -38,15 +38,27 @@ class SystemBackend { } public: - struct Uptime { - uint32_t uptime; - uint32_t idleTime; + struct UptimeInfo { + unsigned long uptime; + unsigned long idleTime; + }; + + struct MemoryInfo { + unsigned long totalMem; + unsigned long freeMem; + unsigned long totalSwap; + unsigned long freeSwap; }; virtual ~SystemBackend() {} - virtual Uptime getUptime() const { - Uptime ret = {0, 0}; + virtual UptimeInfo getUptimeInfo() const { + UptimeInfo ret = {0, 0}; + return ret; + } + + virtual MemoryInfo getMemoryInfo() const { + MemoryInfo ret = {0, 0, 0, 0}; return ret; } diff --git a/src/Core/Makefile.in b/src/Core/Makefile.in index 3590c7f..98a97e0 100644 --- a/src/Core/Makefile.in +++ b/src/Core/Makefile.in @@ -129,6 +129,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Core/RequestHandler/CoreStatusRequestHandler.h b/src/Core/RequestHandler/CoreStatusRequestHandler.h index eec0154..ec94bb5 100644 --- a/src/Core/RequestHandler/CoreStatusRequestHandler.h +++ b/src/Core/RequestHandler/CoreStatusRequestHandler.h @@ -44,9 +44,13 @@ class CoreStatusRequestHandler : public Common::RequestHandler { if(packet.getType() != Net::Packet::TYPE_CORE_STATUS) return false; // TODO Logging - Common::SystemBackend::Uptime uptime = Common::SystemBackend::getBackend()->getUptime(); + // TODO Require authentication - if(!connection->send(Net::Packets::CoreStatusPacket(Net::Packet::TYPE_OK, packet.getRequestId(), uptime.uptime, uptime.idleTime))) + Common::SystemBackend::UptimeInfo uptimeInfo = Common::SystemBackend::getBackend()->getUptimeInfo(); + Common::SystemBackend::MemoryInfo memInfo = Common::SystemBackend::getBackend()->getMemoryInfo(); + + if(!connection->send(Net::Packets::CoreStatusPacket(Net::Packet::TYPE_OK, packet.getRequestId(), uptimeInfo.uptime, uptimeInfo.idleTime, + memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap))) return false; setFinished(); diff --git a/src/Core/RequestHandler/Makefile.in b/src/Core/RequestHandler/Makefile.in index bea3870..2d1cbee 100644 --- a/src/Core/RequestHandler/Makefile.in +++ b/src/Core/RequestHandler/Makefile.in @@ -102,6 +102,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Makefile.in b/src/Makefile.in index bee6a56..f5314e5 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -138,6 +138,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Net/Makefile.in b/src/Net/Makefile.in index e813a71..3d9f7bf 100644 --- a/src/Net/Makefile.in +++ b/src/Net/Makefile.in @@ -130,6 +130,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ diff --git a/src/Net/Packets/CoreStatusPacket.cpp b/src/Net/Packets/CoreStatusPacket.cpp index 4397fa8..ad2a7fb 100644 --- a/src/Net/Packets/CoreStatusPacket.cpp +++ b/src/Net/Packets/CoreStatusPacket.cpp @@ -24,12 +24,20 @@ namespace Mad { namespace Net { namespace Packets { -CoreStatusPacket::CoreStatusPacket(Type type, uint16_t requestId, uint32_t uptime, uint32_t idleTime) : Packet(type, requestId) { +CoreStatusPacket::CoreStatusPacket(Type type, uint16_t requestId, uint32_t uptime, uint32_t idleTime, + uint32_t totalMem, uint32_t freeMem, uint32_t totalSwap, uint32_t freeSwap) +: Packet(type, requestId) +{ setLength(sizeof(CoreStatusData)); coreStatusData = (CoreStatusData*)&rawData->data; coreStatusData->uptime = htonl(uptime); coreStatusData->idleTime = htonl(idleTime); + + coreStatusData->totalMem = htonl(totalMem); + coreStatusData->freeMem = htonl(freeMem); + coreStatusData->totalSwap = htonl(totalSwap); + coreStatusData->freeSwap = htonl(freeSwap); } CoreStatusPacket& CoreStatusPacket::operator=(const Packet &p) { diff --git a/src/Net/Packets/CoreStatusPacket.h b/src/Net/Packets/CoreStatusPacket.h index 14b022e..a19f8e7 100644 --- a/src/Net/Packets/CoreStatusPacket.h +++ b/src/Net/Packets/CoreStatusPacket.h @@ -27,33 +27,34 @@ namespace Net { namespace Packets { class CoreStatusPacket : public Packet { + private: + CoreStatusPacket& operator=(const CoreStatusPacket &p); + CoreStatusPacket(const CoreStatusPacket &p); + protected: struct CoreStatusData { uint32_t uptime; uint32_t idleTime; + + uint32_t totalMem; + uint32_t freeMem; + uint32_t totalSwap; + uint32_t freeSwap; }; CoreStatusData *coreStatusData; public: - CoreStatusPacket(Type type, uint16_t requestId, uint32_t uptime = 0, uint32_t idleTime = 0); + CoreStatusPacket(Type type, uint16_t requestId, uint32_t uptime = 0, uint32_t idleTime = 0, + uint32_t totalMem = 0, uint32_t freeMem = 0, uint32_t totalSwap = 0, uint32_t freeSwap = 0); CoreStatusPacket(const Packet &p) : Packet(p) { setLength(sizeof(CoreStatusData)); coreStatusData = (CoreStatusData*)&rawData->data; } - CoreStatusPacket(const CoreStatusPacket &p) : Packet(p) { - setLength(sizeof(CoreStatusData)); - coreStatusData = (CoreStatusData*)&rawData->data; - } - CoreStatusPacket& operator=(const Packet &p); - CoreStatusPacket& operator=(const CoreStatusPacket &p) { - return (*this = (Packet)p); - } - uint32_t getUptime() const { return ntohl(coreStatusData->uptime); } @@ -61,6 +62,22 @@ class CoreStatusPacket : public Packet { uint32_t getIdleTime() const { return ntohl(coreStatusData->idleTime); } + + uint32_t getTotalMem() const { + return ntohl(coreStatusData->totalMem); + } + + uint32_t getFreeMem() const { + return ntohl(coreStatusData->freeMem); + } + + uint32_t getTotalSwap() const { + return ntohl(coreStatusData->totalSwap); + } + + uint32_t getFreeSwap() const { + return ntohl(coreStatusData->freeSwap); + } }; } diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index 8fa55b0..3af3051 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -119,6 +119,8 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PCRECPP_CFLAGS = @PCRECPP_CFLAGS@ +PCRECPP_LIBS = @PCRECPP_LIBS@ PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ |