xref: /openbmc/bmcweb/features/redfish/lib/manager_diagnostic_data.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3a51fc2d2SSui Chen #pragma once
4a51fc2d2SSui Chen 
53ccb3adbSEd Tanous #include "app.hpp"
63ccb3adbSEd Tanous #include "async_resp.hpp"
7deae6a78SEd Tanous #include "dbus_utility.hpp"
83ccb3adbSEd Tanous #include "http_request.hpp"
93ccb3adbSEd Tanous #include "privileges.hpp"
103ccb3adbSEd Tanous #include "query.hpp"
113ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
123ccb3adbSEd Tanous #include "routing.hpp"
133ccb3adbSEd Tanous 
14e610b316SJagpal Singh Gill #include <boost/system/error_code.hpp>
15e610b316SJagpal Singh Gill #include <boost/system/linux_error.hpp>
16253f11b8SEd Tanous #include <boost/url/format.hpp>
17a51fc2d2SSui Chen #include <nlohmann/json.hpp>
185ace29d2SEd Tanous #include <sdbusplus/asio/property.hpp>
19a51fc2d2SSui Chen 
20e610b316SJagpal Singh Gill #include <functional>
21e610b316SJagpal Singh Gill #include <limits>
22e610b316SJagpal Singh Gill #include <memory>
23a51fc2d2SSui Chen #include <string>
24a51fc2d2SSui Chen 
25a51fc2d2SSui Chen namespace redfish
26a51fc2d2SSui Chen {
27a51fc2d2SSui Chen 
28e610b316SJagpal Singh Gill static constexpr auto healthMonitorServiceName =
29e610b316SJagpal Singh Gill     "xyz.openbmc_project.HealthMon";
30e610b316SJagpal Singh Gill static constexpr auto valueInterface = "xyz.openbmc_project.Metric.Value";
31e610b316SJagpal Singh Gill static constexpr auto valueProperty = "Value";
32e610b316SJagpal Singh Gill 
33e610b316SJagpal Singh Gill inline bool checkErrors(
34e610b316SJagpal Singh Gill     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35e610b316SJagpal Singh Gill     const boost::system::error_code& ec,
36e610b316SJagpal Singh Gill     const std::source_location src = std::source_location::current())
37e610b316SJagpal Singh Gill {
38e610b316SJagpal Singh Gill     if (ec.value() == boost::asio::error::basic_errors::host_unreachable)
39e610b316SJagpal Singh Gill     {
40e610b316SJagpal Singh Gill         BMCWEB_LOG_WARNING("Failed to find server, Dbus error {}", ec);
41e610b316SJagpal Singh Gill         return true;
42e610b316SJagpal Singh Gill     }
43e610b316SJagpal Singh Gill     if (ec.value() == boost::system::linux_error::bad_request_descriptor)
44e610b316SJagpal Singh Gill     {
45e610b316SJagpal Singh Gill         BMCWEB_LOG_WARNING("Invalid Path, Dbus error {}", ec);
46e610b316SJagpal Singh Gill         return true;
47e610b316SJagpal Singh Gill     }
48e610b316SJagpal Singh Gill     if (ec)
49e610b316SJagpal Singh Gill     {
50e610b316SJagpal Singh Gill         BMCWEB_LOG_ERROR("{} failed, error {}", src.function_name(), ec);
51e610b316SJagpal Singh Gill         messages::internalError(asyncResp->res);
52e610b316SJagpal Singh Gill         return true;
53e610b316SJagpal Singh Gill     }
54e610b316SJagpal Singh Gill     return false;
55e610b316SJagpal Singh Gill }
56e610b316SJagpal Singh Gill 
57e610b316SJagpal Singh Gill inline void
58e610b316SJagpal Singh Gill     setBytesProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
59e610b316SJagpal Singh Gill                      const nlohmann::json::json_pointer& jPtr,
60e610b316SJagpal Singh Gill                      const boost::system::error_code& ec, double bytes)
61e610b316SJagpal Singh Gill {
62e610b316SJagpal Singh Gill     if (checkErrors(asyncResp, ec))
63e610b316SJagpal Singh Gill     {
64e610b316SJagpal Singh Gill         return;
65e610b316SJagpal Singh Gill     }
66e610b316SJagpal Singh Gill     if (!std::isfinite(bytes))
67e610b316SJagpal Singh Gill     {
68e610b316SJagpal Singh Gill         BMCWEB_LOG_WARNING("Property read for {} was not finite",
69e610b316SJagpal Singh Gill                            jPtr.to_string());
70e610b316SJagpal Singh Gill         asyncResp->res.jsonValue[jPtr] = nullptr;
71e610b316SJagpal Singh Gill         return;
72e610b316SJagpal Singh Gill     }
73e610b316SJagpal Singh Gill     // If the param is in Kib, make it Kib.  Redfish uses this as a naming
74e610b316SJagpal Singh Gill     // DBus represents as bytes
75e610b316SJagpal Singh Gill     if (std::string_view(jPtr.back()).ends_with("KiB"))
76e610b316SJagpal Singh Gill     {
77e610b316SJagpal Singh Gill         bytes /= 1024.0;
78e610b316SJagpal Singh Gill     }
79e610b316SJagpal Singh Gill 
80e610b316SJagpal Singh Gill     asyncResp->res.jsonValue[jPtr] = static_cast<int64_t>(bytes);
81e610b316SJagpal Singh Gill }
82e610b316SJagpal Singh Gill 
83e610b316SJagpal Singh Gill inline void managerGetStorageStatistics(
84e610b316SJagpal Singh Gill     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
85e610b316SJagpal Singh Gill {
86e610b316SJagpal Singh Gill     constexpr auto freeStorageObjPath =
87e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/storage/rw";
88e610b316SJagpal Singh Gill 
89deae6a78SEd Tanous     dbus::utility::getProperty<double>(
90deae6a78SEd Tanous         healthMonitorServiceName, freeStorageObjPath, valueInterface,
91deae6a78SEd Tanous         valueProperty,
92e610b316SJagpal Singh Gill         std::bind_front(setBytesProperty, asyncResp,
93e610b316SJagpal Singh Gill                         nlohmann::json::json_pointer("/FreeStorageSpaceKiB")));
94e610b316SJagpal Singh Gill }
95e610b316SJagpal Singh Gill 
96e610b316SJagpal Singh Gill inline void
97e610b316SJagpal Singh Gill     setPercentProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
98e610b316SJagpal Singh Gill                        const nlohmann::json::json_pointer& jPtr,
99e610b316SJagpal Singh Gill                        const boost::system::error_code& ec, double userCPU)
100e610b316SJagpal Singh Gill {
101e610b316SJagpal Singh Gill     if (checkErrors(asyncResp, ec))
102e610b316SJagpal Singh Gill     {
103e610b316SJagpal Singh Gill         return;
104e610b316SJagpal Singh Gill     }
105e610b316SJagpal Singh Gill     if (!std::isfinite(userCPU))
106e610b316SJagpal Singh Gill     {
107e610b316SJagpal Singh Gill         asyncResp->res.jsonValue[jPtr] = nullptr;
108e610b316SJagpal Singh Gill         return;
109e610b316SJagpal Singh Gill     }
110e610b316SJagpal Singh Gill 
111e610b316SJagpal Singh Gill     static constexpr double roundFactor = 10000; // 4 decimal places
112bd79bce8SPatrick Williams     asyncResp->res.jsonValue[jPtr] =
113bd79bce8SPatrick Williams         std::round(userCPU * roundFactor) / roundFactor;
114e610b316SJagpal Singh Gill }
115e610b316SJagpal Singh Gill 
116e610b316SJagpal Singh Gill inline void managerGetProcessorStatistics(
117e610b316SJagpal Singh Gill     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
118e610b316SJagpal Singh Gill {
119e610b316SJagpal Singh Gill     constexpr auto kernelCPUObjPath =
120e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/cpu/kernel";
121e610b316SJagpal Singh Gill     constexpr auto userCPUObjPath = "/xyz/openbmc_project/metric/bmc/cpu/user";
122e610b316SJagpal Singh Gill 
123e610b316SJagpal Singh Gill     using json_pointer = nlohmann::json::json_pointer;
124deae6a78SEd Tanous     dbus::utility::getProperty<double>(
125deae6a78SEd Tanous         healthMonitorServiceName, kernelCPUObjPath, valueInterface,
126deae6a78SEd Tanous         valueProperty,
127e610b316SJagpal Singh Gill         std::bind_front(setPercentProperty, asyncResp,
128e610b316SJagpal Singh Gill                         json_pointer("/ProcessorStatistics/KernelPercent")));
129e610b316SJagpal Singh Gill 
130deae6a78SEd Tanous     dbus::utility::getProperty<double>(
131deae6a78SEd Tanous         healthMonitorServiceName, userCPUObjPath, valueInterface, valueProperty,
132e610b316SJagpal Singh Gill         std::bind_front(setPercentProperty, asyncResp,
133e610b316SJagpal Singh Gill                         json_pointer("/ProcessorStatistics/UserPercent")));
134e610b316SJagpal Singh Gill }
135e610b316SJagpal Singh Gill 
136e610b316SJagpal Singh Gill inline void managerGetMemoryStatistics(
137e610b316SJagpal Singh Gill     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
138e610b316SJagpal Singh Gill {
139e610b316SJagpal Singh Gill     using json_pointer = nlohmann::json::json_pointer;
140e610b316SJagpal Singh Gill     constexpr auto availableMemoryObjPath =
141e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/memory/available";
142deae6a78SEd Tanous     dbus::utility::getProperty<double>(
143deae6a78SEd Tanous         healthMonitorServiceName, availableMemoryObjPath, valueInterface,
144deae6a78SEd Tanous         valueProperty,
145e610b316SJagpal Singh Gill         std::bind_front(setBytesProperty, asyncResp,
146e610b316SJagpal Singh Gill                         json_pointer("/MemoryStatistics/AvailableBytes")));
147e610b316SJagpal Singh Gill 
148e610b316SJagpal Singh Gill     constexpr auto bufferedAndCachedMemoryObjPath =
149e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/memory/buffered_and_cached";
150deae6a78SEd Tanous     dbus::utility::getProperty<double>(
151deae6a78SEd Tanous         healthMonitorServiceName, bufferedAndCachedMemoryObjPath,
152deae6a78SEd Tanous         valueInterface, valueProperty,
153e610b316SJagpal Singh Gill         std::bind_front(
154e610b316SJagpal Singh Gill             setBytesProperty, asyncResp,
155e610b316SJagpal Singh Gill             json_pointer("/MemoryStatistics/BuffersAndCacheBytes")));
156e610b316SJagpal Singh Gill 
157e610b316SJagpal Singh Gill     constexpr auto freeMemoryObjPath =
158e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/memory/free";
159deae6a78SEd Tanous     dbus::utility::getProperty<double>(
160deae6a78SEd Tanous         healthMonitorServiceName, freeMemoryObjPath, valueInterface,
161deae6a78SEd Tanous         valueProperty,
162e610b316SJagpal Singh Gill         std::bind_front(setBytesProperty, asyncResp,
163e610b316SJagpal Singh Gill                         json_pointer("/MemoryStatistics/FreeBytes")));
164e610b316SJagpal Singh Gill 
165e610b316SJagpal Singh Gill     constexpr auto sharedMemoryObjPath =
166e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/memory/shared";
167deae6a78SEd Tanous     dbus::utility::getProperty<double>(
168deae6a78SEd Tanous         healthMonitorServiceName, sharedMemoryObjPath, valueInterface,
169deae6a78SEd Tanous         valueProperty,
170e610b316SJagpal Singh Gill         std::bind_front(setBytesProperty, asyncResp,
171e610b316SJagpal Singh Gill                         json_pointer("/MemoryStatistics/SharedBytes")));
172e610b316SJagpal Singh Gill 
173e610b316SJagpal Singh Gill     constexpr auto totalMemoryObjPath =
174e610b316SJagpal Singh Gill         "/xyz/openbmc_project/metric/bmc/memory/total";
175deae6a78SEd Tanous     dbus::utility::getProperty<double>(
176deae6a78SEd Tanous         healthMonitorServiceName, totalMemoryObjPath, valueInterface,
177deae6a78SEd Tanous         valueProperty,
178e610b316SJagpal Singh Gill         std::bind_front(setBytesProperty, asyncResp,
179e610b316SJagpal Singh Gill                         json_pointer("/MemoryStatistics/TotalBytes")));
180e610b316SJagpal Singh Gill }
181e610b316SJagpal Singh Gill 
182ac106bf6SEd Tanous inline void afterGetManagerStartTime(
183ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
184ac106bf6SEd Tanous     const boost::system::error_code& ec, uint64_t bmcwebResetTime)
1855ace29d2SEd Tanous {
1865ace29d2SEd Tanous     if (ec)
1875ace29d2SEd Tanous     {
1885ace29d2SEd Tanous         // Not all servers will be running in systemd, so ignore the error.
1895ace29d2SEd Tanous         return;
1905ace29d2SEd Tanous     }
1915ace29d2SEd Tanous     using std::chrono::steady_clock;
1925ace29d2SEd Tanous 
1935ace29d2SEd Tanous     std::chrono::duration<steady_clock::rep, std::micro> usReset{
1945ace29d2SEd Tanous         bmcwebResetTime};
1955ace29d2SEd Tanous     steady_clock::time_point resetTime{usReset};
1965ace29d2SEd Tanous 
1975ace29d2SEd Tanous     steady_clock::time_point now = steady_clock::now();
1985ace29d2SEd Tanous 
1995ace29d2SEd Tanous     steady_clock::duration runTime = now - resetTime;
2005ace29d2SEd Tanous 
2015ace29d2SEd Tanous     if (runTime < steady_clock::duration::zero())
2025ace29d2SEd Tanous     {
20362598e31SEd Tanous         BMCWEB_LOG_CRITICAL("Uptime was negative????");
204ac106bf6SEd Tanous         messages::internalError(asyncResp->res);
2055ace29d2SEd Tanous         return;
2065ace29d2SEd Tanous     }
2075ace29d2SEd Tanous 
2085ace29d2SEd Tanous     // Floor to the closest millisecond
2095ace29d2SEd Tanous     using Milli = std::chrono::duration<steady_clock::rep, std::milli>;
2105ace29d2SEd Tanous     Milli milli = std::chrono::floor<Milli>(runTime);
2115ace29d2SEd Tanous 
2125ace29d2SEd Tanous     using SecondsFloat = std::chrono::duration<double>;
2135ace29d2SEd Tanous     SecondsFloat sec = std::chrono::duration_cast<SecondsFloat>(milli);
2145ace29d2SEd Tanous 
215ac106bf6SEd Tanous     asyncResp->res.jsonValue["ServiceRootUptimeSeconds"] = sec.count();
2165ace29d2SEd Tanous }
2175ace29d2SEd Tanous 
218ac106bf6SEd Tanous inline void managerGetServiceRootUptime(
219ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2205ace29d2SEd Tanous {
221deae6a78SEd Tanous     dbus::utility::getProperty<uint64_t>(
222deae6a78SEd Tanous         "org.freedesktop.systemd1",
2235ace29d2SEd Tanous         "/org/freedesktop/systemd1/unit/bmcweb_2eservice",
2245ace29d2SEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveEnterTimestampMonotonic",
225ac106bf6SEd Tanous         std::bind_front(afterGetManagerStartTime, asyncResp));
2265ace29d2SEd Tanous }
227a51fc2d2SSui Chen /**
228a51fc2d2SSui Chen  * handleManagerDiagnosticData supports ManagerDiagnosticData.
229a51fc2d2SSui Chen  * It retrieves BMC health information from various DBus resources and returns
230a51fc2d2SSui Chen  * the information through the response.
231a51fc2d2SSui Chen  */
232a51fc2d2SSui Chen inline void handleManagerDiagnosticDataGet(
233a51fc2d2SSui Chen     crow::App& app, const crow::Request& req,
234253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
235253f11b8SEd Tanous     const std::string& managerId)
236a51fc2d2SSui Chen {
237a51fc2d2SSui Chen     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
238a51fc2d2SSui Chen     {
239a51fc2d2SSui Chen         return;
240a51fc2d2SSui Chen     }
241253f11b8SEd Tanous 
242253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
243253f11b8SEd Tanous     {
244253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
245253f11b8SEd Tanous         return;
246253f11b8SEd Tanous     }
247253f11b8SEd Tanous 
248a51fc2d2SSui Chen     asyncResp->res.jsonValue["@odata.type"] =
2495ace29d2SEd Tanous         "#ManagerDiagnosticData.v1_2_0.ManagerDiagnosticData";
250a51fc2d2SSui Chen     asyncResp->res.jsonValue["@odata.id"] =
251253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/ManagerDiagnosticData",
252253f11b8SEd Tanous                             BMCWEB_REDFISH_MANAGER_URI_NAME);
253a51fc2d2SSui Chen     asyncResp->res.jsonValue["Id"] = "ManagerDiagnosticData";
254a51fc2d2SSui Chen     asyncResp->res.jsonValue["Name"] = "Manager Diagnostic Data";
2555ace29d2SEd Tanous 
2565ace29d2SEd Tanous     managerGetServiceRootUptime(asyncResp);
257e610b316SJagpal Singh Gill     managerGetProcessorStatistics(asyncResp);
258e610b316SJagpal Singh Gill     managerGetMemoryStatistics(asyncResp);
259e610b316SJagpal Singh Gill     managerGetStorageStatistics(asyncResp);
260a51fc2d2SSui Chen }
261a51fc2d2SSui Chen 
262a51fc2d2SSui Chen inline void requestRoutesManagerDiagnosticData(App& app)
263a51fc2d2SSui Chen {
264253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ManagerDiagnosticData")
265a51fc2d2SSui Chen         .privileges(redfish::privileges::getManagerDiagnosticData)
266a51fc2d2SSui Chen         .methods(boost::beast::http::verb::get)(
267a51fc2d2SSui Chen             std::bind_front(handleManagerDiagnosticDataGet, std::ref(app)));
268a51fc2d2SSui Chen }
269a51fc2d2SSui Chen 
270a51fc2d2SSui Chen } // namespace redfish
271