Skip to content

Instantly share code, notes, and snippets.

@devn
Created March 29, 2010 03:25
Show Gist options
  • Select an option

  • Save devn/347339 to your computer and use it in GitHub Desktop.

Select an option

Save devn/347339 to your computer and use it in GitHub Desktop.
(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