blob: 1570f12e2537ed31add0ff0ddc290a65e4150ead [file] [log] [blame]
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -08001#include "config.h"
2
3#include "health_metric_config.hpp"
4
5#include <nlohmann/json.hpp>
6#include <phosphor-logging/lg2.hpp>
7
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -08008#include <cmath>
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -08009#include <fstream>
Patrick Williams67b8ebe2024-02-23 20:40:52 -060010#include <ranges>
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080011#include <unordered_map>
Jagpal Singh Gillafbac902024-02-22 18:09:56 -080012#include <unordered_set>
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080013#include <utility>
14
15PHOSPHOR_LOG2_USING;
16
Patrick Williams67b8ebe2024-02-23 20:40:52 -060017namespace phosphor::health::metric
18{
19namespace config
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080020{
21
22using json = nlohmann::json;
23
24// Default health metric config
25extern json defaultHealthMetricConfig;
26
27// Valid thresholds from config
Jagpal Singh Gillafbac902024-02-22 18:09:56 -080028static const auto validThresholdTypesWithBound =
29 std::unordered_set<std::string>{"Critical_Lower", "Critical_Upper",
30 "Warning_Lower", "Warning_Upper"};
31
32static const auto validThresholdBounds =
33 std::unordered_map<std::string, ThresholdIntf::Bound>{
34 {"Lower", ThresholdIntf::Bound::Lower},
35 {"Upper", ThresholdIntf::Bound::Upper}};
36
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080037static const auto validThresholdTypes =
38 std::unordered_map<std::string, ThresholdIntf::Type>{
Jagpal Singh Gill28ce2ef2024-02-28 15:57:10 -080039 {"HardShutdown", ThresholdIntf::Type::HardShutdown},
40 {"SoftShutdown", ThresholdIntf::Type::SoftShutdown},
41 {"PerformanceLoss", ThresholdIntf::Type::PerformanceLoss},
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080042 {"Critical", ThresholdIntf::Type::Critical},
43 {"Warning", ThresholdIntf::Type::Warning}};
44
45// Valid metrics from config
46static const auto validTypes =
47 std::unordered_map<std::string, Type>{{"CPU", Type::cpu},
48 {"Memory", Type::memory},
49 {"Storage", Type::storage},
50 {"Inode", Type::inode}};
51
52// Valid submetrics from config
53static const auto validSubTypes = std::unordered_map<std::string, SubType>{
54 {"CPU", SubType::cpuTotal},
55 {"CPU_User", SubType::cpuUser},
56 {"CPU_Kernel", SubType::cpuKernel},
57 {"Memory", SubType::memoryTotal},
58 {"Memory_Free", SubType::memoryFree},
59 {"Memory_Available", SubType::memoryAvailable},
60 {"Memory_Shared", SubType::memoryShared},
61 {"Memory_Buffered_And_Cached", SubType::memoryBufferedAndCached},
Jagpal Singh Gill97582802024-02-27 13:59:11 -080062 {"Storage_RW", SubType::NA},
63 {"Storage_TMP", SubType::NA}};
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080064
65/** Deserialize a Threshold from JSON. */
66void from_json(const json& j, Threshold& self)
67{
68 self.value = j.value("Value", 100.0);
69 self.log = j.value("Log", false);
70 self.target = j.value("Target", Threshold::defaults::target);
71}
72
73/** Deserialize a HealthMetric from JSON. */
74void from_json(const json& j, HealthMetric& self)
75{
76 self.collectionFreq = std::chrono::seconds(j.value(
77 "Frequency",
78 std::chrono::seconds(HealthMetric::defaults::frequency).count()));
79
80 self.windowSize = j.value("Window_size",
81 HealthMetric::defaults::windowSize);
82 // Path is only valid for storage
83 self.path = j.value("Path", "");
84
85 auto thresholds = j.find("Threshold");
86 if (thresholds == j.end())
87 {
88 return;
89 }
90
91 for (auto& [key, value] : thresholds->items())
92 {
Jagpal Singh Gillafbac902024-02-22 18:09:56 -080093 if (!validThresholdTypesWithBound.contains(key))
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -080094 {
95 warning("Invalid ThresholdType: {TYPE}", "TYPE", key);
96 continue;
97 }
98
99 auto config = value.template get<Threshold>();
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -0800100 if (!std::isfinite(config.value))
101 {
102 throw std::invalid_argument("Invalid threshold value");
103 }
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800104
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800105 static constexpr auto keyDelimiter = "_";
106 std::string typeStr = key.substr(0, key.find_first_of(keyDelimiter));
107 std::string boundStr = key.substr(key.find_last_of(keyDelimiter) + 1,
108 key.length());
109
110 self.thresholds.emplace(
111 std::make_tuple(validThresholdTypes.at(typeStr),
112 validThresholdBounds.at(boundStr)),
113 config);
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800114 }
115}
116
117json parseConfigFile(std::string configFile)
118{
119 std::ifstream jsonFile(configFile);
120 if (!jsonFile.is_open())
121 {
122 info("config JSON file not found: {PATH}", "PATH", configFile);
123 return {};
124 }
125
126 try
127 {
128 return json::parse(jsonFile, nullptr, true);
129 }
130 catch (const json::parse_error& e)
131 {
132 error("Failed to parse JSON config file {PATH}: {ERROR}", "PATH",
133 configFile, "ERROR", e);
134 }
135
136 return {};
137}
138
139void printConfig(HealthMetric::map_t& configs)
140{
141 for (auto& [type, configList] : configs)
142 {
143 for (auto& config : configList)
144 {
145 debug(
Patrick Williams67b8ebe2024-02-23 20:40:52 -0600146 "TYPE={TYPE}, NAME={NAME} SUBTYPE={SUBTYPE} PATH={PATH}, FREQ={FREQ}, WSIZE={WSIZE}",
147 "TYPE", type, "NAME", config.name, "SUBTYPE", config.subType,
148 "PATH", config.path, "FREQ", config.collectionFreq.count(),
149 "WSIZE", config.windowSize);
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800150
151 for (auto& [key, threshold] : config.thresholds)
152 {
153 debug(
154 "THRESHOLD TYPE={TYPE} THRESHOLD BOUND={BOUND} VALUE={VALUE} LOG={LOG} TARGET={TARGET}",
Patrick Williams67b8ebe2024-02-23 20:40:52 -0600155 "TYPE", get<ThresholdIntf::Type>(key), "BOUND",
156 get<ThresholdIntf::Bound>(key), "VALUE", threshold.value,
157 "LOG", threshold.log, "TARGET", threshold.target);
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800158 }
159 }
160 }
161}
162
163auto getHealthMetricConfigs() -> HealthMetric::map_t
164{
165 json mergedConfig(defaultHealthMetricConfig);
166
167 if (auto platformConfig = parseConfigFile(HEALTH_CONFIG_FILE);
168 !platformConfig.empty())
169 {
170 mergedConfig.merge_patch(platformConfig);
171 }
172
173 HealthMetric::map_t configs = {};
174 for (auto& [name, metric] : mergedConfig.items())
175 {
176 static constexpr auto nameDelimiter = "_";
177 std::string typeStr = name.substr(0, name.find_first_of(nameDelimiter));
178
179 auto type = validTypes.find(typeStr);
180 if (type == validTypes.end())
181 {
182 warning("Invalid metric type: {TYPE}", "TYPE", typeStr);
183 continue;
184 }
185
186 auto config = metric.template get<HealthMetric>();
Jagpal Singh Gill1f920052024-02-16 09:57:18 -0800187 config.name = name;
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800188
189 auto subType = validSubTypes.find(name);
190 config.subType = (subType != validSubTypes.end() ? subType->second
191 : SubType::NA);
192
193 configs[type->second].emplace_back(std::move(config));
194 }
195 printConfig(configs);
196 return configs;
197}
198
199json defaultHealthMetricConfig = R"({
200 "CPU": {
201 "Frequency": 1,
202 "Window_size": 120,
203 "Threshold": {
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800204 "Critical_Upper": {
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800205 "Value": 90.0,
206 "Log": true,
207 "Target": ""
208 },
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800209 "Warning_Upper": {
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800210 "Value": 80.0,
211 "Log": false,
212 "Target": ""
213 }
214 }
215 },
216 "CPU_User": {
217 "Frequency": 1,
Patrick Williamsc00c19e2024-02-23 19:07:32 -0600218 "Window_size": 120
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800219 },
220 "CPU_Kernel": {
221 "Frequency": 1,
Patrick Williamsc00c19e2024-02-23 19:07:32 -0600222 "Window_size": 120
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800223 },
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800224 "Memory": {
225 "Frequency": 1,
Patrick Williamsb3a8df22024-02-23 19:08:58 -0600226 "Window_size": 120
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800227 },
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800228 "Memory_Available": {
229 "Frequency": 1,
230 "Window_size": 120,
231 "Threshold": {
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800232 "Critical_Lower": {
233 "Value": 15.0,
234 "Log": true,
235 "Target": ""
236 }
237 }
238 },
239 "Memory_Free": {
240 "Frequency": 1,
Patrick Williams78a224b2024-02-23 19:10:19 -0600241 "Window_size": 120
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800242 },
Jagpal Singh Gillc6897812024-02-16 09:59:47 -0800243 "Memory_Shared": {
244 "Frequency": 1,
245 "Window_size": 120,
246 "Threshold": {
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800247 "Critical_Upper": {
Jagpal Singh Gillc6897812024-02-16 09:59:47 -0800248 "Value": 85.0,
249 "Log": true,
250 "Target": ""
251 }
252 }
253 },
254 "Memory_Buffered_And_Cached": {
255 "Frequency": 1,
Patrick Williamse7b17de2024-02-23 19:12:14 -0600256 "Window_size": 120
Jagpal Singh Gillc6897812024-02-16 09:59:47 -0800257 },
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800258 "Storage_RW": {
259 "Path": "/run/initramfs/rw",
260 "Frequency": 1,
261 "Window_size": 120,
262 "Threshold": {
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800263 "Critical_Lower": {
264 "Value": 15.0,
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800265 "Log": true,
266 "Target": ""
267 }
268 }
Jagpal Singh Gill7f3fd6e2024-02-12 16:20:00 -0800269 },
270 "Storage_TMP": {
271 "Path": "/tmp",
272 "Frequency": 1,
273 "Window_size": 120,
274 "Threshold": {
Jagpal Singh Gillafbac902024-02-22 18:09:56 -0800275 "Critical_Lower": {
276 "Value": 15.0,
Jagpal Singh Gill7f3fd6e2024-02-12 16:20:00 -0800277 "Log": true,
278 "Target": ""
279 }
280 }
Jagpal Singh Gill7e11ab02023-12-08 11:41:41 -0800281 }
282})"_json;
283
Patrick Williams67b8ebe2024-02-23 20:40:52 -0600284} // namespace config
285
286namespace details
287{
288auto reverse_map_search(const auto& m, auto v)
289{
290 if (auto match = std::ranges::find_if(
291 m, [=](const auto& p) { return p.second == v; });
292 match != std::end(m))
293 {
294 return match->first;
295 }
296 return std::format("Enum({})", std::to_underlying(v));
297}
298} // namespace details
299
300// to_string specialization for Type.
301auto to_string(Type t) -> std::string
302{
303 return details::reverse_map_search(config::validTypes, t);
304}
305
306// to_string specializaiton for SubType.
307auto to_string(SubType t) -> std::string
308{
309 return details::reverse_map_search(config::validSubTypes, t);
310}
311
312} // namespace phosphor::health::metric