From 89551ddfc2879530981eba3db9ab857b88409ad8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 20 Sep 2008 11:59:36 +0200 Subject: Lese /proc/loadavg f?r weitere Statusinformationen aus --- src/Common/Backends/SystemBackendProc.cpp | 20 ++++++++++++++++++++ src/Common/Backends/SystemBackendProc.h | 4 +++- src/Common/RequestHandlers/StatusRequestHandler.cpp | 3 ++- src/Common/SystemBackend.h | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) (limited to 'src/Common') diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp index f2d9152..51e14f8 100644 --- a/src/Common/Backends/SystemBackendProc.cpp +++ b/src/Common/Backends/SystemBackendProc.cpp @@ -20,6 +20,7 @@ #include "SystemBackendProc.h" #include +#include namespace Mad { namespace Common { @@ -86,6 +87,25 @@ SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() { return memInfo; } +SystemBackend::LoadInfo SystemBackendProc::getLoadInfo() { + LoadInfo loadInfo = {0, 0, 0, 0, 0}; + + loadFile.seekg(0, std::ios::beg); + + if(!loadFile.good()) + return loadInfo; + + std::string line; + std::getline(loadFile, line); + + if(line.empty()) + return loadInfo; + + std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadInfo.loadAvg1, &loadInfo.loadAvg5, &loadInfo.loadAvg15, &loadInfo.currentLoad, &loadInfo.nProcesses); + + return loadInfo; +} + } } } diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h index fb9ecf5..8caa080 100644 --- a/src/Common/Backends/SystemBackendProc.h +++ b/src/Common/Backends/SystemBackendProc.h @@ -32,8 +32,9 @@ class SystemBackendProc : public SystemBackend { private: std::ifstream uptimeFile; std::ifstream meminfoFile; + std::ifstream loadFile; - SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo") {} + SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} public: static void useBackend() { @@ -42,6 +43,7 @@ class SystemBackendProc : public SystemBackend { virtual UptimeInfo getUptimeInfo(); virtual MemoryInfo getMemoryInfo(); + virtual LoadInfo getLoadInfo(); }; } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index a3f8675..6280ab3 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -41,9 +41,10 @@ void StatusRequestHandler::handlePacket(Net::Connection *connection, const Net:: SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getBackend()->getUptimeInfo(); SystemBackend::MemoryInfo memInfo = SystemBackend::getBackend()->getMemoryInfo(); + SystemBackend::LoadInfo loadInfo = SystemBackend::getBackend()->getLoadInfo(); connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, packet.getRequestId(), uptimeInfo.uptime, uptimeInfo.idleTime, - memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap)); + memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap, loadInfo.currentLoad, loadInfo.nProcesses, loadInfo.loadAvg1, loadInfo.loadAvg5, loadInfo.loadAvg15)); signalFinished().emit(); } diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index 9157e07..8ba1e7d 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -21,6 +21,7 @@ #define MAD_COMMON_SYSTEMBACKEND_H_ #include +#include #include namespace Mad { @@ -50,6 +51,14 @@ class SystemBackend { unsigned long freeSwap; }; + struct LoadInfo { + unsigned long currentLoad; + unsigned long nProcesses; + float loadAvg1; + float loadAvg5; + float loadAvg15; + }; + virtual ~SystemBackend() {} virtual UptimeInfo getUptimeInfo() { @@ -62,6 +71,11 @@ class SystemBackend { return ret; } + virtual LoadInfo getLoadInfo() { + LoadInfo ret = {0, 0, 0, 0, 0}; + return ret; + } + static SystemBackend *getBackend() { return backend.get(); } -- cgit v1.2.3