Skip to content
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
80 changes: 80 additions & 0 deletions lib/systemd_mon/notifiers/flowdock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'systemd_mon/error'
require 'systemd_mon/notifiers/base'

begin
require 'flowdock'
rescue LoadError
raise SystemdMon::NotifierDependencyError, "The 'flowdock' gem is required by the flowdock notifier"
end

module SystemdMon::Notifiers
class Flowdock < Base
def initialize(*)
super
self.client = ::Flowdock::Client.new(flow_token: options['token'])
end

def notify_start!(hostname)
thread_id = "systemd_mon-#{hostname}"
title = "SystemdMon is starting on #{hostname}"
message = ""
chat title, message, thread_id, 'green', "starting"
end

def notify_stop!(hostname)
thread_id = "systemd_mon-#{hostname}"
title = "SystemdMon is stopping on #{hostname}"
message = ""
chat title, message, thread_id, 'yellow', "stopping"
end

def notify!(notification)
unit = notification.unit

title = "#{notification.type_text}: #{unit.name} on #{notification.hostname}: #{unit.state_change.status_text}"
message = "Systemd unit #{unit.name} on #{notification.hostname} #{unit.state_change.status_text}: #{unit.state.active} (#{unit.state.sub})"

if unit.state_change.length > 1
message << SystemdMon::Formatters::StateTableFormatter.new(unit).as_text
end

thread_id = "#{unit.name}-#{notification.hostname}"

chat title, message, thread_id, color(notification.type), unit.state.active

log "sent flowdock notification"
end

protected
attr_accessor :client, :options

def chat(title, message, thread_id, shade, status)
client.post_to_thread(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to satisfy my OCD, could you get everything in the following hash indented with two spaces instead of a mixture of 2 and 3? Thanks!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! Was annoyed by that too, but wanted to make the pull before I fell asleep. Will resolve while testing today

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jsjohnst, much appreciated!

event: "activity",
title: title,
external_thread_id: thread_id,
thread: {
title: title,
body: message,
status: {
color: shade,
value: status
}
}
)
end

def color(type)
case type
when :alert
'red'
when :warning
'yellow'
when :info
'purple'
else
'green'
end
end
end
end