summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/Backends/SystemBackendProc.cpp20
-rw-r--r--src/Common/Backends/SystemBackendProc.h4
-rw-r--r--src/Common/RequestHandlers/StatusRequestHandler.cpp3
-rw-r--r--src/Common/SystemBackend.h14
4 files changed, 39 insertions, 2 deletions
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 <pcrecpp.h>
+#include <cstdio>
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 <stdint.h>
+#include <string>
#include <memory>
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();
}