Module: Icinga2::Downtimes

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

Overview

namespace for downtimes handling

Schedule a downtime for hosts and services.

Instance Method Summary collapse

Instance Method Details

#add_downtime(params) ⇒ Hash

add downtime

Examples:

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

Parameters:

Options Hash (params):

  • host_name (String)
  • host_group (String)
  • type (String)

    ‘host’ or ‘service’ downtime

  • start_time (Integer) — default: Time.new.to_i

    Timestamp marking the beginning of the downtime. (required)

  • end_time (Integer)

    Timestamp marking the end of the downtime. (required)

  • author (String)

    Name of the author. (required)

  • comment (String)

    Comment text. (required)

  • config_owner (String)
  • duration (Integer)

    Duration of the downtime in seconds if fixed is set to false. (Required for flexible downtimes.)

  • entry_time (Integer)
  • fixed (Bool) — default: true

    Defaults to true. If true, the downtime is fixed otherwise flexible. See downtimes for more information.

  • scheduled_by (String)
  • service_name (String)
  • triggered_by (String)

    Sets the trigger for a triggered downtime. See downtimes for more information on triggered downtimes.

Returns:

Raises:

  • (ArgumentError)


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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/icinga2/downtimes.rb', line 44

def add_downtime( 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_name       = validate( params, required: false, var: 'host_name', type: String )
  host_group      = validate( params, required: false, var: 'host_group', type: String )
  start_time      = validate( params, required: false, var: 'start_time', type: Integer ) || Time.now.to_i
  end_time        = validate( params, required: false, var: 'end_time', type: Integer )
  author          = validate( params, required: true, var: 'author', type: String )
  comment         = validate( params, required: true, var: 'comment', type: String )
  type            = validate( params, required: false, var: 'type', type: String )
  fixed           = validate( params, required: false, var: 'fixed', type: Boolean ) || true
  duration_required = true if( fixed == false )
  duration        = validate( params, required: duration_required, var: 'duration', type: Integer )
  entry_time      = validate( params, required: false, var: 'entry_time', type: Integer )
  scheduled_by    = validate( params, required: false, var: 'scheduled_by', type: String )
  service_name    = validate( params, required: false, var: 'service_name', type: String )
  triggered_by    = validate( params, required: false, var: 'triggered_by', type: String )
  config_owner    = validate( params, required: false, var: 'config_owner', type: String )
  filter          = nil

  # sanitychecks
  #
  raise ArgumentError.new(format('wrong downtype type. only \'host\' or \'service\' allowed, given \%s\'', type)) if( %w[host service].include?(type.downcase) == false )
  raise ArgumentError.new('choose \'host_name\' or \'host_group\', not both') unless( host_group.nil? || host_name.nil? )
  raise ArgumentError.new(format('these \'author\' are not exists: \'%s\'', author)) unless( exists_user?( author ) )
  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 )

  # TODO
  #  - more flexibility (e.g. support scheduled_by ...)

  unless( host_name.nil? )
    return { 'code' => 404, 'name' => host_name, 'status' => 'Object not Found' } unless( exists_host?( host_name ) )
    filter = format( 'host.name == "%s"', host_name )
  end

  unless( host_group.nil? )
    return { 'code' => 404, 'name' => host_group, 'status' => 'Object not Found' } unless( exists_hostgroup?( host_group ) )
    filter = format( '"%s" in host.groups', host_group )
  end

  payload = {
    type: type.capitalize, # we need the first char as Uppercase
    filter: filter,
    fixed: fixed,
    start_time: start_time,
    end_time: end_time,
    author: author,
    comment: comment,
    duration: duration,
    entry_time: entry_time,
    scheduled_by: scheduled_by,
    host_name: host_name,
    host_group: host_group,
    service_name: service_name,
    triggered_by: triggered_by,
    config_owner: config_owner
  }

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

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

#downtimesArray

return downtimes

Examples:

downtimes

Returns:



191
192
193
194
195
196
197
198
# File 'lib/icinga2/downtimes.rb', line 191

def downtimes

  api_data(
    url: format( '%s/objects/downtimes'   , @icinga_api_url_base ),
    headers: @headers,
    options: @options
  )
end

#remove_downtime(params) ⇒ Hash

remove downtime

Examples:

param = {
  comment: 'test downtime',
  author: 'icingaadmin'
}
remove_downtime(param)

param = {
  filter: '"host.name == filterHost && !service && downtime.author == filterAuthor"',
  filter_vars: { filterHost: 'c1-mysql-1', filterAuthor: 'icingaadmin' }
)
remove_downtime(param)

param = {
  host_name: 'c1-mysql-1',
  service_name: 'ping4'
}
remove_downtime(param)

Parameters:

Options Hash (params):

Returns:

Raises:

  • (ArgumentError)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/icinga2/downtimes.rb', line 149

def remove_downtime( 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_name       = validate( params, required: false, var: 'host_name', type: String )
  host_group      = validate( params, required: false, var: 'host_group', type: String )
  author          = validate( params, required: false, var: 'author', type: String )
  comment         = validate( params, required: false, var: 'comment', type: String )
  service_name    = validate( params, required: false, var: 'service_name', type: String )
  filter          = validate( params, required: false, var: 'filter', type: String )
  filter_vars     = validate( params, required: false, var: 'filter_vars', type: Hash )

  payload = {
    type: 'Downtime',
    host_name: host_name,
    service_name: service_name,
    host_group: host_group,
    comment: comment,
    author: author,
    filter: filter,
    filter_vars: filter_vars
  }

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

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