blob: a02f693b3d868e29ecc653bc2ea50732da4c0326 [file] [log] [blame]
#include "systemd_service_parser.hpp"
#include "systemd_target_parser.hpp"
#include "systemd_target_signal.hpp"
#include <CLI/CLI.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdeventplus/event.hpp>
#include <iostream>
#include <vector>
PHOSPHOR_LOG2_USING;
bool gVerbose = false;
void dump_targets(const TargetErrorData& targetData)
{
std::cout << "## Data Structure of Json ##" << std::endl;
for (const auto& [target, value] : targetData)
{
std::cout << target << " " << value.errorToLog << std::endl;
std::cout << " ";
for (auto& eToMonitor : value.errorsToMonitor)
{
std::cout << eToMonitor << ", ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void print_usage(void)
{
std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with "
"target/error mappings"
<< std::endl;
std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with "
"services to monitor for errors"
<< std::endl;
return;
}
int main(int argc, char* argv[])
{
auto bus = sdbusplus::bus::new_default();
auto event = sdeventplus::Event::get_default();
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
std::vector<std::string> targetFilePaths;
std::vector<std::string> serviceFilePaths;
CLI::App app{"OpenBmc systemd target and service monitor"};
app.add_option("-f,--file", targetFilePaths,
"Full path to json file(s) with target/error mappings");
app.add_option("-s,--service", serviceFilePaths,
"Full path to json file(s) with services to monitor");
app.add_flag("-v", gVerbose, "Enable verbose output");
CLI11_PARSE(app, argc, argv);
// target file input required
if (targetFilePaths.empty())
{
error("No input files");
print_usage();
exit(-1);
}
TargetErrorData targetData = parseFiles(targetFilePaths);
if (targetData.size() == 0)
{
error("Invalid input files, no targets found");
print_usage();
exit(-1);
}
ServiceMonitorData serviceData;
if (!serviceFilePaths.empty())
{
serviceData = parseServiceFiles(serviceFilePaths);
}
if (gVerbose)
{
dump_targets(targetData);
}
phosphor::state::manager::SystemdTargetLogging targetMon(targetData,
serviceData, bus);
// Subscribe to systemd D-bus signals indicating target completions
targetMon.subscribeToSystemdSignals();
return event.loop();
}