Last active
May 13, 2018 16:33
-
-
Save alexeypegov/15306b13faa05107e25277dd4f57bacf to your computer and use it in GitHub Desktop.
Extruct java thread dumps from a log file
This file contains 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
#!/usr/bin/env inlein | |
'{:dependencies [[org.clojure/clojure "1.9.0"]]} | |
(require '[clojure.string :as s] | |
'[clojure.java.io :as io]) | |
(def filename (first *command-line-args*)) | |
(when-not filename | |
(println "Usage:" (System/getProperty "$0") "logfile") | |
(System/exit 1)) | |
(defn write-dump | |
[index lines] | |
(let [dump (s/join \newline lines) | |
filename (format "dump-%d.tdump" index)] | |
(println filename) | |
(with-open [w (io/writer (io/file filename))] | |
(.write w dump)))) | |
(defn parse-log | |
[path] | |
(let [lines (s/split-lines (slurp path))] | |
(loop [lines lines | |
dumps [] | |
current [] | |
prev nil] | |
(if lines | |
(let [line (first lines)] | |
(cond | |
(s/starts-with? line "Full thread dump Java") (recur (next lines) dumps (conj [] prev line) line) | |
(s/starts-with? line " class space") (recur (next lines) (conj dumps (conj current line)) [] line) | |
:else (if (empty? current) (recur (next lines) dumps [] line) (recur (next lines) dumps (conj current line) line)))) | |
dumps)))) | |
(doall (map-indexed (fn [index dump] (write-dump index dump)) (parse-log filename))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment