monitor: Capture BMC dumps on fan/ambient shutdowns

When fan-monitor or sensor-monitor generates an EPOW, this change
creates a BMC dump after the system is powered off and all error logs
are created.

Change-Id: Iacdd2d2b388e79988e2536d52497f0e697e1d444
Signed-off-by: Mike Capps <mikepcapps@gmail.com>
diff --git a/monitor/power_interface.cpp b/monitor/power_interface.cpp
index 575d2bc..8d9c808 100644
--- a/monitor/power_interface.cpp
+++ b/monitor/power_interface.cpp
@@ -36,6 +36,17 @@
     util::SDBusPlus::callMethod(
         systemdService, systemdPath, systemdMgrIface, "StartUnit",
         "obmc-chassis-hard-poweroff@0.target", "replace");
+
+    try
+    {
+        util::SDBusPlus::callMethod(
+            "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc",
+            "xyz.openbmc_project.Dump.Create", "CreateDump",
+            std::vector<
+                std::pair<std::string, std::variant<std::string, uint64_t>>>());
+    }
+    catch (const sdbusplus::exception::exception&)
+    {}
 }
 
 void PowerInterface::hardPowerOff()
diff --git a/monitor/power_off_action.hpp b/monitor/power_off_action.hpp
index f0e7531..cada348 100644
--- a/monitor/power_off_action.hpp
+++ b/monitor/power_off_action.hpp
@@ -2,6 +2,7 @@
 
 #include "logging.hpp"
 #include "power_interface.hpp"
+#include "sdbusplus.hpp"
 
 #include <fmt/format.h>
 
@@ -96,6 +97,24 @@
 
   protected:
     /**
+     * @brief Create a BMC Dump
+     */
+    void createBmcDump() const
+    {
+        try
+        {
+            util::SDBusPlus::callMethod(
+                "xyz.openbmc_project.Dump.Manager",
+                "/xyz/openbmc_project/dump/bmc",
+                "xyz.openbmc_project.Dump.Create", "CreateDump",
+                std::vector<std::pair<std::string,
+                                      std::variant<std::string, uint64_t>>>());
+        }
+        catch (const sdbusplus::exception::exception&)
+        {}
+    }
+
+    /**
      * @brief The name of the action, which is set by the
      *        derived class.
      */
@@ -185,7 +204,6 @@
      */
     void powerOff()
     {
-
         if (_prePowerOffFunc)
         {
             _prePowerOffFunc();
@@ -194,6 +212,8 @@
         getLogger().log(
             fmt::format("Action '{}' executing hard power off", name()));
         _powerIface->hardPowerOff();
+
+        createBmcDump();
     }
 
   private:
@@ -284,6 +304,8 @@
         getLogger().log(
             fmt::format("Action '{}' executing soft power off", name()));
         _powerIface->softPowerOff();
+
+        createBmcDump();
     }
 
   private:
@@ -391,6 +413,7 @@
         }
 
         _powerIface->hardPowerOff();
+        createBmcDump();
     }
 
     /**
diff --git a/monitor/system.cpp b/monitor/system.cpp
index fc38645..050913e 100644
--- a/monitor/system.cpp
+++ b/monitor/system.cpp
@@ -507,6 +507,25 @@
     error.commit(ffdc, true);
 
     PowerInterface::executeHardPowerOff();
+
+    createBmcDump();
+}
+
+/**
+ * @brief Create a BMC Dump
+ */
+void System::createBmcDump() const
+{
+    try
+    {
+        util::SDBusPlus::callMethod(
+            "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc",
+            "xyz.openbmc_project.Dump.Create", "CreateDump",
+            std::vector<
+                std::pair<std::string, std::variant<std::string, uint64_t>>>());
+    }
+    catch (const sdbusplus::exception::exception&)
+    {}
 }
 
 } // namespace phosphor::fan::monitor
diff --git a/monitor/system.hpp b/monitor/system.hpp
index 3997807..8c1bb44 100644
--- a/monitor/system.hpp
+++ b/monitor/system.hpp
@@ -132,6 +132,11 @@
      */
     void inventoryOnlineCb(sdbusplus::message::message& msg);
 
+    /**
+     * @brief Create a BMC Dump
+     */
+    void createBmcDump() const;
+
     /* The mode of fan monitor */
     Mode _mode;
 
diff --git a/sensor-monitor/shutdown_alarm_monitor.cpp b/sensor-monitor/shutdown_alarm_monitor.cpp
index e39af1e..419bdec 100644
--- a/sensor-monitor/shutdown_alarm_monitor.cpp
+++ b/sensor-monitor/shutdown_alarm_monitor.cpp
@@ -355,6 +355,20 @@
     timestamps.erase(alarmKey);
 }
 
+void ShutdownAlarmMonitor::createBmcDump() const
+{
+    try
+    {
+        util::SDBusPlus::callMethod(
+            "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc",
+            "xyz.openbmc_project.Dump.Create", "CreateDump",
+            std::vector<
+                std::pair<std::string, std::variant<std::string, uint64_t>>>());
+    }
+    catch (const sdbusplus::exception::exception&)
+    {}
+}
+
 void ShutdownAlarmMonitor::timerExpired(const AlarmKey& alarmKey)
 {
     const auto& [sensorPath, shutdownType, alarmType] = alarmKey;
@@ -378,6 +392,7 @@
                           "replace");
 
     timestamps.erase(alarmKey);
+    createBmcDump();
 }
 
 void ShutdownAlarmMonitor::powerStateChanged(bool powerStateOn)
diff --git a/sensor-monitor/shutdown_alarm_monitor.hpp b/sensor-monitor/shutdown_alarm_monitor.hpp
index d500523..b63fe83 100644
--- a/sensor-monitor/shutdown_alarm_monitor.hpp
+++ b/sensor-monitor/shutdown_alarm_monitor.hpp
@@ -162,6 +162,11 @@
                         bool isPowerOffError = false);
 
     /**
+     * @brief Create a BMC Dump
+     */
+    void createBmcDump() const;
+
+    /**
      * @brief The sdbusplus bus object
      */
     sdbusplus::bus::bus& bus;