From 252980a6bdb9a47c1828b2a34a3738e5fa82fe51 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 17 Apr 2014 16:48:59 +0200 Subject: [PATCH] Add statusmap_image and coords host attributes. They are as important as action_url, etc. coords is made a dictionary consisting of x_2d and y_2d as keys. Fixes #6044 --- components/compat/statusdatawriter.cpp | 10 +++- components/livestatus/hoststable.cpp | 12 ++++- doc/4.3-object-types.md | 8 ++-- doc/8-differences-between-icinga-1x-and-2.md | 16 ------- lib/db_ido/hostdbobject.cpp | 19 ++++---- lib/icinga/compatutility.cpp | 48 +++++--------------- lib/icinga/compatutility.h | 14 +----- lib/icinga/host.ti | 2 + lib/icinga/icinga-type.conf | 5 ++ test/livestatus/queries/host/legacy | 2 +- 10 files changed, 53 insertions(+), 83 deletions(-) diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index c0101a4bd..dba05b265 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -235,11 +235,17 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) String action_url = host->GetActionUrl(); String icon_image = host->GetIconImage(); String icon_image_alt = host->GetIconImageAlt(); - String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"); + String statusmap_image = host->GetStatusmapImage(); String display_name = host->GetDisplayName(); String address = host->GetAddress(); String address6 = host->GetAddress6(); + double x_2d, y_2d; + CompatUtility::GetHost2dCoordX(host, &x_2d); + CompatUtility::GetHost2dCoordY(host, &y_2d); + + String coords = Convert::ToString(x_2d) + "," + Convert::ToString(y_2d); + fp << "define host {" "\n" "\t" "host_name" "\t" << host->GetName() << "\n"; if (!display_name.IsEmpty()) { @@ -262,6 +268,8 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n"; if (!statusmap_image.IsEmpty()) fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n"; + if (!coords.IsEmpty()) + fp << "\t" "2d_coords" "\t" << coords << "\n"; std::set parents = host->GetParents(); diff --git a/components/livestatus/hoststable.cpp b/components/livestatus/hoststable.cpp index 5077438c1..d46246e22 100644 --- a/components/livestatus/hoststable.cpp +++ b/components/livestatus/hoststable.cpp @@ -821,7 +821,11 @@ Value HostsTable::X2dAccessor(const Value& row) if (!host) return Empty; - return CompatUtility::GetHost2dCoordX(host); + double coord; + if (!CompatUtility::GetHost2dCoordX(host, &coord)) + return Empty; + + return coord; } Value HostsTable::Y2dAccessor(const Value& row) @@ -831,7 +835,11 @@ Value HostsTable::Y2dAccessor(const Value& row) if (!host) return Empty; - return CompatUtility::GetHost2dCoordY(host); + double coord; + if (!CompatUtility::GetHost2dCoordY(host, &coord)) + return Empty; + + return coord; } Value HostsTable::LatencyAccessor(const Value& row) diff --git a/doc/4.3-object-types.md b/doc/4.3-object-types.md index d467ddca8..2f14aed06 100644 --- a/doc/4.3-object-types.md +++ b/doc/4.3-object-types.md @@ -45,17 +45,15 @@ Attributes: notes_url |**Optional.** Url for notes for the host (for example, in notification commands). action_url |**Optional.** Url for actions for the host (for example, an external graphing tool). icon_image |**Optional.** Icon image for the host. Required for external interfaces only. - icon_image_alt |**Optional.** Icon image description for the host. Required for external interface only. + icon_image_alt |**Optional.** Icon image description for the host. Required for external interfaces only. + statusmap_image |**Optional.** Statusmap image for the host. Required for external interfaces only. + coords |**Optional.** A dictionary containing the keys `x_2d` and `y_2d` as floating point values. Required for external interfaces and status maps. > **Best Practice** > > The `address` and `address6` attributes are required for running commands using > the `$address$` and `$address6` runtime macros. -Unlike Icinga 1.x the `statusmap_image` and `2d_coords` attributes are -custom attributes only in Icinga 2. - - ### HostGroup A group of hosts. diff --git a/doc/8-differences-between-icinga-1x-and-2.md b/doc/8-differences-between-icinga-1x-and-2.md index 162724b1a..82af46c78 100644 --- a/doc/8-differences-between-icinga-1x-and-2.md +++ b/doc/8-differences-between-icinga-1x-and-2.md @@ -168,18 +168,6 @@ Icinga 2 allows you to define custom attributes in the `vars` dictionary. The `notes`, `notes_url`, `action_url`, `icon_image`, `icon_image_alt` attributes for host and service objects are still available in Icinga 2. -### Statusmap Image, 2D Coords - -These attributes can be set using the `vars` dictionary in Icinga 2 `Host` -or `Service` objects: - - vars = { - "2d_coords" = "1,2" - statusmap_image = "../../images/logos/icinga.gif" - } - -External interfaces will recognize and display these attributes accordingly. - ### Custom Variables Icinga 1.x custom variable attributes must be prefixed using an underscore (`_`). @@ -190,8 +178,6 @@ In Icinga 2 these attributes must be added to the `vars` dictionary as custom at CV = "my custom cmdb description" } -TODO - ## Host Service Relation In Icinga 1.x a service object is associated with a host by defining the @@ -528,8 +514,6 @@ object for the escalation itself. ### Notification Options -TODO - Unlike Icinga 1.x with the 'notification_options' attribute with comma-separated state and type filters, Icinga 2 uses two configuration attributes for that. All state and type filter use long names or'd with a pipe together diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index 8b597e504..0e5e1f309 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -102,16 +102,15 @@ Dictionary::Ptr HostDbObject::GetConfigFields(void) const fields->Set("action_url", host->GetActionUrl()); fields->Set("icon_image", host->GetIconImage()); fields->Set("icon_image_alt", host->GetIconImageAlt()); - fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image")); - - Host2dCoords coords = CompatUtility::GetHost2dCoords(host); - - fields->Set("have_2d_coords", coords.have_2d_coords); - - if (coords.have_2d_coords) { - fields->Set("x_2d", coords.x_2d); - fields->Set("y_2d", coords.y_2d); - } + fields->Set("statusmap_image", host->GetStatusmapImage()); + + double x_2d, y_2d; + if (CompatUtility::GetHost2dCoordX(host, &x_2d) && CompatUtility::GetHost2dCoordY(host, &y_2d)) { + fields->Set("have_2d_coords", 1); + fields->Set("x_2d", x_2d); + fields->Set("y_2d", y_2d); + } else + fields->Set("have_2d_coords", 0); /* deprecated in 1.x */ fields->Set("have_3d_coords", 0); diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index 92f67eca6..b7645fd9c 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -70,52 +70,28 @@ String CompatUtility::GetHostAlias(const Host::Ptr& host) return host->GetDisplayName(); } -Host2dCoords CompatUtility::GetHost2dCoords(const Host::Ptr& host) +bool CompatUtility::GetHost2dCoordX(const Host::Ptr& host, double *coord) { - ASSERT(host->OwnsLock()); - - Dictionary::Ptr vars = host->GetVars(); - Host2dCoords bag; - - if (vars) { - String coords = vars->Get("2d_coords"); - bag.have_2d_coords = (!coords.IsEmpty() ? 1 : 0); - - std::vector tokens; - boost::algorithm::split(tokens, coords, boost::is_any_of(",")); + Dictionary::Ptr coords = host->GetCoords(); - if (tokens.size() != 2) - bag.have_2d_coords = 0; + if (!coords) + return false; - if (bag.have_2d_coords == 1) { - bag.x_2d = tokens[0]; - bag.y_2d = tokens[1]; - } - } else { - bag.have_2d_coords = 0; - } + *coord = coords->Get("x_2d"); - return bag; + return true; } -String CompatUtility::GetHost2dCoordX(const Host::Ptr& host) +bool CompatUtility::GetHost2dCoordY(const Host::Ptr& host, double *coord) { - Host2dCoords bag = GetHost2dCoords(host); + Dictionary::Ptr coords = host->GetCoords(); - if (bag.have_2d_coords == 0) - return Empty; - - return bag.x_2d; -} + if (!coords) + return false; -String CompatUtility::GetHost2dCoordY(const Host::Ptr& host) -{ - Host2dCoords bag = GetHost2dCoords(host); - - if (bag.have_2d_coords == 0) - return Empty; + *coord = coords->Get("y_2d"); - return bag.y_2d; + return true; } int CompatUtility::GetHostNotifyOnDown(const Host::Ptr& host) diff --git a/lib/icinga/compatutility.h b/lib/icinga/compatutility.h index d88109f4f..2b05bde31 100644 --- a/lib/icinga/compatutility.h +++ b/lib/icinga/compatutility.h @@ -31,15 +31,6 @@ namespace icinga { -/** - * @ingroup icinga - */ -struct Host2dCoords { - int have_2d_coords; - String x_2d; - String y_2d; -}; - /** * Compatibility utility functions. * @@ -51,9 +42,8 @@ public: /* host */ static String GetHostAlias(const Host::Ptr& host); - static Host2dCoords GetHost2dCoords(const Host::Ptr& host); - static String GetHost2dCoordX(const Host::Ptr& host); - static String GetHost2dCoordY(const Host::Ptr& host); + static bool GetHost2dCoordX(const Host::Ptr& host, double *coord); + static bool GetHost2dCoordY(const Host::Ptr& host, double *coord); static int GetHostNotifyOnDown(const Host::Ptr& host); static int GetHostNotifyOnUnreachable(const Host::Ptr& host); diff --git a/lib/icinga/host.ti b/lib/icinga/host.ti index a45493258..2028ac1ce 100644 --- a/lib/icinga/host.ti +++ b/lib/icinga/host.ti @@ -18,6 +18,8 @@ class Host : Checkable [config] String address; [config] String address6; + [config] String statusmap_image; + [config] Dictionary::Ptr coords; }; } diff --git a/lib/icinga/icinga-type.conf b/lib/icinga/icinga-type.conf index 048d6535a..6eb41dc82 100644 --- a/lib/icinga/icinga-type.conf +++ b/lib/icinga/icinga-type.conf @@ -68,6 +68,11 @@ %attribute %string "address", %attribute %string "address6", + %attribute %string "statusmap_image", + %attribute %dictionary "coords" { + %attribute %number "x_2d", + %attribute %number "y_2d" + } } %type HostGroup { diff --git a/test/livestatus/queries/host/legacy b/test/livestatus/queries/host/legacy index 7a949326d..38cfd1392 100644 --- a/test/livestatus/queries/host/legacy +++ b/test/livestatus/queries/host/legacy @@ -1,4 +1,4 @@ GET hosts -Columns: name notes notes_url action_url icon_image icon_image_alt +Columns: name notes notes_url action_url icon_image icon_image_alt statusmap_image x_2d y_2d ResponseHeader: fixed16 -- 2.40.0