blob: a02f693b3d868e29ecc653bc2ea50732da4c0326 [file] [log] [blame]
Andrew Geissler9e3afdf2022-02-10 15:06:16 -06001#include "systemd_service_parser.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05002#include "systemd_target_parser.hpp"
3#include "systemd_target_signal.hpp"
4
Andrew Geissleree3a2c82019-07-30 15:16:20 -05005#include <CLI/CLI.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -05006#include <phosphor-logging/lg2.hpp>
Andrew Geissler891213b2019-07-30 14:08:17 -05007#include <sdbusplus/bus.hpp>
8#include <sdeventplus/event.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -05009
10#include <iostream>
Andrew Geissleree3a2c82019-07-30 15:16:20 -050011#include <vector>
12
Andrew Geissler8ffdb262021-09-20 15:25:19 -050013PHOSPHOR_LOG2_USING;
Andrew Geissleree3a2c82019-07-30 15:16:20 -050014
Andrew Geissler693eaed2019-08-02 14:12:28 -050015bool gVerbose = false;
16
17void 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
Andrew Geissleree3a2c82019-07-30 15:16:20 -050033void 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;
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060038 std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with "
39 "services to monitor for errors"
40 << std::endl;
Andrew Geissleree3a2c82019-07-30 15:16:20 -050041 return;
42}
Andrew Geissler891213b2019-07-30 14:08:17 -050043
44int 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);
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060049 std::vector<std::string> targetFilePaths;
50 std::vector<std::string> serviceFilePaths;
Andrew Geissler891213b2019-07-30 14:08:17 -050051
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060052 CLI::App app{"OpenBmc systemd target and service monitor"};
53 app.add_option("-f,--file", targetFilePaths,
Andrew Geissleree3a2c82019-07-30 15:16:20 -050054 "Full path to json file(s) with target/error mappings");
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060055 app.add_option("-s,--service", serviceFilePaths,
56 "Full path to json file(s) with services to monitor");
Andrew Geissler693eaed2019-08-02 14:12:28 -050057 app.add_flag("-v", gVerbose, "Enable verbose output");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050058
59 CLI11_PARSE(app, argc, argv);
60
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060061 // target file input required
62 if (targetFilePaths.empty())
Andrew Geissleree3a2c82019-07-30 15:16:20 -050063 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050064 error("No input files");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050065 print_usage();
66 exit(-1);
67 }
Andrew Geissler891213b2019-07-30 14:08:17 -050068
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060069 TargetErrorData targetData = parseFiles(targetFilePaths);
Andrew Geissler693eaed2019-08-02 14:12:28 -050070 if (targetData.size() == 0)
71 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050072 error("Invalid input files, no targets found");
Andrew Geissler693eaed2019-08-02 14:12:28 -050073 print_usage();
74 exit(-1);
75 }
76
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060077 ServiceMonitorData serviceData;
78 if (!serviceFilePaths.empty())
79 {
80 serviceData = parseServiceFiles(serviceFilePaths);
81 }
82
Andrew Geissler693eaed2019-08-02 14:12:28 -050083 if (gVerbose)
84 {
85 dump_targets(targetData);
86 }
Andrew Geissler891213b2019-07-30 14:08:17 -050087
Andrew Geisslerf3870c62022-02-10 16:15:28 -060088 phosphor::state::manager::SystemdTargetLogging targetMon(targetData,
89 serviceData, bus);
Andrew Geissler234a3172019-08-09 14:30:02 -050090
91 // Subscribe to systemd D-bus signals indicating target completions
92 targetMon.subscribeToSystemdSignals();
Andrew Geissler891213b2019-07-30 14:08:17 -050093
94 return event.loop();
95}