blob: dc9495120bd6c571e74507827699c46ee55bb37f [file] [log] [blame]
Andrew Geissleree3a2c82019-07-30 15:16:20 -05001#include <CLI/CLI.hpp>
2#include <iostream>
3#include <phosphor-logging/log.hpp>
Andrew Geissler891213b2019-07-30 14:08:17 -05004#include <sdbusplus/bus.hpp>
5#include <sdeventplus/event.hpp>
Andrew Geissler693eaed2019-08-02 14:12:28 -05006#include <systemd_target_parser.hpp>
Andrew Geissler234a3172019-08-09 14:30:02 -05007#include <systemd_target_signal.hpp>
Andrew Geissleree3a2c82019-07-30 15:16:20 -05008#include <vector>
9
10using phosphor::logging::level;
11using phosphor::logging::log;
12
Andrew Geissler693eaed2019-08-02 14:12:28 -050013bool gVerbose = false;
14
15void dump_targets(const TargetErrorData& targetData)
16{
17 std::cout << "## Data Structure of Json ##" << std::endl;
18 for (const auto& [target, value] : targetData)
19 {
20 std::cout << target << " " << value.errorToLog << std::endl;
21 std::cout << " ";
22 for (auto& eToMonitor : value.errorsToMonitor)
23 {
24 std::cout << eToMonitor << ", ";
25 }
26 std::cout << std::endl;
27 }
28 std::cout << std::endl;
29}
30
Andrew Geissleree3a2c82019-07-30 15:16:20 -050031void print_usage(void)
32{
33 std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with "
34 "target/error mappings"
35 << std::endl;
36 return;
37}
Andrew Geissler891213b2019-07-30 14:08:17 -050038
39int main(int argc, char* argv[])
40{
41 auto bus = sdbusplus::bus::new_default();
42 auto event = sdeventplus::Event::get_default();
43 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Andrew Geissleree3a2c82019-07-30 15:16:20 -050044 std::vector<std::string> filePaths;
Andrew Geissler891213b2019-07-30 14:08:17 -050045
Andrew Geissleree3a2c82019-07-30 15:16:20 -050046 CLI::App app{"OpenBmc systemd target monitor"};
47 app.add_option("-f,--file", filePaths,
48 "Full path to json file(s) with target/error mappings");
Andrew Geissler693eaed2019-08-02 14:12:28 -050049 app.add_flag("-v", gVerbose, "Enable verbose output");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050050
51 CLI11_PARSE(app, argc, argv);
52
53 if (filePaths.empty())
54 {
55 log<level::ERR>("No input files");
56 print_usage();
57 exit(-1);
58 }
Andrew Geissler891213b2019-07-30 14:08:17 -050059
Andrew Geissler693eaed2019-08-02 14:12:28 -050060 TargetErrorData targetData = parseFiles(filePaths);
61
62 if (targetData.size() == 0)
63 {
64 log<level::ERR>("Invalid input files, no targets found");
65 print_usage();
66 exit(-1);
67 }
68
69 if (gVerbose)
70 {
71 dump_targets(targetData);
72 }
Andrew Geissler891213b2019-07-30 14:08:17 -050073
Andrew Geissler234a3172019-08-09 14:30:02 -050074 phosphor::state::manager::SystemdTargetLogging targetMon(targetData, bus);
75
76 // Subscribe to systemd D-bus signals indicating target completions
77 targetMon.subscribeToSystemdSignals();
Andrew Geissler891213b2019-07-30 14:08:17 -050078
79 return event.loop();
80}