Class: SabaWebhookGateway::GoogleChat

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

Instance Method Summary collapse

Methods inherited from Base

#handle_by_event, #initialize, #monitor_create, #monitor_delete, #monitor_update, #parse, #run, #to_date

Constructor Details

This class inherits a constructor from SabaWebhookGateway::Base

Instance Method Details

#alert(h) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/saba-webhook-gateway/googlechat.rb', line 25

def alert(h)
  header = { title: %Q([#{h[:orgName]}] #{h[:alert][:status].upcase}: #{h[:alert][:monitorName]}) }
  start_time = to_date(h[:alert][:openedAt])
  end_time = h[:alert][:closedAt] ? to_date(h[:alert][:closedAt]) : nil
  header[:subtitle] = if end_time
                        %Q(from #{start_time} to #{end_time})
                      else
                        %Q(from #{start_time})
                      end

  sa = []
  sa << %Q(<a href="#{h[:alert][:url]}">View Alert</a>)

  if h[:host] && h[:host][:roles]
    # Host
    sa << %Q(Host: <a href="#{h[:host][:url]}">#{h[:host][:name]}</a>)
    h[:host][:roles].each do |a|
      sa << %Q(Role: [<a href="#{a[:serviceUrl]}">#{a[:serviceName]}</a>] <a href="#{a[:roleUrl]}">#{a[:roleName]}</a>)
    end
  end

  if h[:service] && h[:service][:roles]
    # Service
    sa << %Q(Service: #{h[:service][:name]})
    h[:service][:roles].each do |a|
      sa << %Q(Role: [<a href="#{a[:serviceUrl]}">#{a[:serviceName]}</a>] <a href="#{a[:roleUrl]}">#{a[:roleName]}</a>)
    end
  end

  if h[:alert][:metricLabel]
    # Host, Service, Expression
    if h[:alert][:status] == 'ok'
      sa << %Q(Metric: #{h[:alert][:metricLabel]} #{h[:alert][:metricValue]})
    else
      threshold = if h[:alert][:status] == 'critical'
                    h[:alert][:criticalThreshold]
                  else
                    h[:alert][:warningThreshold]
                  end
      sa << %Q(Metric: #{h[:alert][:metricLabel]} #{h[:alert][:metricValue]} #{h[:alert][:monitorOperator]} #{threshold})
    end
  end

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  if h[:imageUrl]
    widget2 = [{
      image: {
        imageUrl: h[:imageUrl],
        onClick: {
          openLink: {
            url: h[:alert][:url]
          }
        }
      }
    }]
    sections.push({ widgets: widget2 })
  end

  if h[:memo]
    widget3 = [{ textParagraph: { text: h[:memo] } }]
    sections.push({ widgets: widget3 })
  end

  googlechat_card(header, sections)
end

#alert_group(h) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/saba-webhook-gateway/googlechat.rb', line 93

def alert_group(h)
  header = { title: %Q([#{h[:orgName]}] #{h[:alertGroup][:status].upcase}: #{h[:alertGroupSetting][:name]}) }
  start_time = to_date(h[:alertGroup][:createdAt])  # createdAt
  end_time = h[:alertGroup][:closedAt] ? to_date(h[:alertGroup][:closedAt]) : nil
  header[:subtitle] = if end_time
                        %Q(from #{start_time} to #{end_time})
                      else
                        %Q(from #{start_time})
                      end

  sa = []
  sa << %Q(<a href="#{h[:alertGroup][:url]}">View Alert</a>)

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  if h[:alertGroupSetting][:memo]
    widget2 = [{ textParagraph: { text: h[:alertGroupSetting][:memo] } }]
    sections.push({ widgets: widget2 })
  end

  googlechat_card(header, sections)
end

#googlechat_card(header, sections) ⇒ String

Make Google Chat Card style JSON.

Parameters:

  • :header (Hash)

    Header area Hash object.

  • :sections (Hash)

    Section area Hash object.

Returns:

  • (String)


13
14
15
16
# File 'lib/saba-webhook-gateway/googlechat.rb', line 13

def googlechat_card(header, sections)
  j = { cards: { header: header, sections: sections } }
  JSON.generate(j)
end

#host_register(h) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/saba-webhook-gateway/googlechat.rb', line 117

def host_register(h)
  header = { title: %Q([#{h[:orgName]}] Host #{h[:host][:name]} is registered) }

  sa = []
  if h[:user]
    sa << %Q(Registered by: #{h[:user][:screenName]})
  end

  if h[:host][:roles]
    sa << %Q(Host: <a href="#{h[:host][:url]}">#{h[:host][:name]}</a>)
    h[:host][:roles].each do |a|
      sa << %Q(Role: [<a href="#{a[:serviceUrl]}">#{a[:serviceName]}</a>] <a href="#{a[:roleUrl]}">#{a[:roleName]}</a>)
    end
  end

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  googlechat_card(header, sections)
end

#host_retire(h) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/saba-webhook-gateway/googlechat.rb', line 160

def host_retire(h)
  header = { title: %Q([#{h[:orgName]}] Host #{h[:host][:name]} is retired) }

  sa = []
  if h[:user]
    sa << %Q(Retired by: #{h[:user][:screenName]})
  end

  if h[:host][:roles]
    sa << %Q(Host: <a href="#{h[:host][:url]}">#{h[:host][:name]}</a>)
    h[:host][:roles].each do |a|
      sa << %Q(Role: [<a href="#{a[:serviceUrl]}">#{a[:serviceName]}</a>] <a href="#{a[:roleUrl]}">#{a[:roleName]}</a>)
    end
  end

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  googlechat_card(header, sections)
end

#host_status(h) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/saba-webhook-gateway/googlechat.rb', line 138

def host_status(h)
  header = { title: %Q([#{h[:orgName]}] Host #{h[:host][:name]} is changed to #{h[:host][:status]}) }

  sa = []
  if h[:user]
    sa << %Q(Chagned by: #{h[:user][:screenName]})
  end

  sa << %Q(Previous: #{h[:fromStatus]})
  if h[:host][:roles]
    sa << %Q(Host: <a href="#{h[:host][:url]}">#{h[:host][:name]}</a>)
    h[:host][:roles].each do |a|
      sa << %Q(Role: [<a href="#{a[:serviceUrl]}">#{a[:serviceName]}</a>] <a href="#{a[:roleUrl]}">#{a[:roleName]}</a>)
    end
  end

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  googlechat_card(header, sections)
end

#monitor_create_anomaly_detection(h) ⇒ Object



214
215
216
# File 'lib/saba-webhook-gateway/googlechat.rb', line 214

def monitor_create_anomaly_detection(h)
  monitor_create_common(h, %Q(Target: Anormaly detection (#{h[:monitor][:scopes].join(', ')})))
end

#monitor_create_common(h, target) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/saba-webhook-gateway/googlechat.rb', line 181

def monitor_create_common(h, target)
  header = { title: %Q([#{h[:orgName]}] Monitor #{h[:monitor][:name]} is created) }

  sa = []
  if h[:user]
    sa << %Q(Created by: #{h[:user][:screenName]})
  end

  sa << target

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  if h[:monitor][:memo]
    widget2 = [{ textParagraph: { text: h[:monitor][:memo] } }]
    sections.push({ widgets: widget2 })
  end

  googlechat_card(header, sections)
end

#monitor_create_connectivity(h) ⇒ Object



222
223
224
# File 'lib/saba-webhook-gateway/googlechat.rb', line 222

def monitor_create_connectivity(h)
  monitor_create_common(h, %Q(Target: Connectivity (#{h[:monitor][:scopes].join(', ')})))
end

#monitor_create_expression(h) ⇒ Object



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

def monitor_create_expression(h)
  monitor_create_common(h, %Q(Target: Expression (#{h[:monitor][:expression]})))
end

#monitor_create_external(h) ⇒ Object



206
207
208
# File 'lib/saba-webhook-gateway/googlechat.rb', line 206

def monitor_create_external(h)
  monitor_create_common(h, %Q(Target: External (#{h[:monitor][:url]})))
end

#monitor_create_host(h) ⇒ Object



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

def monitor_create_host(h)
  monitor_create_common(h, %Q(Target: Host metric (#{h[:monitor][:metric]})))
end

#monitor_create_service(h) ⇒ Object



218
219
220
# File 'lib/saba-webhook-gateway/googlechat.rb', line 218

def monitor_create_service(h)
  monitor_create_common(h, %Q(Target: Service metric (#{h[:monitor][:metric]})))
end

#monitor_delete_anomaly_detection(h) ⇒ Object



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

def monitor_delete_anomaly_detection(h)
  monitor_delete_common(h, %Q(Target: Anormaly detection (#{h[:monitor][:scopes].join(', ')})))
end

#monitor_delete_common(h, target) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/saba-webhook-gateway/googlechat.rb', line 271

def monitor_delete_common(h, target)
  header = { title: %Q([#{h[:orgName]}] Monitor #{h[:monitor][:name]} is deleted) }

  sa = []
  if h[:user]
    sa << %Q(Deleted by: #{h[:user][:screenName]})
  end

  sa << target

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  if h[:monitor][:memo]
    widget2 = [{ textParagraph: { text: h[:monitor][:memo] } }]
    sections.push({ widgets: widget2 })
  end

  googlechat_card(header, sections)
end

#monitor_delete_connectivity(h) ⇒ Object



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

def monitor_delete_connectivity(h)
  monitor_delete_common(h, %Q(Target: Connectivity (#{h[:monitor][:scopes].join(', ')})))
end

#monitor_delete_expression(h) ⇒ Object



300
301
302
# File 'lib/saba-webhook-gateway/googlechat.rb', line 300

def monitor_delete_expression(h)
  monitor_delete_common(h, %Q(Target: Expression (#{h[:monitor][:expression]})))
end

#monitor_delete_external(h) ⇒ Object



296
297
298
# File 'lib/saba-webhook-gateway/googlechat.rb', line 296

def monitor_delete_external(h)
  monitor_delete_common(h, %Q(Target: External (#{h[:monitor][:url]})))
end

#monitor_delete_host(h) ⇒ Object



292
293
294
# File 'lib/saba-webhook-gateway/googlechat.rb', line 292

def monitor_delete_host(h)
  monitor_delete_common(h, %Q(Target: Host metric (#{h[:monitor][:metric]})))
end

#monitor_delete_service(h) ⇒ Object



308
309
310
# File 'lib/saba-webhook-gateway/googlechat.rb', line 308

def monitor_delete_service(h)
  monitor_delete_common(h, %Q(Target: Service metric (#{h[:monitor][:metric]})))
end

#monitor_update_anomaly_detection(h) ⇒ Object



259
260
261
# File 'lib/saba-webhook-gateway/googlechat.rb', line 259

def monitor_update_anomaly_detection(h)
  monitor_update_common(h, %Q(Target: Anormaly detection (#{h[:monitor][:scopes].join(', ')})))
end

#monitor_update_common(h, target) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/saba-webhook-gateway/googlechat.rb', line 226

def monitor_update_common(h, target)
  header = { title: %Q([#{h[:orgName]}] Monitor #{h[:monitor][:name]} is updated) }

  sa = []
  if h[:user]
    sa << %Q(Updated by: #{h[:user][:screenName]})
  end

  sa << target

  widget1 = [{ textParagraph: { text: sa.join("\n") } }]
  sections = [{ widgets: widget1 }]

  if h[:monitor][:memo]
    widget2 = [{ textParagraph: { text: h[:monitor][:memo] } }]
    sections.push({ widgets: widget2 })
  end

  googlechat_card(header, sections)
end

#monitor_update_connectivity(h) ⇒ Object



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

def monitor_update_connectivity(h)
  monitor_update_common(h, %Q(Target: Connectivity (#{h[:monitor][:scopes].join(', ')})))
end

#monitor_update_expression(h) ⇒ Object



255
256
257
# File 'lib/saba-webhook-gateway/googlechat.rb', line 255

def monitor_update_expression(h)
  monitor_update_common(h, %Q(Target: Expression (#{h[:monitor][:expression]})))
end

#monitor_update_external(h) ⇒ Object



251
252
253
# File 'lib/saba-webhook-gateway/googlechat.rb', line 251

def monitor_update_external(h)
  monitor_update_common(h, %Q(Target: External (#{h[:monitor][:url]})))
end

#monitor_update_host(h) ⇒ Object



247
248
249
# File 'lib/saba-webhook-gateway/googlechat.rb', line 247

def monitor_update_host(h)
  monitor_update_common(h, %Q(Target: Host metric (#{h[:monitor][:metric]})))
end

#monitor_update_service(h) ⇒ Object



263
264
265
# File 'lib/saba-webhook-gateway/googlechat.rb', line 263

def monitor_update_service(h)
  monitor_update_common(h, %Q(Target: Service metric (#{h[:monitor][:metric]})))
end

#post(json) ⇒ Object



316
317
318
319
320
321
322
323
324
# File 'lib/saba-webhook-gateway/googlechat.rb', line 316

def post(json)
  resp = Faraday.post(ENV['GOOGLECHAT_WEBHOOK']) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = json
  end
  if @debug
    p("[post]\n#{resp.body}")
  end
end

#sample(h) ⇒ Object



18
19
20
21
22
23
# File 'lib/saba-webhook-gateway/googlechat.rb', line 18

def sample(h)
  header = { title: 'notification test' }
  widget1 = [{ textParagraph: { text: h[:message] } }]
  sections = [{ widgets: widget1 }]
  googlechat_card(header, sections)
end