1 /** 2 * Copyright © 2020 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "config.h" 17 18 #include "system.hpp" 19 20 #include "fan.hpp" 21 #include "fan_defs.hpp" 22 #include "tach_sensor.hpp" 23 #include "trust_manager.hpp" 24 #include "types.hpp" 25 #ifdef MONITOR_USE_JSON 26 #include "json_parser.hpp" 27 #endif 28 29 #include <nlohmann/json.hpp> 30 #include <phosphor-logging/log.hpp> 31 #include <sdbusplus/bus.hpp> 32 #include <sdeventplus/event.hpp> 33 #include <sdeventplus/source/signal.hpp> 34 35 namespace phosphor::fan::monitor 36 { 37 38 using json = nlohmann::json; 39 using namespace phosphor::logging; 40 41 System::System(Mode mode, sdbusplus::bus::bus& bus, 42 const sdeventplus::Event& event) : 43 _mode(mode), 44 _bus(bus), _event(event) 45 { 46 json jsonObj = json::object(); 47 #ifdef MONITOR_USE_JSON 48 jsonObj = getJsonObj(bus); 49 #endif 50 // Retrieve and set trust groups within the trust manager 51 setTrustMgr(getTrustGroups(jsonObj)); 52 // Retrieve fan definitions and create fan objects to be monitored 53 setFans(getFanDefinitions(jsonObj)); 54 log<level::INFO>("Configuration loaded"); 55 } 56 57 void System::sighupHandler(sdeventplus::source::Signal&, 58 const struct signalfd_siginfo*) 59 { 60 try 61 { 62 json jsonObj = json::object(); 63 #ifdef MONITOR_USE_JSON 64 jsonObj = getJsonObj(_bus); 65 #endif 66 auto trustGrps = getTrustGroups(jsonObj); 67 auto fanDefs = getFanDefinitions(jsonObj); 68 // Set configured trust groups 69 setTrustMgr(trustGrps); 70 // Clear/set configured fan definitions 71 _fans.clear(); 72 setFans(fanDefs); 73 log<level::INFO>("Configuration reloaded successfully"); 74 } 75 catch (std::runtime_error& re) 76 { 77 log<level::ERR>("Error reloading config, no config changes made", 78 entry("LOAD_ERROR=%s", re.what())); 79 } 80 } 81 82 const std::vector<CreateGroupFunction> 83 System::getTrustGroups(const json& jsonObj) 84 { 85 #ifdef MONITOR_USE_JSON 86 return getTrustGrps(jsonObj); 87 #else 88 return trustGroups; 89 #endif 90 } 91 92 void System::setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs) 93 { 94 _trust = std::make_unique<trust::Manager>(groupFuncs); 95 } 96 97 const std::vector<FanDefinition> System::getFanDefinitions(const json& jsonObj) 98 { 99 #ifdef MONITOR_USE_JSON 100 return getFanDefs(jsonObj); 101 #else 102 return fanDefinitions; 103 #endif 104 } 105 106 void System::setFans(const std::vector<FanDefinition>& fanDefs) 107 { 108 for (const auto& fanDef : fanDefs) 109 { 110 // Check if a condition exists on the fan 111 auto condition = std::get<conditionField>(fanDef); 112 if (condition) 113 { 114 // Condition exists, skip adding fan if it fails 115 if (!(*condition)(_bus)) 116 { 117 continue; 118 } 119 } 120 _fans.emplace_back( 121 std::make_unique<Fan>(_mode, _bus, _event, _trust, fanDef)); 122 } 123 } 124 125 } // namespace phosphor::fan::monitor 126