Commit b2db835a authored by NexAdn's avatar NexAdn
Browse files

Update ampel to gather thresholds and credentials from command line

parent cb3b85d6
......@@ -17,16 +17,13 @@
namespace
{
constexpr const char* HOST_INFO_URL =
"https://localhost:5665/v1/objects/"
"https://bigsister.fem.tu-ilmenau.de:5665/v1/objects/"
"hosts?attrs=acknowledgement&attrs=last_check_result&attrs=vars";
constexpr const char* SERVICE_INFO_URL =
"https://localhost:5665/v1/objects/"
"https://bigsister.fem.tu-ilmenau.de:5665/v1/objects/"
"services?attrs=acknowledgement&attrs=last_check_result&attrs=vars";
constexpr bool SKIP_CERT_VERIFICATION = true;
constexpr float THRESHOLD_WARNING = 99.f;
constexpr float THRESHOLD_CRITICAL = 98.f;
constexpr int RETURN_OK = 0;
constexpr int RETURN_WARNING = 1;
constexpr int RETURN_CRITICAL = 2;
......@@ -34,12 +31,10 @@ constexpr int RETURN_UNKNOWN = 3;
constexpr int MAX_RETURN_VALUE = 2;
constexpr const char* USER_PWD = "ampel:asdf";
std::ostringstream output_buf;
using json = nlohmann::json;
using health_data_t = std::tuple<int, int>;
using health_data_t = std::tuple<unsigned long, unsigned long>;
using health_data_map_t = std::unordered_map<int, health_data_t>;
} // namespace
......@@ -61,12 +56,12 @@ void print_plugin_output(int return_code, std::string status_message)
}
}
void print_health(int return_code, float health)
void print_health(int return_code, float health, float threshold_warning, float threshold_critical)
{
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");
+ std::to_string(threshold_warning) + ":;"
+ std::to_string(threshold_critical) + ":;0;100");
}
#ifndef NDEBUG
......@@ -98,6 +93,11 @@ void parse_result(health_data_map_t& output, const std::string& raw_data)
int last_result = object["attrs"]["last_check_result"]["exit_status"];
if (last_result >= 255) {
print_debug("Received invalid last check result. Skipping.\n");
continue;
}
auto target_it = output.find(ampel_group);
if (target_it == output.end()) {
output.insert(
......@@ -122,6 +122,18 @@ size_t write_memory_cb(void* content, size_t size, size_t n_memb, void* userdata
int main(int argc, char** argv)
{
if (argc < 4) {
std::cout << "Too few arguments. Syntax: icinga-ampel [Warning%] [Critical%] [auth]\n";
return 3;
}
float warn, crit;
std::string auth;
std::stringstream args;
args << argv[1] << ' ' << argv[2] << ' ' << argv[3];
args >> warn >> crit >> auth;
print_debug("Initializing cURL\n");
CURL* curl;
......@@ -137,7 +149,7 @@ int main(int argc, char** argv)
print_debug("Setting cURL opts\n");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_cb);
curl_easy_setopt(curl, CURLOPT_USERPWD, USER_PWD);
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
curl_easy_setopt(curl, CURLOPT_URL, HOST_INFO_URL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, (SKIP_CERT_VERIFICATION ? 0l : 1l));
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, (SKIP_CERT_VERIFICATION ? 0l : 1l));
......@@ -153,6 +165,14 @@ int main(int argc, char** argv)
return RETURN_UNKNOWN;
}
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) {
print_plugin_output(RETURN_UNKNOWN, "Failed cURL request\n");
std::clog << output_buf.str() << std::endl;
return RETURN_UNKNOWN;
}
parse_result(health_data, output_buf.str());
output_buf = std::ostringstream();
......@@ -160,11 +180,18 @@ int main(int argc, char** argv)
print_debug("Performing cURL request\n");
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
print_plugin_output(RETURN_UNKNOWN, std::string("Failed to perfor cURL request: ")
print_plugin_output(RETURN_UNKNOWN, std::string("Failed to perform cURL request: ")
+ curl_easy_strerror(res));
return RETURN_UNKNOWN;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) {
print_plugin_output(RETURN_UNKNOWN, "Failed cURL request\n");
std::clog << output_buf.str() << std::endl;
return RETURN_UNKNOWN;
}
parse_result(health_data, output_buf.str());
float min_health{1.f};
......@@ -174,6 +201,7 @@ int main(int argc, char** argv)
if (this_health < min_health) {
min_health = this_health;
}
print_debug("Calculated min health: " + std::to_string(min_health) + '\n');
}
print_debug("Performing global cleanup\n");
......@@ -181,14 +209,14 @@ int main(int argc, char** argv)
float health = min_health * 100.f;
if (health < THRESHOLD_CRITICAL) {
print_health(RETURN_CRITICAL, health);
if (health < crit) {
print_health(RETURN_CRITICAL, health, warn, crit);
return RETURN_CRITICAL;
} else if (health < THRESHOLD_WARNING) {
print_health(RETURN_WARNING, health);
} else if (health < warn) {
print_health(RETURN_WARNING, health, warn, crit);
return RETURN_WARNING;
} else {
print_health(RETURN_OK, health);
print_health(RETURN_OK, health, warn, crit);
return RETURN_OK;
}
......
Supports Markdown
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