From: Michael Friedrich Date: Fri, 4 Apr 2014 18:09:23 +0000 (+0200) Subject: Implement ${host,service}.$ runtime macros. X-Git-Tag: v0.0.10~218 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0a0399826f2a58e18dfdd941afc5e42e11cdb9e4;p=icinga2 Implement ${host,service}.$ runtime macros. Refs #5855 --- diff --git a/doc/3.03-custom-attributes-runtime-macros.md b/doc/3.03-custom-attributes-runtime-macros.md index e3eb74e7a..e904bf29f 100644 --- a/doc/3.03-custom-attributes-runtime-macros.md +++ b/doc/3.03-custom-attributes-runtime-macros.md @@ -159,26 +159,28 @@ hosts or services: Name | Description -----------------------|-------------- - HOSTNAME | The name of the host object. - HOSTDISPLAYNAME | The value of the `display_name` attribute. - HOSTALIAS | This is an alias for the `HOSTDISPLAYNAME` macro. - HOSTSTATE | The host's current state. Can be one of `UNREACHABLE`, `UP` and `DOWN`. - HOSTSTATEID | The host's current state. Can be one of `0` (up), `1` (down) and `2` (unreachable). - HOSTSTATETYPE | The host's current state type. Can be one of `SOFT` and `HARD`. - HOSTATTEMPT | The current check attempt number. - MAXHOSTATTEMPT | The maximum number of checks which are executed before changing to a hard state. - LASTHOSTSTATE | The host's previous state. Can be one of `UNREACHABLE`, `UP` and `DOWN`. - LASTHOSTSTATEID | The host's previous state. Can be one of `0` (up), `1` (down) and `2` (unreachable). - LASTHOSTSTATETYPE | The host's previous state type. Can be one of `SOFT` and `HARD`. - LASTHOSTSTATECHANGE | The last state change's timestamp. - HOSTDURATIONSEC | The time since the last state change. - HOSTLATENCY | The host's check latency. - HOSTEXECUTIONTIME | The host's check execution time. - HOSTOUTPUT | The last check's output. - HOSTPERFDATA | The last check's performance data. - LASTHOSTCHECK | The timestamp when the last check was executed. - 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. + host.name | The name of the host object. + host.displayname | The value of the `display_name` attribute. + host.state | The host's current state. Can be one of `UNREACHABLE`, `UP` and `DOWN`. + host.stateid | The host's current state. Can be one of `0` (up), `1` (down) and `2` (unreachable). + host.statetype | The host's current state type. Can be one of `SOFT` and `HARD`. + host.attempt | The current check attempt number. + host.maxattempt | The maximum number of checks which are executed before changing to a hard state. + host.laststate | The host's previous state. Can be one of `UNREACHABLE`, `UP` and `DOWN`. + host.laststateid | The host's previous state. Can be one of `0` (up), `1` (down) and `2` (unreachable). + host.laststatetype | The host's previous state type. Can be one of `SOFT` and `HARD`. + host.laststatechange | The last state change's timestamp. + host.durationsec | The time since the last state change. + host.latency | The host's check latency. + host.executiontime | The host's check execution time. + host.output | The last check's output. + host.perfdata | The last check's performance data. + host.lastcheck | The timestamp when the last check was executed. + host.totalservices | Number of services associated with the host. + host.totalservicesok | Number of services associated with the host which are in an `OK` state. + host.totalserviceswarning | Number of services associated with the host which are in a `WARNING` state. + 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** > @@ -195,29 +197,24 @@ services: Name | Description -----------------------|-------------- - SERVICEDESC | The short name of the service object. - SERVICEDISPLAYNAME | The value of the `display_name` attribute. - SERVICECHECKCOMMAND | This is an alias for the `SERVICEDISPLAYNAME` macro. - SERVICESTATE | The service's current state. Can be one of `OK`, `WARNING`, `CRITICAL` and `UNKNOWN`. - SERVICESTATEID | The service's current state. Can be one of `0` (ok), `1` (warning), `2` (critical) and `3` (unknown). - SERVICESTATETYPE | The service's current state type. Can be one of `SOFT` and `HARD`. - SERVICEATTEMPT | The current check attempt number. - MAXSERVICEATTEMPT | The maximum number of checks which are executed before changing to a hard state. - LASTSERVICESTATE | The service's previous state. Can be one of `OK`, `WARNING`, `CRITICAL` and `UNKNOWN`. - LASTSERVICESTATEID | The service's previous state. Can be one of `0` (ok), `1` (warning), `2` (critical) and `3` (unknown). - LASTSERVICESTATETYPE | The service's previous state type. Can be one of `SOFT` and `HARD`. - LASTSERVICESTATECHANGE | The last state change's timestamp. - SERVICEDURATIONSEC | The time since the last state change. - SERVICELATENCY | The service's check latency. - SERVICEEXECUTIONTIME | The service's check execution time. - SERVICEOUTPUT | The last check's output. - SERVICEPERFDATA | The last check's performance data. - LASTSERVICECHECK | The timestamp when the last check was executed. - TOTALHOSTSERVICES | Number of services associated with the host. - TOTALHOSTSERVICESOK | Number of services associated with the host which are in an `OK` state. - TOTALHOSTSERVICESWARNING | Number of services associated with the host which are in a `WARNING` state. - 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. + service.description | The short name of the service object. + service.displayname | The value of the `display_name` attribute. + service.checkcommand | This is an alias for the `SERVICEDISPLAYNAME` macro. + service.state | The service's current state. Can be one of `OK`, `WARNING`, `CRITICAL` and `UNKNOWN`. + service.stateid | The service's current state. Can be one of `0` (ok), `1` (warning), `2` (critical) and `3` (unknown). + service.statetype | The service's current state type. Can be one of `SOFT` and `HARD`. + service.attempt | The current check attempt number. + service.maxattempt | The maximum number of checks which are executed before changing to a hard state. + service.laststate | The service's previous state. Can be one of `OK`, `WARNING`, `CRITICAL` and `UNKNOWN`. + service.laststateid | The service's previous state. Can be one of `0` (ok), `1` (warning), `2` (critical) and `3` (unknown). + service.laststatetype | The service's previous state type. Can be one of `SOFT` and `HARD`. + service.laststatechange| The last state change's timestamp. + service.durationsec | The time since the last state change. + service.latency | The service's check latency. + service.executiontime | The service's check execution time. + service.output | The last check's output. + service.perfdata | The last check's performance data. + service.lastcheck | The timestamp when the last check was executed. diff --git a/doc/8-differences-between-icinga-1x-and-2.md b/doc/8-differences-between-icinga-1x-and-2.md index 1b879c48b..dde71baaa 100644 --- a/doc/8-differences-between-icinga-1x-and-2.md +++ b/doc/8-differences-between-icinga-1x-and-2.md @@ -301,6 +301,60 @@ attribute in command objects. Icinga 2 requires an object specific namespace when accessing configuration and stateful runtime macros. Custom attributes can be access directly. +Changes to host runtime macros + + Icinga 1.x | Icinga 2 + -----------------------|---------------------- + +Changes to service runtime macros + + Icinga 1.x | Icinga 2 + -----------------------|---------------------- + SERVICEDESC | service.description + SERVICEDISPLAYNAME | service.displayname + SERVICECHECKCOMMAND | service.checkcommand + SERVICESTATE | service.state + SERVICESTATEID | service.stateid + SERVICESTATETYPE | service.statetype + SERVICEATTEMPT | service.attempt + MAXSERVICEATTEMPT | service.maxattempt + LASTSERVICESTATE | service.laststate + LASTSERVICESTATEID | service.laststateid + LASTSERVICESTATETYPE | service.laststatetype + LASTSERVICESTATECHANGE | service.laststatechange + SERVICEDURATIONSEC | service.durationsec + SERVICELATENCY | service.latency + SERVICEEXECUTIONTIME | service.executiontime + SERVICEOUTPUT | service.output + SERVICEPERFDATA | service.perfdata + LASTSERVICECHECK | service.lastcheck + + +Changes to user (contact) runtime macros + + Icinga 1.x | Icinga 2 + -----------------------|---------------------- + HOSTNAME | host.name + HOSTDISPLAYNAME | host.displayname + HOSTALIAS | .. + HOSTSTATE | host.state + HOSTSTATEID | host.stateid + HOSTSTATETYPE | host.statetype + HOSTATTEMPT | host.attempt + MAXHOSTATTEMPT | host.maxattempt + LASTHOSTSTATE | host.laststate + LASTHOSTSTATEID | host.laststateid + LASTHOSTSTATETYPE | host.laststatetype + LASTHOSTSTATECHANGE | host.laststatechange + HOSTDURATIONSEC | host.durationsec + HOSTLATENCY | host.latency + HOSTEXECUTIONTIME | host.executiontime + HOSTOUTPUT | host.output + HOSTPERFDATA | host.perfdata + LASTHOSTCHECK | host.lastcheck + HOSTADDRESS | -- + HOSTADDRESS6 | -- + Changes to global runtime macros: Icinga 1.x | Icinga 2 diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index dd0e7f1b6..253d76bf5 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -258,113 +258,124 @@ String Host::StateTypeToString(StateType type) bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const { - if (macro == "HOSTNAME") { - *result = GetName(); - return true; - } - else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") { - *result = GetDisplayName(); - return true; - } + /* require prefix for object macros */ + if (macro.SubStr(0, 5) == "host.") { + String key = macro.SubStr(5); - CheckResult::Ptr cr = GetLastCheckResult(); - - if (macro == "HOSTSTATE") { - switch (GetState()) { - case HostUnreachable: - *result = "UNREACHABLE"; - break; - case HostUp: - *result = "UP"; - break; - case HostDown: - *result = "DOWN"; - break; - default: - ASSERT(0); + if (key == "name") { + *result = GetName(); + return true; + } + else if (key == "displaymane") { + *result = GetDisplayName(); + return true; } - return true; - } else if (macro == "HOSTSTATEID") { - *result = Convert::ToString(GetState()); - return true; - } else if (macro == "HOSTSTATETYPE") { - *result = StateTypeToString(GetStateType()); - return true; - } else if (macro == "HOSTATTEMPT") { - *result = Convert::ToString(GetCheckAttempt()); - return true; - } else if (macro == "MAXHOSTATTEMPT") { - *result = Convert::ToString(GetMaxCheckAttempts()); - return true; - } else if (macro == "LASTHOSTSTATE") { - *result = StateToString(GetLastState()); - return true; - } else if (macro == "LASTHOSTSTATEID") { - *result = Convert::ToString(GetLastState()); - return true; - } else if (macro == "LASTHOSTSTATETYPE") { - *result = StateTypeToString(GetLastStateType()); - return true; - } else if (macro == "LASTHOSTSTATECHANGE") { - *result = Convert::ToString((long)GetLastStateChange()); - return true; - } else if (macro == "HOSTDURATIONSEC") { - *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange())); - return true; - } else if (macro == "HOSTCHECKCOMMAND") { - CheckCommand::Ptr commandObj = GetCheckCommand(); - - if (commandObj) - *result = commandObj->GetName(); - else - *result = ""; - - return true; - } - + CheckResult::Ptr cr = GetLastCheckResult(); + + if (key == "state") { + switch (GetState()) { + case HostUnreachable: + *result = "UNREACHABLE"; + break; + case HostUp: + *result = "UP"; + break; + case HostDown: + *result = "DOWN"; + break; + default: + ASSERT(0); + } - if (cr) { - if (macro == "HOSTLATENCY") { - *result = Convert::ToString(Service::CalculateLatency(cr)); return true; - } else if (macro == "HOSTEXECUTIONTIME") { - *result = Convert::ToString(Service::CalculateExecutionTime(cr)); + } else if (key == "stateid") { + *result = Convert::ToString(GetState()); return true; - } else if (macro == "HOSTOUTPUT") { - *result = cr->GetOutput(); + } else if (key == "statetype") { + *result = StateTypeToString(GetStateType()); return true; - } else if (macro == "HOSTPERFDATA") { - *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData()); + } else if (key == "attempt") { + *result = Convert::ToString(GetCheckAttempt()); return true; - } else if (macro == "LASTHOSTCHECK") { - *result = Convert::ToString((long)cr->GetScheduleStart()); + } else if (key == "maxattempt") { + *result = Convert::ToString(GetMaxCheckAttempts()); return true; - } - } - - Dictionary::Ptr vars = GetVars(); - - if (macro.SubStr(0, 5) == "_HOST") { - *result = vars ? vars->Get(macro.SubStr(5)) : ""; - return true; - } + } else if (key == "laststate") { + *result = StateToString(GetLastState()); + return true; + } else if (key == "laststateid") { + *result = Convert::ToString(GetLastState()); + return true; + } else if (key == "laststatetype") { + *result = StateTypeToString(GetLastStateType()); + return true; + } else if (key == "laststatechange") { + *result = Convert::ToString((long)GetLastStateChange()); + return true; + } else if (key == "durationsec") { + *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange())); + return true; + } else if (key == "checkcommand") { + CheckCommand::Ptr commandObj = GetCheckCommand(); - String name = macro; + if (commandObj) + *result = commandObj->GetName(); + else + *result = ""; - if (name == "HOSTADDRESS") - name = "address"; - else if (macro == "HOSTADDRESS6") - name = "address6"; + return true; + } else if (key == "totalservices" || key == "totalservicesok" || key == "totalserviceswarning" + || key == "totalservicesunknown" || key == "totalservicescritical") { + int filter = -1; + int count = 0; + + if (key == "totalservicesok") + filter = StateOK; + else if (key == "totalserviceswarning") + filter = StateWarning; + else if (key == "totalservicesunknown") + filter = StateUnknown; + else if (key == "totalservicescritical") + filter = StateCritical; + + BOOST_FOREACH(const Service::Ptr& service, GetServices()) { + if (filter != -1 && service->GetState() != filter) + continue; + + count++; + } + + *result = Convert::ToString(count); + return true; + } + + + if (cr) { + if (key == "latency") { + *result = Convert::ToString(Service::CalculateLatency(cr)); + return true; + } else if (key == "executiontime") { + *result = Convert::ToString(Service::CalculateExecutionTime(cr)); + return true; + } else if (key == "output") { + *result = cr->GetOutput(); + return true; + } else if (key == "perfdata") { + *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData()); + return true; + } else if (key == "lastcheck") { + *result = Convert::ToString((long)cr->GetScheduleStart()); + return true; + } + } - if (vars && vars->Contains(name)) { - *result = vars->Get(name); - return true; - } + Dictionary::Ptr vars = GetVars(); - if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") { - *result = GetName(); - return true; + if (vars && vars->Contains(key)) { + *result = vars->Get(key); + return true; + } } return false; diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index de39684ba..a1d527536 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -134,35 +134,34 @@ 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 (key == "timet") { - *result = Convert::ToString((long)now); - return true; - } else if (key == "longdatetime") { - *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", now); - return true; - } else if (key == "shortdatetime") { - *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", now); - return true; - } else if (key == "date") { - *result = Utility::FormatDateTime("%Y-%m-%d", now); - return true; - } else if (key == "time") { - *result = Utility::FormatDateTime("%H:%M:%S %z", now); - return true; - } - - Dictionary::Ptr vars = GetVars(); - - if (vars && vars->Contains(key)) { - *result = vars->Get(key); - return true; + if (macro.SubStr(0, 7) == "icinga.") { + String key = macro.SubStr(7); + + double now = Utility::GetTime(); + + if (key == "timet") { + *result = Convert::ToString((long)now); + return true; + } else if (key == "longdatetime") { + *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", now); + return true; + } else if (key == "shortdatetime") { + *result = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", now); + return true; + } else if (key == "date") { + *result = Utility::FormatDateTime("%Y-%m-%d", now); + return true; + } else if (key == "time") { + *result = Utility::FormatDateTime("%H:%M:%S %z", now); + return true; + } + + Dictionary::Ptr vars = GetVars(); + + if (vars && vars->Contains(key)) { + *result = vars->Get(key); + return true; + } } return false; diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 950cd05f7..2f9eac67a 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -128,108 +128,84 @@ String Service::StateTypeToString(StateType type) bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const { - if (macro == "SERVICEDESC") { - *result = GetShortName(); - return true; - } else if (macro == "SERVICEDISPLAYNAME") { - *result = GetDisplayName(); - return true; - } else if (macro == "SERVICECHECKCOMMAND") { - CheckCommand::Ptr commandObj = GetCheckCommand(); - - if (commandObj) - *result = commandObj->GetName(); - else - *result = ""; - - return true; - } + /* require prefix for object macros */ + if (macro.SubStr(0, 8) == "service.") { + String key = macro.SubStr(8); - if (macro == "SERVICESTATE") { - *result = StateToString(GetState()); - return true; - } else if (macro == "SERVICESTATEID") { - *result = Convert::ToString(GetState()); - return true; - } else if (macro == "SERVICESTATETYPE") { - *result = StateTypeToString(GetStateType()); - return true; - } else if (macro == "SERVICEATTEMPT") { - *result = Convert::ToString(GetCheckAttempt()); - return true; - } else if (macro == "MAXSERVICEATTEMPT") { - *result = Convert::ToString(GetMaxCheckAttempts()); - return true; - } else if (macro == "LASTSERVICESTATE") { - *result = StateToString(GetLastState()); - return true; - } else if (macro == "LASTSERVICESTATEID") { - *result = Convert::ToString(GetLastState()); - return true; - } else if (macro == "LASTSERVICESTATETYPE") { - *result = StateTypeToString(GetLastStateType()); - return true; - } else if (macro == "LASTSERVICESTATECHANGE") { - *result = Convert::ToString((long)GetLastStateChange()); - return true; - } else if (macro == "SERVICEDURATIONSEC") { - *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange())); - return true; - } else if (macro == "TOTALHOSTSERVICES" || macro == "TOTALHOSTSERVICESOK" || macro == "TOTALHOSTSERVICESWARNING" - || macro == "TOTALHOSTSERVICESUNKNOWN" || macro == "TOTALHOSTSERVICESCRITICAL") { - int filter = -1; - int count = 0; - - if (macro == "TOTALHOSTSERVICESOK") - filter = StateOK; - else if (macro == "TOTALHOSTSERVICESWARNING") - filter = StateWarning; - else if (macro == "TOTALHOSTSERVICESUNKNOWN") - filter = StateUnknown; - else if (macro == "TOTALHOSTSERVICESCRITICAL") - filter = StateCritical; - - BOOST_FOREACH(const Service::Ptr& service, GetHost()->GetServices()) { - if (filter != -1 && service->GetState() != filter) - continue; - - count++; - } + if (key == "description") { + *result = GetShortName(); + return true; + } else if (key == "displayname") { + *result = GetDisplayName(); + return true; + } else if (key == "checkcommand") { + CheckCommand::Ptr commandObj = GetCheckCommand(); - *result = Convert::ToString(count); - return true; - } + if (commandObj) + *result = commandObj->GetName(); + else + *result = ""; - if (cr) { - if (macro == "SERVICELATENCY") { - *result = Convert::ToString(Service::CalculateLatency(cr)); return true; - } else if (macro == "SERVICEEXECUTIONTIME") { - *result = Convert::ToString(Service::CalculateExecutionTime(cr)); + } + + if (key == "state") { + *result = StateToString(GetState()); + return true; + } else if (key == "stateid") { + *result = Convert::ToString(GetState()); + return true; + } else if (key == "statetype") { + *result = StateTypeToString(GetStateType()); + return true; + } else if (key == "attempt") { + *result = Convert::ToString(GetCheckAttempt()); return true; - } else if (macro == "SERVICEOUTPUT") { - *result = cr->GetOutput(); + } else if (key == "maxattempt") { + *result = Convert::ToString(GetMaxCheckAttempts()); return true; - } else if (macro == "SERVICEPERFDATA") { - *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData()); + } else if (key == "laststate") { + *result = StateToString(GetLastState()); return true; - } else if (macro == "LASTSERVICECHECK") { - *result = Convert::ToString((long)cr->GetExecutionEnd()); + } else if (key == "laststateid") { + *result = Convert::ToString(GetLastState()); + return true; + } else if (key == "laststatetype") { + *result = StateTypeToString(GetLastStateType()); + return true; + } else if (key == "laststatechange") { + *result = Convert::ToString((long)GetLastStateChange()); + return true; + } else if (key == "durationsec") { + *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange())); return true; } - } - - Dictionary::Ptr vars = GetVars(); - if (macro.SubStr(0, 8) == "_SERVICE") { - *result = vars ? vars->Get(macro.SubStr(8)) : ""; - return true; - } + if (cr) { + if (key == "latency") { + *result = Convert::ToString(Service::CalculateLatency(cr)); + return true; + } else if (key == "executiontime") { + *result = Convert::ToString(Service::CalculateExecutionTime(cr)); + return true; + } else if (key == "output") { + *result = cr->GetOutput(); + return true; + } else if (key == "perfdata") { + *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData()); + return true; + } else if (key == "lastcheck") { + *result = Convert::ToString((long)cr->GetExecutionEnd()); + return true; + } + } + Dictionary::Ptr vars = GetVars(); - if (vars && vars->Contains(macro)) { - *result = vars->Get(macro); - return true; + if (vars && vars->Contains(key)) { + *result = vars->Get(key); + return true; + } } return false;