diff --git a/doc/18-library-reference.md b/doc/18-library-reference.md index 62e212407..7caa64613 100644 --- a/doc/18-library-reference.md +++ b/doc/18-library-reference.md @@ -547,9 +547,57 @@ Returns the Host object with the specified name, or `null` if no such Host objec Signature: function get_service(host_name, service_name); + function get_service(host, service_name); -Returns the Service object with the specified name, or `null` if no such Service object exists. +Returns the Service object with the specified host name or object and service name pair, +or `null` if no such Service object exists. +Example in the [debug console](11-cli-commands.md#cli-command-console) +which fetches the `disk` service object from the current Icinga 2 node: + +``` +$ ICINGA2_API_PASSWORD=icinga icinga2 console --connect 'https://root@localhost:5665/' +Icinga 2 (version: v2.7.0) + +<1> => get_service(NodeName, "disk") +<2> => get_service(NodeName, "disk").__name +"icinga2-master1.localdomain!disk" + +<3> => get_service(get_host(NodeName), "disk").__name +"icinga2-master1.localdomain!disk" +``` + +### get_services + +Signature: + + function get_services(host_name); + function get_services(host); + +Returns an [array](17-language-reference.md#array) of service objects for the specified host name or object, +or `null` if no such host object exists. + +Example in the [debug console](11-cli-commands.md#cli-command-console) +which fetches all service objects from the current Icinga 2 node: + +``` +$ ICINGA2_API_PASSWORD=icinga icinga2 console --connect 'https://root@localhost:5665/' +Icinga 2 (version: v2.7.0) + +<1> => get_services(NodeName).map(s => s.name) +[ "disk", "disk /", "http", "icinga", "load", "ping4", "ping6", "procs", "ssh", "users" ] +``` + +Note: [map](18-library-reference.md#array-map) takes a [lambda function](17-language-reference.md#lambdas) as argument. In this example +we only want to collect and print the `name` attribute with `s => s.name`. + +This works in a similar fashion for a host object where you can extract all service states +in using the [map](18-library-reference.md#array-map) functionality: + +``` +<2> => get_services(get_host(NodeName)).map(s => s.state) +[ 2.000000, 2.000000, 2.000000, 0.000000, 0.000000, 0.000000, 2.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000 ] +``` ### get_user diff --git a/lib/icinga/objectutils.cpp b/lib/icinga/objectutils.cpp index be58508ba..bde6cfd9a 100644 --- a/lib/icinga/objectutils.cpp +++ b/lib/icinga/objectutils.cpp @@ -31,6 +31,7 @@ using namespace icinga; REGISTER_SCRIPTFUNCTION_NS(System, get_host, &Host::GetByName, "name"); REGISTER_SCRIPTFUNCTION_NS(System, get_service, &ObjectUtils::GetService, "host:name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_services, &ObjectUtils::GetServices, "host"); REGISTER_SCRIPTFUNCTION_NS(System, get_user, &User::GetByName, "name"); REGISTER_SCRIPTFUNCTION_NS(System, get_check_command, &CheckCommand::GetByName, "name"); REGISTER_SCRIPTFUNCTION_NS(System, get_event_command, &EventCommand::GetByName, "name"); @@ -40,13 +41,32 @@ REGISTER_SCRIPTFUNCTION_NS(System, get_service_group, &ServiceGroup::GetByName, REGISTER_SCRIPTFUNCTION_NS(System, get_user_group, &UserGroup::GetByName, "name"); REGISTER_SCRIPTFUNCTION_NS(System, get_time_period, &TimePeriod::GetByName, "name"); -Service::Ptr ObjectUtils::GetService(const String& host, const String& name) +Service::Ptr ObjectUtils::GetService(const Value& host, const String& name) { - Host::Ptr host_obj = Host::GetByName(host); + Host::Ptr hostObj; - if (!host_obj) + if (host.IsObjectType()) + hostObj = host; + else + hostObj = Host::GetByName(host); + + if (!hostObj) return Service::Ptr(); - return host_obj->GetServiceByShortName(name); + return hostObj->GetServiceByShortName(name); } +Array::Ptr ObjectUtils::GetServices(const Value& host) +{ + Host::Ptr hostObj; + + if (host.IsObjectType()) + hostObj = host; + else + hostObj = Host::GetByName(host); + + if (!hostObj) + return Array::Ptr(); + + return Array::FromVector(hostObj->GetServices()); +} diff --git a/lib/icinga/objectutils.hpp b/lib/icinga/objectutils.hpp index cb783e828..e1997c65c 100644 --- a/lib/icinga/objectutils.hpp +++ b/lib/icinga/objectutils.hpp @@ -22,6 +22,7 @@ #include "base/i2-base.hpp" #include "base/string.hpp" +#include "base/array.hpp" #include "icinga/service.hpp" namespace icinga @@ -33,7 +34,8 @@ namespace icinga class I2_ICINGA_API ObjectUtils { public: - static Service::Ptr GetService(const String& host, const String& name); + static Service::Ptr GetService(const Value& host, const String& name); + static Array::Ptr GetServices(const Value& host); private: ObjectUtils(void);