1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once
#include "bmcweb_config.h"
#include "app.hpp"
#include "async_resp.hpp"
#include "error_messages.hpp"
#include "generated/enums/log_entry.hpp"
#include "gzfile.hpp"
#include "http_request.hpp"
#include "human_sort.hpp"
#include "logging.hpp"
#include "query.hpp"
#include "registries/privilege_registry.hpp"
#include "utils/query_param.hpp"
#include <boost/beast/http/verb.hpp>
#include <boost/url/format.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <format>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
#include <vector>
namespace redfish
{
constexpr const char* hostLoggerFolderPath = "/var/log/console";
inline bool getHostLoggerFiles(
const std::string& hostLoggerFilePath,
std::vector<std::filesystem::path>& hostLoggerFiles)
{
std::error_code ec;
std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
if (ec)
{
BMCWEB_LOG_WARNING("{}", ec.message());
return false;
}
for (const std::filesystem::directory_entry& it : logPath)
{
std::string filename = it.path().filename();
// Prefix of each log files is "log". Find the file and save the
// path
if (filename.starts_with("log"))
{
hostLoggerFiles.emplace_back(it.path());
}
}
// As the log files rotate, they are appended with a ".#" that is higher for
// the older logs. Since we start from oldest logs, sort the name in
// descending order.
std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
AlphanumLess<std::string>());
return true;
}
inline bool getHostLoggerEntries(
const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
{
GzFileReader logFile;
// Go though all log files and expose host logs.
for (const std::filesystem::path& it : hostLoggerFiles)
{
if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
{
BMCWEB_LOG_ERROR("fail to expose host logs");
return false;
}
}
// Get lastMessage from constructor by getter
std::string lastMessage = logFile.getLastMessage();
if (!lastMessage.empty())
{
logCount++;
if (logCount > skip && logCount <= (skip + top))
{
logEntries.push_back(lastMessage);
}
}
return true;
}
inline void fillHostLoggerEntryJson(std::string_view logEntryID,
std::string_view msg,
nlohmann::json::object_t& logEntryJson)
{
// Fill in the log entry with the gathered data.
logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
logEntryJson["@odata.id"] = boost::urls::format(
"/redfish/v1/Systems/{}/LogServices/HostLogger/Entries/{}",
BMCWEB_REDFISH_SYSTEM_URI_NAME, logEntryID);
logEntryJson["Name"] = "Host Logger Entry";
logEntryJson["Id"] = logEntryID;
logEntryJson["Message"] = msg;
logEntryJson["EntryType"] = log_entry::LogEntryType::Oem;
logEntryJson["Severity"] = log_entry::EventSeverity::OK;
logEntryJson["OemRecordFormat"] = "Host Logger Entry";
}
inline void handleSystemsLogServicesHostloggerGet(
App& app, const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& systemName)
{
if (!redfish::setUpRedfishRoute(app, req, asyncResp))
{
return;
}
if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
{
// Option currently returns no systems. TBD
messages::resourceNotFound(asyncResp->res, "ComputerSystem",
systemName);
return;
}
if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
{
messages::resourceNotFound(asyncResp->res, "ComputerSystem",
systemName);
return;
}
asyncResp->res.jsonValue["@odata.id"] =
std::format("/redfish/v1/Systems/{}/LogServices/HostLogger",
BMCWEB_REDFISH_SYSTEM_URI_NAME);
asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
asyncResp->res.jsonValue["Name"] = "Host Logger Service";
asyncResp->res.jsonValue["Description"] = "Host Logger Service";
asyncResp->res.jsonValue["Id"] = "HostLogger";
asyncResp->res.jsonValue["Entries"]["@odata.id"] =
std::format("/redfish/v1/Systems/{}/LogServices/HostLogger/Entries",
BMCWEB_REDFISH_SYSTEM_URI_NAME);
}
inline void handleSystemsLogServicesHostloggerEntriesGet(
App& app, const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& systemName)
{
query_param::QueryCapabilities capabilities = {
.canDelegateTop = true,
.canDelegateSkip = true,
};
query_param::Query delegatedQuery;
if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
delegatedQuery, capabilities))
{
return;
}
if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
{
// Option currently returns no systems. TBD
messages::resourceNotFound(asyncResp->res, "ComputerSystem",
systemName);
return;
}
if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
{
messages::resourceNotFound(asyncResp->res, "ComputerSystem",
systemName);
return;
}
asyncResp->res.jsonValue["@odata.id"] =
std::format("/redfish/v1/Systems/{}/LogServices/HostLogger/Entries",
BMCWEB_REDFISH_SYSTEM_URI_NAME);
asyncResp->res.jsonValue["@odata.type"] =
"#LogEntryCollection.LogEntryCollection";
asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
asyncResp->res.jsonValue["Description"] =
"Collection of HostLogger Entries";
nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
logEntryArray = nlohmann::json::array();
asyncResp->res.jsonValue["Members@odata.count"] = 0;
std::vector<std::filesystem::path> hostLoggerFiles;
if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
{
BMCWEB_LOG_DEBUG("Failed to get host log file path");
return;
}
// If we weren't provided top and skip limits, use the defaults.
size_t skip = delegatedQuery.skip.value_or(0);
size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
size_t logCount = 0;
// This vector only store the entries we want to expose that
// control by skip and top.
std::vector<std::string> logEntries;
if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries, logCount))
{
messages::internalError(asyncResp->res);
return;
}
// If vector is empty, that means skip value larger than total
// log count
if (logEntries.empty())
{
asyncResp->res.jsonValue["Members@odata.count"] = logCount;
return;
}
if (!logEntries.empty())
{
for (size_t i = 0; i < logEntries.size(); i++)
{
nlohmann::json::object_t hostLogEntry;
fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
hostLogEntry);
logEntryArray.emplace_back(std::move(hostLogEntry));
}
asyncResp->res.jsonValue["Members@odata.count"] = logCount;
if (skip + top < logCount)
{
asyncResp->res.jsonValue["Members@odata.nextLink"] =
std::format(
"/redfish/v1/Systems/{}/LogServices/HostLogger/Entries?$skip=",
BMCWEB_REDFISH_SYSTEM_URI_NAME) +
std::to_string(skip + top);
}
}
}
inline void handleSystemsLogServicesHostloggerEntriesEntryGet(
App& app, const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& systemName, const std::string& param)
{
if (!redfish::setUpRedfishRoute(app, req, asyncResp))
{
return;
}
if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
{
// Option currently returns no systems. TBD
messages::resourceNotFound(asyncResp->res, "ComputerSystem",
systemName);
return;
}
if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
{
messages::resourceNotFound(asyncResp->res, "ComputerSystem",
systemName);
return;
}
std::string_view targetID = param;
uint64_t idInt = 0;
auto [ptr, ec] = std::from_chars(targetID.begin(), targetID.end(), idInt);
if (ec != std::errc{} || ptr != targetID.end())
{
messages::resourceNotFound(asyncResp->res, "LogEntry", param);
return;
}
std::vector<std::filesystem::path> hostLoggerFiles;
if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
{
BMCWEB_LOG_DEBUG("Failed to get host log file path");
return;
}
size_t logCount = 0;
size_t top = 1;
std::vector<std::string> logEntries;
// We can get specific entry by skip and top. For example, if we
// want to get nth entry, we can set skip = n-1 and top = 1 to
// get that entry
if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
logCount))
{
messages::internalError(asyncResp->res);
return;
}
if (!logEntries.empty())
{
nlohmann::json::object_t hostLogEntry;
fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
asyncResp->res.jsonValue.update(hostLogEntry);
return;
}
// Requested ID was not found
messages::resourceNotFound(asyncResp->res, "LogEntry", param);
}
inline void requestRoutesSystemsLogServiceHostlogger(App& app)
{
BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
.privileges(redfish::privileges::getLogService)
.methods(boost::beast::http::verb::get)(std::bind_front(
handleSystemsLogServicesHostloggerGet, std::ref(app)));
BMCWEB_ROUTE(app,
"/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
.privileges(redfish::privileges::getLogEntryCollection)
.methods(boost::beast::http::verb::get)(std::bind_front(
handleSystemsLogServicesHostloggerEntriesGet, std::ref(app)));
BMCWEB_ROUTE(
app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
.privileges(redfish::privileges::getLogEntry)
.methods(boost::beast::http::verb::get)(std::bind_front(
handleSystemsLogServicesHostloggerEntriesEntryGet, std::ref(app)));
}
} // namespace redfish
|