Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to work with Pagerduty API v2 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions lib/pagerduty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize
end

def pagerduty_url
"https://#{@config['account_name']}.pagerduty.com"
"https://api.pagerduty.com"
end

def request(endpoint)
Expand All @@ -22,7 +22,7 @@ def request(endpoint)
# Some endpoints have query strings already. Detect this, and add onto the query string
# if it exists, or otherwise create one
char = endpoint.include?('?') ? '&' : '?'
endpoint = "#{endpoint}#{char}sort_by=created_on:desc&offset=#{pagination_offset}"
endpoint = "#{endpoint}#{char}sort_by=created_at:desc&offset=#{pagination_offset}"
response = HTTParty.get(
endpoint,
:headers => {
Expand All @@ -42,21 +42,17 @@ def request(endpoint)
def incidents(start_date, finish_date = nil)
finish_clause = finish_date ? "&until=#{finish_date}" : ''
time_zone = @config['time_zone']
endpoint = "#{pagerduty_url}/api/v1/incidents?since=#{start_date}#{finish_clause}&time_zone=#{time_zone}"
endpoint = "#{pagerduty_url}/incidents?since=#{start_date}#{finish_clause}&time_zone=#{time_zone}"
response = request(endpoint)
incidents = response.map do |incident|
tmp = {
:id => incident['id'],
:created_on => incident['created_on'],
:description => incident['trigger_summary_data']['description'],
:created_on => incident['created_at'],
:description => incident['description'],
:incident_key => incident['incident_key'],
:input_type => incident['service']['name'],
:input_type => incident['service']['summary'],
:category => 'not set'
}
if incident['trigger_summary_data']['description'].nil?
# some alerts don't have a description (e.g.: website pulse), fall back on subject and service name
tmp[:description] = "#{incident['service']['name']}: #{incident['trigger_summary_data']['subject']}"
end
tmp
end
add_ack_resolve(incidents)
Expand All @@ -65,9 +61,9 @@ def incidents(start_date, finish_date = nil)
def add_ack_resolve(incidents)
Parallel.each(incidents, :in_threads => 20) do |incident|
incident_id = incident[:id]
log = request("#{pagerduty_url}/api/v1/incidents/#{incident_id}/log_entries")
log = request("#{pagerduty_url}/incidents/#{incident_id}/log_entries")
.sort_by { |x| x['created_at'] }
problem = log.find { |x| x['type'] == 'trigger' }
problem = log.find { |x| x['type'] == 'trigger_log_entry' }
problem_time = Time.parse(problem['created_at'])
acknowledge_by = nil
time_to_ack = nil
Expand Down