Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <sdbusplus/bus.hpp> |
| 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace unit |
| 8 | { |
| 9 | namespace 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 | */ |
| 24 | class Monitor |
| 25 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 26 | public: |
| 27 | /** |
| 28 | * The valid actions - either starting or stopping a unit |
| 29 | */ |
| 30 | enum class Action |
| 31 | { |
| 32 | start, |
| 33 | stop |
| 34 | }; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 35 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 36 | Monitor() = delete; |
| 37 | Monitor(const Monitor&) = delete; |
| 38 | Monitor(Monitor&&) = default; |
| 39 | Monitor& operator=(const Monitor&) = delete; |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 40 | Monitor& operator=(Monitor&&) = delete; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 41 | ~Monitor() = default; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 42 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 43 | /** |
| 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) : |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 52 | bus(sdbusplus::bus::new_default()), |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 53 | source(sourceUnit), target(targetUnit), action(action) |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 54 | {} |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 55 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Analyzes the source unit to check if it is in a failed state. |
| 58 | * If it is, then it runs the action on the target unit. |
| 59 | */ |
| 60 | void analyze(); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 61 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 62 | private: |
| 63 | /** |
| 64 | * Returns the dbus object path of the source unit |
| 65 | */ |
| 66 | std::string getSourceUnitPath(); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 67 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Says if the unit object passed in has an |
| 70 | * ActiveState property equal to 'failed'. |
| 71 | * |
| 72 | * @param[in] path - the unit object path to check |
| 73 | * |
| 74 | * @return - true if this unit is in the failed state |
| 75 | */ |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 76 | bool inFailedState(const std::string& path); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 77 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Runs the action on the target unit. |
| 80 | */ |
| 81 | void runTargetAction(); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 82 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 83 | /** |
| 84 | * The dbus object |
| 85 | */ |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 86 | sdbusplus::bus_t bus; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 87 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 88 | /** |
| 89 | * The source unit |
| 90 | */ |
| 91 | const std::string source; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 92 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 93 | /** |
| 94 | * The target unit |
| 95 | */ |
| 96 | const std::string target; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 97 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 98 | /** |
| 99 | * The action to run on the target if the source |
| 100 | * unit is in failed state. |
| 101 | */ |
| 102 | const Action action; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 103 | }; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 104 | } // namespace failure |
| 105 | } // namespace unit |
| 106 | } // namespace phosphor |