Skip to content

Commit 9e3afdf

Browse files
geissonatorAndrew Geissler
authored and
Andrew Geissler
committed
crit-service: initial service and parsing
This is an initial commit in a series of commits that will introduce a service monitoring feature within the current target monitoring function. This new feature will allow a user to pass in a json file with systemd service names that they wish this function to monitor. If a monitored services goes into an error state (exhausted all retries and service has been stopped) then the monitor service will create an error and collect appropriate debug data. This commit focuses on defining the new json service file and adapting the existing target monitor to take this file as input. Future commits in this series will build on this. Tested: - Verified new service json could be input to application and it was parsed correctly Signed-off-by: Andrew Geissler <[email protected]> Change-Id: Ifcc512b8fc868e2a1004a184fa50e4d4c826e8ee
1 parent 6810ad5 commit 9e3afdf

5 files changed

+96
-6
lines changed

Diff for: data/phosphor-service-monitor-default.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"services" : [
3+
"xyz.openbmc_project.biosconfig_manager.service",
4+
"xyz.openbmc_project.Dump.Manager.service",
5+
"xyz.openbmc_project.EntityManager.service",
6+
"xyz.openbmc_project.Inventory.Manager.service",
7+
"xyz.openbmc_project.Logging.service",
8+
"xyz.openbmc_project.Network.service",
9+
"xyz.openbmc_project.ObjectMapper.service",
10+
"xyz.openbmc_project.Settings.service",
11+
"xyz.openbmc_project.Software.BMC.Updater.service",
12+
"xyz.openbmc_project.Software.Download.service",
13+
"xyz.openbmc_project.Software.Version.service",
14+
"xyz.openbmc_project.State.BMC.service",
15+
"xyz.openbmc_project.State.Chassis.service",
16+
"xyz.openbmc_project.State.Host.service",
17+
"xyz.openbmc_project.Time.Manager.service",
18+
"xyz.openbmc_project.User.Manager.service",
19+
"bmcweb.service"
20+
]
21+
}

Diff for: meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ executable('phosphor-discover-system-state',
144144
)
145145

146146
executable('phosphor-systemd-target-monitor',
147+
'systemd_service_parser.cpp',
147148
'systemd_target_monitor.cpp',
148149
'systemd_target_parser.cpp',
149150
'systemd_target_signal.cpp',

Diff for: systemd_service_parser.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "systemd_service_parser.hpp"
2+
3+
#include <fstream>
4+
#include <iostream>
5+
6+
ServiceMonitorData parseServiceFiles(const std::vector<std::string>& filePaths)
7+
{
8+
ServiceMonitorData systemdServiceMap;
9+
for (const auto& jsonFile : filePaths)
10+
{
11+
if (gVerbose)
12+
{
13+
std::cout << "Parsing input service file " << jsonFile << std::endl;
14+
}
15+
std::ifstream fileStream(jsonFile);
16+
auto j = json::parse(fileStream);
17+
18+
for (auto& service : j["services"].items())
19+
{
20+
if (gVerbose)
21+
{
22+
std::cout << "service: " << service.value() << std::endl;
23+
}
24+
25+
systemdServiceMap.push_back(service.value());
26+
}
27+
}
28+
return systemdServiceMap;
29+
}

Diff for: systemd_service_parser.hpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <nlohmann/json.hpp>
4+
5+
#include <map>
6+
#include <string>
7+
#include <vector>
8+
9+
/** @brief Array of services to monitor */
10+
using ServiceMonitorData = std::vector<std::string>;
11+
12+
using json = nlohmann::json;
13+
14+
extern bool gVerbose;
15+
16+
/** @brief Parse input json file(s) for services to monitor
17+
*
18+
* @note This function will throw exceptions for an invalid json file
19+
* @note See phosphor-service-monitor-default.json for example of json file
20+
* format
21+
*
22+
* @param[in] filePaths - The file(s) to parse
23+
*
24+
* @return Service(s) to monitor for errors
25+
*/
26+
ServiceMonitorData parseServiceFiles(const std::vector<std::string>& filePaths);

Diff for: systemd_target_monitor.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "systemd_service_parser.hpp"
12
#include "systemd_target_parser.hpp"
23
#include "systemd_target_signal.hpp"
34

@@ -34,6 +35,9 @@ void print_usage(void)
3435
std::cout << "[-f <file1> -f <file2> ...] : Full path to json file(s) with "
3536
"target/error mappings"
3637
<< std::endl;
38+
std::cout << "[-s <file1> -s <file2> ...] : Full path to json file(s) with "
39+
"services to monitor for errors"
40+
<< std::endl;
3741
return;
3842
}
3943

@@ -42,31 +46,40 @@ int main(int argc, char* argv[])
4246
auto bus = sdbusplus::bus::new_default();
4347
auto event = sdeventplus::Event::get_default();
4448
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
45-
std::vector<std::string> filePaths;
49+
std::vector<std::string> targetFilePaths;
50+
std::vector<std::string> serviceFilePaths;
4651

47-
CLI::App app{"OpenBmc systemd target monitor"};
48-
app.add_option("-f,--file", filePaths,
52+
CLI::App app{"OpenBmc systemd target and service monitor"};
53+
app.add_option("-f,--file", targetFilePaths,
4954
"Full path to json file(s) with target/error mappings");
55+
app.add_option("-s,--service", serviceFilePaths,
56+
"Full path to json file(s) with services to monitor");
5057
app.add_flag("-v", gVerbose, "Enable verbose output");
5158

5259
CLI11_PARSE(app, argc, argv);
5360

54-
if (filePaths.empty())
61+
// target file input required
62+
if (targetFilePaths.empty())
5563
{
5664
error("No input files");
5765
print_usage();
5866
exit(-1);
5967
}
6068

61-
TargetErrorData targetData = parseFiles(filePaths);
62-
69+
TargetErrorData targetData = parseFiles(targetFilePaths);
6370
if (targetData.size() == 0)
6471
{
6572
error("Invalid input files, no targets found");
6673
print_usage();
6774
exit(-1);
6875
}
6976

77+
ServiceMonitorData serviceData;
78+
if (!serviceFilePaths.empty())
79+
{
80+
serviceData = parseServiceFiles(serviceFilePaths);
81+
}
82+
7083
if (gVerbose)
7184
{
7285
dump_targets(targetData);

0 commit comments

Comments
 (0)