bin/pgutils
#!/usr/bin/env ruby -w
require 'tapjoy/pagerduty'
def load_trigger(trigger_name, message)
config_file = "#{ENV['PAGERDUTY_CONFIG_DIR'] ? ENV['PAGERDUTY_CONFIG_DIR'] + 'triggers.yml' : ENV['HOME'] + '/.pgutils/triggers.yaml'}"
if File.readable?(config_file)
triggers = YAML.load_file(config_file)
else
abort("#{config_file} not found")
end
pg = Tapjoy::PagerDuty::Base.new
triggers[trigger_name.to_sym][:description] = message if message
puts pg.post_trigger(**triggers[trigger_name.to_sym])
end
def get_level_one_users(pg)
return pg.get_users_on_call['escalation_policies'].flat_map do |policy|
policy['on_call'].map do |oncall|
oncall['user']['id'] if oncall['start'] and oncall['level'] == 1
end.compact
end
end
SUB_COMMANDS = %w(set_override trigger get_on_call audit)
Trollop::options do
usage '[SUB_COMMAND] [options]'
synopsis "\nTool to integrate with PagerDuty API.\nAvailable subcommands are: #{SUB_COMMANDS}"
version "#{File.basename($0)} #{Tapjoy::PagerDuty::VERSION} \u00A9 2014-2015 Tapjoy, Inc."
stop_on SUB_COMMANDS
end
cmd = ARGV.shift
case cmd
when 'set_override'
opts = Trollop::options do
usage 'set_override [options]'
synopsis "\nTemporarily override the primary person on-call for a given schedule."
opt(:email, 'Specify email address of override user', required: true, :type => :string)
opt(:schedule_name, 'Name of schedule to override', default: 'Default', :type => :string)
opt(:override_length, 'Number of seconds to maintain override for', default: 3600, :type => :int)
end
Tapjoy::PagerDuty::Override.new(opts[:email], opts[:schedule_name], opts[:override_length])
when 'trigger'
opts = Trollop::options do
usage 'trigger [options]'
synopsis "\nThis script will create a pagerduty alert, based on the name of the trigger hash as specified in 'triggers.yaml'"
opt :trigger, 'Name of trigger to alert on', required: true, type: :string
opt :message, 'Message to include in trigger', type: :string
end
load_trigger(opts[:trigger], opts[:message])
when 'get_on_call'
Trollop::options do
usage 'get_on_call'
synopsis "\nThis script will return the list of each person on-call sorted by schedule.\nThere are no options available for this command at this time."
end
pg = Tapjoy::PagerDuty::Base.new
get_level_one_users(pg).each do |u|
user = pg.get_user_details(u)['user']
on_call = user['on_call']
puts "Name: #{user['name']}"
on_call.each do |oc|
puts "\tGroup: #{oc['escalation_policy']['name']}"
puts "\tStart: #{oc['start']}"
puts "\tEnd: #{oc['end']}"
puts "\n"
end
puts '---'
end
when 'audit'
opts = Trollop::options do
usage 'audit'
opt :since, 'Get list of incidents since the given time window in days', default: 30, type: :int
opt :until, 'Get list of incidents until the given time window in days relative to since parameter', default: 30, type: :int
synopsis "\nBy default, this script will return the last 30 days of pages or last 100 pages, which ever is fewer.\nThere are parameters to adjust the date range, but 100 pages is still the cap."
end
incidents = Tapjoy::PagerDuty::Base.new.get_incidents(query_start: opts[:since], query_end: opts[:until])
puts incidents['incidents'].to_yaml
puts "Total Alerts: #{incidents['total']}"
else
Trollop::educate
end