Module: Icinga2::Servicegroups

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

Overview

namespace for servicegroup handling

Instance Method Summary collapse

Instance Method Details

#add_servicegroup(params) ⇒ Hash

add a servicegroup

Examples:

@icinga.add_servicegroup(service_group: 'foo', display_name: 'FOO')

Parameters:

  • params (Hash)

Options Hash (params):

  • :service_group (String)

    servicegroup 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
38
39
# File 'lib/icinga2/servicegroups.rb', line 20

def add_servicegroup( params )

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

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

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

  payload = { 'attrs' => { 'display_name' => display_name } }

  Network.put(
    url: format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#delete_servicegroup(params) ⇒ Array

delete a servicegroup

Examples:

@icinga.delete_servicegroup(service_group: 'foo')

Parameters:

  • params (Hash)

Options Hash (params):

  • :service_group (String)

    servicegroup to delete

Returns:

  • (Array)

    result

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/icinga2/servicegroups.rb', line 51

def delete_servicegroup( params )

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

  service_group = params.dig(:service_group)

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

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

#exists_servicegroup?(service_group) ⇒ Bool

checks if the servicegroup exists

Examples:

@icinga.exists_servicegroup?('disk')

Parameters:

  • service_group (String)

    the name of the servicegroup

Returns:

  • (Bool)

    returns true if the servicegroup exists

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/icinga2/servicegroups.rb', line 111

def exists_servicegroup?( service_group )

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


  result = servicegroups( service_group: service_group )
  result = JSON.parse( result ) if  result.is_a?( String )

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

  false
end

#servicegroups(params = {}) ⇒ Array

returns all servicegroups

Examples:

to get all users

@icinga.servicegroups

to get one user

@icinga.servicegroups(service_group: 'disk')

Parameters:

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

Options Hash (params):

  • :service_group (String) — default: ''

    optional for a single servicegroup

Returns:

  • (Array)

    returns a hash with all servicegroups



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

def servicegroups( params = {} )

  service_group = params.dig(:service_group)

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

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

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

  nil
end