Module: Icinga2::Hostgroups

Included in:
Client
Defined in:
lib/icinga2/hostgroups.rb

Overview

namespace for hostgroup handling

Instance Method Summary collapse

Instance Method Details

#add_hostgroup(params) ⇒ Hash

add a hostgroup

Examples:

@icinga.add_hostgroup(host_group: 'foo', display_name: 'FOO')

Parameters:

  • params (Hash)

Options Hash (params):

  • :host_group (String)

    hostgroup to create

  • :display_name (String)

    the displayed name

Returns:

  • (Hash)

    result

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/icinga2/hostgroups.rb', line 20

def add_hostgroup( params )

  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing params') if( params.size.zero? )

  host_group   = params.dig(:host_group)
  display_name = params.dig(:display_name)

  raise ArgumentError.new('Missing host_group') if( host_group.nil? )
  raise ArgumentError.new('Missing display_name') if( display_name.nil? )

  Network.put(
    url: format('%s/objects/hostgroups/%s', @icinga_api_url_base, host_group),
    headers: @headers,
    options: @options,
    payload: { 'attrs' => { 'display_name' => display_name } }
  )
end

#delete_hostgroup(params) ⇒ Hash

delete a hostgroup

Examples:

@icinga.delete_hostgroup(host_group: 'foo')

Parameters:

  • params (Hash)

Options Hash (params):

  • :name (String)

    hostgroup to delete

Returns:

  • (Hash)

    result

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/icinga2/hostgroups.rb', line 49

def delete_hostgroup( params )

  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing params') if( params.size.zero? )

  host_group = params.dig(:host_group)

  raise ArgumentError.new('Missing host_group') if( host_group.nil? )

  Network.delete(
    url: format('%s/objects/hostgroups/%s?cascade=1', @icinga_api_url_base, host_group),
    headers: @headers,
    options: @options
  )
end

#exists_hostgroup?(host_group) ⇒ Bool

returns true if the hostgroup exists

Examples:

@icinga.exists_hostgroup?('linux-servers')

Parameters:

  • host_group (String)

    the name of the hostgroup

Returns:

  • (Bool)

    returns true if the hostgroup exists

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/icinga2/hostgroups.rb', line 109

def exists_hostgroup?( host_group )

  raise ArgumentError.new('only String are allowed') unless( host_group.is_a?(String) )
  raise ArgumentError.new('Missing host_group') if( host_group.size.zero? )

  result = hostgroups(host_group: host_group)
  result = JSON.parse( result ) if  result.is_a?( String )

  return true if  !result.nil? && result.is_a?(Array)

  false
end

#hostgroups(params = {}) ⇒ Hash

returns all usersgroups

Examples:

to get all users

@icinga.hostgroups

to get one user

@icinga.hostgroups(host_group: 'linux-servers')

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :host_group (String) — default: ''

    optional for a single hostgroup

Returns:

  • (Hash)

    returns a hash with all hostgroups



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/icinga2/hostgroups.rb', line 78

def hostgroups( params = {} )

  host_group = params.dig(:host_group)

  url =
  if( host_group.nil? )
    format( '%s/objects/hostgroups'   , @icinga_api_url_base )
  else
    format( '%s/objects/hostgroups/%s', @icinga_api_url_base, host_group )
  end

  data = Network.api_data(
    url: url,
    headers: @headers,
    options: @options
  )

  return data.dig('results') if( data.dig(:status).nil? )

  nil
end