Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Václav Kubernát | 069d3a9 | 2021-11-14 12:37:46 +0100 | [diff] [blame] | 8 | #include <chrono> |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include "IETFHardware.h" |
| 11 | #include "utils/log.h" |
| 12 | #include "utils/time.h" |
| 13 | |
| 14 | using namespace std::literals; |
| 15 | |
| 16 | namespace { |
| 17 | |
Tomáš Pecka | 83b62e1 | 2020-12-16 14:50:49 +0100 | [diff] [blame] | 18 | static const std::string ietfHardwareStatePrefix = "/ietf-hardware:hardware"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 19 | |
| 20 | /** @brief Constructs a full XPath for a specific component */ |
| 21 | std::string xpathForComponent(const std::string& componentName) |
| 22 | { |
| 23 | return ietfHardwareStatePrefix + "/component[name='" + componentName + "']/"; |
| 24 | } |
| 25 | |
| 26 | /** @brief Prefix all properties from values DataTree with a component name (calculated from @p componentName) and push them into the DataTree */ |
| 27 | void addComponent(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::optional<std::string>& parent, const velia::ietf_hardware::DataTree& values) |
| 28 | { |
| 29 | auto componentPrefix = xpathForComponent(componentName); |
| 30 | |
| 31 | if (parent) { |
| 32 | res[componentPrefix + "parent"] = *parent; |
| 33 | } |
| 34 | for (const auto& [k, v] : values) { |
| 35 | res[componentPrefix + k] = v; |
| 36 | } |
Tomáš Pecka | 7eb0c42 | 2023-04-21 15:36:33 +0200 | [diff] [blame] | 37 | |
| 38 | res[componentPrefix + "state/oper-state"] = "enabled"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** @brief Write a sensor-data @p value for a component @p componentName and push it into the @p res DataTree */ |
| 42 | void addSensorValue(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::string& value) |
| 43 | { |
| 44 | const auto componentPrefix = xpathForComponent(componentName); |
| 45 | res[componentPrefix + "sensor-data/value"] = value; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | namespace velia::ietf_hardware { |
| 50 | |
| 51 | IETFHardware::IETFHardware() = default; |
| 52 | |
| 53 | IETFHardware::~IETFHardware() = default; |
| 54 | |
| 55 | std::map<std::string, std::string> IETFHardware::process() |
| 56 | { |
| 57 | std::map<std::string, std::string> res; |
| 58 | |
| 59 | for (auto& dataReader : m_callbacks) { |
| 60 | res.merge(dataReader()); |
| 61 | } |
| 62 | |
| 63 | res[ietfHardwareStatePrefix + "/last-change"] = velia::utils::yangTimeFormat(std::chrono::system_clock::now()); |
| 64 | return res; |
| 65 | } |
| 66 | |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 67 | /** @brief A namespace containing predefined data readers for IETFHardware class. |
| 68 | * @see IETFHardware for more information |
| 69 | */ |
| 70 | namespace data_reader { |
| 71 | |
| 72 | DataReader::DataReader(std::string componentName, std::optional<std::string> parent) |
| 73 | : m_componentName(std::move(componentName)) |
| 74 | , m_parent(std::move(parent)) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | /** @brief Constructs a component without any sensor-data and provide only the static data @p dataTree passed via constructor. |
| 79 | * @param componentName the name of the component in the resulting tree |
| 80 | * @param parent The component in YANG model has a link to parent. Specify who is the parent. |
| 81 | * @param dataTree static data to insert into the resulting tree. The dataTree keys should only contain the YANG node name, not full XPath. The full XPath is constructed from @componentName and the map key. |
| 82 | */ |
| 83 | StaticData::StaticData(std::string componentName, std::optional<std::string> parent, DataTree dataTree) |
| 84 | : DataReader(std::move(componentName), std::move(parent)) |
| 85 | { |
| 86 | addComponent(m_staticData, |
| 87 | m_componentName, |
| 88 | m_parent, |
| 89 | dataTree); |
| 90 | } |
| 91 | |
| 92 | DataTree StaticData::operator()() const { return m_staticData; } |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 93 | ThresholdsBySensorPath StaticData::thresholds() const { return {}; } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 94 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 95 | Fans::Fans(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, unsigned fanChannelsCount, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 96 | : DataReader(std::move(componentName), std::move(parent)) |
| 97 | , m_hwmon(std::move(hwmon)) |
| 98 | , m_fanChannelsCount(fanChannelsCount) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 99 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 100 | { |
| 101 | // fans |
| 102 | addComponent(m_staticData, |
| 103 | m_componentName, |
| 104 | m_parent, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 105 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 106 | {"class", "iana-hardware:module"}, // FIXME: Read (or pass via constructor) additional properties (mfg, model, ...). They should be in the fans' tray EEPROM. |
| 107 | }); |
| 108 | |
| 109 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 110 | // fans -> fan_i |
| 111 | addComponent(m_staticData, |
| 112 | m_componentName + ":fan" + std::to_string(i), |
| 113 | m_componentName, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 114 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 115 | {"class", "iana-hardware:fan"}, |
| 116 | }); |
| 117 | |
| 118 | // fans -> fan_i -> sensor-data |
| 119 | addComponent(m_staticData, |
| 120 | m_componentName + ":fan" + std::to_string(i) + ":rpm", |
| 121 | m_componentName + ":fan" + std::to_string(i), |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 122 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 123 | {"class", "iana-hardware:sensor"}, |
| 124 | {"sensor-data/value-type", "rpm"}, |
| 125 | {"sensor-data/value-scale", "units"}, |
| 126 | {"sensor-data/value-precision", "0"}, |
| 127 | {"sensor-data/oper-status", "ok"}, |
| 128 | }); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | DataTree Fans::operator()() const |
| 133 | { |
| 134 | DataTree res(m_staticData); |
| 135 | |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 136 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 137 | const auto sensorComponentName = m_componentName + ":fan" + std::to_string(i) + ":rpm"; |
| 138 | const auto attribute = "fan"s + std::to_string(i) + "_input"; |
| 139 | |
Václav Kubernát | b0939dd | 2021-04-28 04:08:48 +0200 | [diff] [blame] | 140 | addSensorValue(res, sensorComponentName, std::to_string(m_hwmon->attribute(attribute))); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return res; |
| 144 | } |
| 145 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 146 | ThresholdsBySensorPath Fans::thresholds() const |
| 147 | { |
| 148 | ThresholdsBySensorPath res; |
| 149 | |
| 150 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 151 | res.emplace(xpathForComponent(m_componentName + ":fan" + std::to_string(i) + ":rpm") + "sensor-data/value", m_thresholds); |
| 152 | } |
| 153 | |
| 154 | return res; |
| 155 | } |
| 156 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 157 | std::string getSysfsFilename(const SensorType type, int sysfsChannelNr) |
| 158 | { |
| 159 | switch (type) { |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 160 | case SensorType::Temperature: |
| 161 | return "temp"s + std::to_string(sysfsChannelNr) + "_input"; |
| 162 | case SensorType::Current: |
| 163 | return "curr"s + std::to_string(sysfsChannelNr) + "_input"; |
| 164 | case SensorType::Power: |
| 165 | return "power"s + std::to_string(sysfsChannelNr) + "_input"; |
| 166 | case SensorType::VoltageAC: |
| 167 | case SensorType::VoltageDC: |
| 168 | return "in"s + std::to_string(sysfsChannelNr) + "_input"; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | __builtin_unreachable(); |
| 172 | } |
| 173 | |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 174 | template <SensorType TYPE> |
| 175 | const DataTree sysfsStaticData; |
| 176 | template <> |
| 177 | const DataTree sysfsStaticData<SensorType::Temperature> = { |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 178 | {"class", "iana-hardware:sensor"}, |
| 179 | {"sensor-data/value-type", "celsius"}, |
| 180 | {"sensor-data/value-scale", "milli"}, |
| 181 | {"sensor-data/value-precision", "0"}, |
| 182 | {"sensor-data/oper-status", "ok"}, |
| 183 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 184 | template <> |
| 185 | const DataTree sysfsStaticData<SensorType::Current> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 186 | {"class", "iana-hardware:sensor"}, |
| 187 | {"sensor-data/value-type", "amperes"}, |
| 188 | {"sensor-data/value-scale", "milli"}, |
| 189 | {"sensor-data/value-precision", "0"}, |
| 190 | {"sensor-data/oper-status", "ok"}, |
| 191 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 192 | template <> |
| 193 | const DataTree sysfsStaticData<SensorType::Power> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 194 | {"class", "iana-hardware:sensor"}, |
| 195 | {"sensor-data/value-type", "watts"}, |
| 196 | {"sensor-data/value-scale", "micro"}, |
| 197 | {"sensor-data/value-precision", "0"}, |
| 198 | {"sensor-data/oper-status", "ok"}, |
| 199 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 200 | template <> |
| 201 | const DataTree sysfsStaticData<SensorType::VoltageAC> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 202 | {"class", "iana-hardware:sensor"}, |
| 203 | {"sensor-data/value-type", "volts-AC"}, |
Tomáš Pecka | 7632360 | 2022-05-11 09:33:59 +0200 | [diff] [blame] | 204 | {"sensor-data/value-scale", "milli"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 205 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 206 | {"sensor-data/oper-status", "ok"}}; |
| 207 | template <> |
| 208 | const DataTree sysfsStaticData<SensorType::VoltageDC> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 209 | {"class", "iana-hardware:sensor"}, |
| 210 | {"sensor-data/value-type", "volts-DC"}, |
Tomáš Pecka | 7632360 | 2022-05-11 09:33:59 +0200 | [diff] [blame] | 211 | {"sensor-data/value-scale", "milli"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 212 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 213 | {"sensor-data/oper-status", "ok"}}; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 214 | |
| 215 | template <SensorType TYPE> |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 216 | SysfsValue<TYPE>::SysfsValue(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 217 | : DataReader(std::move(componentName), std::move(parent)) |
| 218 | , m_hwmon(std::move(hwmon)) |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 219 | , m_sysfsFile(getSysfsFilename(TYPE, sysfsChannelNr)) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 220 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 221 | { |
| 222 | addComponent(m_staticData, |
| 223 | m_componentName, |
| 224 | m_parent, |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 225 | sysfsStaticData<TYPE>); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 228 | template <SensorType TYPE> |
| 229 | DataTree SysfsValue<TYPE>::operator()() const |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 230 | { |
| 231 | DataTree res(m_staticData); |
| 232 | |
Václav Kubernát | 0dee6b9 | 2021-04-13 09:14:04 +0200 | [diff] [blame] | 233 | int64_t sensorValue = m_hwmon->attribute(m_sysfsFile); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 234 | addSensorValue(res, m_componentName, std::to_string(sensorValue)); |
| 235 | |
| 236 | return res; |
| 237 | } |
| 238 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 239 | template <SensorType TYPE> |
| 240 | ThresholdsBySensorPath SysfsValue<TYPE>::thresholds() const |
| 241 | { |
| 242 | return {{xpathForComponent(m_componentName) + "sensor-data/value", m_thresholds}}; |
| 243 | } |
| 244 | |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 245 | template struct SysfsValue<SensorType::Current>; |
| 246 | template struct SysfsValue<SensorType::Power>; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 247 | template struct SysfsValue<SensorType::Temperature>; |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 248 | template struct SysfsValue<SensorType::VoltageAC>; |
| 249 | template struct SysfsValue<SensorType::VoltageDC>; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 250 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 251 | EMMC::EMMC(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 252 | : DataReader(std::move(componentName), std::move(parent)) |
| 253 | , m_emmc(std::move(emmc)) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 254 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 255 | { |
| 256 | auto emmcAttrs = m_emmc->attributes(); |
| 257 | |
| 258 | // date is specified in MM/YYYY format (source: kernel core/mmc.c) and mfg-date is unfortunately of type yang:date-and-time |
| 259 | std::string mfgDate = emmcAttrs.at("date"); |
Tomáš Pecka | f1cba74 | 2023-05-03 16:26:37 +0200 | [diff] [blame] | 260 | std::chrono::year_month_day calendarDate( |
| 261 | std::chrono::year(std::stoi(mfgDate.substr(3, 4))), |
| 262 | std::chrono::month(std::stoi(mfgDate.substr(0, 2))), |
| 263 | std::chrono::day(1)); |
| 264 | mfgDate = velia::utils::yangTimeFormat(std::chrono::sys_days{calendarDate}); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 265 | |
| 266 | addComponent(m_staticData, |
| 267 | m_componentName, |
| 268 | m_parent, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 269 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 270 | {"class", "iana-hardware:module"}, |
| 271 | {"mfg-date", mfgDate}, |
| 272 | {"serial-num", emmcAttrs.at("serial")}, |
| 273 | {"model-name", emmcAttrs.at("name")}, |
| 274 | }); |
| 275 | |
| 276 | addComponent(m_staticData, |
| 277 | m_componentName + ":lifetime", |
| 278 | m_componentName, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 279 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 280 | {"class", "iana-hardware:sensor"}, |
| 281 | {"sensor-data/value-type", "other"}, |
| 282 | {"sensor-data/value-scale", "units"}, |
| 283 | {"sensor-data/value-precision", "0"}, |
| 284 | {"sensor-data/oper-status", "ok"}, |
| 285 | {"sensor-data/units-display", "percent"s}, |
| 286 | }); |
| 287 | } |
| 288 | |
| 289 | DataTree EMMC::operator()() const |
| 290 | { |
| 291 | DataTree res(m_staticData); |
| 292 | |
| 293 | auto emmcAttrs = m_emmc->attributes(); |
| 294 | addSensorValue(res, m_componentName + ":lifetime", emmcAttrs.at("life_time")); |
| 295 | |
| 296 | return res; |
| 297 | } |
Tomáš Pecka | 2a4c9f6 | 2023-03-26 10:54:57 +0200 | [diff] [blame] | 298 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 299 | ThresholdsBySensorPath EMMC::thresholds() const |
Tomáš Pecka | 2a4c9f6 | 2023-03-26 10:54:57 +0200 | [diff] [blame] | 300 | { |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 301 | return {{xpathForComponent(m_componentName + ":lifetime") + "sensor-data/value", m_thresholds}}; |
Tomáš Pecka | 2a4c9f6 | 2023-03-26 10:54:57 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | DataTree Group::operator()() const |
| 305 | { |
| 306 | DataTree res; |
| 307 | for (const auto& reader : m_readers) { |
| 308 | res.merge(reader()); |
| 309 | } |
| 310 | return res; |
| 311 | } |
| 312 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame^] | 313 | ThresholdsBySensorPath Group::thresholds() const |
| 314 | { |
| 315 | return m_thresholds; |
| 316 | } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 317 | } |
| 318 | } |