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