Class: SabaWebhookGateway::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/saba-webhook-gateway/base.rb

Direct Known Subclasses

GoogleChat

Instance Method Summary collapse

Constructor Details

#initializeSabaWebhookGateway::Base

Initialize a new SabaWebhookGateway object.



11
12
13
# File 'lib/saba-webhook-gateway/base.rb', line 11

def initialize
  @debug = ENV['DEBUG'] || nil
end

Instance Method Details

#alert(h) ⇒ String

Process alert notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


51
52
53
54
# File 'lib/saba-webhook-gateway/base.rb', line 51

def alert(h)
  puts "[alert]\n#{h}"
  '{ "event": "alert" }'
end

#alert_group(h) ⇒ String

Process alert group notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


60
61
62
63
# File 'lib/saba-webhook-gateway/base.rb', line 60

def alert_group(h)
  puts "[alert_group]\n#{h}"
  '{ "event": "alertGroup" }'
end

#handle_by_event(h) ⇒ String

Call the respective notification method according to the event.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/saba-webhook-gateway/base.rb', line 321

def handle_by_event(h)
  case h[:event]
  when 'sample'
    sample(h)
  when 'alert'
    alert(h)
  when 'alertGroup'
    alert_group(h)
  when 'hostRegister'
    host_register(h)
  when 'hostStatus'
    host_status(h)
  when 'hostRetire'
    host_retire(h)
  when 'monitorCreate'
    monitor_create(h)
  when 'monitorUpdate'
    monitor_update(h)
  when 'monitorDelete'
    monitor_delete(h)
  end
end

#host_register(h) ⇒ String

Process host registration notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


69
70
71
72
# File 'lib/saba-webhook-gateway/base.rb', line 69

def host_register(h)
  puts "[host_register]\n#{h}"
  '{ "event": "hostRegister" }'
end

#host_retire(h) ⇒ String

Process host retirement notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


87
88
89
90
# File 'lib/saba-webhook-gateway/base.rb', line 87

def host_retire(h)
  puts "[host_retire]\n#{h}"
  '{ "event": "hostRetire" }'
end

#host_status(h) ⇒ String

Process host status change notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


78
79
80
81
# File 'lib/saba-webhook-gateway/base.rb', line 78

def host_status(h)
  puts "[host_status]\n#{h}"
  '{ "event": "hostStatus" }'
end

#monitor_create(h) ⇒ String

Process monitor creation notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/saba-webhook-gateway/base.rb', line 96

def monitor_create(h)
  case h[:monitor][:type]
  when 'host'
    monitor_create_host(h)
  when 'external'
    monitor_create_external(h)
  when 'expression'
    monitor_create_expression(h)
  when 'anomalyDetection'
    monitor_create_anomaly_detection(h)
  when 'service'
    monitor_create_service(h)
  when 'connectivity'
    monitor_create_connectivity(h)
  end
end

#monitor_create_anomaly_detection(h) ⇒ String

Process monitor creation (anomaly detection) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


144
145
146
147
# File 'lib/saba-webhook-gateway/base.rb', line 144

def monitor_create_anomaly_detection(h)
  puts "[monitor_create_anomaly_detection]\n#{h}"
  '{ "event": "monitorCreate/anomalyDetection" }'
end

#monitor_create_connectivity(h) ⇒ String

Process monitor creation (connectivity) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


162
163
164
165
# File 'lib/saba-webhook-gateway/base.rb', line 162

def monitor_create_connectivity(h)
  puts "[monitor_create_connectivity]\n#{h}"
  '{ "event": "monitorCreate/connectivity" }'
end

#monitor_create_expression(h) ⇒ String

Process monitor creation (expression) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


135
136
137
138
# File 'lib/saba-webhook-gateway/base.rb', line 135

def monitor_create_expression(h)
  puts "[monitor_create_expression]\n#{h}"
  '{ "event": "monitorCreate/expression" }'
end

#monitor_create_external(h) ⇒ String

Process monitor creation (external) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


126
127
128
129
# File 'lib/saba-webhook-gateway/base.rb', line 126

def monitor_create_external(h)
  puts "[monitor_create_external]\n#{h}"
  '{ "event": "monitorCreate/external" }'
end

#monitor_create_host(h) ⇒ String

Process monitor creation (host) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


117
118
119
120
# File 'lib/saba-webhook-gateway/base.rb', line 117

def monitor_create_host(h)
  puts "[monitor_create_host]\n#{h}"
  '{ "event": "monitorCreate/host" }'
end

#monitor_create_service(h) ⇒ String

Process monitor creation (service) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


153
154
155
156
# File 'lib/saba-webhook-gateway/base.rb', line 153

def monitor_create_service(h)
  puts "[monitor_create_service]\n#{h}"
  '{ "event": "monitorCreate/service" }'
end

#monitor_delete(h) ⇒ String

Process monitor deletion notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/saba-webhook-gateway/base.rb', line 246

def monitor_delete(h)
  case h[:monitor][:type]
  when 'host'
    monitor_delete_host(h)
  when 'external'
    monitor_delete_external(h)
  when 'expression'
    monitor_delete_expression(h)
  when 'anomalyDetection'
    monitor_delete_anomaly_detection(h)
  when 'service'
    monitor_delete_service(h)
  when 'connectivity'
    monitor_delete_connectivity(h)
  end
end

#monitor_delete_anomaly_detection(h) ⇒ String

Process monitor deletion (anomaly detection) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


294
295
296
297
# File 'lib/saba-webhook-gateway/base.rb', line 294

def monitor_delete_anomaly_detection(h)
  puts "[monitor_delete_anomaly_detection]\n#{h}"
  '{ "event": "monitorDelete/anomalyDetection" }'
end

#monitor_delete_connectivity(h) ⇒ String

Process monitor deletion (connectivity) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


312
313
314
315
# File 'lib/saba-webhook-gateway/base.rb', line 312

def monitor_delete_connectivity(h)
  puts "[monitor_delete_connectivity]\n#{h}"
  '{ "event": "monitorDelete/connectivity" }'
end

#monitor_delete_expression(h) ⇒ String

Process monitor deletion (expression) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


285
286
287
288
# File 'lib/saba-webhook-gateway/base.rb', line 285

def monitor_delete_expression(h)
  puts "[monitor_delete_expression]\n#{h}"
  '{ "event": "monitorDelete/expression" }'
end

#monitor_delete_external(h) ⇒ String

Process monitor deletion (external) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


276
277
278
279
# File 'lib/saba-webhook-gateway/base.rb', line 276

def monitor_delete_external(h)
  puts "[monitor_delete_external]\n#{h}"
  '{ "event": "monitorDelete/external" }'
end

#monitor_delete_host(h) ⇒ String

Process monitor deletion (host) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


267
268
269
270
# File 'lib/saba-webhook-gateway/base.rb', line 267

def monitor_delete_host(h)
  puts "[monitor_delete_host]\n#{h}"
  '{ "event": "monitorDelete/host" }'
end

#monitor_delete_service(h) ⇒ String

Process monitor deletion (service) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


303
304
305
306
# File 'lib/saba-webhook-gateway/base.rb', line 303

def monitor_delete_service(h)
  puts "[monitor_delete_service]\n#{h}"
  '{ "event": "monitorDelete/service" }'
end

#monitor_update(h) ⇒ String

Process monitor update notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/saba-webhook-gateway/base.rb', line 171

def monitor_update(h)
  case h[:monitor][:type]
  when 'host'
    monitor_update_host(h)
  when 'external'
    monitor_update_external(h)
  when 'expression'
    monitor_update_expression(h)
  when 'anomalyDetection'
    monitor_update_anomaly_detection(h)
  when 'service'
    monitor_update_service(h)
  when 'connectivity'
    monitor_update_connectivity(h)
  end
end

#monitor_update_anomaly_detection(h) ⇒ String

Process monitor update (anomaly detection) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


219
220
221
222
# File 'lib/saba-webhook-gateway/base.rb', line 219

def monitor_update_anomaly_detection(h)
  puts "[monitor_update_anomaly_detection]\n#{h}"
  '{ "event": "monitorUpdate/anomalyDetection" }'
end

#monitor_update_connectivity(h) ⇒ String

Process monitor update (connectivity) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


237
238
239
240
# File 'lib/saba-webhook-gateway/base.rb', line 237

def monitor_update_connectivity(h)
  puts "[monitor_update_connectivity]\n#{h}"
  '{ "event": "monitorUpdate/connectivity" }'
end

#monitor_update_expression(h) ⇒ String

Process monitor update (expression) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


210
211
212
213
# File 'lib/saba-webhook-gateway/base.rb', line 210

def monitor_update_expression(h)
  puts "[monitor_update_expression]\n#{h}"
  '{ "event": "monitorUpdate/expression" }'
end

#monitor_update_external(h) ⇒ String

Process monitor update (external) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


201
202
203
204
# File 'lib/saba-webhook-gateway/base.rb', line 201

def monitor_update_external(h)
  puts "[monitor_update_external]\n#{h}"
  '{ "event": "monitorUpdate/external" }'
end

#monitor_update_host(h) ⇒ String

Process monitor update (host) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


192
193
194
195
# File 'lib/saba-webhook-gateway/base.rb', line 192

def monitor_update_host(h)
  puts "[monitor_update_host]\n#{h}"
  '{ "event": "monitorUpdate/host" }'
end

#monitor_update_service(h) ⇒ String

Process monitor update (service) notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


228
229
230
231
# File 'lib/saba-webhook-gateway/base.rb', line 228

def monitor_update_service(h)
  puts "[monitor_update_service]\n#{h}"
  '{ "event": "monitorUpdate/service" }'
end

#parse(json) ⇒ Hash

Convert JSON to Ruby Hash.

Parameters:

  • :json (String)

    JSON content.

Returns:

  • (Hash)


27
28
29
30
31
32
33
34
35
36
# File 'lib/saba-webhook-gateway/base.rb', line 27

def parse(json)
  begin
    JSON.parse(json, symbolize_names: true)
  rescue JSON::ParserError => e
    if @debug
      puts "[json error]\n#{e}"
    end
    nil
  end
end

#post(json) ⇒ Object

Post JSON to the target.

Parameters:

  • :json (String)

    JSON content.



347
348
349
# File 'lib/saba-webhook-gateway/base.rb', line 347

def post(json)
  puts "[post]\n#{json}"
end

#run(h) ⇒ Object

Main routine. It receive and process event hash objects and make JSON post calls.

Parameters:

  • :h (Hash)

    Event Hash object.



354
355
356
357
# File 'lib/saba-webhook-gateway/base.rb', line 354

def run(h)
  json = handle_by_event(h)
  post(json) if json
end

#sample(h) ⇒ String

Process sample notifications.

Parameters:

  • :h (Hash)

    Event Hash object.

Returns:

  • (String)


42
43
44
45
# File 'lib/saba-webhook-gateway/base.rb', line 42

def sample(h)
  puts "[sample]\n#{h}"
  '{ "event": "sample" }'
end

#to_date(epoch) ⇒ String

Convert epoch to ‘MM/DD hh:mm’.

Parameters:

  • :epoch (Integer)

    Time epoch.

Returns:

  • (String)


19
20
21
# File 'lib/saba-webhook-gateway/base.rb', line 19

def to_date(epoch)
  Time.at(epoch.to_i).strftime('%m/%d %H:%M')
end