From 3a10127bf4f8deb8e91996ee4da99ed541f80b79 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 18 Dec 2013 10:53:26 +0100 Subject: [PATCH] Implement support for using custom variables as macros. Fixes #5364 --- doc/3.03-macros.md | 14 ++++++++++++++ lib/icinga/host.cpp | 6 ++++++ lib/icinga/notification.cpp | 6 ++++++ lib/icinga/service.cpp | 6 ++++++ lib/icinga/user.cpp | 8 ++++++++ 5 files changed, 40 insertions(+) diff --git a/doc/3.03-macros.md b/doc/3.03-macros.md index 9ac62ca42..4775d98ce 100644 --- a/doc/3.03-macros.md +++ b/doc/3.03-macros.md @@ -106,6 +106,9 @@ hosts or services: HOSTADDRESS | This is an alias for the `address` macro. If the `address` macro is not defined the host object's name is used instead. HOSTADDRESS6 | This is an alias for the `address6` macro. If the `address` macro is not defined the host object's name is used instead. +Custom variables are made available as macros with the name "_HOST" +where is the name of the custom variable. + ### Service Macros The following service macros are available in all commands that are executed for @@ -137,6 +140,9 @@ services: TOTALHOSTSERVICESUNKNOWN | Number of services associated with the host which are in an `UNKNOWN` state. TOTALHOSTSERVICESCRITICAL | Number of services associated with the host which are in a `CRITICAL` state. +Custom variables are made available as macros with the name "_SERVICE" +where is the name of the custom variable. + ### User Macros The following service macros are available in all commands that are executed for @@ -149,6 +155,14 @@ users: USEREMAIL | This is an alias for the `email` macro. USERPAGER | This is an alias for the `pager` macro. +Custom variables are made available as macros with the name "_USER" and +"_CONTACT" where is the name of the custom variable. + +### Notification Macros + +Custom variables are made available as macros with the name "_NOTIFICATION" +where is the name of the custom variable. + ### Global Macros The following macros are available in all commands: diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index 74e3a9c53..8fd27f1cb 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -615,6 +615,12 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *re } } + if (macro.SubStr(0, 5) == "_HOST") { + Dictionary::Ptr custom = GetCustom(); + *result = custom ? custom->Get(macro.SubStr(5)) : ""; + return true; + } + Dictionary::Ptr macros = GetMacros(); String name = macro; diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index 28a5aa72b..85fb50b0c 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -325,6 +325,12 @@ void Notification::ExecuteNotificationHelper(NotificationType type, const User:: bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const { + if (macro.SubStr(0, 13) == "_NOTIFICATION") { + Dictionary::Ptr custom = GetCustom(); + *result = custom ? custom->Get(macro.SubStr(13)) : ""; + return true; + } + Dictionary::Ptr macros = GetMacros(); if (macros && macros->Contains(macro)) { diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index be50f34f9..9a63b9f8b 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -474,6 +474,12 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Stri } } + if (macro.SubStr(0, 8) == "_SERVICE") { + Dictionary::Ptr custom = GetCustom(); + *result = custom ? custom->Get(macro.SubStr(8)) : ""; + return true; + } + Dictionary::Ptr macros = GetMacros(); if (macros && macros->Contains(macro)) { diff --git a/lib/icinga/user.cpp b/lib/icinga/user.cpp index 23d92f82f..18935106e 100644 --- a/lib/icinga/user.cpp +++ b/lib/icinga/user.cpp @@ -74,6 +74,14 @@ bool User::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *re } else if (macro == "USERDISPLAYNAME" || macro == "CONTACTALIAS") { *result = GetDisplayName(); return true; + } else if (macro.SubStr(0, 5) == "_USER") { + Dictionary::Ptr custom = GetCustom(); + *result = custom ? custom->Get(macro.SubStr(5)) : ""; + return true; + } else if (macro.SubStr(0, 8) == "_CONTACT") { + Dictionary::Ptr custom = GetCustom(); + *result = custom ? custom->Get(macro.SubStr(8)) : ""; + return true; } else { String tmacro; -- 2.40.0