From: Michael Friedrich Date: Fri, 4 Apr 2014 17:35:47 +0000 (+0200) Subject: Implement global runtime macros as $icinga.$. X-Git-Tag: v0.0.10~219 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e375f15e5b59558de33def13031a1e6eefe57016;p=icinga2 Implement global runtime macros as $icinga.$. Refs #5855 --- diff --git a/components/perfdata/perfdatawriter.ti b/components/perfdata/perfdatawriter.ti index 400392103..5f577aa98 100644 --- a/components/perfdata/perfdatawriter.ti +++ b/components/perfdata/perfdatawriter.ti @@ -21,7 +21,7 @@ class PerfdataWriter : DynamicObject [config] String host_format_template { default {{{ return "DATATYPE::HOSTPERFDATA\t" - "TIMET::$TIMET$\t" + "TIMET::$icinga.timet$\t" "HOSTNAME::$HOSTNAME$\t" "HOSTPERFDATA::$HOSTPERFDATA$\t" "HOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\t" @@ -32,7 +32,7 @@ class PerfdataWriter : DynamicObject [config] String service_format_template { default {{{ return "DATATYPE::SERVICEPERFDATA\t" - "TIMET::$TIMET$\t" + "TIMET::$icinga.timet$\t" "HOSTNAME::$HOSTNAME$\t" "SERVICEDESC::$SERVICEDESC$\t" "SERVICEPERFDATA::$SERVICEPERFDATA$\t" diff --git a/doc/3.03-custom-attributes-runtime-macros.md b/doc/3.03-custom-attributes-runtime-macros.md index 7d4f1fac0..e3eb74e7a 100644 --- a/doc/3.03-custom-attributes-runtime-macros.md +++ b/doc/3.03-custom-attributes-runtime-macros.md @@ -244,17 +244,19 @@ where is the name of the custom variable. ### Global Runtime Macros -TODO - The following macros are available in all executed commands: +> **Note** +> +> Global application runtime macros require the `icinga.` prefix. + Name | Description -----------------------|-------------- - TIMET | Current UNIX timestamp. - LONGDATETIME | Current date and time including timezone information. Example: `2014-01-03 11:23:08 +0000` - SHORTDATETIME | Current date and time. Example: `2014-01-03 11:23:08` - DATE | Current date. Example: `2014-01-03` - TIME | Current time including timezone information. Example: `11:23:08 +0000` + icinga.timet | Current UNIX timestamp. + icinga.longdatetime | Current date and time including timezone information. Example: `2014-01-03 11:23:08 +0000` + icinga.shortdatetime | Current date and time. Example: `2014-01-03 11:23:08` + icinga.date | Current date. Example: `2014-01-03` + icinga.time | Current time including timezone information. Example: `11:23:08 +0000` diff --git a/doc/8-differences-between-icinga-1x-and-2.md b/doc/8-differences-between-icinga-1x-and-2.md index ff674e21d..1b879c48b 100644 --- a/doc/8-differences-between-icinga-1x-and-2.md +++ b/doc/8-differences-between-icinga-1x-and-2.md @@ -296,6 +296,22 @@ Icinga 2. Macros exported into the environment must be set using the `export_macros` attribute in command objects. +### Runtime Macros + +Icinga 2 requires an object specific namespace when accessing configuration +and stateful runtime macros. Custom attributes can be access directly. + +Changes to global runtime macros: + + Icinga 1.x | Icinga 2 + -----------------------|---------------------- + TIMET | icinga.timet + LONGDATETIME | icinga.longdatetime + SHORTDATETIME | icinga.shortdatetime + DATE | icinga.date + TIME | icinga.time + + ## Checks ### Check Output diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index 82148c6f7..de39684ba 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -133,29 +133,35 @@ String IcingaApplication::GetNodeName(void) const bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const { + /* require icinga prefix for application macros */ + if (macro.SubStr(0, 7) != "icinga.") + return false; + + String key = macro.SubStr(7); + double now = Utility::GetTime(); - if (macro == "TIMET") { + if (key == "timet") { *result = Convert::ToString((long)now); return true; - } else if (macro == "LONGDATETIME") { + } else if (key == "longdatetime") { *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", now); return true; - } else if (macro == "SHORTDATETIME") { + } else if (key == "shortdatetime") { *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", now); return true; - } else if (macro == "DATE") { + } else if (key == "date") { *result = Utility::FormatDateTime("%Y-%m-%d", now); return true; - } else if (macro == "TIME") { + } else if (key == "time") { *result = Utility::FormatDateTime("%H:%M:%S %z", now); return true; } Dictionary::Ptr vars = GetVars(); - if (vars && vars->Contains(macro)) { - *result = vars->Get(macro); + if (vars && vars->Contains(key)) { + *result = vars->Get(key); return true; }