1 #include "systemd_service_parser.hpp"
2 #include "systemd_target_parser.hpp"
3 #include "systemd_target_signal.hpp"
4 
5 #include <CLI/CLI.hpp>
6 #include <phosphor-logging/lg2.hpp>
7 #include <sdbusplus/bus.hpp>
8 #include <sdeventplus/event.hpp>
9 
10 #include <iostream>
11 #include <vector>
12 
13 PHOSPHOR_LOG2_USING;
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     std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with "
39                  "services to monitor for errors"
40               << std::endl;
41     return;
42 }
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);
49     std::vector<std::string> targetFilePaths;
50     std::vector<std::string> serviceFilePaths;
51 
52     CLI::App app{"OpenBmc systemd target and service monitor"};
53     app.add_option("-f,--file", targetFilePaths,
54                    "Full path to json file(s) with target/error mappings");
55     app.add_option("-s,--service", serviceFilePaths,
56                    "Full path to json file(s) with services to monitor");
57     app.add_flag("-v", gVerbose, "Enable verbose output");
58 
59     CLI11_PARSE(app, argc, argv);
60 
61     // target file input required
62     if (targetFilePaths.empty())
63     {
64         error("No input files");
65         print_usage();
66         exit(-1);
67     }
68 
69     TargetErrorData targetData = parseFiles(targetFilePaths);
70     if (targetData.size() == 0)
71     {
72         error("Invalid input files, no targets found");
73         print_usage();
74         exit(-1);
75     }
76 
77     ServiceMonitorData serviceData;
78     if (!serviceFilePaths.empty())
79     {
80         serviceData = parseServiceFiles(serviceFilePaths);
81     }
82 
83     if (gVerbose)
84     {
85         dump_targets(targetData);
86     }
87 
88     phosphor::state::manager::SystemdTargetLogging targetMon(targetData,
89                                                              serviceData, bus);
90 
91     // Subscribe to systemd D-bus signals indicating target completions
92     targetMon.subscribeToSystemdSignals();
93 
94     return event.loop();
95 }
96