`
wuhuizhong
  • 浏览: 669632 次
  • 性别: Icon_minigender_1
  • 来自: 中山
社区版块
存档分类
最新评论

rails 自建 task - 檢查RMAN備份結果

    博客分类:
  • ROR
阅读更多

$ vi lib/tasks/notify.rake

require 'net/ssh'
require 'net/sftp'
def notify_rman(mails,result)
    htmlopen  = %Q{<html>
   <head>
      <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
   </head>
   <body topmargin="10" leftmargin="10" marginheight="0" marginwidth="0" bgcolor="#e0ffff" text="#000000">
      <table height="23" width="100%" border="0" cellSpacing="0" cellPadding="0" bgcolor="#4a6bad">
         <tr>
            <td align="left" style="font-family: Verdana, Arial; font-size:9pt;color:#f2f8ff">檢查RMAN備份結果(#{Time.now.strftime("%Y-%m-%d")})</td>
            <td align="right" style="font-family: Verdana, Arial; font-size:9pt;color:#f2f8ff">
        </td>
         </tr>
      </table>
        <center>
         <table bgcolor="#f2f8ff" border="1" width="100%" style="border-collapse: collapse;font-size:12px" bordercolor="#666699">
            <tr>
               <th>NO</th>
               <th>主機IP</th>
               <th>資料庫用途</th>
               <th>備份結果</th>
               <th align="left">檢查日誌信息</th>
            </tr>}
  htmlclose = %Q{</table>
      </center>
    </body>
</html>}
    table = ''
    i = 0
    result.each do |row|
      i += 1
      if row[:data] =~ /successfully/
        status = "成功"
        bgcolor = "#f2f8ff"
      else
        status = "失敗"
        bgcolor = "#ffffe0"
      end
      #body << "#{row[:ip]} : #{row[:data]}"
      table += "<tr><td bgcolor='#{bgcolor}'> #{i}</td>
                            <td bgcolor='#{bgcolor}'>#{row[:ip]}</td>
                            <td bgcolor='#{bgcolor}'>#{row[:notes]}</td>
                            <td bgcolor='#{bgcolor}'>#{status}</td>
                            <td bgcolor='#{bgcolor}'>#{row[:data]}</td></tr>"
    end
    table += "<tr><td colspan='4'><a href='http://172.18.60.39/oms' >點此登錄主機監控網站查看更多RMAN備份記錄</a></td></tr>"
    #body = %Q{ #{result.inspect}}
    html_body = %{ #{htmlopen}  #{table}  #{htmlclose}}

    Pony.mail(
    :to => mails,
    :from => 'DBA<huizhong.wu@pyzs.com.cn>',
    #:cc => 'Jamie Kao<jamie.kao@pousong.com.cn>',
    :subject => '[Oracle主機監控網站 ] 檢查RMAN備份結果',
    :html_body => html_body,
    :via => :smtp, :via_options => {
      :address              => 'email.pyzs.com.cn',
      :port                 => 25,
      :enable_starttls_auto => true,
      :user_name            => 'username',
      :password             => 'password',
      :authentication       => :login,
      :domain               => "localhost.localdomain" # the HELO domain provided by the client to the server
    })
end

namespace :notify do
  desc "Notify RMAN log"
  task :rmanlog do
    db = Sequel.connect('oracle://netzke_demo:practice@172.18.60.39:1521/test')
    sql = "select t.id,t.ip_address ip,t.notes,t.oracle pwd,t.rman_mk mk,t.rman_log logfile 
                from hosts t
              where t.rman_mk = '1' order by t.ip_address"
    result = []
    db[sql].each do |row|
      ssh_command = "tail -2 #{row[:logfile]}"
      output = ''
      Net::SSH.start(row[:ip], 'oracle', {:password => row[:pwd]}) do |ssh|
        #@log.info "ssh session open"

        # ssh to client
        channel = ssh.open_channel do |ch|
            #@log.info "executing '#{ssh_command}'"
            ch.exec ssh_command do |ch, success|
                unless success
                    #@log.info "ssh command failed"
                    raise "could not execute command: '#{ssh_command}'"
                end

                # "on_data" is called when the process writes something to stdout
                ch.on_data do |c, data|
                    output += data
                    #@log.info "#{row[:ip]}: #{data}"
                end

                # "on_extended_data" is called when the process writes something to stderr
                ch.on_extended_data do |c, type, data|
                    output += data
                    #@log.info "got stderr: #{data}"
                end

                ch.on_request "exit-status" do |c, data|
                    #@log.info "process terminated with exit status: #{data.read_long.to_s}"
                end

                ch.on_close #{ @log.info "channel closed!" }
            end
        end
        # channel.wait
        #@log.info "ssh channel done"
      end
      result << {:ip => row[:ip], :notes => row[:notes], :data => output}
    end
    mails = ['WHZ<huizhong.wu@pyzs.com.cn>', 'LJP<jing.liu@pousong.com.cn>', 'LCL<lcl@pyzs.com.cn>', 'ZSH<zsh8@pousong.com.cn>', 'CH<david.chen@pyzs.com.cn>'
]
    #mails =  ['WHZ<huizhong.wu@pyzs.com.cn>']
    notify_rman(mails,result)

  end
end

 $ rake notify:rmanlog

 

常見問題:

1.不能連接被監控的主機,報錯:no route to host

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics