From 005259d7e8f05033913361e2a58ffc0f549dc005 Mon Sep 17 00:00:00 2001 From: Frederik Lauber Date: Wed, 15 Jan 2020 15:17:17 +0100 Subject: [PATCH] Added a handler to generate Desktop Notifications via DBus so you notice automatically that your services have failed. --- README.md | 3 ++ lib/systemd_mon/notifiers/desktop.rb | 50 ++++++++++++++++++++++++++++ systemd_mon.gemspec | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 lib/systemd_mon/notifiers/desktop.rb diff --git a/README.md b/README.md index f513f8b..e6d615c 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,9 @@ notifiers: token: bigsecrettokenhere room: myroom username: doge + desktop: + start_stop_message: false + timeout: 2000 units: - unicorn.service - nginx.service diff --git a/lib/systemd_mon/notifiers/desktop.rb b/lib/systemd_mon/notifiers/desktop.rb new file mode 100644 index 0000000..cd4442f --- /dev/null +++ b/lib/systemd_mon/notifiers/desktop.rb @@ -0,0 +1,50 @@ +require 'systemd_mon/error' +require 'systemd_mon/notifiers/base' +require 'systemd_mon/formatters/state_table_formatter' + + +module SystemdMon::Notifiers + class Desktop < Base + bus = DBus::SessionBus.instance + not_service = bus.service("org.freedesktop.Notifications") + not_object = not_service.object("/org/freedesktop/Notifications") + not_object.introspect + DesktopNotifier = not_object["org.freedesktop.Notifications"] + + def initialize(*) + super + if options['start_stop_message'] + @startstopmessage = true + else + @startstopmessage = false + end + if options['timeout'] + @timeout = timeout + else + @timeout = 2000 + end + end + + + def notify_start!(hostname) + if @startstopmessage + DesktopNotifier.Notify("Systemd_mon", 0, "info", "Started", "", [], {}, @timeout) + end + end + + def notify_stop!(hostname) + if @startstopmessage + DesktopNotifier.Notify("Systemd_mon", 0, "info", "Stopped", "", [], {}, @timeout) + end + end + + def notify!(notification) + unit = notification.unit + DesktopNotifier.Notify("Systemd_mon", 0, "#{notification.type_text}", "#{unit.name} changed to #{unit.state_change.status_text}", "", [], {}, @timeout) + end + end +end + + + + diff --git a/systemd_mon.gemspec b/systemd_mon.gemspec index accc5a6..c42c316 100644 --- a/systemd_mon.gemspec +++ b/systemd_mon.gemspec @@ -19,6 +19,6 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency "ruby-dbus", "~> 0.11.0" - spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "bundler", ">= 1.6" spec.add_development_dependency "rake" end