blob: df8a30a620809d13d3b7ca83654a6cb7204b25cb [file] [log] [blame]
Matt Spinler7c33bff2017-06-02 12:29:41 -05001#pragma once
2
3#include <sdbusplus/bus.hpp>
4
5namespace phosphor
6{
7namespace unit
8{
9namespace failure
10{
11
12/**
13 * @class Monitor
14 *
15 * This class will analyze a unit to see if it is in the failed
16 * state. If it is, it will either start or stop a target unit.
17 *
18 * The use case is for running from the OnFailure directive in a
19 * unit file. If that unit keeps failing and restarting, it will
20 * eventually exceed its rate limits and stop being restarted.
21 * This application will allow another unit to be started when that
22 * occurs.
23 */
24class Monitor
25{
Ed Tanous167e2372018-05-07 11:59:10 -070026 public:
27 /**
28 * The valid actions - either starting or stopping a unit
29 */
30 enum class Action
31 {
32 start,
33 stop
34 };
Matt Spinler7c33bff2017-06-02 12:29:41 -050035
Ed Tanous167e2372018-05-07 11:59:10 -070036 Monitor() = delete;
37 Monitor(const Monitor&) = delete;
38 Monitor(Monitor&&) = default;
39 Monitor& operator=(const Monitor&) = delete;
40 Monitor& operator=(Monitor&&) = default;
41 ~Monitor() = default;
Matt Spinler7c33bff2017-06-02 12:29:41 -050042
Ed Tanous167e2372018-05-07 11:59:10 -070043 /**
44 * Constructor
45 *
46 * @param[in] sourceUnit - the source unit
47 * @param[in] targetUnit - the target unit
48 * @param[in] action - the action to run on the target
49 */
50 Monitor(const std::string& sourceUnit, const std::string& targetUnit,
51 Action action) :
52 bus(std::move(sdbusplus::bus::new_default())),
53 source(sourceUnit), target(targetUnit), action(action)
54 {
55 }
Matt Spinler7c33bff2017-06-02 12:29:41 -050056
Ed Tanous167e2372018-05-07 11:59:10 -070057 /**
58 * Analyzes the source unit to check if it is in a failed state.
59 * If it is, then it runs the action on the target unit.
60 */
61 void analyze();
Matt Spinler7c33bff2017-06-02 12:29:41 -050062
Ed Tanous167e2372018-05-07 11:59:10 -070063 private:
64 /**
65 * Returns the dbus object path of the source unit
66 */
67 std::string getSourceUnitPath();
Matt Spinler7c33bff2017-06-02 12:29:41 -050068
Ed Tanous167e2372018-05-07 11:59:10 -070069 /**
70 * Says if the unit object passed in has an
71 * ActiveState property equal to 'failed'.
72 *
73 * @param[in] path - the unit object path to check
74 *
75 * @return - true if this unit is in the failed state
76 */
77 bool inFailedState(const std::string&& path);
Matt Spinler7c33bff2017-06-02 12:29:41 -050078
Ed Tanous167e2372018-05-07 11:59:10 -070079 /**
80 * Runs the action on the target unit.
81 */
82 void runTargetAction();
Matt Spinler7c33bff2017-06-02 12:29:41 -050083
Ed Tanous167e2372018-05-07 11:59:10 -070084 /**
85 * The dbus object
86 */
87 sdbusplus::bus::bus bus;
Matt Spinler7c33bff2017-06-02 12:29:41 -050088
Ed Tanous167e2372018-05-07 11:59:10 -070089 /**
90 * The source unit
91 */
92 const std::string source;
Matt Spinler7c33bff2017-06-02 12:29:41 -050093
Ed Tanous167e2372018-05-07 11:59:10 -070094 /**
95 * The target unit
96 */
97 const std::string target;
Matt Spinler7c33bff2017-06-02 12:29:41 -050098
Ed Tanous167e2372018-05-07 11:59:10 -070099 /**
100 * The action to run on the target if the source
101 * unit is in failed state.
102 */
103 const Action action;
Matt Spinler7c33bff2017-06-02 12:29:41 -0500104};
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500105} // namespace failure
106} // namespace unit
107} // namespace phosphor