Skip to content

Instantly share code, notes, and snippets.

@simonszu
Last active October 19, 2017 12:26
Show Gist options
  • Save simonszu/7b9a3e9287fe958e56e9889672bf7128 to your computer and use it in GitHub Desktop.
Save simonszu/7b9a3e9287fe958e56e9889672bf7128 to your computer and use it in GitHub Desktop.
Scans a mailserver logfile for very big mails
#! /usr/bin/env ruby
class SizeParse
def initialize(args)
if ((args.length < 2) || (args[0] == "help"))
help()
else
@filename = ARGV[0]
@size = ARGV[1]
parse(@filename, @size)
end
end
def help()
puts <<END_HELP
Email-Groessenfilter
by Simon Szustkowski, ECOFIS GmbH
Nutzung: check-filesize.rb <logfile> <Groesse in Byte>
Output: Mailadressen aller Mails die groesser sind
Anzahl aller gefilterten Mails
END_HELP
end
def parse(filename, size)
counter = 0
hosts = Array.new
counts = Hash.new(0)
f = File.open(filename, "r")
f.each do |line|
if ( line =~ /^.*from=<.*@(.*)>, size=(\d+),.*/ )
host = $1
groesse = $2
# Gesamtcounter erhoehen, Hostname zum Array pushen
if (groesse.to_i >= size.to_i)
counter += 1
hosts.push(host)
end
end
end
# Sort the hostname array
hosts.sort!
# Count each hostname in the array and store the result in a hash
hosts.each{|name| counts[name] += 1}
# Sort the hash
# Output all the things
counts.each{|hostname, count| puts "Host #{hostname}: #{count}"}
puts "Gesamt: #{counter}"
end
end
main = SizeParse.new(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment