Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "healthMonitor.hpp" |
| 4 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <boost/asio/deadline_timer.hpp> |
| 8 | #include <sdbusplus/asio/connection.hpp> |
| 9 | #include <sdbusplus/asio/object_server.hpp> |
| 10 | #include <sdbusplus/asio/sd_event.hpp> |
| 11 | #include <sdbusplus/bus/match.hpp> |
Vijay Khemka | 1d0d012 | 2020-09-29 12:17:43 -0700 | [diff] [blame] | 12 | #include <sdbusplus/server/manager.hpp> |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 13 | #include <sdeventplus/event.hpp> |
| 14 | |
| 15 | #include <fstream> |
| 16 | #include <iostream> |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 17 | #include <memory> |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 18 | #include <numeric> |
| 19 | #include <sstream> |
| 20 | |
| 21 | extern "C" |
| 22 | { |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 23 | #include <sys/statvfs.h> |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 24 | #include <sys/sysinfo.h> |
| 25 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 26 | |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 27 | PHOSPHOR_LOG2_USING; |
| 28 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 29 | static constexpr bool DEBUG = false; |
Vijay Khemka | 415dcd2 | 2020-09-21 15:58:21 -0700 | [diff] [blame] | 30 | static constexpr uint8_t defaultHighThreshold = 100; |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 31 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 32 | // Limit sensor recreation interval to 10s |
| 33 | bool needUpdate; |
| 34 | static constexpr int TIMER_INTERVAL = 10; |
| 35 | std::shared_ptr<boost::asio::deadline_timer> sensorRecreateTimer; |
| 36 | std::shared_ptr<phosphor::health::HealthMon> healthMon; |
| 37 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 38 | namespace phosphor |
| 39 | { |
| 40 | namespace health |
| 41 | { |
| 42 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 43 | // Example values for iface: |
| 44 | // BMC_CONFIGURATION |
| 45 | // BMC_INVENTORY_ITEM |
Patrick Williams | 9ca0045 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 46 | std::vector<std::string> findPathsWithType(sdbusplus::bus_t& bus, |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 47 | const std::string& iface) |
| 48 | { |
| 49 | PHOSPHOR_LOG2_USING; |
| 50 | std::vector<std::string> ret; |
| 51 | |
| 52 | // Find all BMCs (DBus objects implementing the |
| 53 | // Inventory.Item.Bmc interface that may be created by |
| 54 | // configuring the Inventory Manager) |
Patrick Williams | 9ca0045 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 55 | sdbusplus::message_t msg = bus.new_method_call( |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 56 | "xyz.openbmc_project.ObjectMapper", |
| 57 | "/xyz/openbmc_project/object_mapper", |
| 58 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"); |
| 59 | |
| 60 | // "/": No limit for paths for all the paths that may be touched |
| 61 | // in this daemon |
| 62 | |
| 63 | // 0: Limit the depth to 0 to match both objects created by |
| 64 | // EntityManager and by InventoryManager |
| 65 | |
| 66 | // {iface}: The endpoint of the Association Definition must have |
| 67 | // the Inventory.Item.Bmc interface |
| 68 | msg.append("/", 0, std::vector<std::string>{iface}); |
| 69 | |
| 70 | try |
| 71 | { |
| 72 | bus.call(msg, 0).read(ret); |
| 73 | |
| 74 | if (!ret.empty()) |
| 75 | { |
| 76 | debug("{IFACE} found", "IFACE", iface); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | debug("{IFACE} not found", "IFACE", iface); |
| 81 | } |
| 82 | } |
| 83 | catch (std::exception& e) |
| 84 | { |
| 85 | error("Exception occurred while calling {PATH}: {ERROR}", "PATH", |
| 86 | InventoryPath, "ERROR", e); |
| 87 | } |
| 88 | return ret; |
| 89 | } |
| 90 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 91 | enum CPUStatesTime |
| 92 | { |
| 93 | USER_IDX = 0, |
| 94 | NICE_IDX, |
| 95 | SYSTEM_IDX, |
| 96 | IDLE_IDX, |
| 97 | IOWAIT_IDX, |
| 98 | IRQ_IDX, |
| 99 | SOFTIRQ_IDX, |
| 100 | STEAL_IDX, |
| 101 | GUEST_USER_IDX, |
| 102 | GUEST_NICE_IDX, |
| 103 | NUM_CPU_STATES_TIME |
| 104 | }; |
| 105 | |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 106 | // # cat /proc/stat|grep 'cpu ' |
| 107 | // cpu 5750423 14827 1572788 9259794 1317 0 28879 0 0 0 |
| 108 | static_assert(NUM_CPU_STATES_TIME == 10); |
| 109 | |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 110 | enum CPUUtilizationType |
| 111 | { |
| 112 | USER = 0, |
| 113 | KERNEL, |
| 114 | TOTAL |
| 115 | }; |
| 116 | |
| 117 | double readCPUUtilization(enum CPUUtilizationType type) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 118 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 119 | auto proc_stat = "/proc/stat"; |
| 120 | std::ifstream fileStat(proc_stat); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 121 | if (!fileStat.is_open()) |
| 122 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 123 | error("cpu file not available: {PATH}", "PATH", proc_stat); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | std::string firstLine, labelName; |
| 128 | std::size_t timeData[NUM_CPU_STATES_TIME]; |
| 129 | |
| 130 | std::getline(fileStat, firstLine); |
| 131 | std::stringstream ss(firstLine); |
| 132 | ss >> labelName; |
| 133 | |
| 134 | if (DEBUG) |
| 135 | std::cout << "CPU stats first Line is " << firstLine << "\n"; |
| 136 | |
| 137 | if (labelName.compare("cpu")) |
| 138 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 139 | error("CPU data not available"); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | int i; |
| 144 | for (i = 0; i < NUM_CPU_STATES_TIME; i++) |
| 145 | { |
| 146 | if (!(ss >> timeData[i])) |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | if (i != NUM_CPU_STATES_TIME) |
| 151 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 152 | error("CPU data not correct"); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 156 | static std::unordered_map<enum CPUUtilizationType, uint64_t> preActiveTime, |
| 157 | preTotalTime; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 158 | |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 159 | // These are actually Jiffies. On the BMC, 1 jiffy usually corresponds to |
| 160 | // 0.01 second. |
| 161 | uint64_t activeTime = 0, activeTimeDiff = 0, totalTime = 0, |
| 162 | totalTimeDiff = 0; |
| 163 | double activePercValue = 0; |
| 164 | |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 165 | if (type == TOTAL) |
| 166 | { |
| 167 | activeTime = timeData[USER_IDX] + timeData[NICE_IDX] + |
| 168 | timeData[SYSTEM_IDX] + timeData[IRQ_IDX] + |
| 169 | timeData[SOFTIRQ_IDX] + timeData[STEAL_IDX] + |
| 170 | timeData[GUEST_USER_IDX] + timeData[GUEST_NICE_IDX]; |
| 171 | } |
| 172 | else if (type == KERNEL) |
| 173 | { |
| 174 | activeTime = timeData[SYSTEM_IDX]; |
| 175 | } |
| 176 | else if (type == USER) |
| 177 | { |
| 178 | activeTime = timeData[USER_IDX]; |
| 179 | } |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 180 | |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 181 | totalTime = std::accumulate(std::begin(timeData), std::end(timeData), 0); |
| 182 | |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 183 | activeTimeDiff = activeTime - preActiveTime[type]; |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 184 | totalTimeDiff = totalTime - preTotalTime[type]; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 185 | |
| 186 | /* Store current idle and active time for next calculation */ |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 187 | preActiveTime[type] = activeTime; |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 188 | preTotalTime[type] = totalTime; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 189 | |
Sui Chen | ec6601d | 2023-01-09 14:55:54 -0800 | [diff] [blame] | 190 | activePercValue = (100.0 * activeTimeDiff) / totalTimeDiff; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 191 | |
| 192 | if (DEBUG) |
| 193 | std::cout << "CPU Utilization is " << activePercValue << "\n"; |
| 194 | |
| 195 | return activePercValue; |
| 196 | } |
| 197 | |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 198 | auto readCPUUtilizationTotal([[maybe_unused]] const std::string& path) |
| 199 | { |
| 200 | return readCPUUtilization(CPUUtilizationType::TOTAL); |
| 201 | } |
| 202 | |
| 203 | auto readCPUUtilizationKernel([[maybe_unused]] const std::string& path) |
| 204 | { |
| 205 | return readCPUUtilization(CPUUtilizationType::KERNEL); |
| 206 | } |
| 207 | |
| 208 | auto readCPUUtilizationUser([[maybe_unused]] const std::string& path) |
| 209 | { |
| 210 | return readCPUUtilization(CPUUtilizationType::USER); |
| 211 | } |
| 212 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 213 | double readMemoryUtilization([[maybe_unused]] const std::string& path) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 214 | { |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 215 | /* Unused var: path */ |
| 216 | std::ignore = path; |
Potin Lai | b7d7bd5 | 2022-08-23 01:47:13 +0000 | [diff] [blame] | 217 | std::ifstream meminfo("/proc/meminfo"); |
| 218 | std::string line; |
| 219 | double memTotal = -1; |
| 220 | double memAvail = -1; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 221 | |
Potin Lai | b7d7bd5 | 2022-08-23 01:47:13 +0000 | [diff] [blame] | 222 | while (std::getline(meminfo, line)) |
| 223 | { |
| 224 | std::string name; |
| 225 | double value; |
| 226 | std::istringstream iss(line); |
| 227 | |
| 228 | if (!(iss >> name >> value)) |
| 229 | { |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | if (name.starts_with("MemTotal")) |
| 234 | { |
| 235 | memTotal = value; |
| 236 | } |
| 237 | else if (name.starts_with("MemAvailable")) |
| 238 | { |
| 239 | memAvail = value; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (memTotal <= 0 || memAvail <= 0) |
| 244 | { |
| 245 | return std::numeric_limits<double>::quiet_NaN(); |
| 246 | } |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 247 | |
| 248 | if (DEBUG) |
| 249 | { |
Potin Lai | b7d7bd5 | 2022-08-23 01:47:13 +0000 | [diff] [blame] | 250 | std::cout << "MemTotal: " << memTotal << " MemAvailable: " << memAvail |
| 251 | << std::endl; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Potin Lai | b7d7bd5 | 2022-08-23 01:47:13 +0000 | [diff] [blame] | 254 | return (memTotal - memAvail) / memTotal * 100; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 257 | double readStorageUtilization([[maybe_unused]] const std::string& path) |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 258 | { |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 259 | struct statvfs buffer |
| 260 | {}; |
| 261 | int ret = statvfs(path.c_str(), &buffer); |
| 262 | double total = 0; |
| 263 | double available = 0; |
| 264 | double used = 0; |
| 265 | double usedPercentage = 0; |
| 266 | |
| 267 | if (ret != 0) |
| 268 | { |
| 269 | auto e = errno; |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 270 | std::cerr << "Error from statvfs: " << strerror(e) << ",path: " << path |
| 271 | << std::endl; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | total = buffer.f_blocks * (buffer.f_frsize / 1024); |
| 276 | available = buffer.f_bfree * (buffer.f_frsize / 1024); |
| 277 | used = total - available; |
| 278 | usedPercentage = (used / total) * 100; |
| 279 | |
| 280 | if (DEBUG) |
| 281 | { |
| 282 | std::cout << "Total:" << total << "\n"; |
| 283 | std::cout << "Available:" << available << "\n"; |
| 284 | std::cout << "Used:" << used << "\n"; |
| 285 | std::cout << "Storage utilization is:" << usedPercentage << "\n"; |
| 286 | } |
| 287 | |
| 288 | return usedPercentage; |
| 289 | } |
| 290 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 291 | double readInodeUtilization([[maybe_unused]] const std::string& path) |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 292 | { |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 293 | struct statvfs buffer |
| 294 | {}; |
| 295 | int ret = statvfs(path.c_str(), &buffer); |
| 296 | double totalInodes = 0; |
| 297 | double availableInodes = 0; |
| 298 | double used = 0; |
| 299 | double usedPercentage = 0; |
| 300 | |
| 301 | if (ret != 0) |
| 302 | { |
| 303 | auto e = errno; |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 304 | std::cerr << "Error from statvfs: " << strerror(e) << ",path: " << path |
| 305 | << std::endl; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | totalInodes = buffer.f_files; |
| 310 | availableInodes = buffer.f_ffree; |
| 311 | used = totalInodes - availableInodes; |
| 312 | usedPercentage = (used / totalInodes) * 100; |
| 313 | |
| 314 | if (DEBUG) |
| 315 | { |
| 316 | std::cout << "Total Inodes:" << totalInodes << "\n"; |
| 317 | std::cout << "Available Inodes:" << availableInodes << "\n"; |
| 318 | std::cout << "Used:" << used << "\n"; |
| 319 | std::cout << "Inodes utilization is:" << usedPercentage << "\n"; |
| 320 | } |
| 321 | |
| 322 | return usedPercentage; |
| 323 | } |
| 324 | |
| 325 | constexpr auto storage = "Storage"; |
| 326 | constexpr auto inode = "Inode"; |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 327 | |
| 328 | /** Map of read function for each health sensors supported |
| 329 | * |
| 330 | * The following health sensors are read in the ManagerDiagnosticData |
| 331 | * Redfish resource: |
| 332 | * - CPU_Kernel populates ProcessorStatistics.KernelPercent |
| 333 | * - CPU_User populates ProcessorStatistics.UserPercent |
| 334 | */ |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 335 | const std::map<std::string, std::function<double(const std::string& path)>> |
Sui Chen | 51bcfcb | 2021-11-01 15:28:51 -0700 | [diff] [blame] | 336 | readSensors = {{"CPU", readCPUUtilizationTotal}, |
| 337 | {"CPU_Kernel", readCPUUtilizationKernel}, |
| 338 | {"CPU_User", readCPUUtilizationUser}, |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 339 | {"Memory", readMemoryUtilization}, |
| 340 | {storage, readStorageUtilization}, |
| 341 | {inode, readInodeUtilization}}; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 342 | |
| 343 | void HealthSensor::setSensorThreshold(double criticalHigh, double warningHigh) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 344 | { |
| 345 | CriticalInterface::criticalHigh(criticalHigh); |
Yong Li | f8d7973 | 2021-03-12 09:12:19 +0800 | [diff] [blame] | 346 | CriticalInterface::criticalLow(std::numeric_limits<double>::quiet_NaN()); |
| 347 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 348 | WarningInterface::warningHigh(warningHigh); |
Yong Li | f8d7973 | 2021-03-12 09:12:19 +0800 | [diff] [blame] | 349 | WarningInterface::warningLow(std::numeric_limits<double>::quiet_NaN()); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 352 | void HealthSensor::setSensorValueToDbus(const double value) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 353 | { |
| 354 | ValueIface::value(value); |
| 355 | } |
| 356 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 357 | void HealthSensor::initHealthSensor( |
| 358 | const std::vector<std::string>& bmcInventoryPaths) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 359 | { |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 360 | info("{SENSOR} Health Sensor initialized", "SENSOR", sensorConfig.name); |
| 361 | |
| 362 | /* Look for sensor read functions and Read Sensor values */ |
| 363 | auto it = readSensors.find(sensorConfig.name); |
| 364 | |
| 365 | if (sensorConfig.name.rfind(storage, 0) == 0) |
| 366 | { |
| 367 | it = readSensors.find(storage); |
| 368 | } |
| 369 | else if (sensorConfig.name.rfind(inode, 0) == 0) |
| 370 | { |
| 371 | it = readSensors.find(inode); |
| 372 | } |
| 373 | else if (it == readSensors.end()) |
| 374 | { |
| 375 | error("Sensor read function not available"); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | double value = it->second(sensorConfig.path); |
| 380 | |
| 381 | if (value < 0) |
| 382 | { |
| 383 | error("Reading Sensor Utilization failed: {SENSOR}", "SENSOR", |
| 384 | sensorConfig.name); |
| 385 | return; |
| 386 | } |
| 387 | |
Vijay Khemka | 0879770 | 2020-09-21 14:53:57 -0700 | [diff] [blame] | 388 | /* Initialize unit value (Percent) for utilization sensor */ |
| 389 | ValueIface::unit(ValueIface::Unit::Percent); |
| 390 | |
Konstantin Aladyshev | 9d29b37 | 2021-12-21 15:45:02 +0300 | [diff] [blame] | 391 | ValueIface::maxValue(100); |
| 392 | ValueIface::minValue(0); |
Potin Lai | c82e616 | 2022-08-02 10:22:56 +0000 | [diff] [blame] | 393 | ValueIface::value(std::numeric_limits<double>::quiet_NaN()); |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 394 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 395 | // Associate the sensor to chassis |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 396 | // This connects the DBus object to a Chassis. |
| 397 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 398 | std::vector<AssociationTuple> associationTuples; |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 399 | for (const auto& chassisId : bmcInventoryPaths) |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 400 | { |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 401 | // This utilization sensor "is monitoring" the BMC with path chassisId. |
| 402 | // The chassisId is "monitored_by" this utilization sensor. |
| 403 | associationTuples.push_back({"monitors", "monitored_by", chassisId}); |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 404 | } |
| 405 | AssociationDefinitionInterface::associations(associationTuples); |
| 406 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 407 | /* Start the timer for reading sensor data at regular interval */ |
| 408 | readTimer.restart(std::chrono::milliseconds(sensorConfig.freq * 1000)); |
| 409 | } |
| 410 | |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 411 | void HealthSensor::checkSensorThreshold(const double value) |
| 412 | { |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 413 | if (std::isfinite(sensorConfig.criticalHigh) && |
| 414 | (value > sensorConfig.criticalHigh)) |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 415 | { |
| 416 | if (!CriticalInterface::criticalAlarmHigh()) |
| 417 | { |
| 418 | CriticalInterface::criticalAlarmHigh(true); |
| 419 | if (sensorConfig.criticalLog) |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 420 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 421 | error( |
| 422 | "ASSERT: sensor {SENSOR} is above the upper threshold critical high", |
| 423 | "SENSOR", sensorConfig.name); |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 424 | startUnit(sensorConfig.criticalTgt); |
| 425 | } |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 426 | } |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 427 | return; |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 428 | } |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 429 | |
| 430 | if (CriticalInterface::criticalAlarmHigh()) |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 431 | { |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 432 | CriticalInterface::criticalAlarmHigh(false); |
| 433 | if (sensorConfig.criticalLog) |
| 434 | info( |
| 435 | "DEASSERT: sensor {SENSOR} is under the upper threshold critical high", |
| 436 | "SENSOR", sensorConfig.name); |
| 437 | } |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 438 | |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 439 | if (std::isfinite(sensorConfig.warningHigh) && |
| 440 | (value > sensorConfig.warningHigh)) |
| 441 | { |
| 442 | if (!WarningInterface::warningAlarmHigh()) |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 443 | { |
| 444 | WarningInterface::warningAlarmHigh(true); |
| 445 | if (sensorConfig.warningLog) |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 446 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 447 | error( |
| 448 | "ASSERT: sensor {SENSOR} is above the upper threshold warning high", |
| 449 | "SENSOR", sensorConfig.name); |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 450 | startUnit(sensorConfig.warningTgt); |
| 451 | } |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 452 | } |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | if (WarningInterface::warningAlarmHigh()) |
| 457 | { |
| 458 | WarningInterface::warningAlarmHigh(false); |
| 459 | if (sensorConfig.warningLog) |
| 460 | info( |
| 461 | "DEASSERT: sensor {SENSOR} is under the upper threshold warning high", |
| 462 | "SENSOR", sensorConfig.name); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 466 | void HealthSensor::readHealthSensor() |
| 467 | { |
| 468 | /* Read current sensor value */ |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 469 | double value; |
| 470 | |
| 471 | if (sensorConfig.name.rfind(storage, 0) == 0) |
| 472 | { |
| 473 | value = readSensors.find(storage)->second(sensorConfig.path); |
| 474 | } |
| 475 | else if (sensorConfig.name.rfind(inode, 0) == 0) |
| 476 | { |
| 477 | value = readSensors.find(inode)->second(sensorConfig.path); |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | value = readSensors.find(sensorConfig.name)->second(sensorConfig.path); |
| 482 | } |
| 483 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 484 | if (value < 0) |
| 485 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 486 | error("Reading Sensor Utilization failed: {SENSOR}", "SENSOR", |
| 487 | sensorConfig.name); |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | /* Remove first item from the queue */ |
Potin Lai | c82e616 | 2022-08-02 10:22:56 +0000 | [diff] [blame] | 492 | if (valQueue.size() >= sensorConfig.windowSize) |
| 493 | { |
| 494 | valQueue.pop_front(); |
| 495 | } |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 496 | /* Add new item at the back */ |
| 497 | valQueue.push_back(value); |
Potin Lai | c82e616 | 2022-08-02 10:22:56 +0000 | [diff] [blame] | 498 | /* Wait until the queue is filled with enough reference*/ |
| 499 | if (valQueue.size() < sensorConfig.windowSize) |
| 500 | { |
| 501 | return; |
| 502 | } |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 503 | |
| 504 | /* Calculate average values for the given window size */ |
| 505 | double avgValue = 0; |
| 506 | avgValue = accumulate(valQueue.begin(), valQueue.end(), avgValue); |
| 507 | avgValue = avgValue / sensorConfig.windowSize; |
| 508 | |
| 509 | /* Set this new value to dbus */ |
| 510 | setSensorValueToDbus(avgValue); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 511 | |
| 512 | /* Check the sensor threshold and log required message */ |
| 513 | checkSensorThreshold(avgValue); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 516 | void HealthSensor::startUnit(const std::string& sysdUnit) |
| 517 | { |
| 518 | if (sysdUnit.empty()) |
| 519 | { |
| 520 | return; |
| 521 | } |
| 522 | |
Patrick Williams | bbfe718 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 523 | sdbusplus::message_t msg = bus.new_method_call( |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 524 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 525 | "org.freedesktop.systemd1.Manager", "StartUnit"); |
| 526 | msg.append(sysdUnit, "replace"); |
| 527 | bus.call_noreply(msg); |
| 528 | } |
| 529 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 530 | void HealthMon::recreateSensors() |
| 531 | { |
| 532 | PHOSPHOR_LOG2_USING; |
| 533 | healthSensors.clear(); |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 534 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 535 | // Find BMC inventory paths and create health sensors |
| 536 | std::vector<std::string> bmcInventoryPaths = |
| 537 | findPathsWithType(bus, BMC_INVENTORY_ITEM); |
| 538 | createHealthSensors(bmcInventoryPaths); |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 541 | void printConfig(HealthConfig& cfg) |
| 542 | { |
| 543 | std::cout << "Name: " << cfg.name << "\n"; |
| 544 | std::cout << "Freq: " << (int)cfg.freq << "\n"; |
| 545 | std::cout << "Window Size: " << (int)cfg.windowSize << "\n"; |
| 546 | std::cout << "Critical value: " << (int)cfg.criticalHigh << "\n"; |
| 547 | std::cout << "warning value: " << (int)cfg.warningHigh << "\n"; |
| 548 | std::cout << "Critical log: " << (int)cfg.criticalLog << "\n"; |
| 549 | std::cout << "Warning log: " << (int)cfg.warningLog << "\n"; |
| 550 | std::cout << "Critical Target: " << cfg.criticalTgt << "\n"; |
| 551 | std::cout << "Warning Target: " << cfg.warningTgt << "\n\n"; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 552 | std::cout << "Path : " << cfg.path << "\n\n"; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 555 | /* Create dbus utilization sensor object for each configured sensors */ |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 556 | void HealthMon::createHealthSensors( |
| 557 | const std::vector<std::string>& bmcInventoryPaths) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 558 | { |
| 559 | for (auto& cfg : sensorConfigs) |
| 560 | { |
| 561 | std::string objPath = std::string(HEALTH_SENSOR_PATH) + cfg.name; |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 562 | auto healthSensor = std::make_shared<HealthSensor>( |
| 563 | bus, objPath.c_str(), cfg, bmcInventoryPaths); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 564 | healthSensors.emplace(cfg.name, healthSensor); |
| 565 | |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 566 | info("{SENSOR} Health Sensor created", "SENSOR", cfg.name); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 567 | |
| 568 | /* Set configured values of crtical and warning high to dbus */ |
| 569 | healthSensor->setSensorThreshold(cfg.criticalHigh, cfg.warningHigh); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** @brief Parsing Health config JSON file */ |
| 574 | Json HealthMon::parseConfigFile(std::string configFile) |
| 575 | { |
| 576 | std::ifstream jsonFile(configFile); |
| 577 | if (!jsonFile.is_open()) |
| 578 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 579 | error("config JSON file not found: {PATH}", "PATH", configFile); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | auto data = Json::parse(jsonFile, nullptr, false); |
| 583 | if (data.is_discarded()) |
| 584 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 585 | error("config readings JSON parser failure: {PATH}", "PATH", |
| 586 | configFile); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return data; |
| 590 | } |
| 591 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 592 | void HealthMon::getConfigData(Json& data, HealthConfig& cfg) |
| 593 | { |
| 594 | |
| 595 | static const Json empty{}; |
| 596 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 597 | /* Default frerquency of sensor polling is 1 second */ |
| 598 | cfg.freq = data.value("Frequency", 1); |
| 599 | |
| 600 | /* Default window size sensor queue is 1 */ |
| 601 | cfg.windowSize = data.value("Window_size", 1); |
| 602 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 603 | auto threshold = data.value("Threshold", empty); |
| 604 | if (!threshold.empty()) |
| 605 | { |
| 606 | auto criticalData = threshold.value("Critical", empty); |
| 607 | if (!criticalData.empty()) |
| 608 | { |
Vijay Khemka | 415dcd2 | 2020-09-21 15:58:21 -0700 | [diff] [blame] | 609 | cfg.criticalHigh = |
| 610 | criticalData.value("Value", defaultHighThreshold); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 611 | cfg.criticalLog = criticalData.value("Log", true); |
| 612 | cfg.criticalTgt = criticalData.value("Target", ""); |
| 613 | } |
| 614 | auto warningData = threshold.value("Warning", empty); |
| 615 | if (!warningData.empty()) |
| 616 | { |
Vijay Khemka | 415dcd2 | 2020-09-21 15:58:21 -0700 | [diff] [blame] | 617 | cfg.warningHigh = warningData.value("Value", defaultHighThreshold); |
| 618 | cfg.warningLog = warningData.value("Log", false); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 619 | cfg.warningTgt = warningData.value("Target", ""); |
| 620 | } |
| 621 | } |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 622 | cfg.path = data.value("Path", ""); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 625 | std::vector<HealthConfig> HealthMon::getHealthConfig() |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 626 | { |
| 627 | |
| 628 | std::vector<HealthConfig> cfgs; |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 629 | auto data = parseConfigFile(HEALTH_CONFIG_FILE); |
| 630 | |
| 631 | // print values |
| 632 | if (DEBUG) |
| 633 | std::cout << "Config json data:\n" << data << "\n\n"; |
| 634 | |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 635 | /* Get data items from config json data*/ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 636 | for (auto& j : data.items()) |
| 637 | { |
| 638 | auto key = j.key(); |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 639 | /* key need match default value in map readSensors or match the key |
| 640 | * start with "Storage" or "Inode" */ |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 641 | bool isStorageOrInode = |
| 642 | (key.rfind(storage, 0) == 0 || key.rfind(inode, 0) == 0); |
| 643 | if (readSensors.find(key) != readSensors.end() || isStorageOrInode) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 644 | { |
| 645 | HealthConfig cfg = HealthConfig(); |
| 646 | cfg.name = j.key(); |
| 647 | getConfigData(j.value(), cfg); |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 648 | if (isStorageOrInode) |
| 649 | { |
| 650 | struct statvfs buffer |
| 651 | {}; |
| 652 | int ret = statvfs(cfg.path.c_str(), &buffer); |
| 653 | if (ret != 0) |
| 654 | { |
| 655 | auto e = errno; |
| 656 | std::cerr << "Error from statvfs: " << strerror(e) |
| 657 | << ", name: " << cfg.name |
| 658 | << ", path: " << cfg.path |
| 659 | << ", please check your settings in config file." |
| 660 | << std::endl; |
| 661 | continue; |
| 662 | } |
| 663 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 664 | cfgs.push_back(cfg); |
| 665 | if (DEBUG) |
| 666 | printConfig(cfg); |
| 667 | } |
| 668 | else |
| 669 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 670 | error("{SENSOR} Health Sensor not supported", "SENSOR", key); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | return cfgs; |
| 674 | } |
| 675 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 676 | // Two caveats here. |
| 677 | // 1. The BMC Inventory will only show up by the nearest ObjectMapper polling |
| 678 | // interval. |
| 679 | // 2. InterfacesAdded events will are not emitted like they are with E-M. |
| 680 | void HealthMon::createBmcInventoryIfNotCreated() |
| 681 | { |
| 682 | if (bmcInventory == nullptr) |
| 683 | { |
| 684 | info("createBmcInventory"); |
| 685 | bmcInventory = std::make_shared<phosphor::health::BmcInventory>( |
| 686 | bus, "/xyz/openbmc_project/inventory/bmc"); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | bool HealthMon::bmcInventoryCreated() |
| 691 | { |
| 692 | return bmcInventory != nullptr; |
| 693 | } |
| 694 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 695 | } // namespace health |
| 696 | } // namespace phosphor |
| 697 | |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 698 | void sensorRecreateTimerCallback( |
Patrick Williams | 9ca0045 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 699 | std::shared_ptr<boost::asio::deadline_timer> timer, sdbusplus::bus_t& bus) |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 700 | { |
| 701 | timer->expires_from_now(boost::posix_time::seconds(TIMER_INTERVAL)); |
| 702 | timer->async_wait([timer, &bus](const boost::system::error_code& ec) { |
| 703 | if (ec == boost::asio::error::operation_aborted) |
| 704 | { |
| 705 | info("sensorRecreateTimer aborted"); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | // When Entity-manager is already running |
| 710 | if (!needUpdate) |
| 711 | { |
| 712 | if ((!healthMon->bmcInventoryCreated()) && |
| 713 | (!phosphor::health::findPathsWithType(bus, BMC_CONFIGURATION) |
| 714 | .empty())) |
| 715 | { |
| 716 | healthMon->createBmcInventoryIfNotCreated(); |
| 717 | needUpdate = true; |
| 718 | } |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | |
| 723 | // If this daemon maintains its own DBus object, we must make sure |
| 724 | // the object is registered to ObjectMapper |
| 725 | if (phosphor::health::findPathsWithType(bus, BMC_INVENTORY_ITEM) |
| 726 | .empty()) |
| 727 | { |
| 728 | info( |
| 729 | "BMC inventory item not registered to Object Mapper yet, waiting for next iteration"); |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | info( |
| 734 | "BMC inventory item registered to Object Mapper, creating sensors now"); |
| 735 | healthMon->recreateSensors(); |
| 736 | needUpdate = false; |
| 737 | } |
| 738 | } |
| 739 | sensorRecreateTimerCallback(timer, bus); |
| 740 | }); |
| 741 | } |
| 742 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 743 | /** |
| 744 | * @brief Main |
| 745 | */ |
| 746 | int main() |
| 747 | { |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 748 | // The io_context is needed for the timer |
| 749 | boost::asio::io_context io; |
| 750 | |
| 751 | // DBus connection |
| 752 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 753 | |
| 754 | conn->request_name(HEALTH_BUS_NAME); |
| 755 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 756 | // Get a default event loop |
| 757 | auto event = sdeventplus::Event::get_default(); |
| 758 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 759 | // Create an health monitor object |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 760 | healthMon = std::make_shared<phosphor::health::HealthMon>(*conn); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 761 | |
Yong Li | f8d7973 | 2021-03-12 09:12:19 +0800 | [diff] [blame] | 762 | // Add object manager through object_server |
| 763 | sdbusplus::asio::object_server objectServer(conn); |
Vijay Khemka | 1d0d012 | 2020-09-29 12:17:43 -0700 | [diff] [blame] | 764 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 765 | sdbusplus::asio::sd_event_wrapper sdEvents(io); |
| 766 | |
| 767 | sensorRecreateTimer = std::make_shared<boost::asio::deadline_timer>(io); |
| 768 | |
| 769 | // If the SystemInventory does not exist: wait for the InterfaceAdded signal |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 770 | auto interfacesAddedSignalHandler = std::make_unique< |
| 771 | sdbusplus::bus::match_t>( |
| 772 | static_cast<sdbusplus::bus_t&>(*conn), |
| 773 | sdbusplus::bus::match::rules::interfacesAdded(), |
Patrick Williams | 9ca0045 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 774 | [conn](sdbusplus::message_t& msg) { |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 775 | using Association = |
| 776 | std::tuple<std::string, std::string, std::string>; |
| 777 | using InterfacesAdded = std::vector<std::pair< |
| 778 | std::string, |
| 779 | std::vector<std::pair< |
| 780 | std::string, std::variant<std::vector<Association>>>>>>; |
| 781 | |
| 782 | sdbusplus::message::object_path o; |
| 783 | InterfacesAdded interfacesAdded; |
| 784 | |
| 785 | try |
| 786 | { |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 787 | msg.read(o); |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 788 | msg.read(interfacesAdded); |
| 789 | } |
| 790 | catch (const std::exception& e) |
| 791 | { |
| 792 | error( |
| 793 | "Exception occurred while processing interfacesAdded: {EXCEPTION}", |
| 794 | "EXCEPTION", e.what()); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | // Ignore any signal coming from health-monitor itself. |
| 799 | if (msg.get_sender() == conn->get_unique_name()) |
| 800 | { |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | // Check if the BMC Inventory is in the interfaces created. |
| 805 | bool hasBmcConfiguration = false; |
| 806 | for (const auto& x : interfacesAdded) |
| 807 | { |
| 808 | if (x.first == BMC_CONFIGURATION) |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 809 | { |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 810 | hasBmcConfiguration = true; |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 811 | } |
Sui Chen | 517524a | 2021-12-19 20:52:46 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | if (hasBmcConfiguration) |
| 815 | { |
| 816 | info( |
| 817 | "BMC configuration detected, will create a corresponding Inventory item"); |
| 818 | healthMon->createBmcInventoryIfNotCreated(); |
| 819 | needUpdate = true; |
| 820 | } |
| 821 | }); |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 822 | |
| 823 | // Start the timer |
Ed Tanous | a19c6fb | 2023-03-06 13:53:27 -0800 | [diff] [blame] | 824 | boost::asio::post(io, [conn]() { |
| 825 | sensorRecreateTimerCallback(sensorRecreateTimer, *conn); |
| 826 | }); |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 827 | io.run(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 828 | |
| 829 | return 0; |
| 830 | } |