xref: /openbmc/phosphor-state-manager/systemd_target_monitor.cpp (revision 1b2c3c035ecd647cef996982df00378506f93887)
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 
9 #include <iostream>
10 #include <vector>
11 
12 PHOSPHOR_LOG2_USING;
13 
14 bool gVerbose = false;
15 
dump_targets(const TargetErrorData & targetData)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 (const auto& eToMonitor : value.errorsToMonitor)
24         {
25             std::cout << eToMonitor << ", ";
26         }
27         std::cout << std::endl;
28     }
29     std::cout << std::endl;
30 }
31 
print_usage(void)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     std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with "
38                  "services to monitor for errors"
39               << std::endl;
40     return;
41 }
42 
main(int argc,char * argv[])43 int main(int argc, char* argv[])
44 {
45     auto bus = sdbusplus::bus::new_default();
46     std::vector<std::string> targetFilePaths;
47     std::vector<std::string> serviceFilePaths;
48 
49     CLI::App app{"OpenBmc systemd target and service monitor"};
50     app.add_option("-f,--file", targetFilePaths,
51                    "Full path to json file(s) with target/error mappings");
52     app.add_option("-s,--service", serviceFilePaths,
53                    "Full path to json file(s) with services to monitor");
54     app.add_flag("-v", gVerbose, "Enable verbose output");
55 
56     CLI11_PARSE(app, argc, argv);
57 
58     // target file input required
59     if (targetFilePaths.empty())
60     {
61         error("No input files");
62         print_usage();
63         exit(-1);
64     }
65 
66     TargetErrorData targetData = parseFiles(targetFilePaths);
67     if (targetData.size() == 0)
68     {
69         error("Invalid input files, no targets found");
70         print_usage();
71         exit(-1);
72     }
73 
74     ServiceMonitorData serviceData;
75     if (!serviceFilePaths.empty())
76     {
77         serviceData = parseServiceFiles(serviceFilePaths);
78     }
79 
80     if (gVerbose)
81     {
82         dump_targets(targetData);
83     }
84 
85     phosphor::state::manager::SystemdTargetLogging targetMon(
86         targetData, serviceData, bus);
87 
88     // Subscribe to systemd D-bus signals indicating target completions
89     targetMon.subscribeToSystemdSignals();
90 
91     bus.process_loop();
92     return 0;
93 }
94