Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "threshold_alarm_logger.hpp" |
| 17 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame^] | 18 | #include "sdbusplus.hpp" |
| 19 | |
| 20 | #include <fmt/format.h> |
| 21 | |
| 22 | #include <phosphor-logging/log.hpp> |
| 23 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 24 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 25 | namespace sensor::monitor |
| 26 | { |
| 27 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame^] | 28 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 29 | using namespace phosphor::logging; |
| 30 | using namespace phosphor::fan::util; |
| 31 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 32 | const std::string warningInterface = |
| 33 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
| 34 | const std::string criticalInterface = |
| 35 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 36 | const std::string perfLossInterface = |
| 37 | "xyz.openbmc_project.Sensor.Threshold.PerformanceLoss"; |
| 38 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame^] | 39 | using ErrorData = std::tuple<ErrorName, Entry::Level>; |
| 40 | |
| 41 | /** |
| 42 | * Map of threshold interfaces and alarm properties and values to error data. |
| 43 | */ |
| 44 | const std::map<InterfaceName, std::map<PropertyName, std::map<bool, ErrorData>>> |
| 45 | thresholdData{ |
| 46 | |
| 47 | {warningInterface, |
| 48 | {{"WarningAlarmHigh", |
| 49 | {{true, ErrorData{"WarningHigh", Entry::Level::Warning}}, |
| 50 | {false, |
| 51 | ErrorData{"WarningHighClear", Entry::Level::Informational}}}}, |
| 52 | {"WarningAlarmLow", |
| 53 | {{true, ErrorData{"WarningLow", Entry::Level::Warning}}, |
| 54 | {false, |
| 55 | ErrorData{"WarningLowClear", Entry::Level::Informational}}}}}}, |
| 56 | |
| 57 | {criticalInterface, |
| 58 | {{"CriticalAlarmHigh", |
| 59 | {{true, ErrorData{"CriticalHigh", Entry::Level::Critical}}, |
| 60 | {false, |
| 61 | ErrorData{"CriticalHighClear", Entry::Level::Informational}}}}, |
| 62 | {"CriticalAlarmLow", |
| 63 | {{true, ErrorData{"CriticalLow", Entry::Level::Critical}}, |
| 64 | {false, |
| 65 | ErrorData{"CriticalLowClear", Entry::Level::Informational}}}}}}, |
| 66 | |
| 67 | {perfLossInterface, |
| 68 | {{"PerfLossAlarmHigh", |
| 69 | {{true, ErrorData{"PerfLossHigh", Entry::Level::Warning}}, |
| 70 | {false, |
| 71 | ErrorData{"PerfLossHighClear", Entry::Level::Informational}}}}, |
| 72 | {"PerfLossAlarmLow", |
| 73 | {{true, ErrorData{"PerfLossLow", Entry::Level::Warning}}, |
| 74 | {false, |
| 75 | ErrorData{"PerfLossLowClear", Entry::Level::Informational}}}}}}}; |
| 76 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 77 | ThresholdAlarmLogger::ThresholdAlarmLogger(sdbusplus::bus::bus& bus, |
| 78 | sdeventplus::Event& event) : |
| 79 | bus(bus), |
| 80 | event(event), |
| 81 | warningMatch(bus, |
| 82 | "type='signal',member='PropertiesChanged'," |
| 83 | "path_namespace='/xyz/openbmc_project/sensors'," |
| 84 | "arg0='" + |
| 85 | warningInterface + "'", |
| 86 | std::bind(&ThresholdAlarmLogger::propertiesChanged, this, |
| 87 | std::placeholders::_1)), |
| 88 | criticalMatch(bus, |
| 89 | "type='signal',member='PropertiesChanged'," |
| 90 | "path_namespace='/xyz/openbmc_project/sensors'," |
| 91 | "arg0='" + |
| 92 | criticalInterface + "'", |
| 93 | std::bind(&ThresholdAlarmLogger::propertiesChanged, this, |
| 94 | std::placeholders::_1)), |
| 95 | perfLossMatch(bus, |
| 96 | "type='signal',member='PropertiesChanged'," |
| 97 | "path_namespace='/xyz/openbmc_project/sensors'," |
| 98 | "arg0='" + |
| 99 | perfLossInterface + "'", |
| 100 | std::bind(&ThresholdAlarmLogger::propertiesChanged, this, |
| 101 | std::placeholders::_1)) |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame^] | 102 | { |
| 103 | // check for any currently asserted threshold alarms |
| 104 | std::for_each( |
| 105 | thresholdData.begin(), thresholdData.end(), |
| 106 | [this](const auto& thresholdInterface) { |
| 107 | const auto& interface = thresholdInterface.first; |
| 108 | auto objects = |
| 109 | SDBusPlus::getSubTreeRaw(this->bus, "/", interface, 0); |
| 110 | std::for_each(objects.begin(), objects.end(), |
| 111 | [interface, this](const auto& object) { |
| 112 | const auto& path = object.first; |
| 113 | const auto& service = |
| 114 | object.second.begin()->first; |
| 115 | checkThresholds(interface, path, service); |
| 116 | }); |
| 117 | }); |
| 118 | } |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 119 | |
| 120 | void ThresholdAlarmLogger::propertiesChanged(sdbusplus::message::message& msg) |
| 121 | { |
| 122 | // TODO |
| 123 | } |
| 124 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame^] | 125 | void ThresholdAlarmLogger::checkThresholds(const std::string& interface, |
| 126 | const std::string& sensorPath, |
| 127 | const std::string& service) |
| 128 | { |
| 129 | auto properties = thresholdData.find(interface); |
| 130 | if (properties == thresholdData.end()) |
| 131 | { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | for (const auto& [property, unused] : properties->second) |
| 136 | { |
| 137 | try |
| 138 | { |
| 139 | auto alarmValue = SDBusPlus::getProperty<bool>( |
| 140 | bus, service, sensorPath, interface, property); |
| 141 | alarms[InterfaceKey(sensorPath, interface)][property] = alarmValue; |
| 142 | |
| 143 | // This is just for checking alarms on startup, |
| 144 | // so only look for active alarms. |
| 145 | if (alarmValue) |
| 146 | { |
| 147 | createEventLog(sensorPath, interface, property, alarmValue); |
| 148 | } |
| 149 | } |
| 150 | catch (const DBusError& e) |
| 151 | { |
| 152 | log<level::ERR>( |
| 153 | fmt::format("Failed reading sensor threshold properties: {}", |
| 154 | e.what()) |
| 155 | .c_str()); |
| 156 | continue; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ThresholdAlarmLogger::createEventLog(const std::string& sensorPath, |
| 162 | const std::string& interface, |
| 163 | const std::string& alarmProperty, |
| 164 | bool alarmValue) |
| 165 | { |
| 166 | // TODO |
| 167 | } |
| 168 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 169 | } // namespace sensor::monitor |