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:

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)
  • vars (Hash) — default: {}

Returns:

Raises:

  • (ArgumentError)


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 24

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 = validate( params, required: true, var: 'host_group', type: String )
  display_name = validate( params, required: false, var: 'display_name', type: String )
  notes = validate( params, required: false, var: 'notes', type: String )
  notes_url = validate( params, required: false, var: 'notes_url', type: String )
  action_url = validate( params, required: false, var: 'action_url', type: String )
  vars   = validate( params, required: false, var: 'vars', type: Hash ) || {}

  payload = {
    attrs: {
      display_name: display_name,
      notes: notes,
      notes_url: notes_url,
      action_url: action_url,
      vars: vars
    }
  }

  # 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:

delete_hostgroup(name: 'foo')
delete_hostgroup(name: 'foo', cascade: true)

Parameters:

Options Hash (params):

  • name (String)

    hostgroup to delete

  • cascade (Bool) — default: false

    delete hostgroup also when other objects depend on it

Returns:

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/icinga2/hostgroups.rb', line 70

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? )

  name = validate( params, required: true, var: 'name', type: String )
  cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false

  return { 'code' => 404, 'status' => 'Object not Found' } if( exists_hostgroup?( name ) == false )

  url = format( '%s/objects/hostgroups/%s%s', @icinga_api_url_base, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )

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

#exists_hostgroup?(host_group) ⇒ Bool

returns true if the hostgroup exists

Examples:

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)
  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(host_group = nil) ⇒ Hash

returns all usersgroups

Examples:

to get all users

hostgroups

to get one user

hostgroups(host_group: 'linux-servers')

Parameters:

  • host_group (String) (defaults to: nil)

    (nil) optional for a single hostgroup

Returns:

  • (Hash)

    returns a hash with all hostgroups

Raises:

  • (ArgumentError)


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

def hostgroups( host_group = nil )

  raise ArgumentError.new(format('wrong type. \'host_group\' must be an String, given \'%s\'', host_group.class.to_s)) unless( host_group.nil? || host_group.is_a?(String) )

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

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