Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 1 | #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 Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 8 | #include <cmath> |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 9 | #include <fstream> |
| 10 | #include <unordered_map> |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 11 | #include <unordered_set> |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
| 14 | PHOSPHOR_LOG2_USING; |
| 15 | |
| 16 | namespace phosphor::health::metric::config |
| 17 | { |
| 18 | |
| 19 | using json = nlohmann::json; |
| 20 | |
| 21 | // Default health metric config |
| 22 | extern json defaultHealthMetricConfig; |
| 23 | |
| 24 | // Valid thresholds from config |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 25 | static const auto validThresholdTypesWithBound = |
| 26 | std::unordered_set<std::string>{"Critical_Lower", "Critical_Upper", |
| 27 | "Warning_Lower", "Warning_Upper"}; |
| 28 | |
| 29 | static const auto validThresholdBounds = |
| 30 | std::unordered_map<std::string, ThresholdIntf::Bound>{ |
| 31 | {"Lower", ThresholdIntf::Bound::Lower}, |
| 32 | {"Upper", ThresholdIntf::Bound::Upper}}; |
| 33 | |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 34 | static const auto validThresholdTypes = |
| 35 | std::unordered_map<std::string, ThresholdIntf::Type>{ |
| 36 | {"Critical", ThresholdIntf::Type::Critical}, |
| 37 | {"Warning", ThresholdIntf::Type::Warning}}; |
| 38 | |
| 39 | // Valid metrics from config |
| 40 | static const auto validTypes = |
| 41 | std::unordered_map<std::string, Type>{{"CPU", Type::cpu}, |
| 42 | {"Memory", Type::memory}, |
| 43 | {"Storage", Type::storage}, |
| 44 | {"Inode", Type::inode}}; |
| 45 | |
| 46 | // Valid submetrics from config |
| 47 | static const auto validSubTypes = std::unordered_map<std::string, SubType>{ |
| 48 | {"CPU", SubType::cpuTotal}, |
| 49 | {"CPU_User", SubType::cpuUser}, |
| 50 | {"CPU_Kernel", SubType::cpuKernel}, |
| 51 | {"Memory", SubType::memoryTotal}, |
| 52 | {"Memory_Free", SubType::memoryFree}, |
| 53 | {"Memory_Available", SubType::memoryAvailable}, |
| 54 | {"Memory_Shared", SubType::memoryShared}, |
| 55 | {"Memory_Buffered_And_Cached", SubType::memoryBufferedAndCached}, |
Jagpal Singh Gill | 7f3fd6e | 2024-02-12 16:20:00 -0800 | [diff] [blame] | 56 | {"Storage_RW", SubType::storageReadWrite}, |
Jagpal Singh Gill | dfe839f | 2024-02-16 09:54:02 -0800 | [diff] [blame] | 57 | {"Storage_TMP", SubType::storageTmp}}; |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 58 | |
| 59 | /** Deserialize a Threshold from JSON. */ |
| 60 | void from_json(const json& j, Threshold& self) |
| 61 | { |
| 62 | self.value = j.value("Value", 100.0); |
| 63 | self.log = j.value("Log", false); |
| 64 | self.target = j.value("Target", Threshold::defaults::target); |
| 65 | } |
| 66 | |
| 67 | /** Deserialize a HealthMetric from JSON. */ |
| 68 | void from_json(const json& j, HealthMetric& self) |
| 69 | { |
| 70 | self.collectionFreq = std::chrono::seconds(j.value( |
| 71 | "Frequency", |
| 72 | std::chrono::seconds(HealthMetric::defaults::frequency).count())); |
| 73 | |
| 74 | self.windowSize = j.value("Window_size", |
| 75 | HealthMetric::defaults::windowSize); |
| 76 | // Path is only valid for storage |
| 77 | self.path = j.value("Path", ""); |
| 78 | |
| 79 | auto thresholds = j.find("Threshold"); |
| 80 | if (thresholds == j.end()) |
| 81 | { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | for (auto& [key, value] : thresholds->items()) |
| 86 | { |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 87 | if (!validThresholdTypesWithBound.contains(key)) |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 88 | { |
| 89 | warning("Invalid ThresholdType: {TYPE}", "TYPE", key); |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | auto config = value.template get<Threshold>(); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 94 | if (!std::isfinite(config.value)) |
| 95 | { |
| 96 | throw std::invalid_argument("Invalid threshold value"); |
| 97 | } |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 98 | |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 99 | static constexpr auto keyDelimiter = "_"; |
| 100 | std::string typeStr = key.substr(0, key.find_first_of(keyDelimiter)); |
| 101 | std::string boundStr = key.substr(key.find_last_of(keyDelimiter) + 1, |
| 102 | key.length()); |
| 103 | |
| 104 | self.thresholds.emplace( |
| 105 | std::make_tuple(validThresholdTypes.at(typeStr), |
| 106 | validThresholdBounds.at(boundStr)), |
| 107 | config); |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | json parseConfigFile(std::string configFile) |
| 112 | { |
| 113 | std::ifstream jsonFile(configFile); |
| 114 | if (!jsonFile.is_open()) |
| 115 | { |
| 116 | info("config JSON file not found: {PATH}", "PATH", configFile); |
| 117 | return {}; |
| 118 | } |
| 119 | |
| 120 | try |
| 121 | { |
| 122 | return json::parse(jsonFile, nullptr, true); |
| 123 | } |
| 124 | catch (const json::parse_error& e) |
| 125 | { |
| 126 | error("Failed to parse JSON config file {PATH}: {ERROR}", "PATH", |
| 127 | configFile, "ERROR", e); |
| 128 | } |
| 129 | |
| 130 | return {}; |
| 131 | } |
| 132 | |
| 133 | void printConfig(HealthMetric::map_t& configs) |
| 134 | { |
| 135 | for (auto& [type, configList] : configs) |
| 136 | { |
| 137 | for (auto& config : configList) |
| 138 | { |
| 139 | debug( |
| 140 | "MTYPE={MTYPE}, MNAME={MNAME} MSTYPE={MSTYPE} PATH={PATH}, FREQ={FREQ}, WSIZE={WSIZE}", |
| 141 | "MTYPE", std::to_underlying(type), "MNAME", config.name, |
| 142 | "MSTYPE", std::to_underlying(config.subType), "PATH", |
| 143 | config.path, "FREQ", config.collectionFreq.count(), "WSIZE", |
| 144 | config.windowSize); |
| 145 | |
| 146 | for (auto& [key, threshold] : config.thresholds) |
| 147 | { |
| 148 | debug( |
| 149 | "THRESHOLD TYPE={TYPE} THRESHOLD BOUND={BOUND} VALUE={VALUE} LOG={LOG} TARGET={TARGET}", |
| 150 | "TYPE", std::to_underlying(get<ThresholdIntf::Type>(key)), |
| 151 | "BOUND", std::to_underlying(get<ThresholdIntf::Bound>(key)), |
| 152 | "VALUE", threshold.value, "LOG", threshold.log, "TARGET", |
| 153 | threshold.target); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | auto getHealthMetricConfigs() -> HealthMetric::map_t |
| 160 | { |
| 161 | json mergedConfig(defaultHealthMetricConfig); |
| 162 | |
| 163 | if (auto platformConfig = parseConfigFile(HEALTH_CONFIG_FILE); |
| 164 | !platformConfig.empty()) |
| 165 | { |
| 166 | mergedConfig.merge_patch(platformConfig); |
| 167 | } |
| 168 | |
| 169 | HealthMetric::map_t configs = {}; |
| 170 | for (auto& [name, metric] : mergedConfig.items()) |
| 171 | { |
| 172 | static constexpr auto nameDelimiter = "_"; |
| 173 | std::string typeStr = name.substr(0, name.find_first_of(nameDelimiter)); |
| 174 | |
| 175 | auto type = validTypes.find(typeStr); |
| 176 | if (type == validTypes.end()) |
| 177 | { |
| 178 | warning("Invalid metric type: {TYPE}", "TYPE", typeStr); |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | auto config = metric.template get<HealthMetric>(); |
Jagpal Singh Gill | 1f92005 | 2024-02-16 09:57:18 -0800 | [diff] [blame] | 183 | config.name = name; |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 184 | |
| 185 | auto subType = validSubTypes.find(name); |
| 186 | config.subType = (subType != validSubTypes.end() ? subType->second |
| 187 | : SubType::NA); |
| 188 | |
| 189 | configs[type->second].emplace_back(std::move(config)); |
| 190 | } |
| 191 | printConfig(configs); |
| 192 | return configs; |
| 193 | } |
| 194 | |
| 195 | json defaultHealthMetricConfig = R"({ |
| 196 | "CPU": { |
| 197 | "Frequency": 1, |
| 198 | "Window_size": 120, |
| 199 | "Threshold": { |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 200 | "Critical_Upper": { |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 201 | "Value": 90.0, |
| 202 | "Log": true, |
| 203 | "Target": "" |
| 204 | }, |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 205 | "Warning_Upper": { |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 206 | "Value": 80.0, |
| 207 | "Log": false, |
| 208 | "Target": "" |
| 209 | } |
| 210 | } |
| 211 | }, |
| 212 | "CPU_User": { |
| 213 | "Frequency": 1, |
Patrick Williams | c00c19e | 2024-02-23 19:07:32 -0600 | [diff] [blame] | 214 | "Window_size": 120 |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 215 | }, |
| 216 | "CPU_Kernel": { |
| 217 | "Frequency": 1, |
Patrick Williams | c00c19e | 2024-02-23 19:07:32 -0600 | [diff] [blame] | 218 | "Window_size": 120 |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 219 | }, |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 220 | "Memory": { |
| 221 | "Frequency": 1, |
Patrick Williams | b3a8df2 | 2024-02-23 19:08:58 -0600 | [diff] [blame] | 222 | "Window_size": 120 |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 223 | }, |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 224 | "Memory_Available": { |
| 225 | "Frequency": 1, |
| 226 | "Window_size": 120, |
| 227 | "Threshold": { |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 228 | "Critical_Lower": { |
| 229 | "Value": 15.0, |
| 230 | "Log": true, |
| 231 | "Target": "" |
| 232 | } |
| 233 | } |
| 234 | }, |
| 235 | "Memory_Free": { |
| 236 | "Frequency": 1, |
Patrick Williams | 78a224b | 2024-02-23 19:10:19 -0600 | [diff] [blame] | 237 | "Window_size": 120 |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 238 | }, |
Jagpal Singh Gill | c689781 | 2024-02-16 09:59:47 -0800 | [diff] [blame] | 239 | "Memory_Shared": { |
| 240 | "Frequency": 1, |
| 241 | "Window_size": 120, |
| 242 | "Threshold": { |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 243 | "Critical_Upper": { |
Jagpal Singh Gill | c689781 | 2024-02-16 09:59:47 -0800 | [diff] [blame] | 244 | "Value": 85.0, |
| 245 | "Log": true, |
| 246 | "Target": "" |
| 247 | } |
| 248 | } |
| 249 | }, |
| 250 | "Memory_Buffered_And_Cached": { |
| 251 | "Frequency": 1, |
Patrick Williams | e7b17de | 2024-02-23 19:12:14 -0600 | [diff] [blame^] | 252 | "Window_size": 120 |
Jagpal Singh Gill | c689781 | 2024-02-16 09:59:47 -0800 | [diff] [blame] | 253 | }, |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 254 | "Storage_RW": { |
| 255 | "Path": "/run/initramfs/rw", |
| 256 | "Frequency": 1, |
| 257 | "Window_size": 120, |
| 258 | "Threshold": { |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 259 | "Critical_Lower": { |
| 260 | "Value": 15.0, |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 261 | "Log": true, |
| 262 | "Target": "" |
| 263 | } |
| 264 | } |
Jagpal Singh Gill | 7f3fd6e | 2024-02-12 16:20:00 -0800 | [diff] [blame] | 265 | }, |
| 266 | "Storage_TMP": { |
| 267 | "Path": "/tmp", |
| 268 | "Frequency": 1, |
| 269 | "Window_size": 120, |
| 270 | "Threshold": { |
Jagpal Singh Gill | afbac90 | 2024-02-22 18:09:56 -0800 | [diff] [blame] | 271 | "Critical_Lower": { |
| 272 | "Value": 15.0, |
Jagpal Singh Gill | 7f3fd6e | 2024-02-12 16:20:00 -0800 | [diff] [blame] | 273 | "Log": true, |
| 274 | "Target": "" |
| 275 | } |
| 276 | } |
Jagpal Singh Gill | 7e11ab0 | 2023-12-08 11:41:41 -0800 | [diff] [blame] | 277 | } |
| 278 | })"_json; |
| 279 | |
| 280 | } // namespace phosphor::health::metric::config |