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) (defaults to: {})

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)


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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/icinga2/downtimes.rb', line 35

def add_downtime( params = {} )

  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
  #
  if( name.nil? )
    return {
      status: 404,
      message: 'missing downtime name'
    }
  end

  if( %w[host service].include?(type.downcase) == false )
    return {
      status: 404,
      message: "wrong downtype type. only 'host' or' service' allowed ('#{type}' giving"
    }
  else
    # we need the first char as Uppercase
    type = type.capitalize
  end

  if( !host_group.nil? && !host_name.nil? )
    return {
      status: 404,
      message: 'choose host or host_group, not both'
    }
  end

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

    return {
      status: 404,
      message: 'missing host or host_group for downtime'
    }
  end

  if( comment.nil? )
    return {
      status: 404,
      message: 'missing downtime comment'
    }
  end

  if( author.nil? )
    return {
      status: 404,
      message: 'missing downtime author'
    }
  elsif( exists_user?( author ) == false )
    return {
      status: 404,
      message: "these author ar not exists: #{author}"
    }
  end

  if( end_time.nil? )
    return {
      status: 404,
      message: 'missing end_time'
    }
  elsif( end_time.to_i <= start_time )
    return {
      status: 404,
      message: 'end_time are equal or smaller then start_time'
    }
  end

  payload = {
    'type'        => type,
    'start_time'  => start_time,
    'end_time'    => end_time,
    'author'      => author,
    'comment'     => comment,
    'fixed'       => true,
    'duration'    => 30,
    'filter'      => filter
  }

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


  # schedule downtime for a host
  #  --data '{ "type": "Host", "filter": "host.name==\"api_dummy_host_1\"", ... }'

  # schedule downtime for all services of a host
  #  --data '{ "type": "Service", "filter": "host.name==\"api_dummy_host_1\"", ... }'

  # schedule downtime for all hosts and services in a hostgroup
  #  --data '{ "type": "Host", "filter": "\"api_dummy_hostgroup\" in host.groups", ... }'

  #  --data '{ "type": "Service", "filter": "\"api_dummy_hostgroup\" in host.groups)", ... }'

end

#downtimes(params = {}) ⇒ Hash

return downtimes

Examples:

@icinga.downtimes

Parameters:

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

Options Hash (params):

  • :host (String)

Returns:

  • (Hash)


161
162
163
164
165
166
167
168
169
170
# File 'lib/icinga2/downtimes.rb', line 161

def downtimes( params = {} )

  host = params.dig(:host)

  Network.get(         host: host,
    url: format( '%s/v1/objects/downtimes/%s', @icinga_api_url_base, host ),
    headers: @headers,
    options: @options )

end