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> |
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 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 12 | PHOSPHOR_LOG2_USING; |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 13 | |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 14 | bool gVerbose = false; |
| 15 | |
| 16 | void dump_targets(const TargetErrorData& targetData) |
| 17 | { |
| 18 | std::cout << "## Data Structure of Json ##" << std::endl; |
| 19 | for (const auto& [target, value] : targetData) |
| 20 | { |
| 21 | std::cout << target << " " << value.errorToLog << std::endl; |
| 22 | std::cout << " "; |
Pavithra Barithaya | 319eda4 | 2024-06-21 11:54:43 -0500 | [diff] [blame] | 23 | for (const auto& eToMonitor : value.errorsToMonitor) |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 24 | { |
| 25 | std::cout << eToMonitor << ", "; |
| 26 | } |
| 27 | std::cout << std::endl; |
| 28 | } |
| 29 | std::cout << std::endl; |
| 30 | } |
| 31 | |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 32 | void print_usage(void) |
| 33 | { |
| 34 | std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with " |
| 35 | "target/error mappings" |
| 36 | << std::endl; |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 37 | std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with " |
| 38 | "services to monitor for errors" |
| 39 | << std::endl; |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 40 | return; |
| 41 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 42 | |
| 43 | int main(int argc, char* argv[]) |
| 44 | { |
| 45 | auto bus = sdbusplus::bus::new_default(); |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 46 | std::vector<std::string> targetFilePaths; |
| 47 | std::vector<std::string> serviceFilePaths; |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 48 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 49 | CLI::App app{"OpenBmc systemd target and service monitor"}; |
| 50 | app.add_option("-f,--file", targetFilePaths, |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 51 | "Full path to json file(s) with target/error mappings"); |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 52 | app.add_option("-s,--service", serviceFilePaths, |
| 53 | "Full path to json file(s) with services to monitor"); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 54 | app.add_flag("-v", gVerbose, "Enable verbose output"); |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 55 | |
| 56 | CLI11_PARSE(app, argc, argv); |
| 57 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 58 | // target file input required |
| 59 | if (targetFilePaths.empty()) |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 60 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 61 | error("No input files"); |
Andrew Geissler | ee3a2c8 | 2019-07-30 15:16:20 -0500 | [diff] [blame] | 62 | print_usage(); |
| 63 | exit(-1); |
| 64 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 65 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 66 | TargetErrorData targetData = parseFiles(targetFilePaths); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 67 | if (targetData.size() == 0) |
| 68 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 69 | error("Invalid input files, no targets found"); |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 70 | print_usage(); |
| 71 | exit(-1); |
| 72 | } |
| 73 | |
Andrew Geissler | 9e3afdf | 2022-02-10 15:06:16 -0600 | [diff] [blame] | 74 | ServiceMonitorData serviceData; |
| 75 | if (!serviceFilePaths.empty()) |
| 76 | { |
| 77 | serviceData = parseServiceFiles(serviceFilePaths); |
| 78 | } |
| 79 | |
Andrew Geissler | 693eaed | 2019-08-02 14:12:28 -0500 | [diff] [blame] | 80 | if (gVerbose) |
| 81 | { |
| 82 | dump_targets(targetData); |
| 83 | } |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 84 | |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 85 | phosphor::state::manager::SystemdTargetLogging targetMon( |
| 86 | targetData, serviceData, bus); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 87 | |
| 88 | // Subscribe to systemd D-bus signals indicating target completions |
| 89 | targetMon.subscribeToSystemdSignals(); |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 90 | |
Patrick Williams | f3d21f4 | 2024-01-27 07:31:40 -0600 | [diff] [blame] | 91 | bus.process_loop(); |
| 92 | return 0; |
Andrew Geissler | 891213b | 2019-07-30 14:08:17 -0500 | [diff] [blame] | 93 | } |