17c33bff3SMatt Spinler #pragma once 27c33bff3SMatt Spinler 37c33bff3SMatt Spinler #include <sdbusplus/bus.hpp> 47c33bff3SMatt Spinler 57c33bff3SMatt Spinler namespace phosphor 67c33bff3SMatt Spinler { 77c33bff3SMatt Spinler namespace unit 87c33bff3SMatt Spinler { 97c33bff3SMatt Spinler namespace failure 107c33bff3SMatt Spinler { 117c33bff3SMatt Spinler 127c33bff3SMatt Spinler /** 137c33bff3SMatt Spinler * @class Monitor 147c33bff3SMatt Spinler * 157c33bff3SMatt Spinler * This class will analyze a unit to see if it is in the failed 167c33bff3SMatt Spinler * state. If it is, it will either start or stop a target unit. 177c33bff3SMatt Spinler * 187c33bff3SMatt Spinler * The use case is for running from the OnFailure directive in a 197c33bff3SMatt Spinler * unit file. If that unit keeps failing and restarting, it will 207c33bff3SMatt Spinler * eventually exceed its rate limits and stop being restarted. 217c33bff3SMatt Spinler * This application will allow another unit to be started when that 227c33bff3SMatt Spinler * occurs. 237c33bff3SMatt Spinler */ 247c33bff3SMatt Spinler class Monitor 257c33bff3SMatt Spinler { 267c33bff3SMatt Spinler public: 277c33bff3SMatt Spinler /** 287c33bff3SMatt Spinler * The valid actions - either starting or stopping a unit 297c33bff3SMatt Spinler */ 307c33bff3SMatt Spinler enum class Action 317c33bff3SMatt Spinler { 327c33bff3SMatt Spinler start, 337c33bff3SMatt Spinler stop 347c33bff3SMatt Spinler }; 357c33bff3SMatt Spinler 367c33bff3SMatt Spinler Monitor() = delete; 377c33bff3SMatt Spinler Monitor(const Monitor&) = delete; 387c33bff3SMatt Spinler Monitor(Monitor&&) = default; 397c33bff3SMatt Spinler Monitor& operator=(const Monitor&) = delete; 401f62380aSBrad Bishop Monitor& operator=(Monitor&&) = delete; 417c33bff3SMatt Spinler ~Monitor() = default; 427c33bff3SMatt Spinler 437c33bff3SMatt Spinler /** 447c33bff3SMatt Spinler * Constructor 457c33bff3SMatt Spinler * 467c33bff3SMatt Spinler * @param[in] sourceUnit - the source unit 477c33bff3SMatt Spinler * @param[in] targetUnit - the target unit 487c33bff3SMatt Spinler * @param[in] action - the action to run on the target 497c33bff3SMatt Spinler */ Monitor(const std::string & sourceUnit,const std::string & targetUnit,Action action)50167e2379SEd Tanous Monitor(const std::string& sourceUnit, const std::string& targetUnit, 517c33bff3SMatt Spinler Action action) : 52*9052ebd3SPatrick Williams bus(sdbusplus::bus::new_default()), source(sourceUnit), 53*9052ebd3SPatrick Williams target(targetUnit), action(action) 542352088eSBrad Bishop {} 557c33bff3SMatt Spinler 567c33bff3SMatt Spinler /** 577c33bff3SMatt Spinler * Analyzes the source unit to check if it is in a failed state. 587c33bff3SMatt Spinler * If it is, then it runs the action on the target unit. 597c33bff3SMatt Spinler */ 607c33bff3SMatt Spinler void analyze(); 617c33bff3SMatt Spinler 627c33bff3SMatt Spinler private: 637c33bff3SMatt Spinler /** 647c33bff3SMatt Spinler * Returns the dbus object path of the source unit 657c33bff3SMatt Spinler */ 667c33bff3SMatt Spinler std::string getSourceUnitPath(); 677c33bff3SMatt Spinler 687c33bff3SMatt Spinler /** 697c33bff3SMatt Spinler * Says if the unit object passed in has an 707c33bff3SMatt Spinler * ActiveState property equal to 'failed'. 717c33bff3SMatt Spinler * 727c33bff3SMatt Spinler * @param[in] path - the unit object path to check 737c33bff3SMatt Spinler * 747c33bff3SMatt Spinler * @return - true if this unit is in the failed state 757c33bff3SMatt Spinler */ 761f62380aSBrad Bishop bool inFailedState(const std::string& path); 777c33bff3SMatt Spinler 787c33bff3SMatt Spinler /** 797c33bff3SMatt Spinler * Runs the action on the target unit. 807c33bff3SMatt Spinler */ 817c33bff3SMatt Spinler void runTargetAction(); 827c33bff3SMatt Spinler 837c33bff3SMatt Spinler /** 847c33bff3SMatt Spinler * The dbus object 857c33bff3SMatt Spinler */ 86cc8070baSPatrick Williams sdbusplus::bus_t bus; 877c33bff3SMatt Spinler 887c33bff3SMatt Spinler /** 897c33bff3SMatt Spinler * The source unit 907c33bff3SMatt Spinler */ 917c33bff3SMatt Spinler const std::string source; 927c33bff3SMatt Spinler 937c33bff3SMatt Spinler /** 947c33bff3SMatt Spinler * The target unit 957c33bff3SMatt Spinler */ 967c33bff3SMatt Spinler const std::string target; 977c33bff3SMatt Spinler 987c33bff3SMatt Spinler /** 997c33bff3SMatt Spinler * The action to run on the target if the source 1007c33bff3SMatt Spinler * unit is in failed state. 1017c33bff3SMatt Spinler */ 1027c33bff3SMatt Spinler const Action action; 1037c33bff3SMatt Spinler }; 104cc6ee9cbSMatt Spinler } // namespace failure 105cc6ee9cbSMatt Spinler } // namespace unit 106cc6ee9cbSMatt Spinler } // namespace phosphor 107