Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "systemd_target_parser.hpp" |
| 2 | #include "systemd_target_signal.hpp" |
| 3 | |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 4 | #include <CLI/CLI.hpp> |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdeventplus/event.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | |
| 9 | #include <iostream> |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | using phosphor::logging::level; |
| 13 | using phosphor::logging::log; |
| 14 | |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 15 | bool gVerbose = false; |
| 16 | |
| 17 | void dump_targets(const TargetErrorData& targetData) |
| 18 | { |
| 19 | std::cout << "## Data Structure of Json ##" << std::endl; |
| 20 | for (const auto& [target, value] : targetData) |
| 21 | { |
| 22 | std::cout << target << " " << value.errorToLog << std::endl; |
| 23 | std::cout << " "; |
| 24 | for (auto& eToMonitor : value.errorsToMonitor) |
| 25 | { |
| 26 | std::cout << eToMonitor << ", "; |
| 27 | } |
| 28 | std::cout << std::endl; |
| 29 | } |
| 30 | std::cout << std::endl; |
| 31 | } |
| 32 | |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 33 | void print_usage(void) |
| 34 | { |
| 35 | std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with " |
| 36 | "target/error mappings" |
| 37 | << std::endl; |
| 38 | return; |
| 39 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 40 | |
| 41 | int main(int argc, char* argv[]) |
| 42 | { |
| 43 | auto bus = sdbusplus::bus::new_default(); |
| 44 | auto event = sdeventplus::Event::get_default(); |
| 45 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 46 | std::vector<std::string> filePaths; |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 47 | |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 48 | CLI::App app{"OpenBmc systemd target monitor"}; |
| 49 | app.add_option("-f,--file", filePaths, |
| 50 | "Full path to json file(s) with target/error mappings"); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 51 | app.add_flag("-v", gVerbose, "Enable verbose output"); |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 52 | |
| 53 | CLI11_PARSE(app, argc, argv); |
| 54 | |
| 55 | if (filePaths.empty()) |
| 56 | { |
| 57 | log<level::ERR>("No input files"); |
| 58 | print_usage(); |
| 59 | exit(-1); |
| 60 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 61 | |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 62 | TargetErrorData targetData = parseFiles(filePaths); |
| 63 | |
| 64 | if (targetData.size() == 0) |
| 65 | { |
| 66 | log<level::ERR>("Invalid input files, no targets found"); |
| 67 | print_usage(); |
| 68 | exit(-1); |
| 69 | } |
| 70 | |
| 71 | if (gVerbose) |
| 72 | { |
| 73 | dump_targets(targetData); |
| 74 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 75 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 76 | phosphor::state::manager::SystemdTargetLogging targetMon(targetData, bus); |
| 77 | |
| 78 | // Subscribe to systemd D-bus signals indicating target completions |
| 79 | targetMon.subscribeToSystemdSignals(); |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 80 | |
| 81 | return event.loop(); |
| 82 | } |