mirror of
https://github.com/meshtastic/firmware.git
synced 2025-09-04 02:34:14 +00:00

Some checks failed
CI / setup (check) (push) Has been cancelled
CI / setup (esp32) (push) Has been cancelled
CI / setup (esp32c3) (push) Has been cancelled
CI / setup (esp32c6) (push) Has been cancelled
CI / setup (esp32s3) (push) Has been cancelled
CI / setup (nrf52840) (push) Has been cancelled
CI / setup (rp2040) (push) Has been cancelled
CI / setup (rp2350) (push) Has been cancelled
CI / setup (stm32) (push) Has been cancelled
CI / version (push) Has been cancelled
CI / build-debian-src (push) Has been cancelled
CI / package-pio-deps-native-tft (push) Has been cancelled
CI / test-native (push) Has been cancelled
CI / docker-deb-amd64 (push) Has been cancelled
CI / docker-deb-amd64-tft (push) Has been cancelled
CI / docker-alp-amd64 (push) Has been cancelled
CI / docker-alp-amd64-tft (push) Has been cancelled
CI / docker-deb-arm64 (push) Has been cancelled
CI / docker-deb-armv7 (push) Has been cancelled
CI / check (push) Has been cancelled
CI / build-esp32 (push) Has been cancelled
CI / build-esp32s3 (push) Has been cancelled
CI / build-esp32c3 (push) Has been cancelled
CI / build-esp32c6 (push) Has been cancelled
CI / build-nrf52840 (push) Has been cancelled
CI / build-rp2040 (push) Has been cancelled
CI / build-rp2350 (push) Has been cancelled
CI / build-stm32 (push) Has been cancelled
CI / gather-artifacts (esp32) (push) Has been cancelled
CI / gather-artifacts (esp32c3) (push) Has been cancelled
CI / gather-artifacts (esp32c6) (push) Has been cancelled
CI / gather-artifacts (esp32s3) (push) Has been cancelled
CI / gather-artifacts (nrf52840) (push) Has been cancelled
CI / gather-artifacts (rp2040) (push) Has been cancelled
CI / gather-artifacts (rp2350) (push) Has been cancelled
CI / gather-artifacts (stm32) (push) Has been cancelled
CI / release-artifacts (push) Has been cancelled
CI / release-firmware (esp32) (push) Has been cancelled
CI / release-firmware (esp32c3) (push) Has been cancelled
CI / release-firmware (esp32c6) (push) Has been cancelled
CI / release-firmware (esp32s3) (push) Has been cancelled
CI / release-firmware (nrf52840) (push) Has been cancelled
CI / release-firmware (rp2040) (push) Has been cancelled
CI / release-firmware (rp2350) (push) Has been cancelled
CI / release-firmware (stm32) (push) Has been cancelled
CI / publish-firmware (push) Has been cancelled
* treewide: make 'ifdef DEBUG_PORT' guards also take into account DEBUG_MUTE * stm32wl: Add a guard against having debug prints turned on without PIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF defined --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
144 lines
5.7 KiB
C++
144 lines
5.7 KiB
C++
#include "HostMetrics.h"
|
|
#include "../mesh/generated/meshtastic/telemetry.pb.h"
|
|
#include "MeshService.h"
|
|
#if ARCH_PORTDUINO
|
|
#include "PortduinoGlue.h"
|
|
#include <filesystem>
|
|
#endif
|
|
|
|
int32_t HostMetricsModule::runOnce()
|
|
{
|
|
#if ARCH_PORTDUINO
|
|
if (settingsMap[hostMetrics_interval] == 0) {
|
|
return disable();
|
|
} else {
|
|
sendMetrics();
|
|
return 60 * 1000 * settingsMap[hostMetrics_interval];
|
|
}
|
|
#else
|
|
return disable();
|
|
#endif
|
|
}
|
|
|
|
bool HostMetricsModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_Telemetry *t)
|
|
{
|
|
// Don't worry about storing telemetry in NodeDB if we're a repeater
|
|
if (config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER)
|
|
return false;
|
|
|
|
if (t->which_variant == meshtastic_Telemetry_host_metrics_tag) {
|
|
#if defined(DEBUG_PORT) && !defined(DEBUG_MUTE)
|
|
const char *sender = getSenderShortName(mp);
|
|
if (t->variant.host_metrics.has_user_string)
|
|
t->variant.host_metrics.user_string[sizeof(t->variant.host_metrics.user_string) - 1] = '\0';
|
|
|
|
LOG_INFO("(Received Host Metrics from %s): uptime=%u, diskfree=%lu, memory free=%lu, load=%04.2f, %04.2f, %04.2f", sender,
|
|
t->variant.host_metrics.uptime_seconds, t->variant.host_metrics.diskfree1_bytes,
|
|
t->variant.host_metrics.freemem_bytes, static_cast<float>(t->variant.host_metrics.load1) / 100,
|
|
static_cast<float>(t->variant.host_metrics.load5) / 100,
|
|
static_cast<float>(t->variant.host_metrics.load15) / 100);
|
|
// t->variant.host_metrics.has_user_string ? t->variant.host_metrics.user_string : "");
|
|
#endif
|
|
}
|
|
return false; // Let others look at this message also if they want
|
|
}
|
|
|
|
/*
|
|
meshtastic_MeshPacket *HostMetricsModule::allocReply()
|
|
{
|
|
if (currentRequest) {
|
|
auto req = *currentRequest;
|
|
const auto &p = req.decoded;
|
|
meshtastic_Telemetry scratch;
|
|
meshtastic_Telemetry *decoded = NULL;
|
|
memset(&scratch, 0, sizeof(scratch));
|
|
if (pb_decode_from_bytes(p.payload.bytes, p.payload.size, &meshtastic_HostMetrics_msg, &scratch)) {
|
|
decoded = &scratch;
|
|
} else {
|
|
LOG_ERROR("Error decoding HostMetrics module!");
|
|
return NULL;
|
|
}
|
|
// Check for a request for device metrics
|
|
if (decoded->which_variant == meshtastic_Telemetry_host_metrics_tag) {
|
|
LOG_INFO("Device telemetry reply to request");
|
|
return allocDataProtobuf(getHostMetrics());
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
*/
|
|
|
|
#if ARCH_PORTDUINO
|
|
meshtastic_Telemetry HostMetricsModule::getHostMetrics()
|
|
{
|
|
std::string file_line;
|
|
meshtastic_Telemetry t = meshtastic_Telemetry_init_zero;
|
|
t.which_variant = meshtastic_Telemetry_host_metrics_tag;
|
|
t.variant.host_metrics = meshtastic_HostMetrics_init_zero;
|
|
|
|
if (access("/proc/uptime", R_OK) == 0) {
|
|
std::ifstream proc_uptime("/proc/uptime");
|
|
if (proc_uptime.is_open()) {
|
|
std::getline(proc_uptime, file_line, ' ');
|
|
proc_uptime.close();
|
|
t.variant.host_metrics.uptime_seconds = stoul(file_line);
|
|
}
|
|
}
|
|
|
|
std::filesystem::space_info root = std::filesystem::space("/");
|
|
t.variant.host_metrics.diskfree1_bytes = root.available;
|
|
|
|
if (access("/proc/meminfo", R_OK) == 0) {
|
|
std::ifstream proc_meminfo("/proc/meminfo");
|
|
if (proc_meminfo.is_open()) {
|
|
do {
|
|
std::getline(proc_meminfo, file_line);
|
|
} while (file_line.find("MemAvailable") == std::string::npos);
|
|
proc_meminfo.close();
|
|
t.variant.host_metrics.freemem_bytes = stoull(file_line.substr(file_line.find_first_of("0123456789"))) * 1024;
|
|
}
|
|
}
|
|
if (access("/proc/loadavg", R_OK) == 0) {
|
|
std::ifstream proc_loadavg("/proc/loadavg");
|
|
if (proc_loadavg.is_open()) {
|
|
std::getline(proc_loadavg, file_line, ' ');
|
|
t.variant.host_metrics.load1 = stof(file_line) * 100;
|
|
std::getline(proc_loadavg, file_line, ' ');
|
|
t.variant.host_metrics.load5 = stof(file_line) * 100;
|
|
std::getline(proc_loadavg, file_line, ' ');
|
|
t.variant.host_metrics.load15 = stof(file_line) * 100;
|
|
proc_loadavg.close();
|
|
}
|
|
}
|
|
if (settingsStrings[hostMetrics_user_command] != "") {
|
|
std::string userCommandResult = exec(settingsStrings[hostMetrics_user_command].c_str());
|
|
if (userCommandResult.length() > 1) {
|
|
strncpy(t.variant.host_metrics.user_string, userCommandResult.c_str(), sizeof(t.variant.host_metrics.user_string));
|
|
t.variant.host_metrics.user_string[sizeof(t.variant.host_metrics.user_string) - 1] = '\0';
|
|
t.variant.host_metrics.has_user_string = true;
|
|
}
|
|
}
|
|
return t;
|
|
}
|
|
|
|
bool HostMetricsModule::sendMetrics()
|
|
{
|
|
meshtastic_Telemetry telemetry = getHostMetrics();
|
|
LOG_INFO("Send: uptime=%u, diskfree=%lu, memory free=%lu, load=%04.2f, %04.2f, %04.2f",
|
|
telemetry.variant.host_metrics.uptime_seconds, telemetry.variant.host_metrics.diskfree1_bytes,
|
|
telemetry.variant.host_metrics.freemem_bytes, static_cast<float>(telemetry.variant.host_metrics.load1) / 100,
|
|
static_cast<float>(telemetry.variant.host_metrics.load5) / 100,
|
|
static_cast<float>(telemetry.variant.host_metrics.load15) / 100);
|
|
// telemetry.variant.host_metrics.has_user_string ? telemetry.variant.host_metrics.user_string : "");
|
|
|
|
meshtastic_MeshPacket *p = allocDataProtobuf(telemetry);
|
|
p->to = NODENUM_BROADCAST;
|
|
p->decoded.want_response = false;
|
|
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;
|
|
p->channel = settingsMap[hostMetrics_channel];
|
|
LOG_INFO("Send packet to mesh");
|
|
service->sendToMesh(p, RX_SRC_LOCAL, true);
|
|
return true;
|
|
}
|
|
#endif
|