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
|
// 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 "dbus_utility.hpp"
#include "error_messages.hpp"
#include "http_request.hpp"
#include "logging.hpp"
#include "query.hpp"
#include "registries/privilege_registry.hpp"
#include <boost/asio/error.hpp>
#include <boost/beast/http/verb.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/linux_error.hpp>
#include <boost/url/format.hpp>
#include <nlohmann/json.hpp>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <functional>
#include <memory>
#include <ratio>
#include <source_location>
#include <string>
namespace redfish
{
static constexpr auto healthMonitorServiceName =
"xyz.openbmc_project.HealthMon";
static constexpr auto valueInterface = "xyz.openbmc_project.Metric.Value";
static constexpr auto valueProperty = "Value";
inline bool checkErrors(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const boost::system::error_code& ec,
const std::source_location src = std::source_location::current())
{
if (ec.value() == boost::asio::error::basic_errors::host_unreachable)
{
BMCWEB_LOG_WARNING("Failed to find server, Dbus error {}", ec);
return true;
}
if (ec.value() == boost::system::linux_error::bad_request_descriptor)
{
BMCWEB_LOG_WARNING("Invalid Path, Dbus error {}", ec);
return true;
}
if (ec)
{
BMCWEB_LOG_ERROR("{} failed, error {}", src.function_name(), ec);
messages::internalError(asyncResp->res);
return true;
}
return false;
}
inline void setBytesProperty(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const nlohmann::json::json_pointer& jPtr,
const boost::system::error_code& ec, double bytes)
{
if (checkErrors(asyncResp, ec))
{
return;
}
if (!std::isfinite(bytes))
{
BMCWEB_LOG_WARNING("Property read for {} was not finite",
jPtr.to_string());
asyncResp->res.jsonValue[jPtr] = nullptr;
return;
}
// If the param is in Kib, make it Kib. Redfish uses this as a naming
// DBus represents as bytes
if (std::string_view(jPtr.back()).ends_with("KiB"))
{
bytes /= 1024.0;
}
asyncResp->res.jsonValue[jPtr] = static_cast<int64_t>(bytes);
}
inline void managerGetStorageStatistics(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
constexpr auto freeStorageObjPath =
"/xyz/openbmc_project/metric/bmc/storage/rw";
dbus::utility::getProperty<double>(
healthMonitorServiceName, freeStorageObjPath, valueInterface,
valueProperty,
std::bind_front(setBytesProperty, asyncResp,
nlohmann::json::json_pointer("/FreeStorageSpaceKiB")));
}
inline void setPercentProperty(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const nlohmann::json::json_pointer& jPtr,
const boost::system::error_code& ec, double userCPU)
{
if (checkErrors(asyncResp, ec))
{
return;
}
if (!std::isfinite(userCPU))
{
asyncResp->res.jsonValue[jPtr] = nullptr;
return;
}
static constexpr double roundFactor = 10000; // 4 decimal places
asyncResp->res.jsonValue[jPtr] =
std::round(userCPU * roundFactor) / roundFactor;
}
inline void managerGetProcessorStatistics(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
constexpr auto kernelCPUObjPath =
"/xyz/openbmc_project/metric/bmc/cpu/kernel";
constexpr auto userCPUObjPath = "/xyz/openbmc_project/metric/bmc/cpu/user";
using json_pointer = nlohmann::json::json_pointer;
dbus::utility::getProperty<double>(
healthMonitorServiceName, kernelCPUObjPath, valueInterface,
valueProperty,
std::bind_front(setPercentProperty, asyncResp,
json_pointer("/ProcessorStatistics/KernelPercent")));
dbus::utility::getProperty<double>(
healthMonitorServiceName, userCPUObjPath, valueInterface, valueProperty,
std::bind_front(setPercentProperty, asyncResp,
json_pointer("/ProcessorStatistics/UserPercent")));
}
inline void managerGetMemoryStatistics(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
using json_pointer = nlohmann::json::json_pointer;
constexpr auto availableMemoryObjPath =
"/xyz/openbmc_project/metric/bmc/memory/available";
dbus::utility::getProperty<double>(
healthMonitorServiceName, availableMemoryObjPath, valueInterface,
valueProperty,
std::bind_front(setBytesProperty, asyncResp,
json_pointer("/MemoryStatistics/AvailableBytes")));
constexpr auto bufferedAndCachedMemoryObjPath =
"/xyz/openbmc_project/metric/bmc/memory/buffered_and_cached";
dbus::utility::getProperty<double>(
healthMonitorServiceName, bufferedAndCachedMemoryObjPath,
valueInterface, valueProperty,
std::bind_front(
setBytesProperty, asyncResp,
json_pointer("/MemoryStatistics/BuffersAndCacheBytes")));
constexpr auto freeMemoryObjPath =
"/xyz/openbmc_project/metric/bmc/memory/free";
dbus::utility::getProperty<double>(
healthMonitorServiceName, freeMemoryObjPath, valueInterface,
valueProperty,
std::bind_front(setBytesProperty, asyncResp,
json_pointer("/MemoryStatistics/FreeBytes")));
constexpr auto sharedMemoryObjPath =
"/xyz/openbmc_project/metric/bmc/memory/shared";
dbus::utility::getProperty<double>(
healthMonitorServiceName, sharedMemoryObjPath, valueInterface,
valueProperty,
std::bind_front(setBytesProperty, asyncResp,
json_pointer("/MemoryStatistics/SharedBytes")));
constexpr auto totalMemoryObjPath =
"/xyz/openbmc_project/metric/bmc/memory/total";
dbus::utility::getProperty<double>(
healthMonitorServiceName, totalMemoryObjPath, valueInterface,
valueProperty,
std::bind_front(setBytesProperty, asyncResp,
json_pointer("/MemoryStatistics/TotalBytes")));
}
inline void afterGetManagerStartTime(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const boost::system::error_code& ec, uint64_t bmcwebResetTime)
{
if (ec)
{
// Not all servers will be running in systemd, so ignore the error.
return;
}
using std::chrono::steady_clock;
std::chrono::duration<steady_clock::rep, std::micro> usReset{
bmcwebResetTime};
steady_clock::time_point resetTime{usReset};
steady_clock::time_point now = steady_clock::now();
steady_clock::duration runTime = now - resetTime;
if (runTime < steady_clock::duration::zero())
{
BMCWEB_LOG_CRITICAL("Uptime was negative????");
messages::internalError(asyncResp->res);
return;
}
// Floor to the closest millisecond
using Milli = std::chrono::duration<steady_clock::rep, std::milli>;
Milli milli = std::chrono::floor<Milli>(runTime);
using SecondsFloat = std::chrono::duration<double>;
SecondsFloat sec = std::chrono::duration_cast<SecondsFloat>(milli);
asyncResp->res.jsonValue["ServiceRootUptimeSeconds"] = sec.count();
}
inline void managerGetServiceRootUptime(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
dbus::utility::getProperty<uint64_t>(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1/unit/bmcweb_2eservice",
"org.freedesktop.systemd1.Unit", "ActiveEnterTimestampMonotonic",
std::bind_front(afterGetManagerStartTime, asyncResp));
}
/**
* handleManagerDiagnosticData supports ManagerDiagnosticData.
* It retrieves BMC health information from various DBus resources and returns
* the information through the response.
*/
inline void handleManagerDiagnosticDataGet(
crow::App& app, const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& managerId)
{
if (!redfish::setUpRedfishRoute(app, req, asyncResp))
{
return;
}
if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
{
messages::resourceNotFound(asyncResp->res, "Manager", managerId);
return;
}
asyncResp->res.jsonValue["@odata.type"] =
"#ManagerDiagnosticData.v1_2_0.ManagerDiagnosticData";
asyncResp->res.jsonValue["@odata.id"] =
boost::urls::format("/redfish/v1/Managers/{}/ManagerDiagnosticData",
BMCWEB_REDFISH_MANAGER_URI_NAME);
asyncResp->res.jsonValue["Id"] = "ManagerDiagnosticData";
asyncResp->res.jsonValue["Name"] = "Manager Diagnostic Data";
managerGetServiceRootUptime(asyncResp);
managerGetProcessorStatistics(asyncResp);
managerGetMemoryStatistics(asyncResp);
managerGetStorageStatistics(asyncResp);
}
inline void requestRoutesManagerDiagnosticData(App& app)
{
BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ManagerDiagnosticData")
.privileges(redfish::privileges::getManagerDiagnosticData)
.methods(boost::beast::http::verb::get)(
std::bind_front(handleManagerDiagnosticDataGet, std::ref(app)));
}
} // namespace redfish
|