Sha256: 9f5d98715272151d702c9164774394cb2ab65292085b727933987df5804e555a

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8

module Backup
  module Notifier
    class Nagios < Base

      ##
      # Host of Nagios server to notify on backup completion.
      attr_accessor :nagios_host

      ##
      # Port of Nagios server to notify on backup completion.
      attr_accessor :nagios_port

      ##
      # Name of the Nagios service for the backup check.
      attr_accessor :service_name

      ##
      # Host name in Nagios for the backup check.
      attr_accessor :service_host

      def initialize(model, &block)
        super
        instance_eval(&block) if block_given?

        @nagios_host  ||= Config.hostname
        @nagios_port  ||= 5667
        @service_name ||= "Backup #{ model.trigger }"
        @service_host ||= Config.hostname
      end

      private

      ##
      # Notify the user of the backup operation results.
      #
      # `status` indicates one of the following:
      #
      # `:success`
      # : The backup completed successfully.
      # : Notification will be sent if `on_success` is `true`.
      #
      # `:warning`
      # : The backup completed successfully, but warnings were logged.
      # : Notification will be sent if `on_warning` or `on_success` is `true`.
      #
      # `:failure`
      # : The backup operation failed.
      # : Notification will be sent if `on_warning` or `on_success` is `true`.
      #
      def notify!(status)
        message = case status
              when :success then 'Completed Successfully'
              when :warning then 'Completed Successfully (with Warnings)'
              when :failure then 'Failed'
              end
        send_message("#{ message } in #{ model.duration }")
      end

      def send_message(message)
        cmd = "#{ utility(:send_nsca) } -H '#{ nagios_host }' -p '#{ nagios_port }'"
        msg = [service_host, service_name, model.exit_status, message].join("\t")
        run("echo '#{ msg }' | #{ cmd }")
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
backup-3.11.0 lib/backup/notifier/nagios.rb
backup-4.0.2 lib/backup/notifier/nagios.rb
nfm-backup-4.0.1a lib/backup/notifier/nagios.rb
backup-4.0.1 lib/backup/notifier/nagios.rb
backup-4.0.0 lib/backup/notifier/nagios.rb
backup-4.0.0rc1 lib/backup/notifier/nagios.rb
backup-3.10.0 lib/backup/notifier/nagios.rb
backup-3.9.0 lib/backup/notifier/nagios.rb
backup-3.8.0 lib/backup/notifier/nagios.rb
backup-3.7.2 lib/backup/notifier/nagios.rb
backup-3.7.1 lib/backup/notifier/nagios.rb
backup-3.7.0 lib/backup/notifier/nagios.rb