This post originated from an RSS feed registered with Ruby Buzz
by Eric Hodel.
Original Post: Graphing Spam
Feed Title: Segment7
Feed URL: http://blog.segment7.net/articles.rss
Feed Description: Posts about and around Ruby, MetaRuby, ruby2c, ZenTest and work at The Robot Co-op.
For fun I decided to chart various statistics from my mail server’s logs. I ended up with these charts using John Barnette’s GChart, some regular expressions, and cron.
The details of parsing out the data from the log files and setting up a crontab is boring, so I’ll spare you the details of that.
Using GChart is really cool and easy. Here’s what I used to generate the Amavis Statistics chart, the others are all nearly the same, just different labels and titles.
def graph_amavis(data)
# These are the labels and colors I'm using
labels = %w[Banned Spam Spammy Bad\ Header Clean]
colors = %w[000000 ff0000 ff7f00 ffff00 00ff00]
max = data.map { |vals| vals.max }.max
# Since axis labels aren't yet supported by the API in 0.2.0, I use :extras
extras = { 'chxt' => 'r', 'chxl' => "0:|#{axis_labels max}" }
chart = GChart.line :title => 'Amavis Statistics', :data => data,
:labels => labels, :colors => colors, :extras => extras
chart_path = File.join File.dirname(@file), 'amavis_statistics.png'
chart.size = '750x400'# can't be > 300,000 pixels
chart.write chart_path
end
That’s it! One minor gotcha I had was that I needed to transpose the data set after reading it in because it wasn’t in the right order for GChart to consume it, but with Array#transpose, it’s just an extra method call before graphing.
Also, I wrote this simple utility function to calculate some good-enough axis labels:
def axis_labels(max)
axis_labels = []
0.upto 10 do |i| axis_labels << (max * 0.1 * i).to_i end
axis_labels.join '|'
end
The values aren’t prettily chosen, but they work well enough.