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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
# File 'lib/flapjack/gateways/jabber.rb', line 269
def interpret(room, nick, time, command)
msg = nil
action = nil
check = nil
begin
case command
when /^help\s*$/
msg = "commands: \n" +
" find (number) checks matching /pattern/\n" +
" find (number) checks with tag <tag>\n" +
" state of <check>\n" +
" state of checks matching /pattern/\n" +
" state of checks with tag <tag>\n" +
" tell me about <check>\n" +
" tell me about (number) checks matching /pattern/\n" +
" tell me about (number) checks with tag <tag>\n" +
" ACKID <id>[ duration: <time spec>][ comment: <comment>]\n" +
" ack <check>[ duration: <time spec>][ comment: <comment>]\n" +
" ack checks matching /pattern/[ duration: <time spec>][ comment: <comment>]\n" +
" ack checks with tag <tag>[ duration: <time spec>][ comment: <comment>]\n" +
" maint <check>[ (start-at|start-in): <time spec>][ duration: <time spec>][ comment: <comment>]\n" +
" maint checks matching /pattern/[ (start-at|start-in): <time spec>][ duration: <time spec>][ comment: <comment>]\n" +
" maint checks with tag <tag>[ (start-at|start-in): <time spec>][ duration: <time spec>][ comment: <comment>]\n" +
" test notifications for <check>\n" +
" test notifications for checks matching /pattern/\n" +
" test notifications for checks with tag <tag>\n" +
" identify\n" +
" help\n"
when /^identify\s*$/
t = Process.times
fqdn = `/bin/hostname -f`.chomp
pid = Process.pid
msg = "Flapjack #{Flapjack::VERSION} process #{pid} on #{fqdn} \n" +
"Identifiers: #{@bot.identifiers.join(', ')}\n" +
"Boot time: #{@boot_time}\n" +
"User CPU Time: #{t.utime}\n" +
"System CPU Time: #{t.stime}\n" +
`uname -a`.chomp + "\n"
when /^find\s+(\d+\s+)?checks\s+(?:matching\s+\/(.+)\/|with\s+tag\s+(.+))\s*$/im
limit_checks = Regexp.last_match(1).nil? ? 30 : Regexp.last_match(1).strip.to_i
pattern = Regexp.last_match(2)
tag = Regexp.last_match(3)
msg = derive_check_ids_for(pattern, tag, nil, :limit => limit_checks) do |check_ids, descriptor, num_checks|
resp = "Checks #{descriptor}:\n"
checks = Flapjack::Data::Check.find_by_ids(*check_ids)
resp += "Showing first #{limit_checks} results of #{num_checks}:\n" if num_checks > limit_checks
resp += checks.map {|chk|
cond = chk.condition
"#{chk.name} is #{cond.nil? ? '[none]' : cond.upcase}"
}.join(', ')
resp
end
when /^state\s+of\s+#{CHECK_MATCH_FRAGMENT}\s*$/im
pattern = Regexp.last_match(1)
tag = Regexp.last_match(2)
check_name = Regexp.last_match(3)
msg = derive_check_ids_for(pattern, tag, check_name) do |check_ids, descriptor|
"State of checks #{descriptor}:\n" +
Flapjack::Data::Check.intersect(:id => check_ids).collect {|chk|
cond = chk.condition
"#{chk.name} - #{cond.nil? ? '[none]' : cond.upcase}"
}.join("\n")
end
when /^tell\s+me\s+about\s(\d+\s+)?+#{CHECK_MATCH_FRAGMENT}\s*$/im
limit_checks = Regexp.last_match(1).nil? ? 30 : Regexp.last_match(1).strip.to_i
pattern = Regexp.last_match(2)
tag = Regexp.last_match(3)
check_name = Regexp.last_match(4)
msg = derive_check_ids_for(pattern, tag, check_name, :limit => limit_checks,
:lock_klasses => [Flapjack::Data::ScheduledMaintenance,
Flapjack::Data::UnscheduledMaintenance]) do |check_ids, descriptor, num_checks|
current_time = Time.now
resp = "Details of checks #{descriptor}\n"
resp += "Showing first #{limit_checks} results of #{num_checks}:\n" if num_checks > limit_checks
resp += Flapjack::Data::Check.intersect(:id => check_ids).collect {|chk|
get_check_details(chk, current_time)
}.join("")
end
when /^ACKID\s+([0-9A-F]+)(?:\s*(.*?)(?:\s*duration:.*?(\w+.*))?)$/im
ackid = Regexp.last_match(1)
= Regexp.last_match(2)
duration_str = Regexp.last_match(3)
error = nil
dur = nil
if .nil? || (.length == 0)
error = "please provide a comment, eg \"#{@bot.alias}: ACKID #{Regexp.last_match(1)} AL looking\""
elsif duration_str
dur = ChronicDuration.parse(duration_str)
end
four_hours = 4 * 60 * 60
duration = (dur.nil? || (dur <= 0)) ? four_hours : dur
check = Flapjack::Data::Check.intersect(:ack_hash => ackid).all.first
if check.nil?
msg = "ERROR - couldn't ACK #{ackid} - not found"
else
check_name = check.name
details = "#{check_name} (#{ackid})"
if check.in_unscheduled_maintenance?
msg = "Changing ACK for #{details}"
else
msg = "ACKing #{details}"
end
action = Proc.new {
Flapjack::Data::Event.create_acknowledgements(
@config['processor_queue'] || 'events',
[check],
:summary => ( || ''),
:acknowledgement_id => ackid,
:duration => duration,
)
}
end
when /^ack\s+#{CHECK_MATCH_FRAGMENT}(?:\s+(.*?)(?:\s*duration:.*?(\w+.*))?)?\s*$/im
pattern = Regexp.last_match(1)
tag = Regexp.last_match(2)
check_name = Regexp.last_match(3)
= Regexp.last_match(4) ? Regexp.last_match(4).strip : nil
duration_str = Regexp.last_match(5) ? Regexp.last_match(5).strip : '1 hour'
duration = ChronicDuration.parse(duration_str)
msg = derive_check_ids_for(pattern, tag, check_name,
:lock_klasses => [Flapjack::Data::UnscheduledMaintenance]) do |check_ids, descriptor|
failing_checks = Flapjack::Data::Check.
intersect(:id => check_ids, :failing => true)
if failing_checks.empty?
"No failing checks #{descriptor}"
else
summary = "#{nick}: #{.blank? ? 'Set via chatbot' : }"
action = Proc.new {
Flapjack::Data::Event.create_acknowledgements(
@config['processor_queue'] || 'events',
failing_checks,
:summary => summary,
:duration => duration
)
}
"Ack list:\n" + failing_checks.map(&:name).join("\n")
end
end
when /^maint\s+#{CHECK_MATCH_FRAGMENT}\s+(?:start-in:.*?(\w+.*?)|start-at:.*?(\w+.*?))?(?:\s+duration:.*?(\w+.*?))?(?:\s+comment:.*?(\w+.*?))?\s*$/im
pattern = Regexp.last_match(1)
tag = Regexp.last_match(2)
check_name = Regexp.last_match(3)
start_in = Regexp.last_match(4) ? Regexp.last_match(4).strip : nil
start_at = Regexp.last_match(5) ? Regexp.last_match(5).strip : nil
duration_str = Regexp.last_match(6) ? Regexp.last_match(6).strip : '1 hour'
duration = ChronicDuration.parse(duration_str)
= Regexp.last_match(7) ? Regexp.last_match(7).strip : 'Test maintenance'
started = case
when start_in
Time.now.to_i + ChronicDuration.parse(start_in)
when start_at
Chronic.parse(start_at).to_i
else
Time.now.to_i
end
msg = derive_check_ids_for(pattern, tag, check_name,
:lock_klasses => [Flapjack::Data::ScheduledMaintenance]) do |check_ids, descriptor|
checks = Flapjack::Data::Check.find_by_ids(*check_ids)
checks.each do |chk|
sched_maint = Flapjack::Data::ScheduledMaintenance.new(
:start_time => started,
:end_time => started + duration,
:summary =>
)
sched_maint.save
chk.scheduled_maintenances << sched_maint
end
"Scheduled maintenance for #{duration/60} minutes starting at #{Time.at(started)} on:\n" + checks.collect {|c| "#{c.name}" }.join("\n")
end
when /^test\s+notifications\s+for\s+#{CHECK_MATCH_FRAGMENT}\s*$/im
pattern = Regexp.last_match(1)
tag = Regexp.last_match(2)
check_name = Regexp.last_match(3)
msg = derive_check_ids_for(pattern, tag, check_name) do |check_ids, descriptor|
summary = "Testing notifications to all contacts interested in checks #{descriptor}"
checks = Flapjack::Data::Check.find_by_ids(*check_ids)
action = Proc.new {
Flapjack::Data::Event.test_notifications(@config['processor_queue'] || 'events',
checks, :summary => summary)
}
"Testing notifications for check#{(checks.size > 1) ? 's' : ''} #{descriptor}"
end
when /^(.*)/
words = Regexp.last_match(1)
msg = "what do you mean, '#{words}'? Type 'help' for a list of acceptable commands."
end
rescue => e
Flapjack.logger.error { "Exception when interpreting command '#{command}' - #{e.class}, #{e.message}" }
Flapjack.logger.debug { e.backtrace.join("\n") }
msg = "Oops, something went wrong processing that command (#{e.class}, #{e.message})"
end
@bot ||= @siblings && @siblings.detect {|sib| sib.respond_to?(:announce) }
if @bot && (room || nick)
if room
Flapjack.logger.info "sending to room #{room}: #{msg}"
@bot.announce(room, msg)
else
Flapjack.logger.info "sending to user #{nick}: #{msg}"
@bot.say(nick, msg)
end
else
Flapjack.logger.warn "jabber bot not running, won't send #{msg} to #{room || nick}"
end
action.call if action
end
|