summaryrefslogtreecommitdiffstats
path: root/fail-monitor/monitor.hpp
blob: df8a30a620809d13d3b7ca83654a6cb7204b25cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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;
};
} // namespace failure
} // namespace unit
} // namespace phosphor
OpenPOWER on IntegriCloud