Skip to content

Commit

Permalink
Logic Monitor Collector ACK (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
armiiller authored Nov 27, 2023
1 parent f070dcc commit b408aed
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 8 deletions.
39 changes: 36 additions & 3 deletions app/models/pager_tree/integrations/logic_monitor/v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ class LogicMonitor::V3 < Integration
{key: :alert_acknowledged, type: :boolean, default: false},
{key: :logic_monitor_account_name, type: :string, default: nil},
{key: :access_id, type: :string, default: nil},
{key: :access_key, type: :string, default: nil}
{key: :access_key, type: :string, default: nil},
{key: :bearer_token, type: :string, default: nil}
]
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"

validates :option_alert_acknowledged, inclusion: {in: [true, false]}
validates :option_logic_monitor_account_name, presence: true, if: -> { option_alert_acknowledged == true }
validates :option_access_key, presence: true, if: -> { option_alert_acknowledged == true }
validates :option_access_id, presence: true, if: -> { option_alert_acknowledged == true }
validates :option_bearer_token, presence: true, if: -> { option_alert_acknowledged == true }

after_initialize do
self.option_alert_acknowledged = false if option_alert_acknowledged.nil?
Expand Down Expand Up @@ -107,9 +109,19 @@ def _additional_datums
def _on_acknowledge
lm_alert_id = adapter_outgoing_event.alert.thirdparty_id
acknowledger = adapter_outgoing_event.account_user&.name || name || "someone"
resource_path = "/alert/alerts/#{lm_alert_id}/ack"
http_verb = "POST"
send_request_with_hmac(resource_path, http_verb, {ackComment: "Acknowledged by #{acknowledger}"})

if adapter_outgoing_event.alert.source_log&.message&.dig("params", "alerttype") == "agentDownAlert"
# try to get the collector id
collector_id = lm_alert_id.gsub(/[^0-9]/, "")
resource_path = "/setting/collector/collectors/#{collector_id}/ackdown?v=2"
data = {comment: "Acknowledged by #{acknowledger}"}
send_request_with_bearer_token(resource_path, http_verb, data)
else
resource_path = "/alert/alerts/#{lm_alert_id}/ack"
data = {ackComment: "Acknowledged by #{acknowledger}"}
send_request_with_hmac(resource_path, http_verb, data)
end
end

# https://www.logicmonitor.com/support/rest-api-authentication
Expand Down Expand Up @@ -143,6 +155,27 @@ def send_request_with_hmac(resource_path, http_verb, data)

outgoing_webhook_delivery
end

def send_request_with_bearer_token(resource_path, http_verb, data)
base_url = "https://#{option_logic_monitor_account_name}.logicmonitor.com/santaba/rest"
url = base_url + resource_path
headers = {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{option_bearer_token}"
}

# note outgoing webhook delivery only supports the post method
outgoing_webhook_delivery = OutgoingWebhookDelivery.factory(
resource: self,
url: url,
body: data,
options: {headers: headers}
)
outgoing_webhook_delivery.save!
outgoing_webhook_delivery.deliver_later

outgoing_webhook_delivery
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="form-group group">
<%= form.check_box :option_alert_acknowledged, class: "form-checkbox" %>
<%= form.label :option_alert_acknowledged, class: "inline-block" %>
<p class="form-hint md:inline-block"><%== t(".option_alert_acknowledged_hint_html") %></p>
</div>

<div class="form-group group">
<%= form.label :option_logic_monitor_account_name %>
<%= form.text_field :option_logic_monitor_account_name, class: "form-control" %>
Expand All @@ -18,9 +24,8 @@
</div>

<div class="form-group group">
<%= form.check_box :option_alert_acknowledged, class: "form-checkbox" %>
<%= form.label :option_alert_acknowledged, class: "inline-block" %>
<p class="form-hint md:inline-block"><%== t(".option_alert_acknowledged_hint_html") %></p>
<%= form.label :option_bearer_token %>
<%= form.text_field :option_bearer_token, class: "form-control" %>
<p class="form-hint"><%== t(".option_bearer_token_hint_html") %></p>
</div>

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@
</p>
</div>
</dd>
</div>

<div class="sm:col-span-1">
<dt class="text-sm font-medium text-gray-500">
<%= t("activerecord.attributes.pager_tree/integrations/logic_monitor/v3.option_bearer_token") %>
</dt>
<dd class="mt-1 text-sm text-gray-900">
<div class="flex items-center gap-2">
<p class="text-sm truncate">
<%= mask integration.option_bearer_token %>
</p>
</div>
</dd>
</div>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ en:
form_options:
option_access_id_hint_html: "Logic Monitor Access <b>ID</b>"
option_access_key_hint_html: "Logic Monitor Access <b>Key</b>"
option_bearer_token_hint_html: "Logic Monitor <b>Bearer Token</b>"
option_logic_monitor_account_name_hint_html: "Your Logic Monitor account name. (ex: https://<b>account_name</b>.logicmonitor.com)"
option_alert_acknowledged_hint_html: "Acknowledge the alert in Logic Monitor when the alert is acknowledged in PagerTree. (PagerTree => Logic Monitor)"
mattermost:
Expand Down Expand Up @@ -166,6 +167,7 @@ en:
"pager_tree/integrations/logic_monitor/v3":
option_access_id: "Logic Monitor Access ID"
option_access_key: "Logic Monitor Access Key"
option_bearer_token: "Logic Monitor Bearer Token"
option_logic_monitor_account_name: "Logic Monitor Account Name"
option_alert_acknowledged: "Acknowledge in Logic Monitor"
"pager_tree/integrations/sentry/v3":
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/pager_tree/integrations/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ logic_monitor_v3:
alert_acknowledged: true
access_id: abc123
access_key: def345
bearer_token: ghi678
logic_monitor_account_name: acme

live_call_routing_twilio_v3:
Expand Down
46 changes: 45 additions & 1 deletion test/models/pager_tree/integrations/logic_monitor/v3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class LogicMonitor::V3Test < ActiveSupport::TestCase
assert_equal true_alert.to_json, @integration.adapter_process_create.to_json
end

test "can_process_outgoing" do
test "can_process_outgoing_hmac" do
assert_no_performed_jobs

expected_payload = {
Expand Down Expand Up @@ -139,5 +139,49 @@ class LogicMonitor::V3Test < ActiveSupport::TestCase
assert_equal expected_payload.to_json, outgoing_webhook_delivery.body.to_json
assert outgoing_webhook_delivery.options.dig("headers", "Authorization").starts_with?("LMv1 #{@integration.option_access_id}:")
end

test "can_process_outgoing_bearer" do
assert_no_performed_jobs

expected_payload = {
"comment" => "Acknowledged by test-user"
}

outgoing_event = OutgoingEvent.new
outgoing_event.event_name = :alert_acknowledged
outgoing_event.alert = OpenStruct.new(
thirdparty_id: "LM1234567890",
foo: "bar",
source: @integration,
source_log: OpenStruct.new(
message: {
"params" => {
"alerttype" => "agentDownAlert"
}
}
)
)
outgoing_event.changes = [{
before: {
foo: "baz"
},
after: {
foo: "bar"
}
}]
outgoing_event.account_user = OpenStruct.new(
name: "test-user"
)

@integration.adapter_outgoing_event = outgoing_event
outgoing_webhook_delivery = @integration.adapter_process_outgoing

assert_enqueued_jobs 1

assert_equal "https://#{@integration.option_logic_monitor_account_name}.logicmonitor.com/santaba/rest/setting/collector/collectors/1234567890/ackdown?v=2", outgoing_webhook_delivery.url
assert_equal :queued.to_s, outgoing_webhook_delivery.status
assert_equal expected_payload.to_json, outgoing_webhook_delivery.body.to_json
assert outgoing_webhook_delivery.options.dig("headers", "Authorization").starts_with?("Bearer #{@integration.option_bearer_token}")
end
end
end

0 comments on commit b408aed

Please sign in to comment.