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:

Options Hash (params):

  • :host_group (String)

    hostgroup to create

  • :display_name (String)

    the displayed name

  • :notes (String)
  • :notes_url (String)
  • :action_url (String)

Returns:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/icinga2/hostgroups.rb', line 23

def add_hostgroup( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) 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)
  notes        = params.dig(:notes)
  notes_url    = params.dig(:notes_url)
  action_url   = params.dig(:action_url)

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

  payload = {
    'attrs' => {
      'display_name' => display_name,
      'notes' => notes,
      'notes_url' => notes_url,
      'action_url' => action_url
    }
  }

  # remove all empty attrs
  payload.reject!{ |_k, v| v.nil? }
  payload['attrs'].reject!{ |_k, v| v.nil? }

  put(
    url: format('%s/objects/hostgroups/%s', @icinga_api_url_base, host_group),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#delete_hostgroup(params) ⇒ Hash

delete a hostgroup

Examples:

@icinga.delete_hostgroup(host_group: 'foo')

Parameters:

Options Hash (params):

  • :name (String)

    hostgroup to delete

Returns:

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/icinga2/hostgroups.rb', line 68

def delete_hostgroup( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) 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? )

  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)


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/icinga2/hostgroups.rb', line 124

def exists_hostgroup?( host_group )

  raise ArgumentError.new(format('wrong type. \'host_group\' must be an String, given \'%s\'', host_group.class.to_s)) 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) )
  result = result.first if( result.is_a?(Array) )

  return false if( result.is_a?(Hash) && result.dig('code') == 404 )

  true
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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/icinga2/hostgroups.rb', line 97

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

  api_data(
    url: url,
    headers: @headers,
    options: @options
  )
end