RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
SystemHealthComponent.h
Go to the documentation of this file.
1#pragma once
2#include <spawn.h>
3#include <unistd.h>
4#include <sys/wait.h>
5#include <mutex>
6#include "../Common.h"
7#include "../Data/SystemMemoryInformation.h"
8#include "BaseComponent.h"
9
10namespace RumPi
11{
20 {
21 protected:
23 std::map<std::string, std::string> myCommandData;
24 mutable std::mutex myCommandDataMutex;
25
26 private:
27 //We need to control access to this function since it sends a system call directly to the OS.
43 std::string ExecuteCommand(const std::string& command);
44
60 std::string GetThrottledCodeDescription(const std::string& code) const;
61
77 std::string GetCommandOutput(const std::string& command) const;
78
79 public:
91
103 virtual ~SystemHealthComponent();
104
115 virtual void TurnOn() override;
116
127 virtual void TurnOff() override;
128
140 virtual void ProcessRawValues() override;
141
153 virtual void CheckForAlerts();
154
168 virtual std::string ToString() const override;
169
182 float GetSystemTemperatureInC() const;
183
196 double GetFiveVoltPowerVoltage() const;
197
210 std::string GetThrottledCodeAndDescription() const;
211
224 std::string GetFirmwareVersion() const;
225
238 std::string GetLocalIP() const;
239
252 float GetWiFiSignalStrength() const;
253
266 bool GetIsRootFileSystemWritable() const;
267
280 std::string GetSystemStorageDevicePath() const;
281
294 std::string GetSystemStorageTotalSize() const;
295
308 std::string GetSystemStorageUsed() const;
309
322 std::string GetSystemStorageAvailable() const;
323
336 float GetSystemStorageUsedPercent() const;
337
352 };
353} // namespace RumPi
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
Reads Raspberry Pi system health (temperature, voltage, throttling, memory, storage,...
Definition: SystemHealthComponent.h:20
std::string GetThrottledCodeDescription(const std::string &code) const
Parses a vcgencmd throttled code and describes it, decoding each active/past error bit (under-voltage...
Definition: SystemHealthComponent.cpp:450
std::string GetLocalIP() const
Gets the local IP address from the cached hostname -I output.
Definition: SystemHealthComponent.cpp:630
float GetSystemTemperatureInC() const
Parses the system temperature (in Celsius) from the cached vcgencmd measure_temp output.
Definition: SystemHealthComponent.cpp:528
virtual void CheckForAlerts()
Hands each health reading and its thresholds to the AlertManager (temperature, 5V low/high,...
Definition: SystemHealthComponent.cpp:314
SystemMemoryInformation mySystemMemoryInfo
The most recently read system memory information.
Definition: SystemHealthComponent.h:22
float GetWiFiSignalStrength() const
Parses the WiFi signal strength (in dBm) from the cached iw dev wlan0 link output.
Definition: SystemHealthComponent.cpp:647
virtual std::string ToString() const override
Serializes every cached health reading (temperature, voltage, throttling, memory, storage,...
Definition: SystemHealthComponent.cpp:909
SystemMemoryInformation GetSystemMemoryInformation() const
Reads /proc/meminfo and builds a SystemMemoryInformation from the MemTotal/MemFree/MemAvailable/SwapT...
Definition: SystemHealthComponent.cpp:855
std::map< std::string, std::string > myCommandData
Raw output of each health command, keyed by command.
Definition: SystemHealthComponent.h:23
std::mutex myCommandDataMutex
Guards myCommandData: ProcessRawValues (writer) vs the getters/ToString (readers).
Definition: SystemHealthComponent.h:24
float GetSystemStorageUsedPercent() const
Parses the root file system's used-percentage (from the df output) as a float.
Definition: SystemHealthComponent.cpp:819
std::string GetThrottledCodeAndDescription() const
Gets the throttled-status code (from the cached vcgencmd output) with its human-readable description.
Definition: SystemHealthComponent.cpp:430
std::string GetFirmwareVersion() const
Gets the firmware version from the cached vcgencmd version output.
Definition: SystemHealthComponent.cpp:613
double GetFiveVoltPowerVoltage() const
Parses the external 5V rail voltage from the cached vcgencmd pmic_read_adc output.
Definition: SystemHealthComponent.cpp:570
virtual void TurnOff() override
Turns the component off, reporting the call through the AlertManager.
Definition: SystemHealthComponent.cpp:215
SystemHealthComponent()
Builds the system-health component, seeding its command map with every OS command it will run.
Definition: SystemHealthComponent.cpp:21
bool GetIsRootFileSystemWritable() const
Determines whether the root ("/") file system is mounted writable by scanning /proc/mounts for its op...
Definition: SystemHealthComponent.cpp:695
std::string GetSystemStorageDevicePath() const
Gets the root file system's device path (parsed from the df output).
Definition: SystemHealthComponent.cpp:751
std::string GetCommandOutput(const std::string &command) const
Thread-safe read of one command's cached output: copies the value out under the command-data lock so ...
Definition: SystemHealthComponent.cpp:403
virtual void TurnOn() override
Turns the component on, reporting the call through the AlertManager.
Definition: SystemHealthComponent.cpp:200
std::string GetSystemStorageTotalSize() const
Gets the root file system's total size (parsed from the df output).
Definition: SystemHealthComponent.cpp:768
std::string GetSystemStorageUsed() const
Gets the root file system's used size (parsed from the df output).
Definition: SystemHealthComponent.cpp:785
virtual ~SystemHealthComponent()
Destroys the component, turning it off first.
Definition: SystemHealthComponent.cpp:53
virtual void ProcessRawValues() override
Runs every health command, caches the results, parses the storage figures out of the df output,...
Definition: SystemHealthComponent.cpp:231
std::string GetSystemStorageAvailable() const
Gets the root file system's available size (parsed from the df output).
Definition: SystemHealthComponent.cpp:802
std::string ExecuteCommand(const std::string &command)
Runs an OS command by tokenizing it, spawning the process with posix_spawnp with its stdout redirecte...
Definition: SystemHealthComponent.cpp:73
Data class that contains information about the system's memory and swap space, in Kilobytes.
Definition: SystemMemoryInformation.h:13
RumPi is the library's top-level facade.
Definition: AlertManager.h:6