Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 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 | */ |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 16 | #include "manager.hpp" |
| 17 | |
| 18 | #include "additional_data.hpp" |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 19 | #include "pel.hpp" |
| 20 | |
| 21 | #include <filesystem> |
| 22 | #include <fstream> |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 23 | |
| 24 | namespace openpower |
| 25 | { |
| 26 | namespace pels |
| 27 | { |
| 28 | |
| 29 | using namespace phosphor::logging; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 30 | namespace fs = std::filesystem; |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 31 | |
| 32 | namespace additional_data |
| 33 | { |
| 34 | constexpr auto rawPEL = "RAWPEL"; |
| 35 | } |
| 36 | |
| 37 | void Manager::create(const std::string& message, uint32_t obmcLogID, |
| 38 | uint64_t timestamp, Entry::Level severity, |
| 39 | const std::vector<std::string>& additionalData, |
| 40 | const std::vector<std::string>& associations) |
| 41 | { |
| 42 | AdditionalData ad{additionalData}; |
| 43 | |
| 44 | // If a PEL was passed in, use that. Otherwise, create one. |
| 45 | auto rawPelPath = ad.getValue(additional_data::rawPEL); |
| 46 | if (rawPelPath) |
| 47 | { |
| 48 | addRawPEL(*rawPelPath, obmcLogID); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | createPEL(message, obmcLogID, timestamp, severity, additionalData, |
| 53 | associations); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID) |
| 58 | { |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 59 | if (fs::exists(rawPelPath)) |
| 60 | { |
| 61 | std::ifstream file(rawPelPath, std::ios::in | std::ios::binary); |
| 62 | |
| 63 | auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file), |
| 64 | std::istreambuf_iterator<char>()); |
| 65 | if (file.fail()) |
| 66 | { |
| 67 | log<level::ERR>("Filesystem error reading a raw PEL", |
| 68 | entry("PELFILE=%s", rawPelPath.c_str()), |
| 69 | entry("OBMCLOGID=%d", obmcLogID)); |
| 70 | // TODO, Decide what to do here. Maybe nothing. |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | file.close(); |
| 75 | |
| 76 | auto pel = std::make_unique<PEL>(data, obmcLogID); |
| 77 | if (pel->valid()) |
| 78 | { |
| 79 | // PELs created by others still need these fields set by us. |
| 80 | pel->assignID(); |
| 81 | pel->setCommitTime(); |
| 82 | |
| 83 | try |
| 84 | { |
| 85 | _repo.add(pel); |
| 86 | } |
| 87 | catch (std::exception& e) |
| 88 | { |
| 89 | // Probably a full or r/o filesystem, not much we can do. |
| 90 | log<level::ERR>("Unable to add PEL to Repository", |
| 91 | entry("PEL_ID=0x%X", pel->id())); |
| 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | log<level::ERR>("Invalid PEL found", |
| 97 | entry("PELFILE=%s", rawPelPath.c_str()), |
| 98 | entry("OBMCLOGID=%d", obmcLogID)); |
| 99 | // TODO, make a whole new OpenBMC event log + PEL |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | log<level::ERR>("Raw PEL file from BMC event log does not exist", |
| 105 | entry("PELFILE=%s", (rawPelPath).c_str()), |
| 106 | entry("OBMCLOGID=%d", obmcLogID)); |
| 107 | } |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void Manager::erase(uint32_t obmcLogID) |
| 111 | { |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 112 | Repository::LogID id{Repository::LogID::Obmc(obmcLogID)}; |
| 113 | |
| 114 | _repo.remove(id); |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool Manager::isDeleteProhibited(uint32_t obmcLogID) |
| 118 | { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | void Manager::createPEL(const std::string& message, uint32_t obmcLogID, |
| 123 | uint64_t timestamp, |
| 124 | phosphor::logging::Entry::Level severity, |
| 125 | const std::vector<std::string>& additionalData, |
| 126 | const std::vector<std::string>& associations) |
| 127 | { |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 128 | auto entry = _registry.lookup(message); |
| 129 | |
| 130 | if (entry) |
| 131 | { |
| 132 | AdditionalData ad{additionalData}; |
| 133 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 134 | auto pel = std::make_unique<PEL>(*entry, obmcLogID, timestamp, severity, |
| 135 | ad, *_dataIface); |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 136 | |
| 137 | _repo.add(pel); |
| 138 | } |
| 139 | |
| 140 | // TODO ibm-openbmc/dev/1151: When the message registry is actually filled |
| 141 | // in, handle the case where an error isn't in it. |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace pels |
| 145 | } // namespace openpower |