Skip to content

Commit

Permalink
Add basic mail alerting (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosestito authored Feb 10, 2022
1 parent 5a155af commit 727c23a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/tronto/alerts/emails.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Tronto.AlertEmail do
@moduledoc false

import Swoosh.Email

def alert(text) do
new()
|> to({"Tronto Admin", "[email protected]"})
|> from({"Tronto Alerts", "[email protected]"})
|> subject(text)
|> html_body(
"<h3>#{text}<h3><br><br><img src='https://c.tenor.com/MYZgsN2TDJAAAAAC/this-is.gif'>"
)
end
end
40 changes: 40 additions & 0 deletions lib/tronto/monitoring/event_handlers/alerts_event_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Tronto.Monitoring.AlertsEventsHandler do
@moduledoc """
This event hanlder is responsible to forward checks execution request to the agent.
"""

use Commanded.Event.Handler,
application: Tronto.Commanded,
name: "alerts_event_handler"

alias Tronto.Monitoring.Domain.Events.{
ClusterHealthChanged,
HeartbeatFailed
}

alias Tronto.Monitoring.{
ClusterReadModel,
HostReadModel
}

def handle(
%ClusterHealthChanged{cluster_id: cluster_id, health: health},
_metadata
)
when health in [:warning, :critical] do
%ClusterReadModel{name: name} = Tronto.Repo.get!(ClusterReadModel, cluster_id)

email = Tronto.AlertEmail.alert("Cluster #{name} health is now in #{health} state")
Tronto.Mailer.deliver(email)
end

def handle(
%HeartbeatFailed{host_id: host_id},
_metadata
) do
%HostReadModel{hostname: hostname} = Tronto.Repo.get!(HostReadModel, host_id)

email = Tronto.AlertEmail.alert("Host #{hostname} hearbeat failed")
Tronto.Mailer.deliver(email)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Tronto.Monitoring.EventHandlersSupervisor do
@impl true
def init(_init_arg) do
children = [
Tronto.Monitoring.AlertsEventsHandler,
Tronto.Monitoring.ChecksEventsHandler
]

Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule Tronto.MixProject do
{:faker, "~> 0.17", only: [:dev, :test]},
{:floki, ">= 0.30.0", only: :test},
{:gettext, "~> 0.18"},
{:gen_smtp, "~> 1.1.1"},
{:jason, "~> 1.2"},
{:mock, "~> 0.3.0", only: :test},
{:phoenix, "~> 1.6.2"},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.32.0", "f915dc15258bc997d49be1f5ef7d3992f8834d6f5695270acad17b41f5bcc8e2", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "1c5a91cae1fd8931c26a4826b5e2372c284813904c8bacb468b5de39c7ececbd"},
"fsm": {:hex, :fsm, "0.3.1", "087aa9b02779a84320dc7a2d8464452b5308e29877921b2bde81cdba32a12390", [:mix], [], "hexpm", "fbf0d53f89e9082b326b0b5828b94b4c549ff9d1452bbfd00b4d1ac082208e96"},
"gen_smtp": {:hex, :gen_smtp, "1.1.1", "bf9303c31735100631b1d708d629e4c65944319d1143b5c9952054f4a1311d85", [:rebar3], [{:hut, "1.3.0", [hex: :hut, repo: "hexpm", optional: false]}, {:ranch, ">= 1.7.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "51bc50cc017efd4a4248cbc39ea30fb60efa7d4a49688986fafad84434ff9ab7"},
"gen_stage": {:hex, :gen_stage, "1.1.2", "b1656cd4ba431ed02c5656fe10cb5423820847113a07218da68eae5d6a260c23", [:mix], [], "hexpm", "9e39af23140f704e2b07a3e29d8f05fd21c2aaf4088ff43cb82be4b9e3148d02"},
"gettext": {:hex, :gettext, "0.19.1", "564953fd21f29358e68b91634799d9d26989f8d039d7512622efb3c3b1c97892", [:mix], [], "hexpm", "10c656c0912b8299adba9b061c06947511e3f109ab0d18b44a866a4498e77222"},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
Expand Down

0 comments on commit 727c23a

Please sign in to comment.