Created
March 29, 2010 03:25
-
-
Save devn/347339 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn find-lines [#^String text logfile] | |
| (filter | |
| (fn [#^String line] | |
| (< 0 (.indexOf line text))) | |
| (line-seq (reader logfile)))) | |
| (defn of-IPs [coll] | |
| (flatten | |
| (remove empty? | |
| (pmap | |
| #(rest | |
| (re-find | |
| #"Deny tcp src outside:(\d+\.\d+\.\d+\.\d+)" %)) | |
| coll)))) | |
| ;; "-agentlib:hprof=cpu=samples,depth=6,force=n,thread=y,verbose=n" | |
| ;; TODO: check profiler in jvisualvm | |
| (defn count-ips [logfile] | |
| (remove empty? | |
| (persistent! | |
| (reduce | |
| #(assoc! %1 %2 (inc (get %1 %2 0))) | |
| (transient {}) | |
| (of-IPs | |
| (find-lines "Deny tcp src outside" logfile)))))) | |
| (defn add-country-codes [logfile] | |
| (persistent! | |
| (reduce | |
| (fn [m [ip cnt]] | |
| (assoc! m ip {:count cnt | |
| :country (try | |
| (country-name ip) | |
| (catch Exception e "NO COUNTRY"))})) | |
| (transient {}) | |
| (count-ips logfile)))) | |
| ;; Serialization | |
| (defn serialize-to-file [filename logfile] | |
| (spit | |
| (java.io.File. (str *project-root* "serialized/" filename ".sclj")) | |
| (pr-str (add-country-codes logfile)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment