Add class to monitor for unit failures
This class has an analyze() method that will
check the state of a unit, and then stop or start
a target unit if the state is failed.
The units and the action are passed in via the constructor.
Change-Id: Ibc8e54b8371d2261eb55cce5825c5cee6d214bab
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/fail-monitor/monitor.hpp b/fail-monitor/monitor.hpp
new file mode 100644
index 0000000..5aa2170
--- /dev/null
+++ b/fail-monitor/monitor.hpp
@@ -0,0 +1,113 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor
+{
+namespace unit
+{
+namespace failure
+{
+
+/**
+ * @class Monitor
+ *
+ * This class will analyze a unit to see if it is in the failed
+ * state. If it is, it will either start or stop a target unit.
+ *
+ * The use case is for running from the OnFailure directive in a
+ * unit file. If that unit keeps failing and restarting, it will
+ * eventually exceed its rate limits and stop being restarted.
+ * This application will allow another unit to be started when that
+ * occurs.
+ */
+class Monitor
+{
+ public:
+
+ /**
+ * The valid actions - either starting or stopping a unit
+ */
+ enum class Action
+ {
+ start,
+ stop
+ };
+
+ Monitor() = delete;
+ Monitor(const Monitor&) = delete;
+ Monitor(Monitor&&) = default;
+ Monitor& operator=(const Monitor&) = delete;
+ Monitor& operator=(Monitor&&) = default;
+ ~Monitor() = default;
+
+ /**
+ * Constructor
+ *
+ * @param[in] sourceUnit - the source unit
+ * @param[in] targetUnit - the target unit
+ * @param[in] action - the action to run on the target
+ */
+ Monitor(const std::string& sourceUnit,
+ const std::string& targetUnit,
+ Action action) :
+ bus(std::move(sdbusplus::bus::new_default())),
+ source(sourceUnit),
+ target(targetUnit),
+ action(action)
+ {
+ }
+
+ /**
+ * Analyzes the source unit to check if it is in a failed state.
+ * If it is, then it runs the action on the target unit.
+ */
+ void analyze();
+
+ private:
+
+ /**
+ * Returns the dbus object path of the source unit
+ */
+ std::string getSourceUnitPath();
+
+ /**
+ * Says if the unit object passed in has an
+ * ActiveState property equal to 'failed'.
+ *
+ * @param[in] path - the unit object path to check
+ *
+ * @return - true if this unit is in the failed state
+ */
+ bool inFailedState(const std::string&& path);
+
+ /**
+ * Runs the action on the target unit.
+ */
+ void runTargetAction();
+
+ /**
+ * The dbus object
+ */
+ sdbusplus::bus::bus bus;
+
+ /**
+ * The source unit
+ */
+ const std::string source;
+
+ /**
+ * The target unit
+ */
+ const std::string target;
+
+ /**
+ * The action to run on the target if the source
+ * unit is in failed state.
+ */
+ const Action action;
+};
+
+}
+}
+}