blob: 2c58769bfe544119f31b11f61797b9efaea2da1f [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 Geissleree3a2c82019-07-30 15:16:20 -05007#include <vector>
8
9using phosphor::logging::level;
10using phosphor::logging::log;
11
Andrew Geissler693eaed2019-08-02 14:12:28 -050012bool gVerbose = false;
13
14void dump_targets(const TargetErrorData& targetData)
15{
16 std::cout << "## Data Structure of Json ##" << std::endl;
17 for (const auto& [target, value] : targetData)
18 {
19 std::cout << target << " " << value.errorToLog << std::endl;
20 std::cout << " ";
21 for (auto& eToMonitor : value.errorsToMonitor)
22 {
23 std::cout << eToMonitor << ", ";
24 }
25 std::cout << std::endl;
26 }
27 std::cout << std::endl;
28}
29
Andrew Geissleree3a2c82019-07-30 15:16:20 -050030void print_usage(void)
31{
32 std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with "
33 "target/error mappings"
34 << std::endl;
35 return;
36}
Andrew Geissler891213b2019-07-30 14:08:17 -050037
38int main(int argc, char* argv[])
39{
40 auto bus = sdbusplus::bus::new_default();
41 auto event = sdeventplus::Event::get_default();
42 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Andrew Geissleree3a2c82019-07-30 15:16:20 -050043 std::vector<std::string> filePaths;
Andrew Geissler891213b2019-07-30 14:08:17 -050044
Andrew Geissleree3a2c82019-07-30 15:16:20 -050045 CLI::App app{"OpenBmc systemd target monitor"};
46 app.add_option("-f,--file", filePaths,
47 "Full path to json file(s) with target/error mappings");
Andrew Geissler693eaed2019-08-02 14:12:28 -050048 app.add_flag("-v", gVerbose, "Enable verbose output");
Andrew Geissleree3a2c82019-07-30 15:16:20 -050049
50 CLI11_PARSE(app, argc, argv);
51
52 if (filePaths.empty())
53 {
54 log<level::ERR>("No input files");
55 print_usage();
56 exit(-1);
57 }
Andrew Geissler891213b2019-07-30 14:08:17 -050058
Andrew Geissler693eaed2019-08-02 14:12:28 -050059 TargetErrorData targetData = parseFiles(filePaths);
60
61 if (targetData.size() == 0)
62 {
63 log<level::ERR>("Invalid input files, no targets found");
64 print_usage();
65 exit(-1);
66 }
67
68 if (gVerbose)
69 {
70 dump_targets(targetData);
71 }
Andrew Geissler891213b2019-07-30 14:08:17 -050072
73 // TODO - Begin monitoring for systemd unit changes and logging appropriate
74 // errors
75
76 return event.loop();
77}