Commit 1553590a authored by Adrian Schollmeyer's avatar Adrian Schollmeyer
Browse files

Add support for string group names

parent 1cbd83de
......@@ -35,7 +35,7 @@ std::ostringstream output_buf;
using json = nlohmann::json;
using health_data_t = std::tuple<unsigned long, unsigned long>;
using health_data_map_t = std::unordered_map<int, health_data_t>;
using health_data_map_t = std::unordered_map<std::string, health_data_t>;
} // namespace
void print_plugin_output(int return_code, std::string status_message)
......@@ -56,12 +56,13 @@ void print_plugin_output(int return_code, std::string status_message)
}
}
void print_health(int return_code, float health, float threshold_warning, float threshold_critical)
void print_health(int return_code, float health, float threshold_warning, float threshold_critical, const std::string& group_name)
{
std::string health_as_str = std::to_string(health);
print_plugin_output(return_code, "Health " + health_as_str + " | health=" + health_as_str + "%;"
+ std::to_string(threshold_warning) + ":;"
+ std::to_string(threshold_critical) + ":;0;100");
print_plugin_output(return_code, "Health " + health_as_str + " (group "
+ group_name + ") | health=" + health_as_str + "%;" +
std::to_string(threshold_warning) + ":;" +
std::to_string(threshold_critical) + ":;0;100");
}
#ifndef NDEBUG
......@@ -106,9 +107,12 @@ void parse_result(health_data_map_t& output, const std::string& raw_data)
continue;
}
int ampel_group{0};
if (object["attrs"]["vars"]["ampel_group"] > 0) {
ampel_group = static_cast<int>(object["attrs"]["vars"]["ampel_group"]);
std::string ampel_group{"default"};
auto& raw_ampel_group = object["attrs"]["vars"]["ampel_group"];
if (raw_ampel_group.is_string()) {
ampel_group = raw_ampel_group.get<std::string>();
} else if (raw_ampel_group.is_number()) {
ampel_group = std::to_string(raw_ampel_group.get<int>());
}
int last_result = object["attrs"]["last_check_result"]["exit_status"];
......@@ -219,11 +223,13 @@ int main(int argc, char** argv)
parse_result(health_data, output_buf.str());
float min_health{1.f};
std::string min_health_group;
for (auto& health : health_data) {
float this_health = std::get<0>(health.second) * 1.f / std::get<1>(health.second);
if (this_health < min_health) {
min_health = this_health;
min_health_group = health.first;
}
print_debug("Calculated min health: " + std::to_string(min_health) + '\n');
}
......@@ -234,13 +240,13 @@ int main(int argc, char** argv)
float health = min_health * 100.f;
if (health < crit) {
print_health(RETURN_CRITICAL, health, warn, crit);
print_health(RETURN_CRITICAL, health, warn, crit, min_health_group);
return RETURN_CRITICAL;
} else if (health < warn) {
print_health(RETURN_WARNING, health, warn, crit);
print_health(RETURN_WARNING, health, warn, crit, min_health_group);
return RETURN_WARNING;
} else {
print_health(RETURN_OK, health, warn, crit);
print_health(RETURN_OK, health, warn, crit, min_health_group);
return RETURN_OK;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment