-
Notifications
You must be signed in to change notification settings - Fork 8
/
systemd_target_monitor.cpp
95 lines (79 loc) · 2.63 KB
/
systemd_target_monitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#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();
}