blob: 7ae8137081f1c8206be6cd11f056bc6069eeba1d [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>
Andrew Geisslere426b582020-05-28 12:40:55 -05008
9#include <iostream>
Andrew Geissleree3a2c82019-07-30 15:16:20 -050010#include <vector>
11
Andrew Geissler8ffdb262021-09-20 15:25:19 -050012PHOSPHOR_LOG2_USING;
Andrew Geissleree3a2c82019-07-30 15:16:20 -050013
Andrew Geissler693eaed2019-08-02 14:12:28 -050014bool gVerbose = false;
15
16void 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 << " ";
Pavithra Barithaya319eda42024-06-21 11:54:43 -050023 for (const auto& eToMonitor : value.errorsToMonitor)
Andrew Geissler693eaed2019-08-02 14:12:28 -050024 {
25 std::cout << eToMonitor << ", ";
26 }
27 std::cout << std::endl;
28 }
29 std::cout << std::endl;
30}
31
Andrew Geissleree3a2c82019-07-30 15:16:20 -050032void 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;
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060037 std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with "
38 "services to monitor for errors"
39 << std::endl;
Andrew Geissleree3a2c82019-07-30 15:16:20 -050040 return;
41}
Andrew Geissler891213b2019-07-30 14:08:17 -050042
43int main(int argc, char* argv[])
44{
45 auto bus = sdbusplus::bus::new_default();
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060046 std::vector<std::string> targetFilePaths;
47 std::vector<std::string> serviceFilePaths;
Andrew Geissler891213b2019-07-30 14:08:17 -050048
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060049 CLI::App app{"OpenBmc systemd target and service monitor"};
50 app.add_option("-f,--file", targetFilePaths,
Andrew Geissleree3a2c82019-07-30 15:16:20 -050051 "Full path to json file(s) with target/error mappings");
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060052 app.add_option("-s,--service", serviceFilePaths,
53 "Full path to json file(s) with services to monitor");
Andrew Geissler693eaed2019-08-02 14:12:28 -050054 app.add_flag("-v", gVerbose, "Enable verbose output");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050055
56 CLI11_PARSE(app, argc, argv);
57
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060058 // target file input required
59 if (targetFilePaths.empty())
Andrew Geissleree3a2c82019-07-30 15:16:20 -050060 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050061 error("No input files");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050062 print_usage();
63 exit(-1);
64 }
Andrew Geissler891213b2019-07-30 14:08:17 -050065
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060066 TargetErrorData targetData = parseFiles(targetFilePaths);
Andrew Geissler693eaed2019-08-02 14:12:28 -050067 if (targetData.size() == 0)
68 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050069 error("Invalid input files, no targets found");
Andrew Geissler693eaed2019-08-02 14:12:28 -050070 print_usage();
71 exit(-1);
72 }
73
Andrew Geissler9e3afdf2022-02-10 15:06:16 -060074 ServiceMonitorData serviceData;
75 if (!serviceFilePaths.empty())
76 {
77 serviceData = parseServiceFiles(serviceFilePaths);
78 }
79
Andrew Geissler693eaed2019-08-02 14:12:28 -050080 if (gVerbose)
81 {
82 dump_targets(targetData);
83 }
Andrew Geissler891213b2019-07-30 14:08:17 -050084
Patrick Williams1b2c3c02024-08-16 15:20:29 -040085 phosphor::state::manager::SystemdTargetLogging targetMon(
86 targetData, serviceData, bus);
Andrew Geissler234a3172019-08-09 14:30:02 -050087
88 // Subscribe to systemd D-bus signals indicating target completions
89 targetMon.subscribeToSystemdSignals();
Andrew Geissler891213b2019-07-30 14:08:17 -050090
Patrick Williamsf3d21f42024-01-27 07:31:40 -060091 bus.process_loop();
92 return 0;
Andrew Geissler891213b2019-07-30 14:08:17 -050093}