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:

Options Hash (params):

  • :service_group (String)

    servicegroup 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
57
58
59
60
61
62
# File 'lib/icinga2/servicegroups.rb', line 23

def add_servicegroup( 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? )

  service_group = params.dig(:service_group)
  display_name  = params.dig(:display_name)
  notes         = params.dig(:notes)
  notes_url     = params.dig(:notes_url)
  action_url    = params.dig(:action_url)

#      ignore = params.dig(:ignore)
#      assgin = params.dig(:assign)

  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,
      'notes' => notes,
      'notes_url' => notes_url,
      'action_url' => action_url
    }
  }

#      payload['attrs']['assign'] ||= format('assgin where %s', assign) unless(assign.nil?)
#      payload['attrs']['ignore'] ||= format('ignore where %s', assign) unless(assign.nil?)

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

  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:

Options Hash (params):

  • :service_group (String)

    servicegroup to delete

Returns:

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/icinga2/servicegroups.rb', line 74

def delete_servicegroup( 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? )

  service_group = params.dig(:service_group)

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

  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)


130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/icinga2/servicegroups.rb', line 130

def exists_servicegroup?( service_group )

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

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

  true
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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/icinga2/servicegroups.rb', line 103

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

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