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 | |
| 38 | void sensorRecreateTimerCallback( |
| 39 | std::shared_ptr<boost::asio::deadline_timer> timer) |
| 40 | { |
| 41 | timer->expires_from_now(boost::posix_time::seconds(TIMER_INTERVAL)); |
| 42 | timer->async_wait([timer](const boost::system::error_code& ec) { |
| 43 | if (ec == boost::asio::error::operation_aborted) |
| 44 | { |
| 45 | return; |
| 46 | } |
| 47 | if (needUpdate) |
| 48 | { |
| 49 | healthMon->recreateSensors(); |
| 50 | needUpdate = false; |
| 51 | } |
| 52 | sensorRecreateTimerCallback(timer); |
| 53 | }); |
| 54 | } |
| 55 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 56 | namespace phosphor |
| 57 | { |
| 58 | namespace health |
| 59 | { |
| 60 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 61 | enum CPUStatesTime |
| 62 | { |
| 63 | USER_IDX = 0, |
| 64 | NICE_IDX, |
| 65 | SYSTEM_IDX, |
| 66 | IDLE_IDX, |
| 67 | IOWAIT_IDX, |
| 68 | IRQ_IDX, |
| 69 | SOFTIRQ_IDX, |
| 70 | STEAL_IDX, |
| 71 | GUEST_USER_IDX, |
| 72 | GUEST_NICE_IDX, |
| 73 | NUM_CPU_STATES_TIME |
| 74 | }; |
| 75 | |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 76 | double readCPUUtilization([[maybe_unused]] std::string path) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 77 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 78 | auto proc_stat = "/proc/stat"; |
| 79 | std::ifstream fileStat(proc_stat); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 80 | if (!fileStat.is_open()) |
| 81 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 82 | error("cpu file not available: {PATH}", "PATH", proc_stat); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | std::string firstLine, labelName; |
| 87 | std::size_t timeData[NUM_CPU_STATES_TIME]; |
| 88 | |
| 89 | std::getline(fileStat, firstLine); |
| 90 | std::stringstream ss(firstLine); |
| 91 | ss >> labelName; |
| 92 | |
| 93 | if (DEBUG) |
| 94 | std::cout << "CPU stats first Line is " << firstLine << "\n"; |
| 95 | |
| 96 | if (labelName.compare("cpu")) |
| 97 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 98 | error("CPU data not available"); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | int i; |
| 103 | for (i = 0; i < NUM_CPU_STATES_TIME; i++) |
| 104 | { |
| 105 | if (!(ss >> timeData[i])) |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | if (i != NUM_CPU_STATES_TIME) |
| 110 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 111 | error("CPU data not correct"); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | static double preActiveTime = 0, preIdleTime = 0; |
| 116 | double activeTime, activeTimeDiff, idleTime, idleTimeDiff, totalTime, |
| 117 | activePercValue; |
| 118 | |
| 119 | idleTime = timeData[IDLE_IDX] + timeData[IOWAIT_IDX]; |
| 120 | activeTime = timeData[USER_IDX] + timeData[NICE_IDX] + |
| 121 | timeData[SYSTEM_IDX] + timeData[IRQ_IDX] + |
| 122 | timeData[SOFTIRQ_IDX] + timeData[STEAL_IDX] + |
| 123 | timeData[GUEST_USER_IDX] + timeData[GUEST_NICE_IDX]; |
| 124 | |
| 125 | idleTimeDiff = idleTime - preIdleTime; |
| 126 | activeTimeDiff = activeTime - preActiveTime; |
| 127 | |
| 128 | /* Store current idle and active time for next calculation */ |
| 129 | preIdleTime = idleTime; |
| 130 | preActiveTime = activeTime; |
| 131 | |
| 132 | totalTime = idleTimeDiff + activeTimeDiff; |
| 133 | |
| 134 | activePercValue = activeTimeDiff / totalTime * 100; |
| 135 | |
| 136 | if (DEBUG) |
| 137 | std::cout << "CPU Utilization is " << activePercValue << "\n"; |
| 138 | |
| 139 | return activePercValue; |
| 140 | } |
| 141 | |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 142 | double readMemoryUtilization(std::string path) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 143 | { |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 144 | /* Unused var: path */ |
| 145 | std::ignore = path; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 146 | struct sysinfo s_info; |
| 147 | |
| 148 | sysinfo(&s_info); |
| 149 | double usedRam = s_info.totalram - s_info.freeram; |
| 150 | double memUsePerc = usedRam / s_info.totalram * 100; |
| 151 | |
| 152 | if (DEBUG) |
| 153 | { |
| 154 | std::cout << "Memory Utilization is " << memUsePerc << "\n"; |
| 155 | |
| 156 | std::cout << "TotalRam: " << s_info.totalram |
| 157 | << " FreeRam: " << s_info.freeram << "\n"; |
| 158 | std::cout << "UseRam: " << usedRam << "\n"; |
| 159 | } |
| 160 | |
| 161 | return memUsePerc; |
| 162 | } |
| 163 | |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 164 | double readStorageUtilization(std::string path) |
| 165 | { |
| 166 | |
| 167 | struct statvfs buffer |
| 168 | {}; |
| 169 | int ret = statvfs(path.c_str(), &buffer); |
| 170 | double total = 0; |
| 171 | double available = 0; |
| 172 | double used = 0; |
| 173 | double usedPercentage = 0; |
| 174 | |
| 175 | if (ret != 0) |
| 176 | { |
| 177 | auto e = errno; |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 178 | std::cerr << "Error from statvfs: " << strerror(e) << ",path: " << path |
| 179 | << std::endl; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | total = buffer.f_blocks * (buffer.f_frsize / 1024); |
| 184 | available = buffer.f_bfree * (buffer.f_frsize / 1024); |
| 185 | used = total - available; |
| 186 | usedPercentage = (used / total) * 100; |
| 187 | |
| 188 | if (DEBUG) |
| 189 | { |
| 190 | std::cout << "Total:" << total << "\n"; |
| 191 | std::cout << "Available:" << available << "\n"; |
| 192 | std::cout << "Used:" << used << "\n"; |
| 193 | std::cout << "Storage utilization is:" << usedPercentage << "\n"; |
| 194 | } |
| 195 | |
| 196 | return usedPercentage; |
| 197 | } |
| 198 | |
| 199 | double readInodeUtilization(std::string path) |
| 200 | { |
| 201 | |
| 202 | struct statvfs buffer |
| 203 | {}; |
| 204 | int ret = statvfs(path.c_str(), &buffer); |
| 205 | double totalInodes = 0; |
| 206 | double availableInodes = 0; |
| 207 | double used = 0; |
| 208 | double usedPercentage = 0; |
| 209 | |
| 210 | if (ret != 0) |
| 211 | { |
| 212 | auto e = errno; |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 213 | std::cerr << "Error from statvfs: " << strerror(e) << ",path: " << path |
| 214 | << std::endl; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | totalInodes = buffer.f_files; |
| 219 | availableInodes = buffer.f_ffree; |
| 220 | used = totalInodes - availableInodes; |
| 221 | usedPercentage = (used / totalInodes) * 100; |
| 222 | |
| 223 | if (DEBUG) |
| 224 | { |
| 225 | std::cout << "Total Inodes:" << totalInodes << "\n"; |
| 226 | std::cout << "Available Inodes:" << availableInodes << "\n"; |
| 227 | std::cout << "Used:" << used << "\n"; |
| 228 | std::cout << "Inodes utilization is:" << usedPercentage << "\n"; |
| 229 | } |
| 230 | |
| 231 | return usedPercentage; |
| 232 | } |
| 233 | |
| 234 | constexpr auto storage = "Storage"; |
| 235 | constexpr auto inode = "Inode"; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 236 | /** Map of read function for each health sensors supported */ |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 237 | const std::map<std::string, std::function<double(std::string path)>> |
| 238 | readSensors = {{"CPU", readCPUUtilization}, |
| 239 | {"Memory", readMemoryUtilization}, |
| 240 | {storage, readStorageUtilization}, |
| 241 | {inode, readInodeUtilization}}; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 242 | |
| 243 | void HealthSensor::setSensorThreshold(double criticalHigh, double warningHigh) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 244 | { |
| 245 | CriticalInterface::criticalHigh(criticalHigh); |
Yong Li | f8d7973 | 2021-03-12 09:12:19 +0800 | [diff] [blame] | 246 | CriticalInterface::criticalLow(std::numeric_limits<double>::quiet_NaN()); |
| 247 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 248 | WarningInterface::warningHigh(warningHigh); |
Yong Li | f8d7973 | 2021-03-12 09:12:19 +0800 | [diff] [blame] | 249 | WarningInterface::warningLow(std::numeric_limits<double>::quiet_NaN()); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 252 | void HealthSensor::setSensorValueToDbus(const double value) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 253 | { |
| 254 | ValueIface::value(value); |
| 255 | } |
| 256 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 257 | void HealthSensor::initHealthSensor(const std::vector<std::string>& chassisIds) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 258 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 259 | info("{SENSOR} Health Sensor initialized", "SENSOR", sensorConfig.name); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 260 | |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 261 | /* Look for sensor read functions and Read Sensor values */ |
| 262 | double value; |
| 263 | std::map<std::string, |
| 264 | std::function<double(std::string path)>>::const_iterator it; |
| 265 | it = readSensors.find(sensorConfig.name); |
| 266 | |
| 267 | if (sensorConfig.name.rfind(storage, 0) == 0) |
| 268 | { |
| 269 | it = readSensors.find(storage); |
| 270 | } |
| 271 | else if (sensorConfig.name.rfind(inode, 0) == 0) |
| 272 | { |
| 273 | it = readSensors.find(inode); |
| 274 | } |
| 275 | else if (it == readSensors.end()) |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 276 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 277 | error("Sensor read function not available"); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 281 | value = it->second(sensorConfig.path); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 282 | |
| 283 | if (value < 0) |
| 284 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 285 | error("Reading Sensor Utilization failed: {SENSOR}", "SENSOR", |
| 286 | sensorConfig.name); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | |
Vijay Khemka | 0879770 | 2020-09-21 14:53:57 -0700 | [diff] [blame] | 290 | /* Initialize value queue with initial sensor reading */ |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 291 | for (int i = 0; i < sensorConfig.windowSize; i++) |
| 292 | { |
| 293 | valQueue.push_back(value); |
| 294 | } |
Vijay Khemka | 0879770 | 2020-09-21 14:53:57 -0700 | [diff] [blame] | 295 | |
| 296 | /* Initialize unit value (Percent) for utilization sensor */ |
| 297 | ValueIface::unit(ValueIface::Unit::Percent); |
| 298 | |
Konstantin Aladyshev | 9d29b37 | 2021-12-21 15:45:02 +0300 | [diff] [blame] | 299 | ValueIface::maxValue(100); |
| 300 | ValueIface::minValue(0); |
| 301 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 302 | setSensorValueToDbus(value); |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 303 | |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 304 | // Associate the sensor to chassis |
| 305 | std::vector<AssociationTuple> associationTuples; |
| 306 | for (const auto& chassisId : chassisIds) |
| 307 | { |
| 308 | associationTuples.push_back({"bmc", "all_sensors", chassisId}); |
| 309 | } |
| 310 | AssociationDefinitionInterface::associations(associationTuples); |
| 311 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 312 | /* Start the timer for reading sensor data at regular interval */ |
| 313 | readTimer.restart(std::chrono::milliseconds(sensorConfig.freq * 1000)); |
| 314 | } |
| 315 | |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 316 | void HealthSensor::checkSensorThreshold(const double value) |
| 317 | { |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 318 | if (std::isfinite(sensorConfig.criticalHigh) && |
| 319 | (value > sensorConfig.criticalHigh)) |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 320 | { |
| 321 | if (!CriticalInterface::criticalAlarmHigh()) |
| 322 | { |
| 323 | CriticalInterface::criticalAlarmHigh(true); |
| 324 | if (sensorConfig.criticalLog) |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 325 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 326 | error( |
| 327 | "ASSERT: sensor {SENSOR} is above the upper threshold critical high", |
| 328 | "SENSOR", sensorConfig.name); |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 329 | startUnit(sensorConfig.criticalTgt); |
| 330 | } |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 331 | } |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 332 | return; |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 333 | } |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 334 | |
| 335 | if (CriticalInterface::criticalAlarmHigh()) |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 336 | { |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 337 | CriticalInterface::criticalAlarmHigh(false); |
| 338 | if (sensorConfig.criticalLog) |
| 339 | info( |
| 340 | "DEASSERT: sensor {SENSOR} is under the upper threshold critical high", |
| 341 | "SENSOR", sensorConfig.name); |
| 342 | } |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 343 | |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 344 | if (std::isfinite(sensorConfig.warningHigh) && |
| 345 | (value > sensorConfig.warningHigh)) |
| 346 | { |
| 347 | if (!WarningInterface::warningAlarmHigh()) |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 348 | { |
| 349 | WarningInterface::warningAlarmHigh(true); |
| 350 | if (sensorConfig.warningLog) |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 351 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 352 | error( |
| 353 | "ASSERT: sensor {SENSOR} is above the upper threshold warning high", |
| 354 | "SENSOR", sensorConfig.name); |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 355 | startUnit(sensorConfig.warningTgt); |
| 356 | } |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 357 | } |
Konstantin Aladyshev | a6cd704 | 2021-12-21 15:36:01 +0300 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
| 361 | if (WarningInterface::warningAlarmHigh()) |
| 362 | { |
| 363 | WarningInterface::warningAlarmHigh(false); |
| 364 | if (sensorConfig.warningLog) |
| 365 | info( |
| 366 | "DEASSERT: sensor {SENSOR} is under the upper threshold warning high", |
| 367 | "SENSOR", sensorConfig.name); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 371 | void HealthSensor::readHealthSensor() |
| 372 | { |
| 373 | /* Read current sensor value */ |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 374 | double value; |
| 375 | |
| 376 | if (sensorConfig.name.rfind(storage, 0) == 0) |
| 377 | { |
| 378 | value = readSensors.find(storage)->second(sensorConfig.path); |
| 379 | } |
| 380 | else if (sensorConfig.name.rfind(inode, 0) == 0) |
| 381 | { |
| 382 | value = readSensors.find(inode)->second(sensorConfig.path); |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | value = readSensors.find(sensorConfig.name)->second(sensorConfig.path); |
| 387 | } |
| 388 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 389 | if (value < 0) |
| 390 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 391 | error("Reading Sensor Utilization failed: {SENSOR}", "SENSOR", |
| 392 | sensorConfig.name); |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
| 396 | /* Remove first item from the queue */ |
| 397 | valQueue.pop_front(); |
| 398 | /* Add new item at the back */ |
| 399 | valQueue.push_back(value); |
| 400 | |
| 401 | /* Calculate average values for the given window size */ |
| 402 | double avgValue = 0; |
| 403 | avgValue = accumulate(valQueue.begin(), valQueue.end(), avgValue); |
| 404 | avgValue = avgValue / sensorConfig.windowSize; |
| 405 | |
| 406 | /* Set this new value to dbus */ |
| 407 | setSensorValueToDbus(avgValue); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame] | 408 | |
| 409 | /* Check the sensor threshold and log required message */ |
| 410 | checkSensorThreshold(avgValue); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 413 | void HealthSensor::startUnit(const std::string& sysdUnit) |
| 414 | { |
| 415 | if (sysdUnit.empty()) |
| 416 | { |
| 417 | return; |
| 418 | } |
| 419 | |
Patrick Williams | bbfe718 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 420 | sdbusplus::message_t msg = bus.new_method_call( |
Potin Lai | 156ecf3 | 2022-07-11 17:09:10 +0800 | [diff] [blame] | 421 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 422 | "org.freedesktop.systemd1.Manager", "StartUnit"); |
| 423 | msg.append(sysdUnit, "replace"); |
| 424 | bus.call_noreply(msg); |
| 425 | } |
| 426 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 427 | void HealthMon::recreateSensors() |
| 428 | { |
| 429 | PHOSPHOR_LOG2_USING; |
| 430 | healthSensors.clear(); |
| 431 | std::vector<std::string> bmcIds = {}; |
| 432 | if (FindSystemInventoryInObjectMapper(bus)) |
| 433 | { |
| 434 | try |
| 435 | { |
| 436 | // Find all BMCs (DBus objects implementing the |
| 437 | // Inventory.Item.Bmc interface that may be created by |
| 438 | // configuring the Inventory Manager) |
Patrick Williams | bbfe718 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 439 | sdbusplus::message_t msg = bus.new_method_call( |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 440 | "xyz.openbmc_project.ObjectMapper", |
| 441 | "/xyz/openbmc_project/object_mapper", |
| 442 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"); |
| 443 | |
| 444 | // Search term |
| 445 | msg.append(InventoryPath); |
| 446 | |
| 447 | // Limit the depth to 2. Example of "depth": |
| 448 | // /xyz/openbmc_project/inventory/system/chassis has a depth of |
| 449 | // 1 since it has 1 '/' after |
| 450 | // "/xyz/openbmc_project/inventory/system". |
| 451 | msg.append(2); |
| 452 | |
| 453 | // Must have the Inventory.Item.Bmc interface |
| 454 | msg.append(std::vector<std::string>{ |
| 455 | "xyz.openbmc_project.Inventory.Item.Bmc"}); |
| 456 | |
Patrick Williams | bbfe718 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 457 | sdbusplus::message_t reply = bus.call(msg, 0); |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 458 | reply.read(bmcIds); |
| 459 | info("BMC inventory found"); |
| 460 | } |
| 461 | catch (std::exception& e) |
| 462 | { |
| 463 | error("Exception occurred while calling {PATH}: {ERROR}", "PATH", |
| 464 | InventoryPath, "ERROR", e); |
| 465 | } |
| 466 | } |
| 467 | else |
| 468 | { |
| 469 | error("Path {PATH} does not exist in ObjectMapper, cannot " |
| 470 | "create association", |
| 471 | "PATH", InventoryPath); |
| 472 | } |
| 473 | // Create health sensors |
| 474 | createHealthSensors(bmcIds); |
| 475 | } |
| 476 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 477 | void printConfig(HealthConfig& cfg) |
| 478 | { |
| 479 | std::cout << "Name: " << cfg.name << "\n"; |
| 480 | std::cout << "Freq: " << (int)cfg.freq << "\n"; |
| 481 | std::cout << "Window Size: " << (int)cfg.windowSize << "\n"; |
| 482 | std::cout << "Critical value: " << (int)cfg.criticalHigh << "\n"; |
| 483 | std::cout << "warning value: " << (int)cfg.warningHigh << "\n"; |
| 484 | std::cout << "Critical log: " << (int)cfg.criticalLog << "\n"; |
| 485 | std::cout << "Warning log: " << (int)cfg.warningLog << "\n"; |
| 486 | std::cout << "Critical Target: " << cfg.criticalTgt << "\n"; |
| 487 | std::cout << "Warning Target: " << cfg.warningTgt << "\n\n"; |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 488 | std::cout << "Path : " << cfg.path << "\n\n"; |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 491 | /* Create dbus utilization sensor object for each configured sensors */ |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 492 | void HealthMon::createHealthSensors(const std::vector<std::string>& chassisIds) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 493 | { |
| 494 | for (auto& cfg : sensorConfigs) |
| 495 | { |
| 496 | std::string objPath = std::string(HEALTH_SENSOR_PATH) + cfg.name; |
Sui Chen | 670cc13 | 2021-04-13 09:27:22 -0700 | [diff] [blame] | 497 | auto healthSensor = std::make_shared<HealthSensor>(bus, objPath.c_str(), |
| 498 | cfg, chassisIds); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 499 | healthSensors.emplace(cfg.name, healthSensor); |
| 500 | |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 501 | info("{SENSOR} Health Sensor created", "SENSOR", cfg.name); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 502 | |
| 503 | /* Set configured values of crtical and warning high to dbus */ |
| 504 | healthSensor->setSensorThreshold(cfg.criticalHigh, cfg.warningHigh); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /** @brief Parsing Health config JSON file */ |
| 509 | Json HealthMon::parseConfigFile(std::string configFile) |
| 510 | { |
| 511 | std::ifstream jsonFile(configFile); |
| 512 | if (!jsonFile.is_open()) |
| 513 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 514 | error("config JSON file not found: {PATH}", "PATH", configFile); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | auto data = Json::parse(jsonFile, nullptr, false); |
| 518 | if (data.is_discarded()) |
| 519 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 520 | error("config readings JSON parser failure: {PATH}", "PATH", |
| 521 | configFile); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return data; |
| 525 | } |
| 526 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 527 | void HealthMon::getConfigData(Json& data, HealthConfig& cfg) |
| 528 | { |
| 529 | |
| 530 | static const Json empty{}; |
| 531 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 532 | /* Default frerquency of sensor polling is 1 second */ |
| 533 | cfg.freq = data.value("Frequency", 1); |
| 534 | |
| 535 | /* Default window size sensor queue is 1 */ |
| 536 | cfg.windowSize = data.value("Window_size", 1); |
| 537 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 538 | auto threshold = data.value("Threshold", empty); |
| 539 | if (!threshold.empty()) |
| 540 | { |
| 541 | auto criticalData = threshold.value("Critical", empty); |
| 542 | if (!criticalData.empty()) |
| 543 | { |
Vijay Khemka | 415dcd2 | 2020-09-21 15:58:21 -0700 | [diff] [blame] | 544 | cfg.criticalHigh = |
| 545 | criticalData.value("Value", defaultHighThreshold); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 546 | cfg.criticalLog = criticalData.value("Log", true); |
| 547 | cfg.criticalTgt = criticalData.value("Target", ""); |
| 548 | } |
| 549 | auto warningData = threshold.value("Warning", empty); |
| 550 | if (!warningData.empty()) |
| 551 | { |
Vijay Khemka | 415dcd2 | 2020-09-21 15:58:21 -0700 | [diff] [blame] | 552 | cfg.warningHigh = warningData.value("Value", defaultHighThreshold); |
| 553 | cfg.warningLog = warningData.value("Log", false); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 554 | cfg.warningTgt = warningData.value("Target", ""); |
| 555 | } |
| 556 | } |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 557 | cfg.path = data.value("Path", ""); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 560 | std::vector<HealthConfig> HealthMon::getHealthConfig() |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 561 | { |
| 562 | |
| 563 | std::vector<HealthConfig> cfgs; |
| 564 | HealthConfig cfg; |
| 565 | auto data = parseConfigFile(HEALTH_CONFIG_FILE); |
| 566 | |
| 567 | // print values |
| 568 | if (DEBUG) |
| 569 | std::cout << "Config json data:\n" << data << "\n\n"; |
| 570 | |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 571 | /* Get data items from config json data*/ |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 572 | for (auto& j : data.items()) |
| 573 | { |
| 574 | auto key = j.key(); |
Bruceleequantatw | af9acbd | 2020-10-12 15:21:42 +0800 | [diff] [blame] | 575 | /* key need match default value in map readSensors or match the key |
| 576 | * start with "Storage" or "Inode" */ |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 577 | bool isStorageOrInode = |
| 578 | (key.rfind(storage, 0) == 0 || key.rfind(inode, 0) == 0); |
| 579 | if (readSensors.find(key) != readSensors.end() || isStorageOrInode) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 580 | { |
| 581 | HealthConfig cfg = HealthConfig(); |
| 582 | cfg.name = j.key(); |
| 583 | getConfigData(j.value(), cfg); |
Bruceleequantatw | 2b231e8 | 2020-11-23 13:23:45 +0800 | [diff] [blame] | 584 | if (isStorageOrInode) |
| 585 | { |
| 586 | struct statvfs buffer |
| 587 | {}; |
| 588 | int ret = statvfs(cfg.path.c_str(), &buffer); |
| 589 | if (ret != 0) |
| 590 | { |
| 591 | auto e = errno; |
| 592 | std::cerr << "Error from statvfs: " << strerror(e) |
| 593 | << ", name: " << cfg.name |
| 594 | << ", path: " << cfg.path |
| 595 | << ", please check your settings in config file." |
| 596 | << std::endl; |
| 597 | continue; |
| 598 | } |
| 599 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 600 | cfgs.push_back(cfg); |
| 601 | if (DEBUG) |
| 602 | printConfig(cfg); |
| 603 | } |
| 604 | else |
| 605 | { |
Patrick Williams | 957e03c | 2021-09-02 16:38:42 -0500 | [diff] [blame] | 606 | error("{SENSOR} Health Sensor not supported", "SENSOR", key); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | return cfgs; |
| 610 | } |
| 611 | |
| 612 | } // namespace health |
| 613 | } // namespace phosphor |
| 614 | |
| 615 | /** |
| 616 | * @brief Main |
| 617 | */ |
| 618 | int main() |
| 619 | { |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 620 | // The io_context is needed for the timer |
| 621 | boost::asio::io_context io; |
| 622 | |
| 623 | // DBus connection |
| 624 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 625 | |
| 626 | conn->request_name(HEALTH_BUS_NAME); |
| 627 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 628 | // Get a default event loop |
| 629 | auto event = sdeventplus::Event::get_default(); |
| 630 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 631 | // Create an health monitor object |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 632 | healthMon = std::make_shared<phosphor::health::HealthMon>(*conn); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 633 | |
Yong Li | f8d7973 | 2021-03-12 09:12:19 +0800 | [diff] [blame] | 634 | // Add object manager through object_server |
| 635 | sdbusplus::asio::object_server objectServer(conn); |
Vijay Khemka | 1d0d012 | 2020-09-29 12:17:43 -0700 | [diff] [blame] | 636 | |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 637 | sdbusplus::asio::sd_event_wrapper sdEvents(io); |
| 638 | |
| 639 | sensorRecreateTimer = std::make_shared<boost::asio::deadline_timer>(io); |
| 640 | |
| 641 | // If the SystemInventory does not exist: wait for the InterfaceAdded signal |
| 642 | auto interfacesAddedSignalHandler = |
Patrick Williams | bbfe718 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 643 | std::make_unique<sdbusplus::bus::match_t>( |
| 644 | static_cast<sdbusplus::bus_t&>(*conn), |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 645 | sdbusplus::bus::match::rules::interfacesAdded( |
| 646 | phosphor::health::BMCActivationPath), |
Patrick Williams | bbfe718 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 647 | [conn](sdbusplus::message_t& msg) { |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 648 | sdbusplus::message::object_path o; |
| 649 | msg.read(o); |
Willy Tu | 7fc0aa1 | 2022-06-20 20:49:03 -0700 | [diff] [blame] | 650 | if (!needUpdate && o.str == phosphor::health::BMCActivationPath) |
Sui Chen | 036f161 | 2021-07-22 01:31:49 -0700 | [diff] [blame] | 651 | { |
| 652 | info("should recreate sensors now"); |
| 653 | needUpdate = true; |
| 654 | } |
| 655 | }); |
| 656 | |
| 657 | // Start the timer |
| 658 | io.post([]() { sensorRecreateTimerCallback(sensorRecreateTimer); }); |
| 659 | |
| 660 | io.run(); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 661 | |
| 662 | return 0; |
| 663 | } |