Created
June 28, 2017 17:44
-
-
Save hiredman/9d1607b68ed3cf3c101d1cdbc8b6c664 to your computer and use it in GitHub Desktop.
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
(require '[clojure.java.shell :as shell] | |
'[clojure.java.io :as io]) | |
(import '(java.util.concurrent SynchronousQueue)) | |
(defn pings [host output] | |
(future | |
(let [proc (.start | |
(ProcessBuilder. | |
["ping" "-Dni" "120" host]))] | |
(try | |
(let [i (.getInputStream proc) | |
r (io/reader i)] | |
(doseq [l (line-seq r) | |
:when (.contains l "time=") | |
:let [split-line (.split l " ") | |
at (Double/parseDouble | |
(subs (first split-line) 1 (dec (count (first split-line))))) | |
time (Double/parseDouble | |
(last (.split (last (butlast split-line)) "=")))]] | |
(.put output {:host host | |
:at at | |
:time time}))) | |
(finally | |
(.destroy proc)))))) | |
(defmacro with-resources [& body] | |
`(binding [*resources* (atom ())] | |
(try | |
~@body | |
(finally | |
(doseq [r# @*resources*] | |
(.close r#)))))) | |
(let [sql (.start | |
(ProcessBuilder. | |
["psql" "-nq"])) | |
q (SynchronousQueue.)] | |
(pings "192.168.1.1" q) | |
(pings "comcast.net" q) | |
(pings "8.8.8.8" q) | |
(pings "4.2.2.1" q) | |
(pings "127.0.0.1" q) | |
(pings "oz.local" q) | |
(with-open [sql-stream (.getOutputStream sql) | |
sql-writer (io/writer sql-stream)] | |
(while true | |
(let [{:keys [host at time]} (.take q) | |
_ (println "inserting" host at time) | |
cmd (format "INSERT INTO ping VALUES ('%s', to_timestamp(%s) AT TIME ZONE 'PST',INTERVAL '%s ms');\n" | |
host | |
at | |
time)] | |
(.write sql-writer cmd) | |
(.flush sql-writer))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment