Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "healthMonitor.hpp" |
| 4 | |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <sdeventplus/event.hpp> |
| 7 | |
| 8 | #include <fstream> |
| 9 | #include <iostream> |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 10 | #include <numeric> |
| 11 | #include <sstream> |
| 12 | |
| 13 | extern "C" |
| 14 | { |
| 15 | #include <sys/sysinfo.h> |
| 16 | } |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 17 | |
| 18 | static constexpr bool DEBUG = false; |
| 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace health |
| 23 | { |
| 24 | |
| 25 | using namespace phosphor::logging; |
| 26 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 27 | enum CPUStatesTime |
| 28 | { |
| 29 | USER_IDX = 0, |
| 30 | NICE_IDX, |
| 31 | SYSTEM_IDX, |
| 32 | IDLE_IDX, |
| 33 | IOWAIT_IDX, |
| 34 | IRQ_IDX, |
| 35 | SOFTIRQ_IDX, |
| 36 | STEAL_IDX, |
| 37 | GUEST_USER_IDX, |
| 38 | GUEST_NICE_IDX, |
| 39 | NUM_CPU_STATES_TIME |
| 40 | }; |
| 41 | |
| 42 | double readCPUUtilization() |
| 43 | { |
| 44 | std::ifstream fileStat("/proc/stat"); |
| 45 | if (!fileStat.is_open()) |
| 46 | { |
| 47 | log<level::ERR>("cpu file not available", |
| 48 | entry("FILENAME = /proc/stat")); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | std::string firstLine, labelName; |
| 53 | std::size_t timeData[NUM_CPU_STATES_TIME]; |
| 54 | |
| 55 | std::getline(fileStat, firstLine); |
| 56 | std::stringstream ss(firstLine); |
| 57 | ss >> labelName; |
| 58 | |
| 59 | if (DEBUG) |
| 60 | std::cout << "CPU stats first Line is " << firstLine << "\n"; |
| 61 | |
| 62 | if (labelName.compare("cpu")) |
| 63 | { |
| 64 | log<level::ERR>("CPU data not available"); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | int i; |
| 69 | for (i = 0; i < NUM_CPU_STATES_TIME; i++) |
| 70 | { |
| 71 | if (!(ss >> timeData[i])) |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if (i != NUM_CPU_STATES_TIME) |
| 76 | { |
| 77 | log<level::ERR>("CPU data not correct"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | static double preActiveTime = 0, preIdleTime = 0; |
| 82 | double activeTime, activeTimeDiff, idleTime, idleTimeDiff, totalTime, |
| 83 | activePercValue; |
| 84 | |
| 85 | idleTime = timeData[IDLE_IDX] + timeData[IOWAIT_IDX]; |
| 86 | activeTime = timeData[USER_IDX] + timeData[NICE_IDX] + |
| 87 | timeData[SYSTEM_IDX] + timeData[IRQ_IDX] + |
| 88 | timeData[SOFTIRQ_IDX] + timeData[STEAL_IDX] + |
| 89 | timeData[GUEST_USER_IDX] + timeData[GUEST_NICE_IDX]; |
| 90 | |
| 91 | idleTimeDiff = idleTime - preIdleTime; |
| 92 | activeTimeDiff = activeTime - preActiveTime; |
| 93 | |
| 94 | /* Store current idle and active time for next calculation */ |
| 95 | preIdleTime = idleTime; |
| 96 | preActiveTime = activeTime; |
| 97 | |
| 98 | totalTime = idleTimeDiff + activeTimeDiff; |
| 99 | |
| 100 | activePercValue = activeTimeDiff / totalTime * 100; |
| 101 | |
| 102 | if (DEBUG) |
| 103 | std::cout << "CPU Utilization is " << activePercValue << "\n"; |
| 104 | |
| 105 | return activePercValue; |
| 106 | } |
| 107 | |
| 108 | double readMemoryUtilization() |
| 109 | { |
| 110 | struct sysinfo s_info; |
| 111 | |
| 112 | sysinfo(&s_info); |
| 113 | double usedRam = s_info.totalram - s_info.freeram; |
| 114 | double memUsePerc = usedRam / s_info.totalram * 100; |
| 115 | |
| 116 | if (DEBUG) |
| 117 | { |
| 118 | std::cout << "Memory Utilization is " << memUsePerc << "\n"; |
| 119 | |
| 120 | std::cout << "TotalRam: " << s_info.totalram |
| 121 | << " FreeRam: " << s_info.freeram << "\n"; |
| 122 | std::cout << "UseRam: " << usedRam << "\n"; |
| 123 | } |
| 124 | |
| 125 | return memUsePerc; |
| 126 | } |
| 127 | |
| 128 | /** Map of read function for each health sensors supported */ |
| 129 | std::map<std::string, std::function<double()>> readSensors = { |
| 130 | {"CPU", readCPUUtilization}, {"Memory", readMemoryUtilization}}; |
| 131 | |
| 132 | void HealthSensor::setSensorThreshold(double criticalHigh, double warningHigh) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 133 | { |
| 134 | CriticalInterface::criticalHigh(criticalHigh); |
| 135 | WarningInterface::warningHigh(warningHigh); |
| 136 | } |
| 137 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 138 | void HealthSensor::setSensorValueToDbus(const double value) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 139 | { |
| 140 | ValueIface::value(value); |
| 141 | } |
| 142 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 143 | void HealthSensor::initHealthSensor() |
| 144 | { |
| 145 | std::string logMsg = sensorConfig.name + " Health Sensor initialized"; |
| 146 | log<level::INFO>(logMsg.c_str()); |
| 147 | |
| 148 | /* Look for sensor read functions */ |
| 149 | if (readSensors.find(sensorConfig.name) == readSensors.end()) |
| 150 | { |
| 151 | log<level::ERR>("Sensor read function not available"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | /* Read Sensor values */ |
| 156 | auto value = readSensors[sensorConfig.name](); |
| 157 | |
| 158 | if (value < 0) |
| 159 | { |
| 160 | log<level::ERR>("Reading Sensor Utilization failed", |
| 161 | entry("NAME = %s", sensorConfig.name.c_str())); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | /* Initialize value queue with initail sensor reading */ |
| 166 | for (int i = 0; i < sensorConfig.windowSize; i++) |
| 167 | { |
| 168 | valQueue.push_back(value); |
| 169 | } |
| 170 | setSensorValueToDbus(value); |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 171 | |
| 172 | /* Start the timer for reading sensor data at regular interval */ |
| 173 | readTimer.restart(std::chrono::milliseconds(sensorConfig.freq * 1000)); |
| 174 | } |
| 175 | |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame^] | 176 | void HealthSensor::checkSensorThreshold(const double value) |
| 177 | { |
| 178 | if (value > sensorConfig.criticalHigh) |
| 179 | { |
| 180 | if (!CriticalInterface::criticalAlarmHigh()) |
| 181 | { |
| 182 | CriticalInterface::criticalAlarmHigh(true); |
| 183 | if (sensorConfig.criticalLog) |
| 184 | log<level::ERR>("ASSERT: Utilization Sensor has exceeded " |
| 185 | "critical high threshold", |
| 186 | entry("NAME = %s", sensorConfig.name.c_str())); |
| 187 | } |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | if (CriticalInterface::criticalAlarmHigh()) |
| 192 | { |
| 193 | CriticalInterface::criticalAlarmHigh(false); |
| 194 | if (sensorConfig.criticalLog) |
| 195 | log<level::INFO>("DEASSERT: Utilization Sensor is under " |
| 196 | "critical high threshold", |
| 197 | entry("NAME = %s", sensorConfig.name.c_str())); |
| 198 | } |
| 199 | |
| 200 | if ((value > sensorConfig.warningHigh) && |
| 201 | (!WarningInterface::warningAlarmHigh())) |
| 202 | { |
| 203 | WarningInterface::warningAlarmHigh(true); |
| 204 | if (sensorConfig.warningLog) |
| 205 | log<level::ERR>("ASSERT: Utilization Sensor has exceeded " |
| 206 | "warning high threshold", |
| 207 | entry("NAME = %s", sensorConfig.name.c_str())); |
| 208 | } |
| 209 | else if ((value <= sensorConfig.warningHigh) && |
| 210 | (WarningInterface::warningAlarmHigh())) |
| 211 | { |
| 212 | WarningInterface::warningAlarmHigh(false); |
| 213 | if (sensorConfig.warningLog) |
| 214 | log<level::INFO>("DEASSERT: Utilization Sensor is under " |
| 215 | "warning high threshold", |
| 216 | entry("NAME = %s", sensorConfig.name.c_str())); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Vijay Khemka | b38fd58 | 2020-07-23 13:21:23 -0700 | [diff] [blame] | 221 | void HealthSensor::readHealthSensor() |
| 222 | { |
| 223 | /* Read current sensor value */ |
| 224 | double value = readSensors[sensorConfig.name](); |
| 225 | if (value < 0) |
| 226 | { |
| 227 | log<level::ERR>("Reading Sensor Utilization failed", |
| 228 | entry("NAME = %s", sensorConfig.name.c_str())); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | /* Remove first item from the queue */ |
| 233 | valQueue.pop_front(); |
| 234 | /* Add new item at the back */ |
| 235 | valQueue.push_back(value); |
| 236 | |
| 237 | /* Calculate average values for the given window size */ |
| 238 | double avgValue = 0; |
| 239 | avgValue = accumulate(valQueue.begin(), valQueue.end(), avgValue); |
| 240 | avgValue = avgValue / sensorConfig.windowSize; |
| 241 | |
| 242 | /* Set this new value to dbus */ |
| 243 | setSensorValueToDbus(avgValue); |
Vijay Khemka | b7a7b8a | 2020-07-29 12:22:01 -0700 | [diff] [blame^] | 244 | |
| 245 | /* Check the sensor threshold and log required message */ |
| 246 | checkSensorThreshold(avgValue); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void printConfig(HealthConfig& cfg) |
| 250 | { |
| 251 | std::cout << "Name: " << cfg.name << "\n"; |
| 252 | std::cout << "Freq: " << (int)cfg.freq << "\n"; |
| 253 | std::cout << "Window Size: " << (int)cfg.windowSize << "\n"; |
| 254 | std::cout << "Critical value: " << (int)cfg.criticalHigh << "\n"; |
| 255 | std::cout << "warning value: " << (int)cfg.warningHigh << "\n"; |
| 256 | std::cout << "Critical log: " << (int)cfg.criticalLog << "\n"; |
| 257 | std::cout << "Warning log: " << (int)cfg.warningLog << "\n"; |
| 258 | std::cout << "Critical Target: " << cfg.criticalTgt << "\n"; |
| 259 | std::cout << "Warning Target: " << cfg.warningTgt << "\n\n"; |
| 260 | } |
| 261 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 262 | /* Create dbus utilization sensor object for each configured sensors */ |
| 263 | void HealthMon::createHealthSensors() |
| 264 | { |
| 265 | for (auto& cfg : sensorConfigs) |
| 266 | { |
| 267 | std::string objPath = std::string(HEALTH_SENSOR_PATH) + cfg.name; |
| 268 | auto healthSensor = |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 269 | std::make_shared<HealthSensor>(bus, objPath.c_str(), cfg); |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 270 | healthSensors.emplace(cfg.name, healthSensor); |
| 271 | |
| 272 | std::string logMsg = cfg.name + " Health Sensor created"; |
| 273 | log<level::INFO>(logMsg.c_str(), entry("NAME = %s", cfg.name.c_str())); |
| 274 | |
| 275 | /* Set configured values of crtical and warning high to dbus */ |
| 276 | healthSensor->setSensorThreshold(cfg.criticalHigh, cfg.warningHigh); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** @brief Parsing Health config JSON file */ |
| 281 | Json HealthMon::parseConfigFile(std::string configFile) |
| 282 | { |
| 283 | std::ifstream jsonFile(configFile); |
| 284 | if (!jsonFile.is_open()) |
| 285 | { |
| 286 | log<level::ERR>("config JSON file not found", |
| 287 | entry("FILENAME = %s", configFile.c_str())); |
| 288 | } |
| 289 | |
| 290 | auto data = Json::parse(jsonFile, nullptr, false); |
| 291 | if (data.is_discarded()) |
| 292 | { |
| 293 | log<level::ERR>("config readings JSON parser failure", |
| 294 | entry("FILENAME = %s", configFile.c_str())); |
| 295 | } |
| 296 | |
| 297 | return data; |
| 298 | } |
| 299 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 300 | void HealthMon::getConfigData(Json& data, HealthConfig& cfg) |
| 301 | { |
| 302 | |
| 303 | static const Json empty{}; |
| 304 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 305 | /* Default frerquency of sensor polling is 1 second */ |
| 306 | cfg.freq = data.value("Frequency", 1); |
| 307 | |
| 308 | /* Default window size sensor queue is 1 */ |
| 309 | cfg.windowSize = data.value("Window_size", 1); |
| 310 | |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 311 | auto threshold = data.value("Threshold", empty); |
| 312 | if (!threshold.empty()) |
| 313 | { |
| 314 | auto criticalData = threshold.value("Critical", empty); |
| 315 | if (!criticalData.empty()) |
| 316 | { |
| 317 | cfg.criticalHigh = criticalData.value("Value", 0); |
| 318 | cfg.criticalLog = criticalData.value("Log", true); |
| 319 | cfg.criticalTgt = criticalData.value("Target", ""); |
| 320 | } |
| 321 | auto warningData = threshold.value("Warning", empty); |
| 322 | if (!warningData.empty()) |
| 323 | { |
| 324 | cfg.warningHigh = warningData.value("Value", 0); |
| 325 | cfg.warningLog = warningData.value("Log", true); |
| 326 | cfg.warningTgt = warningData.value("Target", ""); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 331 | std::vector<HealthConfig> HealthMon::getHealthConfig() |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 332 | { |
| 333 | |
| 334 | std::vector<HealthConfig> cfgs; |
| 335 | HealthConfig cfg; |
| 336 | auto data = parseConfigFile(HEALTH_CONFIG_FILE); |
| 337 | |
| 338 | // print values |
| 339 | if (DEBUG) |
| 340 | std::cout << "Config json data:\n" << data << "\n\n"; |
| 341 | |
| 342 | /* Get CPU config data */ |
| 343 | for (auto& j : data.items()) |
| 344 | { |
| 345 | auto key = j.key(); |
Vijay Khemka | 1553776 | 2020-07-22 11:44:56 -0700 | [diff] [blame] | 346 | if (readSensors.find(key) != readSensors.end()) |
Vijay Khemka | e279530 | 2020-07-15 17:28:45 -0700 | [diff] [blame] | 347 | { |
| 348 | HealthConfig cfg = HealthConfig(); |
| 349 | cfg.name = j.key(); |
| 350 | getConfigData(j.value(), cfg); |
| 351 | cfgs.push_back(cfg); |
| 352 | if (DEBUG) |
| 353 | printConfig(cfg); |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | std::string logMsg = key + " Health Sensor not supported"; |
| 358 | log<level::ERR>(logMsg.c_str(), entry("NAME = %s", key.c_str())); |
| 359 | } |
| 360 | } |
| 361 | return cfgs; |
| 362 | } |
| 363 | |
| 364 | } // namespace health |
| 365 | } // namespace phosphor |
| 366 | |
| 367 | /** |
| 368 | * @brief Main |
| 369 | */ |
| 370 | int main() |
| 371 | { |
| 372 | |
| 373 | // Get a default event loop |
| 374 | auto event = sdeventplus::Event::get_default(); |
| 375 | |
| 376 | // Get a handle to system dbus |
| 377 | auto bus = sdbusplus::bus::new_default(); |
| 378 | |
| 379 | // Create an health monitor object |
| 380 | phosphor::health::HealthMon healthMon(bus); |
| 381 | |
| 382 | // Request service bus name |
| 383 | bus.request_name(HEALTH_BUS_NAME); |
| 384 | |
| 385 | // Attach the bus to sd_event to service user requests |
| 386 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 387 | event.loop(); |
| 388 | |
| 389 | return 0; |
| 390 | } |