summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJagpal Singh Gill <paligill@gmail.com>2023-12-21 02:35:41 +0300
committerJagpal Singh Gill <paligill@gmail.com>2024-02-23 20:39:54 +0300
commite610b3168321eee167271e4532c37fe1ed9c6f56 (patch)
tree0dc0f86091d788cb32af73fb4cc9540d33f6e7dc
parent78c9020305038b7974b4737c9b1d0b4afb9740f2 (diff)
downloadbmcweb-e610b3168321eee167271e4532c37fe1ed9c6f56.tar.xz
manager_diagnostic_data: add metric get
Add support to fetch MemoryStatistics, FreeStorageSpaceKiB and ProcessorStatistics for Manager Diagnostic Data. https://redfish.dmtf.org/schemas/v1/ManagerDiagnosticData.v1_2_1.json This change is in relation to following design and D-Bus interface - https://gerrit.openbmc.org/c/openbmc/docs/+/64917 https://gerrit.openbmc.org/c/openbmc/phosphor-dbus-interfaces/+/64914 Test: Redfish query output - { "@odata.id": "/redfish/v1/Managers/bmc/ManagerDiagnosticData", "@odata.type": "#ManagerDiagnosticData.v1_2_0.ManagerDiagnosticData", "FreeStorageSpaceKiB": 3772, "Id": "ManagerDiagnosticData", "MemoryStatistics": { "AvailableBytes": 354224066, "BuffersAndCacheBytes": 78984633, "SharedBytes": 11876066, "TotalBytes": 425516000 }, "Name": "Manager Diagnostic Data", "ProcessorStatistics": { "KernelPercent": 13.0234, "UserPercent": 5.7374 }, "ServiceRootUptimeSeconds": 2255.117 } Redfish service validator passing - Elapsed time: 0:03:12 metadataNamespaces: 3726 pass: 5133 passAction: 9 passGet: 205 passRedfishUri: 197 skipNoSchema: 3 skipOptional: 3492 warnDeprecated: 4 warningPresent: 7 Validation has succeeded. Change-Id: I43758a993eb7f342cb9ac5f5574498b37261c2cc Signed-off-by: Jagpal Singh Gill <paligill@gmail.com>
-rw-r--r--Redfish.md8
-rw-r--r--meson.build1
-rw-r--r--redfish-core/lib/manager_diagnostic_data.hpp163
-rw-r--r--test/redfish-core/lib/manager_diagnostic_data_test.cpp75
4 files changed, 247 insertions, 0 deletions
diff --git a/Redfish.md b/Redfish.md
index 811dc87989..cdec1f90c0 100644
--- a/Redfish.md
+++ b/Redfish.md
@@ -606,6 +606,14 @@ other.
#### ManagerDiagnosticData
- ServiceRootUptimeSeconds
+- FreeStorageSpaceKiB
+- MemoryStatistics/AvailableBytes
+- MemoryStatistics/BuffersAndCacheBytes
+- MemoryStatistics/FreeBytes
+- MemoryStatistics/SharedBytes
+- MemoryStatistics/TotalBytes
+- ProcessorStatistics/KernelPercent
+- ProcessorStatistics/UserPercent
### /redfish/v1/Managers/bmc/NetworkProtocol/
diff --git a/meson.build b/meson.build
index 6184f62ba4..f4976c1f9b 100644
--- a/meson.build
+++ b/meson.build
@@ -461,6 +461,7 @@ srcfiles_unittest = files(
'test/redfish-core/lib/service_root_test.cpp',
'test/redfish-core/lib/thermal_subsystem_test.cpp',
'test/redfish-core/lib/power_subsystem_test.cpp',
+ 'test/redfish-core/lib/manager_diagnostic_data_test.cpp',
)
diff --git a/redfish-core/lib/manager_diagnostic_data.hpp b/redfish-core/lib/manager_diagnostic_data.hpp
index 4dedc3fd3a..1ff116f66d 100644
--- a/redfish-core/lib/manager_diagnostic_data.hpp
+++ b/redfish-core/lib/manager_diagnostic_data.hpp
@@ -8,14 +8,174 @@
#include "registries/privilege_registry.hpp"
#include "routing.hpp"
+#include <boost/system/error_code.hpp>
+#include <boost/system/linux_error.hpp>
#include <nlohmann/json.hpp>
#include <sdbusplus/asio/property.hpp>
+#include <functional>
+#include <limits>
+#include <memory>
#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";
+
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, 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;
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, healthMonitorServiceName,
+ kernelCPUObjPath, valueInterface, valueProperty,
+ std::bind_front(setPercentProperty, asyncResp,
+ json_pointer("/ProcessorStatistics/KernelPercent")));
+
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, 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";
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, 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";
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, healthMonitorServiceName,
+ bufferedAndCachedMemoryObjPath, valueInterface, valueProperty,
+ std::bind_front(
+ setBytesProperty, asyncResp,
+ json_pointer("/MemoryStatistics/BuffersAndCacheBytes")));
+
+ constexpr auto freeMemoryObjPath =
+ "/xyz/openbmc_project/metric/bmc/memory/free";
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, healthMonitorServiceName,
+ freeMemoryObjPath, valueInterface, valueProperty,
+ std::bind_front(setBytesProperty, asyncResp,
+ json_pointer("/MemoryStatistics/FreeBytes")));
+
+ constexpr auto sharedMemoryObjPath =
+ "/xyz/openbmc_project/metric/bmc/memory/shared";
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, healthMonitorServiceName,
+ sharedMemoryObjPath, valueInterface, valueProperty,
+ std::bind_front(setBytesProperty, asyncResp,
+ json_pointer("/MemoryStatistics/SharedBytes")));
+
+ constexpr auto totalMemoryObjPath =
+ "/xyz/openbmc_project/metric/bmc/memory/total";
+ sdbusplus::asio::getProperty<double>(
+ *crow::connections::systemBus, 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)
@@ -82,6 +242,9 @@ inline void handleManagerDiagnosticDataGet(
asyncResp->res.jsonValue["Name"] = "Manager Diagnostic Data";
managerGetServiceRootUptime(asyncResp);
+ managerGetProcessorStatistics(asyncResp);
+ managerGetMemoryStatistics(asyncResp);
+ managerGetStorageStatistics(asyncResp);
}
inline void requestRoutesManagerDiagnosticData(App& app)
diff --git a/test/redfish-core/lib/manager_diagnostic_data_test.cpp b/test/redfish-core/lib/manager_diagnostic_data_test.cpp
new file mode 100644
index 0000000000..b59842ab57
--- /dev/null
+++ b/test/redfish-core/lib/manager_diagnostic_data_test.cpp
@@ -0,0 +1,75 @@
+#include "async_resp.hpp"
+#include "manager_diagnostic_data.hpp"
+
+#include <nlohmann/json.hpp>
+
+#include <memory>
+
+#include <gtest/gtest.h>
+
+namespace redfish
+{
+namespace
+{
+
+using json_pointer = nlohmann::json::json_pointer;
+
+void testDataGetNoError(boost::system::error_code ec)
+{
+ auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+
+ setBytesProperty(asyncResp,
+ json_pointer("/MemoryStatistics/FreeStorageSpace"), ec, 0);
+ EXPECT_TRUE(asyncResp->res.jsonValue.is_null());
+ EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
+}
+
+TEST(ManagerDiagnosticDataTest, ManagerDataGetServerUnreachable)
+{
+ testDataGetNoError(boost::asio::error::basic_errors::host_unreachable);
+}
+
+TEST(ManagerDiagnosticDataTest, ManagerDataGetPathInvalid)
+{
+ testDataGetNoError(boost::system::linux_error::bad_request_descriptor);
+}
+
+void verifyError(crow::Response& res)
+{
+ EXPECT_EQ(res.result(), boost::beast::http::status::internal_server_error);
+ res.clear();
+}
+
+TEST(ManagerDiagnosticDataTest, ManagerDataGetFailure)
+{
+ auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+ boost::system::error_code ec = boost::asio::error::operation_aborted;
+
+ setBytesProperty(asyncResp,
+ json_pointer("/MemoryStatistics/FreeStorageSpace"), ec, 0);
+ verifyError(asyncResp->res);
+}
+
+TEST(ManagerDiagnosticDataTest, ManagerDataGetNullPtr)
+{
+ auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+
+ setPercentProperty(
+ asyncResp,
+ nlohmann::json::json_pointer("/MemoryStatistics/FreeStorageSpace"), {},
+ std::numeric_limits<double>::quiet_NaN());
+ EXPECT_EQ(asyncResp->res.jsonValue["FreeStorageSpaceKiB"], nullptr);
+}
+
+TEST(ManagerDiagnosticDataTest, ManagerDataGetSuccess)
+{
+ auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
+
+ setBytesProperty(asyncResp, json_pointer("/FreeStorageSpaceKiB"), {},
+ 204800.0);
+ EXPECT_EQ(asyncResp->res.jsonValue["FreeStorageSpaceKiB"].get<int64_t>(),
+ 200);
+}
+
+} // namespace
+} // namespace redfish