blob: 0c9431745a2670d57cf15cc9f010163301857f74 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 Spinler4e8078c2019-07-09 13:22:32 -050016#include "manager.hpp"
17
18#include "additional_data.hpp"
Matt Spinler89fa0822019-07-17 13:54:30 -050019#include "pel.hpp"
20
21#include <filesystem>
22#include <fstream>
Matt Spinler4e8078c2019-07-09 13:22:32 -050023
24namespace openpower
25{
26namespace pels
27{
28
29using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050030namespace fs = std::filesystem;
Matt Spinler4e8078c2019-07-09 13:22:32 -050031
32namespace additional_data
33{
34constexpr auto rawPEL = "RAWPEL";
35}
36
37void 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
57void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
58{
Matt Spinler89fa0822019-07-17 13:54:30 -050059 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 Spinler4e8078c2019-07-09 13:22:32 -0500108}
109
110void Manager::erase(uint32_t obmcLogID)
111{
Matt Spinler475e5742019-07-18 16:09:49 -0500112 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
113
114 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500115}
116
117bool Manager::isDeleteProhibited(uint32_t obmcLogID)
118{
119 return false;
120}
121
122void 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 Spinler67456c22019-10-21 12:22:49 -0500128 auto entry = _registry.lookup(message);
129
130 if (entry)
131 {
132 AdditionalData ad{additionalData};
133
Matt Spinleraa659472019-10-23 09:26:48 -0500134 auto pel = std::make_unique<PEL>(*entry, obmcLogID, timestamp, severity,
135 ad, *_dataIface);
Matt Spinler67456c22019-10-21 12:22:49 -0500136
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 Spinler4e8078c2019-07-09 13:22:32 -0500142}
143
144} // namespace pels
145} // namespace openpower