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