Module: Icinga2::Hosts

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

Instance Method Summary collapse

Instance Method Details

#addHost(params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
63
64
65
66
67
68
# File 'lib/icinga2/hosts.rb', line 6

def addHost( params = {} )

  name             = params.dig(:name)
  fqdn             = params.dig(:fqdn)
  displayName      = params.dig(:display_name) || name
  notifications    = params.dig(:enable_notifications) || false
  maxCheckAttempts = params.dig(:max_check_attempts) || 3
  checkInterval    = params.dig(:check_interval) || 60
  retryInterval    = params.dig(:retry_interval) || 45
  notes            = params.dig(:notes)
  notesUrl         = params.dig(:notes_url)
  actionUrl        = params.dig(:action_url)
  vars             = params.dig(:vars) || {}

  if( name == nil )

    return {
      :status  => 404,
      :message => 'missing host name'
    }
  end

  if( fqdn == nil )
    # build FQDN
    fqdn = Socket.gethostbyname( name ).first
  end

  payload = {
    "templates" => [ "generic-host" ],
    "attrs"     => {
      "address"              => fqdn,
      "display_name"         => displayName,
      "max_check_attempts"   => maxCheckAttempts.to_i,
      "check_interval"       => checkInterval.to_i,
      "retry_interval"       => retryInterval.to_i,
      "enable_notifications" => notifications,
      "action_url"           => actionUrl,
      "notes"                => notes,
      "notes_url"            => notesUrl
    }
  }

  if( ! vars.empty? )
    payload['attrs']['vars'] = vars
  end

  if( @icingaCluster == true && @icingaSatellite != nil )
    payload['attrs']['zone'] = @icingaSatellite
  end

  logger.debug( JSON.pretty_generate( payload ) )

  result = Network.put( {
    :host    => name,
    :url     => sprintf( '%s/v1/objects/hosts/%s', @icingaApiUrlBase, name ),
    :headers => @headers,
    :options => @options,
    :payload => payload
  } )

  return JSON.pretty_generate( result )

end

#deleteHost(params = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/icinga2/hosts.rb', line 71

def deleteHost( params = {} )

  name = params.dig(:name)

  if( name == nil )

    return {
      :status  => 404,
      :message => 'missing host name'
    }
  end

  result = Network.delete( {
    :host    => name,
    :url     => sprintf( '%s/v1/objects/hosts/%s?cascade=1', @icingaApiUrlBase, name ),
    :headers => @headers,
    :options => @options
  } )

  return JSON.pretty_generate( result )

end

#existsHost?(name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/icinga2/hosts.rb', line 126

def existsHost?( name )

  result = self.listHosts( { :name => name } )

  if( result.is_a?( String ) )
    result = JSON.parse( result )
  end

  status = result.dig('status')

  if( status != nil && status == 200 )
    return true
  end

  return false

end

#hostObjects(params = {}) ⇒ Object



145
146
147
148
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
# File 'lib/icinga2/hosts.rb', line 145

def hostObjects( params = {} )

  attrs   = params.dig(:attrs)
  filter  = params.dig(:filter)
  joins   = params.dig(:joins)
  payload = {}

  if( attrs == nil )
    attrs = ['name','state','acknowledgement','downtime_depth','last_check']
  end

  if( attrs != nil )
    payload['attrs'] = attrs
  end

  if( filter != nil )
    payload['filter'] = filter
  end

  if( joins != nil )
    payload['joins'] = joins
  end

  result = Network.get( {
    :host     => nil,
    :url      => sprintf( '%s/v1/objects/hosts', @icingaApiUrlBase ),
    :headers  => @headers,
    :options  => @options,
    :payload  => payload
  } )

  return JSON.pretty_generate( result )

end

#hostProblemsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/icinga2/hosts.rb', line 181

def hostProblems()

  data     = self.hostObjects()
  problems = 0

  if( data.is_a?(String) )
    data = JSON.parse(data)
  end

  nodes = data.dig('nodes')

  nodes.each do |n|

    attrs           = n.last.dig('attrs')

    state           = attrs.dig('state')           || 0
    downtimeDepth   = attrs.dig('downtime_depth')  || 0
    acknowledgement = attrs.dig('acknowledgement') || 0

    if( state != 0 && downtimeDepth == 0 && acknowledgement == 0 )
      problems += 1
    end

  end

  return problems

end

#hostSeverity(host) ⇒ Object

stolen from Icinga Web 2 ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/icinga2/hosts.rb', line 262

def hostSeverity( host )

  attrs = host["attrs"]

  severity = 0

  if (attrs["state"] == 0)
    if (getObjectHasBeenChecked(host))
      severity += 16
    end

    if (attrs["acknowledgement"] != 0)
      severity += 2
    elsif (attrs["downtime_depth"] > 0)
      severity += 1
    else
      severity += 4
    end
  else
    if (getObjectHasBeenChecked(host))
      severity += 16
    elsif (attrs["state"] == 1)
      severity += 32
    elsif (attrs["state"] == 2)
      severity += 64
    else
      severity += 256
    end

    if (attrs["acknowledgement"] != 0)
      severity += 2
    elsif (attrs["downtime_depth"] > 0)
      severity += 1
    else
      severity += 4
    end
  end

  return severity

end

#listHosts(params = {}) ⇒ Object



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
# File 'lib/icinga2/hosts.rb', line 95

def listHosts( params = {} )

  name   = params.dig(:name)
  attrs  = params.dig(:attrs)
  filter = params.dig(:filter)
  joins  = params.dig(:joins)

  if( attrs != nil )
    payload['attrs'] = attrs
  end

  if( filter != nil )
    payload['filter'] = filter
  end

  if( joins != nil )
    payload['joins'] = joins
  end

  result = Network.get( {
    :host => name,
    :url  => sprintf( '%s/v1/objects/hosts/%s', @icingaApiUrlBase, name ),
    :headers  => @headers,
    :options  => @options
  } )

  return JSON.pretty_generate( result )

end

#problemHosts(max_items = 5) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/icinga2/hosts.rb', line 211

def problemHosts( max_items = 5 )

  count = 0
  @hostProblems = {}
  @hostProblemsSeverity = {}

  hostData = self.hostObjects()

  if( hostData.is_a?(String) )

    hostData = JSON.parse( hostData )
  end

#       logger.debug( hostData )

  hostData = hostData.dig('nodes')

  hostData.each do |host,v|

    name  = v.dig('name')
    state = v.dig('attrs','state')

    if( state == 0 )
      next
    end

    @hostProblems[name] = self.hostSeverity(v)
  end

  # get the count of problems
  #
  @hostProblems.keys[1..max_items].each { |k,v| @hostProblemsSeverity[k] = @hostProblems[k] }

#       @hostProblems.each do |k,v|
#
#         if( count >= max_items )
#           break
#         end
#
#         @hostProblemsSeverity[k] = v
#
#         count += 1
#       end

  return @hostProblemsSeverity

end