1 #include "systemd_target_parser.hpp"
2 #include "systemd_target_signal.hpp"
3 
4 #include <CLI/CLI.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/bus.hpp>
7 #include <sdeventplus/event.hpp>
8 
9 #include <iostream>
10 #include <vector>
11 
12 PHOSPHOR_LOG2_USING;
13 
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 << "    ";
23         for (auto& eToMonitor : value.errorsToMonitor)
24         {
25             std::cout << eToMonitor << ", ";
26         }
27         std::cout << std::endl;
28     }
29     std::cout << std::endl;
30 }
31 
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;
37     return;
38 }
39 
40 int main(int argc, char* argv[])
41 {
42     auto bus = sdbusplus::bus::new_default();
43     auto event = sdeventplus::Event::get_default();
44     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
45     std::vector<std::string> filePaths;
46 
47     CLI::App app{"OpenBmc systemd target monitor"};
48     app.add_option("-f,--file", filePaths,
49                    "Full path to json file(s) with target/error mappings");
50     app.add_flag("-v", gVerbose, "Enable verbose output");
51 
52     CLI11_PARSE(app, argc, argv);
53 
54     if (filePaths.empty())
55     {
56         error("No input files");
57         print_usage();
58         exit(-1);
59     }
60 
61     TargetErrorData targetData = parseFiles(filePaths);
62 
63     if (targetData.size() == 0)
64     {
65         error("Invalid input files, no targets found");
66         print_usage();
67         exit(-1);
68     }
69 
70     if (gVerbose)
71     {
72         dump_targets(targetData);
73     }
74 
75     phosphor::state::manager::SystemdTargetLogging targetMon(targetData, bus);
76 
77     // Subscribe to systemd D-bus signals indicating target completions
78     targetMon.subscribeToSystemdSignals();
79 
80     return event.loop();
81 }
82