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