Guest User

Untitled

a guest
Mar 7th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'optparse'
  4. require 'ostruct'
  5. require 'parsedate'
  6.  
  7. @my_args = ARGV.empty? ? [ "-h" ] : ARGV
  8.  
  9. @options = OpenStruct.new
  10. opts = OptionParser.new
  11. opts.on("-d","--directory DIRECTORY", String, "Directory full of log files to parse") { |val| @options.dir = val }
  12. opts.on("-t","--to ADDRESS", String, "Find emails sent to a specific address") { |val| @options.to = val }
  13. opts.on("-f","--from ADDRESS", String, "Find email sent from a specific address") { |val| @options.from = val }
  14. opts.on("-s","--show-detail", "Output detail instead of summary information") { |val| @options.detail = val }
  15. opts.on("-z=","--time-zone=", "Offset in hours from UTC") { |val| @options.tz }
  16. opts.on_tail("-h", "--help", "Show this message") do
  17. puts opts
  18. exit
  19. end
  20. opts.parse!(@my_args)
  21.  
  22. puts "Exchange 2003 Log Analyzer v.1\n" if !@options.detail
  23. puts "Finding all messages sent to: '#{@options.to}'" if @options.to && !@options.detail
  24. puts "Finding all messages sent from: '#{@options.from}'" if @options.from && !@options.detail
  25.  
  26. @smtp_in = 0
  27. @smtp_out = 0
  28. @local_deliver = 0
  29. @msgids = Array.new
  30. @options.tz = -7 if !@options.tz
  31.  
  32. # this function calculates the various statistics -
  33. # messages are tracked so that they are not counted more than once
  34. def process_line(line)
  35. msgid = line[/^([^\t]+\t){9}([^\t]+)/, 2]
  36.  
  37. if !@msgids.index(msgid)
  38. if line =~ /^([^\t]+\t){8}1019/ then @smtp_in += 1
  39. elsif line =~ /^([^\t]+\t){8}1031/ then @smtp_out += 1
  40. elsif line =~ /^([^\t]+\t){8}1023/ then @local_deliver += 1
  41. else
  42. # make sure that the current msgid is not added to the msgids array
  43. return
  44. end
  45.  
  46. # we only keep track of the last 50 msgid's processed -
  47. # this should be sufficient to ensure an accurate record count
  48. @msgids << msgid
  49. @msgids.shift if @msgids.length > 50
  50.  
  51. # dump detail information to standard output if the -s flag is set
  52. if @options.detail
  53. sdate, stime = line.match(/^([\d]{4}-[\d]{1,2}-[\d]{1,2})\t([\d]{1,2}:[\d]{1,2}:[\d]{1,2})/).captures
  54. res = ParseDate.parsedate(sdate + ' ' + stime)
  55. utc = Time.utc(*res)
  56. local = utc - (@options.tz * 60 * 60)
  57. datestr = local.strftime('%m-%d-%Y %I:%M:%S %p')
  58.  
  59. data = line.split("\t")
  60. puts '"'+datestr+'","'+data[19]+'","'+data[7]+'","'+data[18]+'"'
  61. end
  62. end
  63. end
  64.  
  65. # process each .log file in the directory
  66. Dir.glob(@options.dir+'/*.log') do |entry|
  67. puts "Processing file: #{entry}\n" if !@options.detail
  68. if @options.to && @options.from
  69. regexp_to = /^([^\t]+\t){7}#{@options.to}/
  70. regexp_from = /^([^\t]+\t){19}#{@options.from}/
  71. File.open(entry, 'r').grep(regexp_to).grep(regexp_from) { |line| process_line(line) }
  72. elsif @options.to
  73. regexp = /^([^\t]+\t){7}#{@options.to}/
  74. File.open(entry, 'r').grep(regexp) { |line| process_line(line) }
  75. elsif @options.from
  76. regexp = /^([^\t]+\t){19}#{@options.from}/
  77. File.open(entry, 'r').grep(regexp) { |line| process_line(line) }
  78. else
  79. File.open(entry, 'r').each { |line| process_line(line) }
  80. end
  81. end
  82.  
  83. puts "#{@smtp_in} inbound total"
  84. puts "#{@smtp_out} outbound total"
  85. puts "#{@local_deliver} local messages"
Add Comment
Please, Sign In to add comment