import std.algorithm; import std.regex; import std.stdio; import std.string; /* Parses mail logs like this line: Nov 1 19:37:59 thelocalhost sendmail[10620]: [ID 801593 mail.info] pA20bx2u010620: from=, size=6988, class=0, nrcpts=2, msgid=<5693296.131.1320194279221.JavaMail.gfish@somehost>, proto=ESMTP, daemon=MTA, relay=somehost.bar.com [1.2.3.4] Looks for the relay lines and stores the number of times a particular relay appears. Only relays matching foo.com and bar.corp are stored. At the end the list of relays is displayed along with the number of times that relay appeared in the log. */ void main (string[] args) { int[string] relayHosts; auto regex = regex(r"relay=([0-9a-zA-Z\-\.]+[\w]+)[\.\,]*\s"); foreach (arg; args[1 .. args.length]) { auto file = File(arg, "r"); foreach(char[] line ; file.byLine) { // Find the relay portion of the string (if any) foreach(m; match(line, regex)) { auto hostName = m.captures[1]; if(endsWith(hostName, "foo.com") || endsWith(hostName, "bar.corp")) { relayHosts[hostName.idup]++; } } } file.close(); } /* Sort the host names */ auto hostKeys = relayHosts.keys; sort(hostKeys); /* Display the results */ writeln("Host, Number of relayed emails"); foreach(relay ; hostKeys) { writeln(relay, ",", relayHosts[relay]); } }