1a51fc2d2SSui Chen #pragma once 2a51fc2d2SSui Chen 33ccb3adbSEd Tanous #include "app.hpp" 43ccb3adbSEd Tanous #include "async_resp.hpp" 53ccb3adbSEd Tanous #include "http_request.hpp" 63ccb3adbSEd Tanous #include "privileges.hpp" 73ccb3adbSEd Tanous #include "query.hpp" 83ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 93ccb3adbSEd Tanous #include "routing.hpp" 103ccb3adbSEd Tanous 11e610b316SJagpal Singh Gill #include <boost/system/error_code.hpp> 12e610b316SJagpal Singh Gill #include <boost/system/linux_error.hpp> 13253f11b8SEd Tanous #include <boost/url/format.hpp> 14a51fc2d2SSui Chen #include <nlohmann/json.hpp> 155ace29d2SEd Tanous #include <sdbusplus/asio/property.hpp> 16a51fc2d2SSui Chen 17e610b316SJagpal Singh Gill #include <functional> 18e610b316SJagpal Singh Gill #include <limits> 19e610b316SJagpal Singh Gill #include <memory> 20a51fc2d2SSui Chen #include <string> 21a51fc2d2SSui Chen 22a51fc2d2SSui Chen namespace redfish 23a51fc2d2SSui Chen { 24a51fc2d2SSui Chen 25e610b316SJagpal Singh Gill static constexpr auto healthMonitorServiceName = 26e610b316SJagpal Singh Gill "xyz.openbmc_project.HealthMon"; 27e610b316SJagpal Singh Gill static constexpr auto valueInterface = "xyz.openbmc_project.Metric.Value"; 28e610b316SJagpal Singh Gill static constexpr auto valueProperty = "Value"; 29e610b316SJagpal Singh Gill 30e610b316SJagpal Singh Gill inline bool checkErrors( 31e610b316SJagpal Singh Gill const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 32e610b316SJagpal Singh Gill const boost::system::error_code& ec, 33e610b316SJagpal Singh Gill const std::source_location src = std::source_location::current()) 34e610b316SJagpal Singh Gill { 35e610b316SJagpal Singh Gill if (ec.value() == boost::asio::error::basic_errors::host_unreachable) 36e610b316SJagpal Singh Gill { 37e610b316SJagpal Singh Gill BMCWEB_LOG_WARNING("Failed to find server, Dbus error {}", ec); 38e610b316SJagpal Singh Gill return true; 39e610b316SJagpal Singh Gill } 40e610b316SJagpal Singh Gill if (ec.value() == boost::system::linux_error::bad_request_descriptor) 41e610b316SJagpal Singh Gill { 42e610b316SJagpal Singh Gill BMCWEB_LOG_WARNING("Invalid Path, Dbus error {}", ec); 43e610b316SJagpal Singh Gill return true; 44e610b316SJagpal Singh Gill } 45e610b316SJagpal Singh Gill if (ec) 46e610b316SJagpal Singh Gill { 47e610b316SJagpal Singh Gill BMCWEB_LOG_ERROR("{} failed, error {}", src.function_name(), ec); 48e610b316SJagpal Singh Gill messages::internalError(asyncResp->res); 49e610b316SJagpal Singh Gill return true; 50e610b316SJagpal Singh Gill } 51e610b316SJagpal Singh Gill return false; 52e610b316SJagpal Singh Gill } 53e610b316SJagpal Singh Gill 54e610b316SJagpal Singh Gill inline void 55e610b316SJagpal Singh Gill setBytesProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 56e610b316SJagpal Singh Gill const nlohmann::json::json_pointer& jPtr, 57e610b316SJagpal Singh Gill const boost::system::error_code& ec, double bytes) 58e610b316SJagpal Singh Gill { 59e610b316SJagpal Singh Gill if (checkErrors(asyncResp, ec)) 60e610b316SJagpal Singh Gill { 61e610b316SJagpal Singh Gill return; 62e610b316SJagpal Singh Gill } 63e610b316SJagpal Singh Gill if (!std::isfinite(bytes)) 64e610b316SJagpal Singh Gill { 65e610b316SJagpal Singh Gill BMCWEB_LOG_WARNING("Property read for {} was not finite", 66e610b316SJagpal Singh Gill jPtr.to_string()); 67e610b316SJagpal Singh Gill asyncResp->res.jsonValue[jPtr] = nullptr; 68e610b316SJagpal Singh Gill return; 69e610b316SJagpal Singh Gill } 70e610b316SJagpal Singh Gill // If the param is in Kib, make it Kib. Redfish uses this as a naming 71e610b316SJagpal Singh Gill // DBus represents as bytes 72e610b316SJagpal Singh Gill if (std::string_view(jPtr.back()).ends_with("KiB")) 73e610b316SJagpal Singh Gill { 74e610b316SJagpal Singh Gill bytes /= 1024.0; 75e610b316SJagpal Singh Gill } 76e610b316SJagpal Singh Gill 77e610b316SJagpal Singh Gill asyncResp->res.jsonValue[jPtr] = static_cast<int64_t>(bytes); 78e610b316SJagpal Singh Gill } 79e610b316SJagpal Singh Gill 80e610b316SJagpal Singh Gill inline void managerGetStorageStatistics( 81e610b316SJagpal Singh Gill const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 82e610b316SJagpal Singh Gill { 83e610b316SJagpal Singh Gill constexpr auto freeStorageObjPath = 84e610b316SJagpal Singh Gill "/xyz/openbmc_project/metric/bmc/storage/rw"; 85e610b316SJagpal Singh Gill 86e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 87e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 88e610b316SJagpal Singh Gill freeStorageObjPath, valueInterface, valueProperty, 89e610b316SJagpal Singh Gill std::bind_front(setBytesProperty, asyncResp, 90e610b316SJagpal Singh Gill nlohmann::json::json_pointer("/FreeStorageSpaceKiB"))); 91e610b316SJagpal Singh Gill } 92e610b316SJagpal Singh Gill 93e610b316SJagpal Singh Gill inline void 94e610b316SJagpal Singh Gill setPercentProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 95e610b316SJagpal Singh Gill const nlohmann::json::json_pointer& jPtr, 96e610b316SJagpal Singh Gill const boost::system::error_code& ec, double userCPU) 97e610b316SJagpal Singh Gill { 98e610b316SJagpal Singh Gill if (checkErrors(asyncResp, ec)) 99e610b316SJagpal Singh Gill { 100e610b316SJagpal Singh Gill return; 101e610b316SJagpal Singh Gill } 102e610b316SJagpal Singh Gill if (!std::isfinite(userCPU)) 103e610b316SJagpal Singh Gill { 104e610b316SJagpal Singh Gill asyncResp->res.jsonValue[jPtr] = nullptr; 105e610b316SJagpal Singh Gill return; 106e610b316SJagpal Singh Gill } 107e610b316SJagpal Singh Gill 108e610b316SJagpal Singh Gill static constexpr double roundFactor = 10000; // 4 decimal places 109*bd79bce8SPatrick Williams asyncResp->res.jsonValue[jPtr] = 110*bd79bce8SPatrick Williams std::round(userCPU * roundFactor) / roundFactor; 111e610b316SJagpal Singh Gill } 112e610b316SJagpal Singh Gill 113e610b316SJagpal Singh Gill inline void managerGetProcessorStatistics( 114e610b316SJagpal Singh Gill const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 115e610b316SJagpal Singh Gill { 116e610b316SJagpal Singh Gill constexpr auto kernelCPUObjPath = 117e610b316SJagpal Singh Gill "/xyz/openbmc_project/metric/bmc/cpu/kernel"; 118e610b316SJagpal Singh Gill constexpr auto userCPUObjPath = "/xyz/openbmc_project/metric/bmc/cpu/user"; 119e610b316SJagpal Singh Gill 120e610b316SJagpal Singh Gill using json_pointer = nlohmann::json::json_pointer; 121e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 122e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 123e610b316SJagpal Singh Gill kernelCPUObjPath, valueInterface, valueProperty, 124e610b316SJagpal Singh Gill std::bind_front(setPercentProperty, asyncResp, 125e610b316SJagpal Singh Gill json_pointer("/ProcessorStatistics/KernelPercent"))); 126e610b316SJagpal Singh Gill 127e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 128e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, userCPUObjPath, 129e610b316SJagpal Singh Gill 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"; 140e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 141e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 142e610b316SJagpal Singh Gill availableMemoryObjPath, valueInterface, 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"; 148e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 149e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 150e610b316SJagpal Singh Gill bufferedAndCachedMemoryObjPath, 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"; 157e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 158e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 159e610b316SJagpal Singh Gill freeMemoryObjPath, valueInterface, 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"; 165e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 166e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 167e610b316SJagpal Singh Gill sharedMemoryObjPath, valueInterface, 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"; 173e610b316SJagpal Singh Gill sdbusplus::asio::getProperty<double>( 174e610b316SJagpal Singh Gill *crow::connections::systemBus, healthMonitorServiceName, 175e610b316SJagpal Singh Gill totalMemoryObjPath, valueInterface, 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 { 2195ace29d2SEd Tanous sdbusplus::asio::getProperty<uint64_t>( 2205ace29d2SEd Tanous *crow::connections::systemBus, "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