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