From 9f4edd1bdaf8cd00285ad87214b9d9999e538ca1 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 30 Jul 2013 18:17:13 +0200 Subject: [PATCH] CompatUtility: add service config attributes getter --- lib/icinga/compatutility.cpp | 130 ++++++++++++++++++++++++++++++++++- lib/icinga/compatutility.h | 2 + 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index ce069f4ad..014e6d718 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -23,6 +23,8 @@ #include "icinga/eventcommand.h" #include #include +#include +#include using namespace icinga; @@ -147,11 +149,137 @@ Dictionary::Ptr CompatUtility::GetServiceStatusAttributes(const Service::Ptr& se attr->Set("scheduled_downtime_depth", (service->IsInDowntime() ? 1 : 0)); attr->Set("last_notification", last_notification); attr->Set("next_notification", next_notification); - attr->Set("current_notification_number", notification_number);; + attr->Set("current_notification_number", notification_number); return attr; } +Dictionary::Ptr CompatUtility::GetServiceConfigAttributes(const Service::Ptr& service, CompatObjectType type) +{ + Dictionary::Ptr attr = boost::make_shared(); + + ASSERT(service->OwnsLock()); + + Host::Ptr host = service->GetHost(); + + if (!host) + return Dictionary::Ptr(); + + String check_period_str; + TimePeriod::Ptr check_period = service->GetCheckPeriod(); + if (check_period) + check_period_str = check_period->GetName(); + else + check_period_str = "24x7"; + + double notification_interval = -1; + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { + if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) + notification_interval = notification->GetNotificationInterval(); + } + + if (notification_interval == -1) + notification_interval = 60; + + String check_command_str; + String event_command_str; + CheckCommand::Ptr checkcommand = service->GetCheckCommand(); + EventCommand::Ptr eventcommand = service->GetEventCommand(); + + if (checkcommand) + check_command_str = checkcommand->GetName(); + + if (eventcommand) + event_command_str = eventcommand->GetName(); + + Dictionary::Ptr custom; + Dictionary::Ptr macros; + if (type == CompatTypeHost) { + custom = host->GetCustom(); + macros = host->GetMacros(); + } + else { + custom = service->GetCustom(); + macros = service->GetMacros(); + } + + if (type == CompatTypeHost) { + if (!host->GetDisplayName().IsEmpty()) + attr->Set("alias", host->GetName()); + else + attr->Set("alias", host->GetDisplayName()); + } + + attr->Set("check_period", check_period_str); + attr->Set("check_interval", service->GetCheckInterval() / 60.0); + attr->Set("retry_interval", service->GetRetryInterval() / 60.0); + attr->Set("max_check_attempts", service->GetMaxCheckAttempts()); + attr->Set("active_checks_enabled", (service->GetEnableActiveChecks() ? 1 : 0)); + attr->Set("passive_checks_enabled", (service->GetEnablePassiveChecks() ? 1 : 0)); + attr->Set("flap_detection_enabled", (service->GetEnableFlapping() ? 1 : 0)); + attr->Set("low_flap_threshold", service->GetFlappingThreshold()); + attr->Set("high_flap_threshold", service->GetFlappingThreshold()); + attr->Set("notifications_enabled", (service->GetEnableNotifications() ? 1 : 0)); + attr->Set("eventhandler_enabled", 1); /* always 1 */ + attr->Set("is_volatile", (service->IsVolatile() ? 1 : 0)); + attr->Set("notifications_enabled", (service->GetEnableNotifications() ? 1 : 0)); + attr->Set("notification_options", "u,w,c,r"); + attr->Set("notification_interval", notification_interval / 60.0); + attr->Set("process_performance_data", 1); /* always 1 */ + attr->Set("check_freshness", 1); /* always 1 */ + attr->Set("check_command", check_command_str); + attr->Set("event_handler", event_command_str); + + /* macros attr */ + if (macros) { + if (type == CompatTypeHost) { + attr->Set("address", macros->Get("address")); + attr->Set("address6", macros->Get("address6")); + } + } + + /* custom attr */ + /* TODO resolve all static macros */ + if (custom) { + attr->Set("notes", custom->Get("notes")); + attr->Set("notes_url", custom->Get("notes_url")); + attr->Set("action_url", custom->Get("action_url")); + attr->Set("icon_image", custom->Get("icon_image")); + attr->Set("icon_image_alt", custom->Get("icon_image_alt")); + + if (type == CompatTypeHost) { + attr->Set("statusmap_image", custom->Get("statusmap_image")); + attr->Set("2d_coords", custom->Get("2d_coords")); + + if (!custom->Get("2d_coords").IsEmpty()) { + std::vector tokens; + String coords = custom->Get("2d_coords"); + boost::algorithm::split(tokens, coords, boost::is_any_of(",")); + if (tokens.size() == 2) { + attr->Set("have_2d_coords", 1); + attr->Set("x_2d", tokens[0]); + attr->Set("y_2d", tokens[1]); + } + else + attr->Set("have_2d_coords", 0); + } + else + attr->Set("have_2d_coords", 0); + + /* deprecated in 1.x, but empty */ + attr->Set("vrml_image", Empty); + attr->Set("3d_coords", Empty); + attr->Set("have_3d_coords", 0); + attr->Set("x_3d", Empty); + attr->Set("y_3d", Empty); + attr->Set("z_3d", Empty); + } + } + + return attr; + +} + String CompatUtility::EscapeString(const String& str) { String result = str; diff --git a/lib/icinga/compatutility.h b/lib/icinga/compatutility.h index 41d6c5086..93188cf0c 100644 --- a/lib/icinga/compatutility.h +++ b/lib/icinga/compatutility.h @@ -46,6 +46,8 @@ class I2_ICINGA_API CompatUtility { public: static Dictionary::Ptr GetServiceStatusAttributes(const Service::Ptr& service, CompatObjectType type); + static Dictionary::Ptr GetServiceConfigAttributes(const Service::Ptr& service, CompatObjectType type); + static String EscapeString(const String& str); private: -- 2.40.0