добавил получение sysinfo и вывод аптайма в веб-морде

This commit is contained in:
Vladislav Ostapov 2025-01-09 12:02:42 +03:00
parent ab654a754c
commit 20cf08e2a1
5 changed files with 78 additions and 0 deletions

View File

@ -206,6 +206,8 @@ public:
rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)});
std::string result = R"({"mainState":)"; std::string result = R"({"mainState":)";
result += api->loadTerminalState(); result += api->loadTerminalState();
result += R"(,"sysinfo":)";
result += api->loadSysInfo();
result += "}"; result += "}";
rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
})); }));

View File

@ -8,6 +8,8 @@
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <sys/sysinfo.h>
typedef boost::property_tree::ptree::path_type json_path; typedef boost::property_tree::ptree::path_type json_path;
@ -1091,4 +1093,42 @@ void api_driver::ApiDriver::executeInApi(const std::function<void(TSID sid)>& ca
} }
} }
std::string api_driver::ApiDriver::loadSysInfo() {
std::stringstream result;
struct sysinfo info{};
sysinfo(&info);
struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* Swap space still available */
unsigned short procs; /* Number of current processes */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
};
result << "{\n\"uptime\":" << info.uptime;
result << ",\"load1min\":" << info.loads[0];
result << ",\"load5min\":" << info.loads[1];
result << ",\"load15min\":" << info.loads[2];
result << ",\"totalram\":" << info.totalram;
result << ",\"freeram\":" << info.freeram;
result << ",\"sharedram\":" << info.sharedram;
result << ",\"bufferram\":" << info.bufferram;
result << ",\"totalswap\":" << info.totalswap;
result << ",\"freeswap\":" << info.freeswap;
result << ",\"procs\":" << static_cast<long>(info.procs);
result << ",\"totalhigh\":" << info.totalhigh;
result << ",\"freehigh\":" << info.freehigh;
result << ",\"mem_unit\":" << info.mem_unit;
result << "\n}";
return result.str();
}
api_driver::ApiDriver::~ApiDriver() = default; api_driver::ApiDriver::~ApiDriver() = default;

View File

@ -70,6 +70,8 @@ namespace api_driver {
void executeInApi(const std::function<void(TSID sid)>& callback); void executeInApi(const std::function<void(TSID sid)>& callback);
static std::string loadSysInfo();
~ApiDriver(); ~ApiDriver();
private: private:

View File

@ -118,6 +118,7 @@
<tr><th>Температура ADRV</th><td>{{ stat_device.adrv }} °C</td></tr> <tr><th>Температура ADRV</th><td>{{ stat_device.adrv }} °C</td></tr>
<tr><th>Температура ZYNQ</th><td>{{ stat_device.zynq }} °C</td></tr> <tr><th>Температура ZYNQ</th><td>{{ stat_device.zynq }} °C</td></tr>
<tr><th>Температура FPGA</th><td>{{ stat_device.fpga }} °C</td></tr> <tr><th>Температура FPGA</th><td>{{ stat_device.fpga }} °C</td></tr>
<tr><th>Uptime</th><td>{{ stat_os.uptime }}</td></tr>
</tbody> </tbody>
</table> </table>
</div> </div>
@ -849,6 +850,7 @@
stat_device: { // температурные датчики stat_device: { // температурные датчики
adrv: 0, zynq: 0, fpga: 0 adrv: 0, zynq: 0, fpga: 0
}, },
stat_os: {uptime: '?'},
param: { param: {
general: { general: {
@ -1037,6 +1039,21 @@
this.stat_device.fpga = vals["mainState"]["device.fpga"] this.stat_device.fpga = vals["mainState"]["device.fpga"]
this.testState = vals["mainState"]["testState"] this.testState = vals["mainState"]["testState"]
// аптайм приходит в секундах, надо преобразовать его в человеко-читаемый вид
let uptime = vals["sysinfo"]["uptime"]
if (uptime) {
let secs = uptime % 60; uptime = Math.floor(uptime / 60)
let mins = uptime % 60; uptime = Math.floor(uptime / 60)
let hours = uptime % 24
uptime = Math.floor( uptime / 24)
let res = `${mins}m ${secs}s`
if (hours > 0) { res = `${hours}h ` + res }
if (uptime > 0) { res = `${uptime}d ` + res }
this.stat_os.uptime = res
} else {
this.stat_os.uptime = '?'
}
}, },
resetPacketsStatistics() { resetPacketsStatistics() {

View File

@ -105,6 +105,7 @@
<tr><th>Температура ADRV</th><td>{{ stat_device.adrv }} °C</td></tr> <tr><th>Температура ADRV</th><td>{{ stat_device.adrv }} °C</td></tr>
<tr><th>Температура ZYNQ</th><td>{{ stat_device.zynq }} °C</td></tr> <tr><th>Температура ZYNQ</th><td>{{ stat_device.zynq }} °C</td></tr>
<tr><th>Температура FPGA</th><td>{{ stat_device.fpga }} °C</td></tr> <tr><th>Температура FPGA</th><td>{{ stat_device.fpga }} °C</td></tr>
<tr><th>Uptime</th><td>{{ stat_os.uptime }}</td></tr>
</tbody> </tbody>
</table> </table>
</div> </div>
@ -641,6 +642,7 @@
stat_device: { // температурные датчики stat_device: { // температурные датчики
adrv: 0, zynq: 0, fpga: 0 adrv: 0, zynq: 0, fpga: 0
}, },
stat_os: {uptime: '?'},
param: { param: {
tx: { tx: {
@ -775,6 +777,21 @@
this.stat_device.fpga = vals["mainState"]["device.fpga"] this.stat_device.fpga = vals["mainState"]["device.fpga"]
this.testState = vals["mainState"]["testState"] this.testState = vals["mainState"]["testState"]
// аптайм приходит в секундах, надо преобразовать его в человеко-читаемый вид
let uptime = vals["sysinfo"]["uptime"]
if (uptime) {
let secs = uptime % 60; uptime = Math.floor(uptime / 60)
let mins = uptime % 60; uptime = Math.floor(uptime / 60)
let hours = uptime % 24
uptime = Math.floor( uptime / 24)
let res = `${mins}m ${secs}s`
if (hours > 0) { res = `${hours}h ` + res }
if (uptime > 0) { res = `${uptime}d ` + res }
this.stat_os.uptime = res
} else {
this.stat_os.uptime = '?'
}
}, },
resetPacketsStatistics() { resetPacketsStatistics() {