Sha256: 8f2f1c92623378e9d227c56694bed50d02ca4b80a56395cb5ec335285aba2465

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

class SysWatchdog
    private

    def notify sub, body = ""
        begin
            send_mail sub, body if @conf.smtp_server
        rescue => e
            @logger.error e.desc
        end
        begin
            send_slack_msg "#{sub}\n#{body}" if @conf.slack_token
        rescue => e
            @logger.error e.desc
        end
    end

    def send_slack_msg msg
        slack_client = Slack::Web::Client.new
        slack_client.chat_postMessage(channel: @conf[:slack_channel], text: "[#{server_name}] #{msg}", as_user: true)
    end

    def send_mail sub, body
        @logger.info "Sending email: #{ sub }"

        body ||= ""
        body += append_sys_info
        
        mail = Mail.new do
          from     @conf.mail_from
          to       @conf.mail_to
          subject  "Watchdog #{@conf.name} [#{server_name}]: #{ sub }"
          body     body
        end

        mail.deliver!
    end

    def server_name
        @conf.server_name || `hostname`.strip
    end

    def append_sys_info
        ret = "\n\n--------------- sys info ---------------"
        %Q(
            ps aux
            df -h
            uptime

        ).split("\n").map(&:strip).compact.reject(&:empty?).each do |cmd|
            cmd_result = `#{ cmd }`
            ret += "\n\n#{ cmd }:\n#{ cmd_result }"
        end
        ret
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sys_watchdog-0.1.7 lib/sys_watchdog/notify.rb