1 #include <CLI/CLI.hpp>
2 #include <iostream>
3 #include <phosphor-logging/log.hpp>
4 #include <sdbusplus/bus.hpp>
5 #include <sdeventplus/event.hpp>
6 #include "systemd_target_parser.hpp"
7 #include "systemd_target_signal.hpp"
8 #include <vector>
9 
10 using phosphor::logging::level;
11 using phosphor::logging::log;
12 
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 
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 }
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);
44     std::vector<std::string> filePaths;
45 
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");
49     app.add_flag("-v", gVerbose, "Enable verbose output");
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     }
59 
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     }
73 
74     phosphor::state::manager::SystemdTargetLogging targetMon(targetData, bus);
75 
76     // Subscribe to systemd D-bus signals indicating target completions
77     targetMon.subscribeToSystemdSignals();
78 
79     return event.loop();
80 }
81