From aa5755d4b470ed8af2579a9274f8e746523ec457 Mon Sep 17 00:00:00 2001 From: Kris Moore Date: Thu, 21 Jan 2016 13:39:22 -0500 Subject: [PATCH] New API call to get cpupercentage stats REST Request: ------------------------------- PUT /sysadm/systeminfo { "action" : "cpupercentage" } REST Response: ------------------------------- { "args": { "cpupercentage": { "busytotal": "28", "cpu1": { "busy": "28" }, "cpu2": { "busy": "31" }, "cpu3": { "busy": "29" }, "cpu4": { "busy": "24" } } } } WebSocket Request: ------------------------------- { "args" : { "action" : "cpupercentage" }, "name" : "systeminfo", "id" : "fooid", "namespace" : "sysadm" } WebSocket Response: ------------------------------- { "args": { "cpupercentage": { "busytotal": "28", "cpu1": { "busy": "28" }, "cpu2": { "busy": "31" }, "cpu3": { "busy": "29" }, "cpu4": { "busy": "24" } } }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- src/library/sysadm-systeminfo.cpp | 48 ++++++++++++++++++++++++++++++- src/library/sysadm-systeminfo.h | 1 + src/server/WebBackend.cpp | 4 +++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/library/sysadm-systeminfo.cpp b/src/library/sysadm-systeminfo.cpp index 2dce509..33c5635 100644 --- a/src/library/sysadm-systeminfo.cpp +++ b/src/library/sysadm-systeminfo.cpp @@ -51,12 +51,58 @@ QJsonObject SysInfo::batteryInfo(){ retObject.insert("timeleft", "-1"); } + return retObject; +} +// KPM 1-21-2016 +// This needs to be looked at, I'm not 100% sure it is returning correct busy % +// We probably want to supply more info as well, such as user,nice,system,interrupt,idle +QJsonObject SysInfo::cpuPercentage() { + QJsonObject retObject; + QString tmp; + + //Calculate the percentage based on the kernel information directly - no extra utilities + QStringList result = General::RunCommand("sysctl -n kern.cp_times").split(" "); + static QStringList last = QStringList(); + if(last.isEmpty()){ + //need two ticks before it works properly + sleep(1); + last = result; + result = General::RunCommand("sysctl -n kern.cp_times").split(" "); + } + double tot = 0; + double roundtot; + int cpnum = 0; + for(int i=4; iinsert("batteryinfo", sysadm::SysInfo::batteryInfo()); } + if(act=="cpupercentage"){ + ok = true; + out->insert("cpupercentage", sysadm::SysInfo::cpuPercentage()); + } if(act=="cputemps"){ ok = true; out->insert("cputemps", sysadm::SysInfo::cpuTemps());