Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 1 | #include "systemd_service_parser.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | #include "systemd_target_parser.hpp" |
| 3 | #include "systemd_target_signal.hpp" |
| 4 | |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 5 | #include <CLI/CLI.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdeventplus/event.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 9 | |
| 10 | #include <iostream> |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 13 | PHOSPHOR_LOG2_USING; |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 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; |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 38 | std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with " |
| 39 | "services to monitor for errors" |
| 40 | << std::endl; |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 41 | return; |
| 42 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 43 | |
| 44 | int main(int argc, char* argv[]) |
| 45 | { |
| 46 | auto bus = sdbusplus::bus::new_default(); |
| 47 | auto event = sdeventplus::Event::get_default(); |
| 48 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 49 | std::vector<std::string> targetFilePaths; |
| 50 | std::vector<std::string> serviceFilePaths; |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 51 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 52 | CLI::App app{"OpenBmc systemd target and service monitor"}; |
| 53 | app.add_option("-f,--file", targetFilePaths, |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 54 | "Full path to json file(s) with target/error mappings"); |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 55 | app.add_option("-s,--service", serviceFilePaths, |
| 56 | "Full path to json file(s) with services to monitor"); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 57 | app.add_flag("-v", gVerbose, "Enable verbose output"); |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 58 | |
| 59 | CLI11_PARSE(app, argc, argv); |
| 60 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 61 | // target file input required |
| 62 | if (targetFilePaths.empty()) |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 63 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 64 | error("No input files"); |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 65 | print_usage(); |
| 66 | exit(-1); |
| 67 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 68 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 69 | TargetErrorData targetData = parseFiles(targetFilePaths); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 70 | if (targetData.size() == 0) |
| 71 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 72 | error("Invalid input files, no targets found"); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 73 | print_usage(); |
| 74 | exit(-1); |
| 75 | } |
| 76 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 77 | ServiceMonitorData serviceData; |
| 78 | if (!serviceFilePaths.empty()) |
| 79 | { |
| 80 | serviceData = parseServiceFiles(serviceFilePaths); |
| 81 | } |
| 82 | |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 83 | if (gVerbose) |
| 84 | { |
| 85 | dump_targets(targetData); |
| 86 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 87 | |
Andrew Geissler | f3870c6 | 2022-02-10 16:15:28 -0600 | [diff] [blame] | 88 | phosphor::state::manager::SystemdTargetLogging targetMon(targetData, |
| 89 | serviceData, bus); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 90 | |
| 91 | // Subscribe to systemd D-bus signals indicating target completions |
| 92 | targetMon.subscribeToSystemdSignals(); |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 93 | |
| 94 | return event.loop(); |
| 95 | } |