Implement ${host,service}.<name>$ runtime macros.

Refs #5855
This commit is contained in:
Michael Friedrich 2014-04-04 20:09:23 +02:00
parent e375f15e5b
commit 0a0399826f
5 changed files with 296 additions and 259 deletions

View File

@ -159,26 +159,28 @@ hosts or services:
Name | Description Name | Description
-----------------------|-------------- -----------------------|--------------
HOSTNAME | The name of the host object. host.name | The name of the host object.
HOSTDISPLAYNAME | The value of the `display_name` attribute. host.displayname | The value of the `display_name` attribute.
HOSTALIAS | This is an alias for the `HOSTDISPLAYNAME` macro. host.state | The host's current state. Can be one of `UNREACHABLE`, `UP` and `DOWN`.
HOSTSTATE | 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).
HOSTSTATEID | 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`.
HOSTSTATETYPE | The host's current state type. Can be one of `SOFT` and `HARD`. host.attempt | The current check attempt number.
HOSTATTEMPT | The current check attempt number. host.maxattempt | The maximum number of checks which are executed before changing to a hard state.
MAXHOSTATTEMPT | 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`.
LASTHOSTSTATE | 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).
LASTHOSTSTATEID | 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`.
LASTHOSTSTATETYPE | The host's previous state type. Can be one of `SOFT` and `HARD`. host.laststatechange | The last state change's timestamp.
LASTHOSTSTATECHANGE | The last state change's timestamp. host.durationsec | The time since the last state change.
HOSTDURATIONSEC | The time since the last state change. host.latency | The host's check latency.
HOSTLATENCY | The host's check latency. host.executiontime | The host's check execution time.
HOSTEXECUTIONTIME | The host's check execution time. host.output | The last check's output.
HOSTOUTPUT | The last check's output. host.perfdata | The last check's performance data.
HOSTPERFDATA | The last check's performance data. host.lastcheck | The timestamp when the last check was executed.
LASTHOSTCHECK | The timestamp when the last check was executed. host.totalservices | Number of services associated with the host.
HOSTADDRESS | This is an alias for the `address` macro. If the `address` macro is not defined the host object's name is used instead. host.totalservicesok | Number of services associated with the host which are in an `OK` state.
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.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** > **Note**
> >
@ -195,29 +197,24 @@ services:
Name | Description Name | Description
-----------------------|-------------- -----------------------|--------------
SERVICEDESC | The short name of the service object. service.description | The short name of the service object.
SERVICEDISPLAYNAME | The value of the `display_name` attribute. service.displayname | The value of the `display_name` attribute.
SERVICECHECKCOMMAND | This is an alias for the `SERVICEDISPLAYNAME` macro. service.checkcommand | This is an alias for the `SERVICEDISPLAYNAME` macro.
SERVICESTATE | The service's current state. Can be one of `OK`, `WARNING`, `CRITICAL` and `UNKNOWN`. service.state | 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). service.stateid | 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`. service.statetype | The service's current state type. Can be one of `SOFT` and `HARD`.
SERVICEATTEMPT | The current check attempt number. service.attempt | The current check attempt number.
MAXSERVICEATTEMPT | The maximum number of checks which are executed before changing to a hard state. service.maxattempt | 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`. service.laststate | 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). service.laststateid | 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`. service.laststatetype | The service's previous state type. Can be one of `SOFT` and `HARD`.
LASTSERVICESTATECHANGE | The last state change's timestamp. service.laststatechange| The last state change's timestamp.
SERVICEDURATIONSEC | The time since the last state change. service.durationsec | The time since the last state change.
SERVICELATENCY | The service's check latency. service.latency | The service's check latency.
SERVICEEXECUTIONTIME | The service's check execution time. service.executiontime | The service's check execution time.
SERVICEOUTPUT | The last check's output. service.output | The last check's output.
SERVICEPERFDATA | The last check's performance data. service.perfdata | The last check's performance data.
LASTSERVICECHECK | The timestamp when the last check was executed. service.lastcheck | 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.

View File

@ -301,6 +301,60 @@ attribute in command objects.
Icinga 2 requires an object specific namespace when accessing configuration Icinga 2 requires an object specific namespace when accessing configuration
and stateful runtime macros. Custom attributes can be access directly. 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: Changes to global runtime macros:
Icinga 1.x | Icinga 2 Icinga 1.x | Icinga 2

View File

@ -258,18 +258,22 @@ String Host::StateTypeToString(StateType type)
bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
{ {
if (macro == "HOSTNAME") { /* require prefix for object macros */
if (macro.SubStr(0, 5) == "host.") {
String key = macro.SubStr(5);
if (key == "name") {
*result = GetName(); *result = GetName();
return true; return true;
} }
else if (macro == "HOSTDISPLAYNAME" || macro == "HOSTALIAS") { else if (key == "displaymane") {
*result = GetDisplayName(); *result = GetDisplayName();
return true; return true;
} }
CheckResult::Ptr cr = GetLastCheckResult(); CheckResult::Ptr cr = GetLastCheckResult();
if (macro == "HOSTSTATE") { if (key == "state") {
switch (GetState()) { switch (GetState()) {
case HostUnreachable: case HostUnreachable:
*result = "UNREACHABLE"; *result = "UNREACHABLE";
@ -285,34 +289,34 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *re
} }
return true; return true;
} else if (macro == "HOSTSTATEID") { } else if (key == "stateid") {
*result = Convert::ToString(GetState()); *result = Convert::ToString(GetState());
return true; return true;
} else if (macro == "HOSTSTATETYPE") { } else if (key == "statetype") {
*result = StateTypeToString(GetStateType()); *result = StateTypeToString(GetStateType());
return true; return true;
} else if (macro == "HOSTATTEMPT") { } else if (key == "attempt") {
*result = Convert::ToString(GetCheckAttempt()); *result = Convert::ToString(GetCheckAttempt());
return true; return true;
} else if (macro == "MAXHOSTATTEMPT") { } else if (key == "maxattempt") {
*result = Convert::ToString(GetMaxCheckAttempts()); *result = Convert::ToString(GetMaxCheckAttempts());
return true; return true;
} else if (macro == "LASTHOSTSTATE") { } else if (key == "laststate") {
*result = StateToString(GetLastState()); *result = StateToString(GetLastState());
return true; return true;
} else if (macro == "LASTHOSTSTATEID") { } else if (key == "laststateid") {
*result = Convert::ToString(GetLastState()); *result = Convert::ToString(GetLastState());
return true; return true;
} else if (macro == "LASTHOSTSTATETYPE") { } else if (key == "laststatetype") {
*result = StateTypeToString(GetLastStateType()); *result = StateTypeToString(GetLastStateType());
return true; return true;
} else if (macro == "LASTHOSTSTATECHANGE") { } else if (key == "laststatechange") {
*result = Convert::ToString((long)GetLastStateChange()); *result = Convert::ToString((long)GetLastStateChange());
return true; return true;
} else if (macro == "HOSTDURATIONSEC") { } else if (key == "durationsec") {
*result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange())); *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
return true; return true;
} else if (macro == "HOSTCHECKCOMMAND") { } else if (key == "checkcommand") {
CheckCommand::Ptr commandObj = GetCheckCommand(); CheckCommand::Ptr commandObj = GetCheckCommand();
if (commandObj) if (commandObj)
@ -321,23 +325,46 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *re
*result = ""; *result = "";
return true; 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 (cr) {
if (macro == "HOSTLATENCY") { if (key == "latency") {
*result = Convert::ToString(Service::CalculateLatency(cr)); *result = Convert::ToString(Service::CalculateLatency(cr));
return true; return true;
} else if (macro == "HOSTEXECUTIONTIME") { } else if (key == "executiontime") {
*result = Convert::ToString(Service::CalculateExecutionTime(cr)); *result = Convert::ToString(Service::CalculateExecutionTime(cr));
return true; return true;
} else if (macro == "HOSTOUTPUT") { } else if (key == "output") {
*result = cr->GetOutput(); *result = cr->GetOutput();
return true; return true;
} else if (macro == "HOSTPERFDATA") { } else if (key == "perfdata") {
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData()); *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
return true; return true;
} else if (macro == "LASTHOSTCHECK") { } else if (key == "lastcheck") {
*result = Convert::ToString((long)cr->GetScheduleStart()); *result = Convert::ToString((long)cr->GetScheduleStart());
return true; return true;
} }
@ -345,26 +372,10 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *re
Dictionary::Ptr vars = GetVars(); Dictionary::Ptr vars = GetVars();
if (macro.SubStr(0, 5) == "_HOST") { if (vars && vars->Contains(key)) {
*result = vars ? vars->Get(macro.SubStr(5)) : ""; *result = vars->Get(key);
return true; return true;
} }
String name = macro;
if (name == "HOSTADDRESS")
name = "address";
else if (macro == "HOSTADDRESS6")
name = "address6";
if (vars && vars->Contains(name)) {
*result = vars->Get(name);
return true;
}
if (macro == "HOSTADDRESS" || macro == "HOSTADDRESS6") {
*result = GetName();
return true;
} }
return false; return false;

View File

@ -134,9 +134,7 @@ String IcingaApplication::GetNodeName(void) const
bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
{ {
/* require icinga prefix for application macros */ /* require icinga prefix for application macros */
if (macro.SubStr(0, 7) != "icinga.") if (macro.SubStr(0, 7) == "icinga.") {
return false;
String key = macro.SubStr(7); String key = macro.SubStr(7);
double now = Utility::GetTime(); double now = Utility::GetTime();
@ -164,6 +162,7 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr
*result = vars->Get(key); *result = vars->Get(key);
return true; return true;
} }
}
return false; return false;
} }

View File

@ -128,13 +128,17 @@ String Service::StateTypeToString(StateType type)
bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const
{ {
if (macro == "SERVICEDESC") { /* require prefix for object macros */
if (macro.SubStr(0, 8) == "service.") {
String key = macro.SubStr(8);
if (key == "description") {
*result = GetShortName(); *result = GetShortName();
return true; return true;
} else if (macro == "SERVICEDISPLAYNAME") { } else if (key == "displayname") {
*result = GetDisplayName(); *result = GetDisplayName();
return true; return true;
} else if (macro == "SERVICECHECKCOMMAND") { } else if (key == "checkcommand") {
CheckCommand::Ptr commandObj = GetCheckCommand(); CheckCommand::Ptr commandObj = GetCheckCommand();
if (commandObj) if (commandObj)
@ -145,75 +149,52 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Stri
return true; return true;
} }
if (macro == "SERVICESTATE") { if (key == "state") {
*result = StateToString(GetState()); *result = StateToString(GetState());
return true; return true;
} else if (macro == "SERVICESTATEID") { } else if (key == "stateid") {
*result = Convert::ToString(GetState()); *result = Convert::ToString(GetState());
return true; return true;
} else if (macro == "SERVICESTATETYPE") { } else if (key == "statetype") {
*result = StateTypeToString(GetStateType()); *result = StateTypeToString(GetStateType());
return true; return true;
} else if (macro == "SERVICEATTEMPT") { } else if (key == "attempt") {
*result = Convert::ToString(GetCheckAttempt()); *result = Convert::ToString(GetCheckAttempt());
return true; return true;
} else if (macro == "MAXSERVICEATTEMPT") { } else if (key == "maxattempt") {
*result = Convert::ToString(GetMaxCheckAttempts()); *result = Convert::ToString(GetMaxCheckAttempts());
return true; return true;
} else if (macro == "LASTSERVICESTATE") { } else if (key == "laststate") {
*result = StateToString(GetLastState()); *result = StateToString(GetLastState());
return true; return true;
} else if (macro == "LASTSERVICESTATEID") { } else if (key == "laststateid") {
*result = Convert::ToString(GetLastState()); *result = Convert::ToString(GetLastState());
return true; return true;
} else if (macro == "LASTSERVICESTATETYPE") { } else if (key == "laststatetype") {
*result = StateTypeToString(GetLastStateType()); *result = StateTypeToString(GetLastStateType());
return true; return true;
} else if (macro == "LASTSERVICESTATECHANGE") { } else if (key == "laststatechange") {
*result = Convert::ToString((long)GetLastStateChange()); *result = Convert::ToString((long)GetLastStateChange());
return true; return true;
} else if (macro == "SERVICEDURATIONSEC") { } else if (key == "durationsec") {
*result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange())); *result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
return true; 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++;
}
*result = Convert::ToString(count);
return true;
} }
if (cr) { if (cr) {
if (macro == "SERVICELATENCY") { if (key == "latency") {
*result = Convert::ToString(Service::CalculateLatency(cr)); *result = Convert::ToString(Service::CalculateLatency(cr));
return true; return true;
} else if (macro == "SERVICEEXECUTIONTIME") { } else if (key == "executiontime") {
*result = Convert::ToString(Service::CalculateExecutionTime(cr)); *result = Convert::ToString(Service::CalculateExecutionTime(cr));
return true; return true;
} else if (macro == "SERVICEOUTPUT") { } else if (key == "output") {
*result = cr->GetOutput(); *result = cr->GetOutput();
return true; return true;
} else if (macro == "SERVICEPERFDATA") { } else if (key == "perfdata") {
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData()); *result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
return true; return true;
} else if (macro == "LASTSERVICECHECK") { } else if (key == "lastcheck") {
*result = Convert::ToString((long)cr->GetExecutionEnd()); *result = Convert::ToString((long)cr->GetExecutionEnd());
return true; return true;
} }
@ -221,15 +202,10 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Stri
Dictionary::Ptr vars = GetVars(); Dictionary::Ptr vars = GetVars();
if (macro.SubStr(0, 8) == "_SERVICE") { if (vars && vars->Contains(key)) {
*result = vars ? vars->Get(macro.SubStr(8)) : ""; *result = vars->Get(key);
return true; return true;
} }
if (vars && vars->Contains(macro)) {
*result = vars->Get(macro);
return true;
} }
return false; return false;