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 { 26 public: 27 28 /** 29 * The valid actions - either starting or stopping a unit 30 */ 31 enum class Action 32 { 33 start, 34 stop 35 }; 36 37 Monitor() = delete; 38 Monitor(const Monitor&) = delete; 39 Monitor(Monitor&&) = default; 40 Monitor& operator=(const Monitor&) = delete; 41 Monitor& operator=(Monitor&&) = default; 42 ~Monitor() = default; 43 44 /** 45 * Constructor 46 * 47 * @param[in] sourceUnit - the source unit 48 * @param[in] targetUnit - the target unit 49 * @param[in] action - the action to run on the target 50 */ 51 Monitor(const std::string& sourceUnit, 52 const std::string& targetUnit, 53 Action action) : 54 bus(std::move(sdbusplus::bus::new_default())), 55 source(sourceUnit), 56 target(targetUnit), 57 action(action) 58 { 59 } 60 61 /** 62 * Analyzes the source unit to check if it is in a failed state. 63 * If it is, then it runs the action on the target unit. 64 */ 65 void analyze(); 66 67 private: 68 69 /** 70 * Returns the dbus object path of the source unit 71 */ 72 std::string getSourceUnitPath(); 73 74 /** 75 * Says if the unit object passed in has an 76 * ActiveState property equal to 'failed'. 77 * 78 * @param[in] path - the unit object path to check 79 * 80 * @return - true if this unit is in the failed state 81 */ 82 bool inFailedState(const std::string&& path); 83 84 /** 85 * Runs the action on the target unit. 86 */ 87 void runTargetAction(); 88 89 /** 90 * The dbus object 91 */ 92 sdbusplus::bus::bus bus; 93 94 /** 95 * The source unit 96 */ 97 const std::string source; 98 99 /** 100 * The target unit 101 */ 102 const std::string target; 103 104 /** 105 * The action to run on the target if the source 106 * unit is in failed state. 107 */ 108 const Action action; 109 }; 110 111 } 112 } 113 } 114