Module: Icinga2::Downtimes

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

Overview

namespace for downtimes handling

Instance Method Summary collapse

Instance Method Details

#add_downtime(params) ⇒ Hash

add downtime

Examples:

param = {
  name: 'test',
  type: 'service',
  host: 'icinga2',
  comment: 'test downtime',
  author: 'icingaadmin',
  start_time: Time.now.to_i,
  end_time: Time.now.to_i + 20
}
@icinga.add_downtime(param)

Parameters:

  • params (Hash)

Options Hash (params):

  • :name (String)
  • :host (String)
  • :host_group (String)
  • :start_time (Integer) — default: Time.new.to_i
  • :end_time (Integer)
  • :author (String)
  • :comment (String)
  • :type (String)

    ‘host’ or ‘service’ downtime

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/icinga2/downtimes.rb', line 35

def add_downtime( params )

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

  name            = params.dig(:name)
  host_name       = params.dig(:host)
  host_group      = params.dig(:host_group)
  start_time      = params.dig(:start_time) || Time.now.to_i
  end_time        = params.dig(:end_time)
  author          = params.dig(:author)
  comment         = params.dig(:comment)
  type            = params.dig(:type)
  filter          = nil

  # sanitychecks
  #
  raise ArgumentError.new('Missing name') if( name.nil? )
  raise ArgumentError.new("wrong downtype type. only 'host' or' service' allowed ('#{type}' giving)") if( %w[host service].include?(type.downcase) == false )
  raise ArgumentError.new('choose host or host_group, not both') if( !host_group.nil? && !host_name.nil? )
  raise ArgumentError.new('Missing downtime author') if( author.nil? )
  raise ArgumentError.new("these author ar not exists: #{author}") unless( exists_user?( author ) )
  raise ArgumentError.new('Missing downtime comment') if( comment.nil? )
  raise ArgumentError.new('Missing downtime end_time') if( end_time.nil? )
  raise ArgumentError.new('end_time are equal or smaller then start_time') if( end_time.to_i <= start_time )

  if( !host_name.nil? )

    filter = format( 'host.name=="%s"', host_name )
  elsif( !host_group.nil? )

    # check if hostgroup available ?
    #
    filter = format( '"%s" in host.groups', host_group )
  end

  payload = {
    'type'        => type.capitalize, # we need the first char as Uppercase
    'start_time'  => start_time,
    'end_time'    => end_time,
    'author'      => author,
    'comment'     => comment,
    'fixed'       => true,
    'duration'    => 30,
    'filter'      => filter
  }

  Network.post(
    url: format( '%s/actions/schedule-downtime', @icinga_api_url_base ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#downtimesArray

return downtimes

Examples:

@icinga.downtimes

Returns:

  • (Array)


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/icinga2/downtimes.rb', line 97

def downtimes

  data = Network.api_data(
    url: format( '%s/objects/downtimes'   , @icinga_api_url_base ),
    headers: @headers,
    options: @options
  )

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

  nil
end