From aba4f1a0d5a809b0edfcb8ea4d2da8933b696641 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 4 Apr 2014 21:36:47 +0200 Subject: [PATCH] Implement $user.$ runtime macros. Refs #5855 --- doc/3.03-custom-attributes-runtime-macros.md | 19 +-------- doc/8-differences-between-icinga-1x-and-2.md | 5 +++ lib/icinga/user.cpp | 42 +++++++------------- 3 files changed, 22 insertions(+), 44 deletions(-) diff --git a/doc/3.03-custom-attributes-runtime-macros.md b/doc/3.03-custom-attributes-runtime-macros.md index e904bf29f..6f438bcf9 100644 --- a/doc/3.03-custom-attributes-runtime-macros.md +++ b/doc/3.03-custom-attributes-runtime-macros.md @@ -152,8 +152,6 @@ external commands). ### Host Runtime Macros -TODO - The following host custom attributes are available in all commands that are executed for hosts or services: @@ -182,16 +180,8 @@ hosts or services: host.totalservicesunknown | Number of services associated with the host which are in an `UNKNOWN` state. host.totalservicescritical | Number of services associated with the host which are in a `CRITICAL` state. -> **Note** -> -> `HOSTADDRESS` and `HOSTADDRESS6` macros are available as legacy vars. The -> Icinga 2 Template Library (`ITL`) examples use the `$address$` macro instead -> requiring that macro key to be defined. - ### Service Runtime Macros -TODO - The following service macros are available in all commands that are executed for services: @@ -220,18 +210,13 @@ services: ### User Runtime Macros -TODO - The following custom attributes are available in all commands that are executed for users: Name | Description -----------------------|-------------- - USERNAME | The name of the user object. - USERDISPLAYNAME | The value of the display_name attribute. - USEREMAIL | This is an alias for the `email` macro. - USERPAGER | This is an alias for the `pager` macro. - + user.name | The name of the user object. + user.displayname | The value of the display_name attribute. ### Notification Runtime Macros diff --git a/doc/8-differences-between-icinga-1x-and-2.md b/doc/8-differences-between-icinga-1x-and-2.md index dde71baaa..30a65a2fd 100644 --- a/doc/8-differences-between-icinga-1x-and-2.md +++ b/doc/8-differences-between-icinga-1x-and-2.md @@ -305,6 +305,11 @@ Changes to host runtime macros Icinga 1.x | Icinga 2 -----------------------|---------------------- + USERNAME | user.name + USERDISPLAYNAME | user.displayname + USEREMAIL | email if set as `email` custom attribute. + USERPAGER | pager if set as `pager` custom attribute. + Changes to service runtime macros diff --git a/lib/icinga/user.cpp b/lib/icinga/user.cpp index 966c08f90..62c0d1fe0 100644 --- a/lib/icinga/user.cpp +++ b/lib/icinga/user.cpp @@ -68,37 +68,25 @@ TimePeriod::Ptr User::GetNotificationPeriod(void) const bool User::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const { - if (macro == "USERNAME" || macro == "CONTACTNAME") { - *result = GetName(); - return true; - } else if (macro == "USERDISPLAYNAME" || macro == "CONTACTALIAS") { - *result = GetDisplayName(); - return true; - } else if (macro.SubStr(0, 5) == "_USER") { - Dictionary::Ptr vars = GetVars(); - *result = vars ? vars->Get(macro.SubStr(5)) : ""; - return true; - } else if (macro.SubStr(0, 8) == "_CONTACT") { - Dictionary::Ptr vars = GetVars(); - *result = vars ? vars->Get(macro.SubStr(8)) : ""; - return true; - } else { - String tmacro; - - if (macro == "USEREMAIL" || macro == "CONTACTEMAIL") - tmacro = "email"; - else if (macro == "USERPAGER" || macro == "CONTACTPAGER") - tmacro = "pager"; - else - tmacro = macro; + /* require prefix for object macros */ + if (macro.SubStr(0, 5) == "user.") { + String key = macro.SubStr(5); + + if (key == "name") { + *result = GetName(); + return true; + } else if (key == "displayname") { + *result = GetDisplayName(); + return true; + } Dictionary::Ptr vars = GetVars(); - if (vars && vars->Contains(tmacro)) { - *result = vars->Get(tmacro); + if (vars && vars->Contains(key)) { + *result = vars->Get(key); return true; } - - return false; } + + return false; } -- 2.40.0