blob: cc13711457bd43033fd9890e350b330e19601a3d [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "systemd_target_parser.hpp"
2#include "systemd_target_signal.hpp"
3
Andrew Geissleree3a2c82019-07-30 15:16:20 -05004#include <CLI/CLI.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -05005#include <phosphor-logging/lg2.hpp>
Andrew Geissler891213b2019-07-30 14:08:17 -05006#include <sdbusplus/bus.hpp>
7#include <sdeventplus/event.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 << " ";
23 for (auto& eToMonitor : value.errorsToMonitor)
24 {
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;
37 return;
38}
Andrew Geissler891213b2019-07-30 14:08:17 -050039
40int main(int argc, char* argv[])
41{
42 auto bus = sdbusplus::bus::new_default();
43 auto event = sdeventplus::Event::get_default();
44 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Andrew Geissleree3a2c82019-07-30 15:16:20 -050045 std::vector<std::string> filePaths;
Andrew Geissler891213b2019-07-30 14:08:17 -050046
Andrew Geissleree3a2c82019-07-30 15:16:20 -050047 CLI::App app{"OpenBmc systemd target monitor"};
48 app.add_option("-f,--file", filePaths,
49 "Full path to json file(s) with target/error mappings");
Andrew Geissler693eaed2019-08-02 14:12:28 -050050 app.add_flag("-v", gVerbose, "Enable verbose output");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050051
52 CLI11_PARSE(app, argc, argv);
53
54 if (filePaths.empty())
55 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050056 error("No input files");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050057 print_usage();
58 exit(-1);
59 }
Andrew Geissler891213b2019-07-30 14:08:17 -050060
Andrew Geissler693eaed2019-08-02 14:12:28 -050061 TargetErrorData targetData = parseFiles(filePaths);
62
63 if (targetData.size() == 0)
64 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050065 error("Invalid input files, no targets found");
Andrew Geissler693eaed2019-08-02 14:12:28 -050066 print_usage();
67 exit(-1);
68 }
69
70 if (gVerbose)
71 {
72 dump_targets(targetData);
73 }
Andrew Geissler891213b2019-07-30 14:08:17 -050074
Andrew Geissler234a3172019-08-09 14:30:02 -050075 phosphor::state::manager::SystemdTargetLogging targetMon(targetData, bus);
76
77 // Subscribe to systemd D-bus signals indicating target completions
78 targetMon.subscribeToSystemdSignals();
Andrew Geissler891213b2019-07-30 14:08:17 -050079
80 return event.loop();
81}