Implement summary macros e.g. $icinga.num_services_critical$.

Additional $icinga.uptime$ macro.

Fixes #2742
This commit is contained in:
Michael Friedrich 2014-05-03 02:06:58 +02:00
parent 579fc97ce3
commit 74be8cd2d0
3 changed files with 103 additions and 0 deletions

View File

@ -205,3 +205,24 @@ The following macros are available in all executed commands:
icinga.short_date_time | Current date and time. Example: `2014-01-03 11:23:08` icinga.short_date_time | Current date and time. Example: `2014-01-03 11:23:08`
icinga.date | Current date. Example: `2014-01-03` icinga.date | Current date. Example: `2014-01-03`
icinga.time | Current time including timezone information. Example: `11:23:08 +0000` icinga.time | Current time including timezone information. Example: `11:23:08 +0000`
icinga.uptime | Current uptime of the Icinga 2 process.
The following macros provide global statistics:
Name | Description
----------------------------------|--------------
icinga.num_services_ok | Current number of services in state 'OK'.
icinga.num_services_warning | Current number of services in state 'Warning'.
icinga.num_services_critical | Current number of services in state 'Critical'.
icinga.num_services_unknown | Current number of services in state 'Unknown'.
icinga.num_services_pending | Current number of pending services.
icinga.num_services_unreachable | Current number of unreachable services.
icinga.num_services_flapping | Current number of flapping services.
icinga.num_services_in_downtime | Current number of services in downtime.
icinga.num_services_acknowledged | Current number of acknowledged service problems.
icinga.num_hosts_up | Current number of hosts in state 'Up'.
icinga.num_hosts_down | Current number of hosts in state 'Down'.
icinga.num_hosts_unreachable | Current number of unreachable hosts.
icinga.num_hosts_flapping | Current number of flapping hosts.
icinga.num_hosts_in_downtime | Current number of hosts in downtime.
icinga.num_hosts_acknowledged | Current number of acknowledged host problems.

View File

@ -18,6 +18,7 @@
******************************************************************************/ ******************************************************************************/
#include "icinga/icingaapplication.h" #include "icinga/icingaapplication.h"
#include "icinga/cib.h"
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/objectlock.h" #include "base/objectlock.h"
@ -152,6 +153,9 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr
} else if (macro == "time") { } else if (macro == "time") {
*result = Utility::FormatDateTime("%H:%M:%S %z", now); *result = Utility::FormatDateTime("%H:%M:%S %z", now);
return true; return true;
} else if (macro == "uptime") {
*result = Utility::FormatDuration(Utility::GetTime() - Application::GetStartTime());
return true;
} }
Dictionary::Ptr vars = GetVars(); Dictionary::Ptr vars = GetVars();
@ -161,6 +165,62 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr
return true; return true;
} }
if (macro.Contains("num_services")) {
ServiceStatistics ss = CIB::CalculateServiceStats();
if (macro == "num_services_ok") {
*result = Convert::ToString(ss.services_ok);
return true;
} else if (macro == "num_services_warning") {
*result = Convert::ToString(ss.services_warning);
return true;
} else if (macro == "num_services_critical") {
*result = Convert::ToString(ss.services_critical);
return true;
} else if (macro == "num_services_unknown") {
*result = Convert::ToString(ss.services_unknown);
return true;
} else if (macro == "num_services_pending") {
*result = Convert::ToString(ss.services_pending);
return true;
} else if (macro == "num_services_unreachable") {
*result = Convert::ToString(ss.services_unreachable);
return true;
} else if (macro == "num_services_flapping") {
*result = Convert::ToString(ss.services_flapping);
return true;
} else if (macro == "num_services_in_downtime") {
*result = Convert::ToString(ss.services_in_downtime);
return true;
} else if (macro == "num_services_acknowledged") {
*result = Convert::ToString(ss.services_acknowledged);
return true;
}
}
else if (macro.Contains("num_hosts")) {
HostStatistics hs = CIB::CalculateHostStats();
if (macro == "num_hosts_up") {
*result = Convert::ToString(hs.hosts_up);
return true;
} else if (macro == "num_hosts_down") {
*result = Convert::ToString(hs.hosts_down);
return true;
} else if (macro == "num_hosts_unreachable") {
*result = Convert::ToString(hs.hosts_unreachable);
return true;
} else if (macro == "num_hosts_flapping") {
*result = Convert::ToString(hs.hosts_flapping);
return true;
} else if (macro == "num_hosts_in_downtime") {
*result = Convert::ToString(hs.hosts_in_downtime);
return true;
} else if (macro == "num_hosts_acknowledged") {
*result = Convert::ToString(hs.hosts_acknowledged);
return true;
}
}
return false; return false;
} }

22
test/config/2742.conf Normal file
View File

@ -0,0 +1,22 @@
object CheckCommand "2742-macro-command" {
import "plugin-check-command"
command = "echo UPTIME: $icinga.uptime$ SERVICES warn: $icinga.num_services_warning$ crit: $icinga.num_services_critical$ unknown: $icinga.num_services_unknown$ ackd: $icinga.num_services_acknowledged$ HOST: down: $icinga.num_hosts_down$ unreachable: $icinga.num_hosts_unreachable$"
}
object HostGroup "2742-windows-servers"{
display_name = "2742-windows-servers"
assign where match("2742-*", host.name)
}
apply Service "2742-macro-test" {
import "test-generic-service"
check_command = "2742-macro-command"
assign where match("2742-*", host.name)
}
object Host "2742-server" {
import "test-generic-host"
address = "192.168.1.5",
}