blob: eff0519e2cb681a4bfca97c05a054d91aca44a47 [file] [log] [blame]
Vijay Khemkae2795302020-07-15 17:28:45 -07001#include <nlohmann/json.hpp>
2#include <sdbusplus/bus.hpp>
3#include <sdeventplus/event.hpp>
4#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
5#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
6#include <xyz/openbmc_project/Sensor/Value/server.hpp>
7
8#include <map>
9#include <string>
10
11namespace phosphor
12{
13namespace health
14{
15
16using Json = nlohmann::json;
17using ValueIface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
18
19using CriticalInterface =
20 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
21
22using WarningInterface =
23 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning;
24
25using healthIfaces =
26 sdbusplus::server::object::object<ValueIface, CriticalInterface,
27 WarningInterface>;
28
29class HealthSensor : public healthIfaces
30{
31 public:
32 HealthSensor() = delete;
33 HealthSensor(const HealthSensor&) = delete;
34 HealthSensor& operator=(const HealthSensor&) = delete;
35 HealthSensor(HealthSensor&&) = delete;
36 HealthSensor& operator=(HealthSensor&&) = delete;
37 virtual ~HealthSensor() = default;
38
39 /** @brief Constructs HealthSensor
40 *
41 * @param[in] bus - Handle to system dbus
42 * @param[in] objPath - The Dbus path of health sensor
43 */
44 HealthSensor(sdbusplus::bus::bus& bus, const char* objPath) :
45 healthIfaces(bus, objPath), bus(bus)
46 {}
47
48 /** @brief Set sensor value utilization to health sensor D-bus */
49 void setSensorValueToDbus(const uint8_t value);
50 /** @brief Set Sensor Threshold to D-bus at beginning */
51 void setSensorThreshold(uint8_t criticalHigh, uint8_t warningHigh);
52
53 private:
54 sdbusplus::bus::bus& bus;
55};
56
57class HealthMon
58{
59 public:
60 HealthMon() = delete;
61 HealthMon(const HealthMon&) = delete;
62 HealthMon& operator=(const HealthMon&) = delete;
63 HealthMon(HealthMon&&) = delete;
64 HealthMon& operator=(HealthMon&&) = delete;
65 virtual ~HealthMon() = default;
66
67 /** @brief Constructs HealthMon
68 *
69 * @param[in] bus - Handle to system dbus
70 */
71 HealthMon(sdbusplus::bus::bus& bus) : bus(bus)
72 {
73 // read json file
74 sensorConfigs = getHealthConfig();
75 createHealthSensors();
76 }
77
78 struct HealthConfig
79 {
80 std::string name;
81 uint16_t freq;
82 uint16_t windowSize;
83 uint8_t criticalHigh;
84 uint8_t warningHigh;
85 bool criticalLog;
86 bool warningLog;
87 std::string criticalTgt;
88 std::string warningTgt;
89 };
90
91 /** @brief List of health sensors supported */
92 std::vector<std::string> cfgNames = {"CPU", "Memory"};
93
94 /** @brief Parsing Health config JSON file */
95 Json parseConfigFile(std::string configFile);
96
97 /** @brief reading config for each health sensor component */
98 void getConfigData(Json& data, HealthConfig& cfg);
99
100 /** @brief Map of the object HealthSensor */
101 std::unordered_map<std::string, std::shared_ptr<HealthSensor>>
102 healthSensors;
103
104 /** @brief Create sensors for health monitoring */
105 void createHealthSensors();
106
107 private:
108 sdbusplus::bus::bus& bus;
109 std::vector<HealthConfig> sensorConfigs;
110 std::vector<HealthConfig> getHealthConfig();
111};
112
113} // namespace health
114} // namespace phosphor