Implement 'env' dictionary instead of 'export_macros' array.

Revamp notification runtime macros too.

Refs #5855
This commit is contained in:
Michael Friedrich 2014-04-04 22:57:56 +02:00
parent f13e7b5fa0
commit 09cbf18b26
11 changed files with 101 additions and 111 deletions

View File

@ -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

View File

@ -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$"
}
}
### <a id="configuration-macros"></a> Configuration Macros
@ -221,8 +222,11 @@ users:
### <a id="notification-runtime-macros"></a> Notification Runtime Macros
Custom variables are made available as macros with the name "_NOTIFICATION<name>"
where <name> 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.
### <a id="global-runtime-macros"></a> Global Runtime Macros

View File

@ -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

View File

@ -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:

View File

@ -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$"
}
}

View File

@ -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;
};

View File

@ -161,7 +161,7 @@
%attribute %array "command" {
%attribute %string "*"
},
%attribute %array "export_macros" {
%attribute %dictionary "env" {
%attribute %string "*"
},
%attribute %array "escape_macros" {

View File

@ -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;

View File

@ -61,18 +61,15 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
Dictionary::Ptr envMacros = make_shared<Dictionary>();
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);
}
}

View File

@ -58,18 +58,15 @@ void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable)
Dictionary::Ptr envMacros = make_shared<Dictionary>();
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);
}
}

View File

@ -45,10 +45,9 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c
Value raw_command = commandObj->GetCommandLine();
StaticMacroResolver::Ptr notificationMacroResolver = make_shared<StaticMacroResolver>();
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<Dictionary>();
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);
}
}