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

分析Oracle database alert log

    博客分类:
  • ROR
阅读更多
1.本地alert log
# ora_err.rb
# usage: ruby ora_err.rb < alert.log > ora_err.txt
require 'parsedate'

delimiter = "\t" 
date_mask = '%Y-%m-%d %H:%M:%S'

months = { 'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6,
           'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12 }

class OraErr
  attr_reader :sqlcode, :begin_time, :sqlerrm
  def initialize(sqlcode,begin_time,sqlerrm)
    @sqlcode = sqlcode
    @begin_time = begin_time
    @sqlerrm = sqlerrm
  end
end   

this_log = nil
last_log = nil
this_time = nil

thisTime = nil

$stdin.each() { |line|

  next if $stdin.eof

  line.chomp!

  if line =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)/ then

    thisTime = Time.local(*ParseDate.parsedate(line))

  elsif line =~ /^ORA-([0-9]+)/

    sqlcode = $1
    sqlerrm = line

    this_log = OraErr.new(sqlcode,thisTime,sqlerrm)

    if (last_log!=nil)

      # print the last log
      print \
          last_log.begin_time.strftime(date_mask),delimiter,
          last_log.sqlerrm,"\n" 

    end

    last_log = this_log
    this_log = nil

  end

}


2.遠端alert log
#scan_remote.rb
#usage:ruby scan_remote.rb > scan.txt
require 'rubygems'
require 'net/ssh'
require 'parsedate'

delimiter = "\t" 
date_mask = '%Y-%m-%d %H:%M:%S'

months = { 'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6,
           'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12 }

class OraErr
  attr_reader :sqlcode, :begin_time, :sqlerrm
  def initialize(sqlcode,begin_time,sqlerrm)
    @sqlcode = sqlcode
    @begin_time = begin_time
    @sqlerrm = sqlerrm
  end
end   

this_log = nil
last_log = nil
this_time = nil

thisTime = nil

Net::SSH.start('192.168.0.1', 'oracle',
                   :password=>'oraclepass') do |session|
  cmd = 'cat /orasys/admin/sid/bdump/alert_sid.log'
  #session.exec(cmd).each()
  session.open_channel do |channel|
    channel.on_data do |ch, data|
      data.each_line do |line|
        line.chomp!        
        if line =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)/ then    
          begin
            thisTime = Time.local(*ParseDate.parsedate(line)) 
          rescue # 忽略無效日期
            next
          end    
          #print \
          #ParseDate.parsedate(line) ,line,"\n" 
        elsif line =~ /^ORA-([0-9]+)/
          sqlcode = $1
          sqlerrm = line
          this_log = OraErr.new(sqlcode,thisTime,sqlerrm)    
          if (last_log!=nil)      
            # print the last log
            print \
              last_log.begin_time.strftime(date_mask),delimiter,
              last_log.sqlerrm,"\n"    
          end    
          last_log = this_log
          this_log = nil    
        end # line  
      end # data
    end # channel
    channel.exec cmd
  end # session    
  session.loop
end #NET
分享到:
评论
1 楼 wuhuizhong 2011-11-04  
Ruby 1.9

require 'date/format'
require 'time'

text = "Tue Jun 28 11:58 2011"
array = Date._parse(text, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :wday)
time = Time.mktime(*array)
puts time

相关推荐

Global site tag (gtag.js) - Google Analytics