blob: 9796927999091bafc072b3c3ba5ebada6336e3ba [file] [log] [blame]
Vijay Khemkae2795302020-07-15 17:28:45 -07001#include "config.h"
2
3#include "healthMonitor.hpp"
4
5#include <phosphor-logging/log.hpp>
6#include <sdeventplus/event.hpp>
7
8#include <fstream>
9#include <iostream>
10
11static constexpr bool DEBUG = false;
12
13namespace phosphor
14{
15namespace health
16{
17
18using namespace phosphor::logging;
19
20void HealthSensor::setSensorThreshold(uint8_t criticalHigh, uint8_t warningHigh)
21{
22 CriticalInterface::criticalHigh(criticalHigh);
23 WarningInterface::warningHigh(warningHigh);
24}
25
26void HealthSensor::setSensorValueToDbus(const uint8_t value)
27{
28 ValueIface::value(value);
29}
30
31/* Create dbus utilization sensor object for each configured sensors */
32void HealthMon::createHealthSensors()
33{
34 for (auto& cfg : sensorConfigs)
35 {
36 std::string objPath = std::string(HEALTH_SENSOR_PATH) + cfg.name;
37 auto healthSensor =
38 std::make_shared<HealthSensor>(bus, objPath.c_str());
39 healthSensors.emplace(cfg.name, healthSensor);
40
41 std::string logMsg = cfg.name + " Health Sensor created";
42 log<level::INFO>(logMsg.c_str(), entry("NAME = %s", cfg.name.c_str()));
43
44 /* Set configured values of crtical and warning high to dbus */
45 healthSensor->setSensorThreshold(cfg.criticalHigh, cfg.warningHigh);
46 }
47}
48
49/** @brief Parsing Health config JSON file */
50Json HealthMon::parseConfigFile(std::string configFile)
51{
52 std::ifstream jsonFile(configFile);
53 if (!jsonFile.is_open())
54 {
55 log<level::ERR>("config JSON file not found",
56 entry("FILENAME = %s", configFile.c_str()));
57 }
58
59 auto data = Json::parse(jsonFile, nullptr, false);
60 if (data.is_discarded())
61 {
62 log<level::ERR>("config readings JSON parser failure",
63 entry("FILENAME = %s", configFile.c_str()));
64 }
65
66 return data;
67}
68
69void printConfig(HealthMon::HealthConfig& cfg)
70{
71 std::cout << "Name: " << cfg.name << "\n";
72 std::cout << "Freq: " << cfg.freq << "\n";
73 std::cout << "Window Size: " << cfg.windowSize << "\n";
74 std::cout << "Critical value: " << cfg.criticalHigh << "\n";
75 std::cout << "warning value: " << cfg.warningHigh << "\n";
76 std::cout << "Critical log: " << cfg.criticalLog << "\n";
77 std::cout << "Warning log: " << cfg.warningLog << "\n";
78 std::cout << "Critical Target: " << cfg.criticalTgt << "\n";
79 std::cout << "Warning Target: " << cfg.warningTgt << "\n\n";
80}
81
82void HealthMon::getConfigData(Json& data, HealthConfig& cfg)
83{
84
85 static const Json empty{};
86
87 cfg.freq = data.value("Frequency", 0);
88 cfg.windowSize = data.value("Window_size", 0);
89 auto threshold = data.value("Threshold", empty);
90 if (!threshold.empty())
91 {
92 auto criticalData = threshold.value("Critical", empty);
93 if (!criticalData.empty())
94 {
95 cfg.criticalHigh = criticalData.value("Value", 0);
96 cfg.criticalLog = criticalData.value("Log", true);
97 cfg.criticalTgt = criticalData.value("Target", "");
98 }
99 auto warningData = threshold.value("Warning", empty);
100 if (!warningData.empty())
101 {
102 cfg.warningHigh = warningData.value("Value", 0);
103 cfg.warningLog = warningData.value("Log", true);
104 cfg.warningTgt = warningData.value("Target", "");
105 }
106 }
107}
108
109std::vector<HealthMon::HealthConfig> HealthMon::getHealthConfig()
110{
111
112 std::vector<HealthConfig> cfgs;
113 HealthConfig cfg;
114 auto data = parseConfigFile(HEALTH_CONFIG_FILE);
115
116 // print values
117 if (DEBUG)
118 std::cout << "Config json data:\n" << data << "\n\n";
119
120 /* Get CPU config data */
121 for (auto& j : data.items())
122 {
123 auto key = j.key();
124 if (std::find(cfgNames.begin(), cfgNames.end(), key) != cfgNames.end())
125 {
126 HealthConfig cfg = HealthConfig();
127 cfg.name = j.key();
128 getConfigData(j.value(), cfg);
129 cfgs.push_back(cfg);
130 if (DEBUG)
131 printConfig(cfg);
132 }
133 else
134 {
135 std::string logMsg = key + " Health Sensor not supported";
136 log<level::ERR>(logMsg.c_str(), entry("NAME = %s", key.c_str()));
137 }
138 }
139 return cfgs;
140}
141
142} // namespace health
143} // namespace phosphor
144
145/**
146 * @brief Main
147 */
148int main()
149{
150
151 // Get a default event loop
152 auto event = sdeventplus::Event::get_default();
153
154 // Get a handle to system dbus
155 auto bus = sdbusplus::bus::new_default();
156
157 // Create an health monitor object
158 phosphor::health::HealthMon healthMon(bus);
159
160 // Request service bus name
161 bus.request_name(HEALTH_BUS_NAME);
162
163 // Attach the bus to sd_event to service user requests
164 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
165 event.loop();
166
167 return 0;
168}