From 09cbf18b26298f3e113f1e56437249951e25a02d Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 4 Apr 2014 22:57:56 +0200 Subject: [PATCH] Implement 'env' dictionary instead of 'export_macros' array. Revamp notification runtime macros too. Refs #5855 --- doc/3.02-commands.md | 30 ++++++++--------- doc/3.03-custom-attributes-runtime-macros.md | 20 +++++++----- doc/3.04-notifications.md | 34 +++++++++----------- doc/4.3-object-types.md | 28 ++++++++-------- etc/icinga2/conf.d/notifications.conf | 28 ++++++++-------- lib/icinga/command.ti | 4 +-- lib/icinga/icinga-type.conf | 2 +- lib/icinga/notification.cpp | 14 ++++---- lib/methods/pluginchecktask.cpp | 15 ++++----- lib/methods/plugineventtask.cpp | 15 ++++----- lib/methods/pluginnotificationtask.cpp | 22 ++++++------- 11 files changed, 101 insertions(+), 111 deletions(-) diff --git a/doc/3.02-commands.md b/doc/3.02-commands.md index 3868eab6c..b9336c859 100644 --- a/doc/3.02-commands.md +++ b/doc/3.02-commands.md @@ -105,24 +105,24 @@ TODO command = [ IcingaSysconfDir + "/icinga2/scripts/mail-notification.sh" ] - export_macros = [ - "NOTIFICATIONTYPE", - "SERVICEDESC", - "HOSTALIAS", - "HOSTADDRESS", - "SERVICESTATE", - "LONGDATETIME", - "SERVICEOUTPUT", - "NOTIFICATIONAUTHORNAME", - "NOTIFICATIONCOMMENT", - "HOSTDISPLAYNAME", - "SERVICEDISPLAYNAME", - "USEREMAIL" - ] + env = { + "NOTIFICATIONTYPE" = "$notification.type$" + "SERVICEDESC" = "$service.description$" + "HOSTALIAS" = "$host.displayname$", + "HOSTADDRESS" = "$host.vars.address$", + "SERVICESTATE" = "$service.state$", + "LONGDATETIME" = "$icinga.longdatetime$", + "SERVICEOUTPUT" = "$service.output$", + "NOTIFICATIONAUTHORNAME" = "$notification.author$", + "NOTIFICATIONCOMMENT" = "$notification.comment$", + "HOSTDISPLAYNAME" = "$host.displayname$", + "SERVICEDISPLAYNAME" = "$service.displayname$", + "USEREMAIL" = "$user.vars.email$" + } } The command attribute in the `mail-service-notification` command refers to the following -shell script. The macros specified in the `export_macros` array are exported +shell script. The macros specified in the `env` array are exported as environment variables and can be used in the notification script: #!/usr/bin/env bash diff --git a/doc/3.03-custom-attributes-runtime-macros.md b/doc/3.03-custom-attributes-runtime-macros.md index 6f438bcf9..9103e065b 100644 --- a/doc/3.03-custom-attributes-runtime-macros.md +++ b/doc/3.03-custom-attributes-runtime-macros.md @@ -101,8 +101,9 @@ emitted to the Icinga 2 log. TODO -The `export_macros` command object attribute requires a list of macros which should -be exported as environment variables prior to executing the command. +The `env` command object attribute requires a list of environment variables with values calculated +from either runtime macros or custom attributes which should be exported as environment variables +prior to executing the command. This is useful for example for hiding sensitive information on the command line output when passing credentials to database checks: @@ -117,10 +118,10 @@ when passing credentials to database checks: "MYSQLPASS" = "1c1ng42r0xx" }, - export_macros = [ - "MYSQLUSER", - "MYSQLPASS" - ] + env = { + "MYSQLUSER" = "$MYSQLUSER$", + "MYSQLPASS" = "$MYSQLPASS$" + } } ### Configuration Macros @@ -221,8 +222,11 @@ users: ### Notification Runtime Macros -Custom variables are made available as macros with the name "_NOTIFICATION" -where is the name of the custom variable. + Name | Description + -----------------------|-------------- + notification.type | The type of the notification. + notification.author | The author of the notification comment, if existing. + notification.comment | The comment of the notification, if existing. ### Global Runtime Macros diff --git a/doc/3.04-notifications.md b/doc/3.04-notifications.md index 6608432ee..2fefea44d 100644 --- a/doc/3.04-notifications.md +++ b/doc/3.04-notifications.md @@ -35,7 +35,7 @@ The user `icingaadmin` in the example below will get notified only on `WARNING` vars.pager = "+49123456789" } } - + > **Note** > > If you don't set the `notification_state_filter` and `notification_type_filter` @@ -52,31 +52,29 @@ your environment. > The chain of attribute inheritance including the (additive) vars dictionary for > notifications will allow granular custom attributes for every specific use case. - + There are various custom attributes available at runtime execution of the `NotificationCommand`. The example below may or may not fit your needs. -TODO - object NotificationCommand "mail-service-notification" { import "plugin-notification-command" command = [ IcingaSysconfDir + "/icinga2/scripts/mail-notification.sh" ] - export_macros = [ - "NOTIFICATIONTYPE", - "SERVICEDESC", - "HOSTALIAS", - "HOSTADDRESS", - "SERVICESTATE", - "LONGDATETIME", - "SERVICEOUTPUT", - "NOTIFICATIONAUTHORNAME", - "NOTIFICATIONCOMMENT", - "HOSTDISPLAYNAME", - "SERVICEDISPLAYNAME", - "USEREMAIL" - ] + env = { + "NOTIFICATIONTYPE" = "$notification.type$" + "SERVICEDESC" = "$service.description$" + "HOSTALIAS" = "$host.displayname$", + "HOSTADDRESS" = "$host.vars.address$", + "SERVICESTATE" = "$service.state$", + "LONGDATETIME" = "$icinga.longdatetime$", + "SERVICEOUTPUT" = "$service.output$", + "NOTIFICATIONAUTHORNAME" = "$notification.author$", + "NOTIFICATIONCOMMENT" = "$notification.comment$", + "HOSTDISPLAYNAME" = "$host.displayname$", + "SERVICEDISPLAYNAME" = "$service.displayname$", + "USEREMAIL" = "$user.vars.email$" + } } The command attribute in the `mail-service-notification` command refers to the diff --git a/doc/4.3-object-types.md b/doc/4.3-object-types.md index 1d178754b..b34985020 100644 --- a/doc/4.3-object-types.md +++ b/doc/4.3-object-types.md @@ -451,20 +451,20 @@ Example: IcingaSysconfDir + "/icinga2/scripts/mail-notification.sh" ] - export_macros = [ - "NOTIFICATIONTYPE", - "SERVICEDESC", - "HOSTALIAS", - "HOSTADDRESS", - "SERVICESTATE", - "LONGDATETIME", - "SERVICEOUTPUT", - "NOTIFICATIONAUTHORNAME", - "NOTIFICATIONCOMMENT", - "HOSTDISPLAYNAME", - "SERVICEDISPLAYNAME", - "USEREMAIL" - ] + env = { + "NOTIFICATIONTYPE" = "$notification.type$" + "SERVICEDESC" = "$service.description$" + "HOSTALIAS" = "$host.displayname$", + "HOSTADDRESS" = "$host.vars.address$", + "SERVICESTATE" = "$service.state$", + "LONGDATETIME" = "$icinga.longdatetime$", + "SERVICEOUTPUT" = "$service.output$", + "NOTIFICATIONAUTHORNAME" = "$notification.author$", + "NOTIFICATIONCOMMENT" = "$notification.comment$", + "HOSTDISPLAYNAME" = "$host.displayname$", + "SERVICEDISPLAYNAME" = "$service.displayname$", + "USEREMAIL" = "$user.vars.email$" + } } Attributes: diff --git a/etc/icinga2/conf.d/notifications.conf b/etc/icinga2/conf.d/notifications.conf index 3d9b7d461..636d05125 100644 --- a/etc/icinga2/conf.d/notifications.conf +++ b/etc/icinga2/conf.d/notifications.conf @@ -27,19 +27,19 @@ object NotificationCommand "mail-service-notification" { command = [ IcingaSysconfDir + "/icinga2/scripts/mail-notification.sh" ] - export_macros = [ - "NOTIFICATIONTYPE", - "SERVICEDESC", - "HOSTALIAS", - "HOSTADDRESS", - "SERVICESTATE", - "LONGDATETIME", - "SERVICEOUTPUT", - "NOTIFICATIONAUTHORNAME", - "NOTIFICATIONCOMMENT", - "HOSTDISPLAYNAME", - "SERVICEDISPLAYNAME", - "USEREMAIL" - ] + env = { + "NOTIFICATIONTYPE" = "$notification.type$" + "SERVICEDESC" = "$service.description$" + "HOSTALIAS" = "$host.displayname$", + "HOSTADDRESS" = "$host.vars.address$", + "SERVICESTATE" = "$service.state$", + "LONGDATETIME" = "$icinga.longdatetime$", + "SERVICEOUTPUT" = "$service.output$", + "NOTIFICATIONAUTHORNAME" = "$notification.author$", + "NOTIFICATIONCOMMENT" = "$notification.comment$", + "HOSTDISPLAYNAME" = "$host.displayname$", + "SERVICEDISPLAYNAME" = "$service.displayname$", + "USEREMAIL" = "$user.vars.email$" + } } diff --git a/lib/icinga/command.ti b/lib/icinga/command.ti index f46b633ef..7925caee8 100644 --- a/lib/icinga/command.ti +++ b/lib/icinga/command.ti @@ -9,8 +9,8 @@ abstract class Command : DynamicObject [config] Value timeout { default {{{ return 300; }}} }; - [config] Dictionary::Ptr macros; - [config] Array::Ptr export_macros; + [config] Dictionary::Ptr vars; + [config] Dictionary::Ptr env; [config] Array::Ptr escape_macros; }; diff --git a/lib/icinga/icinga-type.conf b/lib/icinga/icinga-type.conf index d845b97b4..71b1bae86 100644 --- a/lib/icinga/icinga-type.conf +++ b/lib/icinga/icinga-type.conf @@ -161,7 +161,7 @@ %attribute %array "command" { %attribute %string "*" }, - %attribute %array "export_macros" { + %attribute %dictionary "env" { %attribute %string "*" }, %attribute %array "escape_macros" { diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index dbe3f8580..f304f6d9e 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -371,15 +371,13 @@ bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, St { Dictionary::Ptr vars = GetVars(); - if (macro.SubStr(0, 13) == "_NOTIFICATION") { - *result = vars ? vars->Get(macro.SubStr(13)) : ""; - return true; - } - + if (macro.SubStr(0, 13) == "notification.") { + String key = vars->Get(macro.SubStr(13)); - if (vars && vars->Contains(macro)) { - *result = vars->Get(macro); - return true; + if (vars && vars->Contains(key)) { + *result = vars->Get(key); + return true; + } } return false; diff --git a/lib/methods/pluginchecktask.cpp b/lib/methods/pluginchecktask.cpp index 09f045457..517261ec7 100644 --- a/lib/methods/pluginchecktask.cpp +++ b/lib/methods/pluginchecktask.cpp @@ -61,18 +61,15 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes Dictionary::Ptr envMacros = make_shared(); - Array::Ptr export_macros = commandObj->GetExportMacros(); + Dictionary::Ptr env = commandObj->GetEnv(); - if (export_macros) { - BOOST_FOREACH(const String& macro, export_macros) { - String value; + if (env) { + BOOST_FOREACH(const Dictionary::Pair& kv, env) { + String name = kv.second; - if (!MacroProcessor::ResolveMacro(macro, resolvers, checkable->GetLastCheckResult(), &value)) { - Log(LogWarning, "icinga", "export_macros for service '" + service->GetName() + "' refers to unknown macro '" + macro + "'"); - continue; - } + Value value = MacroProcessor::ResolveMacros(name, resolvers, checkable->GetLastCheckResult(), Utility::EscapeShellCmd, commandObj->GetEscapeMacros()); - envMacros->Set(macro, value); + envMacros->Set(kv.first, value); } } diff --git a/lib/methods/plugineventtask.cpp b/lib/methods/plugineventtask.cpp index 036bc2aad..016ae776f 100644 --- a/lib/methods/plugineventtask.cpp +++ b/lib/methods/plugineventtask.cpp @@ -58,18 +58,15 @@ void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable) Dictionary::Ptr envMacros = make_shared(); - Array::Ptr export_macros = commandObj->GetExportMacros(); + Dictionary::Ptr env = commandObj->GetEnv(); - if (export_macros) { - BOOST_FOREACH(const String& macro, export_macros) { - String value; + if (env) { + BOOST_FOREACH(const Dictionary::Pair& kv, env) { + String name = kv.second; - if (!MacroProcessor::ResolveMacro(macro, resolvers, checkable->GetLastCheckResult(), &value)) { - Log(LogWarning, "icinga", "export_macros for command '" + commandObj->GetName() + "' refers to unknown macro '" + macro + "'"); - continue; - } + Value value = MacroProcessor::ResolveMacros(name, resolvers, checkable->GetLastCheckResult(), Utility::EscapeShellCmd, commandObj->GetEscapeMacros()); - envMacros->Set(macro, value); + envMacros->Set(kv.first, value); } } diff --git a/lib/methods/pluginnotificationtask.cpp b/lib/methods/pluginnotificationtask.cpp index d2361e60e..c9c942be6 100644 --- a/lib/methods/pluginnotificationtask.cpp +++ b/lib/methods/pluginnotificationtask.cpp @@ -45,10 +45,9 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c Value raw_command = commandObj->GetCommandLine(); StaticMacroResolver::Ptr notificationMacroResolver = make_shared(); - notificationMacroResolver->Add("NOTIFICATIONTYPE", Notification::NotificationTypeToString(type)); - notificationMacroResolver->Add("NOTIFICATIONAUTHOR", author); - notificationMacroResolver->Add("NOTIFICATIONAUTHORNAME", author); - notificationMacroResolver->Add("NOTIFICATIONCOMMENT", comment); + notificationMacroResolver->Add("notification.type", Notification::NotificationTypeToString(type)); + notificationMacroResolver->Add("notification.author", author); + notificationMacroResolver->Add("notification.comment", comment); Host::Ptr host; Service::Ptr service; @@ -68,18 +67,15 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c Dictionary::Ptr envMacros = make_shared(); - Array::Ptr export_macros = commandObj->GetExportMacros(); + Dictionary::Ptr env = commandObj->GetEnv(); - if (export_macros) { - BOOST_FOREACH(const String& macro, export_macros) { - String value; + if (env) { + BOOST_FOREACH(const Dictionary::Pair& kv, env) { + String name = kv.second; - if (!MacroProcessor::ResolveMacro(macro, resolvers, cr, &value)) { - Log(LogWarning, "icinga", "export_macros for notification '" + notification->GetName() + "' refers to unknown macro '" + macro + "'"); - continue; - } + Value value = MacroProcessor::ResolveMacros(name, resolvers, checkable->GetLastCheckResult(), Utility::EscapeShellCmd, commandObj->GetEscapeMacros()); - envMacros->Set(macro, value); + envMacros->Set(kv.first, value); } } -- 2.40.0