A Ruby gem to interact easily with the Icinga2 API.
Largely copied (and adapted) from gdi/ruby-nagios-api-client.
gem install icinga2-api-client
require 'yaml'
require 'icinga2/api'
client = Icinga2::API::Client.new('https://icinga.example.net:5665',
version: 'v1',
username: 'admin',
password: 'pass',
verify_ssl: false
)
# Get all hosts. It returns a list of Icinga2::API:Host objects
puts YAML.dump client.hosts.all.map(&:to_s)
# Find host by name. It returns a Icinga2::API:Host object
puts YAML.dump client.hosts.find('my.host.net').to_s
# Get all services for host. It returns a list of Icinga2::API:Service objects
puts YAML.dump client.hosts.find('my.host.net').services.all.map(&:to_s)
# Find service by name. It returns a Icinga2::API:Service object
puts YAML.dump client.hosts.find('my.host.net').services.find('ssh').to_s
# Create a schedule downtime
# The duration param seems to be mandatory, even with 'fixed' param set to `true` (which is the default value).
duration = 1.hour
start_time = DateTime.now
end_time = start_time + duration
ssh_downtime =
client.hosts
.find('foo.example.net')
.services
.find('ssh')
.schedule_downtime(
author: 'admin',
comment: 'ok',
start_time: start_time.to_i, # Icinga wants timestamps so call to_i
end_time: end_time.to_i, # Icinga wants timestamps so call to_i
duration: duration.to_i # Icinga wants timestamps so call to_i
)
http_downtime =
client.hosts
.find('foo.example.net')
.services
.find('http')
.schedule_downtime(
author: 'admin',
comment: 'ok',
start_time: start_time.to_i, # Icinga wants timestamps so call to_i
end_time: end_time.to_i, # Icinga wants timestamps so call to_i
duration: duration.to_i # Icinga wants timestamps so call to_i
)
# Get all downtimes across all services. It returns a list of Icinga2::API:Downtime objects
all_downtimes =
client.hosts
.find('foo.example.net')
.services
.downtimes
# Get all downtimes for one service. It returns a list of Icinga2::API:Downtime objects
all_ssh_downtimes =
client.hosts
.find('foo.example.net')
.services
.find('ssh')
.downtimes
all_http_downtimes =
client.hosts
.find('foo.example.net')
.services
.find('http')
.downtimes
# Cancel a downtime
client.hosts
.find('foo.example.net')
.services
.find('http')
.downtimes
.first
.cancel
A big thank to them for their contribution!
And a big thank to gdi/ruby-nagios-api-client for the inspiration.
You can contribute to this plugin in many ways such as :
- Helping with documentation
- Contributing code (features or bugfixes)
- Reporting a bug
- Submitting translations