This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupManager.cpp
123 lines (110 loc) · 4.36 KB
/
GroupManager.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Stephen Foulds
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "GroupManager.h"
#include "Log.h"
GroupManager::GroupManager(nlohmann::json groupList) : mProcessGroups(), mContainerGroups()
{
// Attempt to parse group JSON
// Get process groups
auto processGroups = groupList["processes"];
if (!processGroups.is_array()) {
LOG_ERROR("Process groups not a valid array - cannot map processes to groups");
} else {
for (const auto &group: processGroups) {
if (!group.contains("group")) {
LOG_WARN("Found malformed process group - missing 'group' field");
continue;
}
std::string groupName = group["group"];
if (!group.contains("processes") || !group["processes"].is_array()) {
LOG_WARN("Malformed group %s - no 'processes' array", groupName.c_str());
continue;
}
std::vector<std::string> processList = group["processes"];
// Got valid group info, parse process names into regexes and add to list
std::vector<std::regex> processRegexes{};
std::for_each(processList.begin(), processList.end(), [&](const std::string &name)
{
processRegexes.emplace_back(name);
});
Group processGroup(groupName, processRegexes);
mProcessGroups.emplace_back(processGroup);
}
LOG_INFO("Loaded %zu process groups", mProcessGroups.size());
}
// Get container groups
auto containerGroups = groupList["containers"];
if (!containerGroups.is_array()) {
LOG_ERROR("Container groups not a valid array - cannot map containers to groups");
return;
} else {
for (const auto &group: containerGroups) {
if (!group.contains("group")) {
LOG_WARN("Found malformed container group - missing 'group' field");
continue;
}
std::string groupName = group["group"];
if (!group.contains("containers") || !group["containers"].is_array()) {
LOG_WARN("Malformed group %s - no 'containers' array", groupName.c_str());
continue;
}
std::vector<std::string> containerList = group["containers"];
std::vector<std::regex> containerRegexes{};
std::for_each(containerList.begin(), containerList.end(), [&](const std::string &name)
{
containerRegexes.emplace_back(name);
});
Group containerGroup(groupName, containerRegexes);
mContainerGroups.emplace_back(containerGroup);
}
LOG_INFO("Loaded %zu container groups", mContainerGroups.size());
}
}
/**
* Work out which group a provided item belongs to based on it's name, based on the loaded JSON groups file
*
* @param type Type of group to check
* @param name Name of the process/container to check the group for
*
* @return If the group is known, then return the name of the group. Otherwise return nullopt to indicate the process
* does not belong to a known group
*/
std::optional<std::string> GroupManager::getGroup(groupType type, const std::string &name)
{
switch (type) {
case groupType::PROCESS: {
for (const auto &group: mProcessGroups) {
if (group.isMatch(name)) {
return group.name();
}
}
return std::nullopt;
}
case groupType::CONTAINER: {
for (const auto &group: mContainerGroups) {
if (group.isMatch(name)) {
return group.name();
}
}
return std::nullopt;
}
default:
return std::nullopt;
}
}