/
resource_tracker.clj
46 lines (40 loc) · 1.48 KB
/
resource_tracker.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns nl.openweb.test.resource-tracker
(:require [clj-docker-client.core :as docker]))
(def bytes-in-mb (* 1024. 1024.))
(defn init
[uri container-name]
(let [docker-conn (docker/connect {:uri uri
:connect-timeout 1000
:read-timeout 5000
:write-timeout 5000
:call-timeout 30000})
containers (docker/client {:category :containers :conn docker-conn})
stats-f (fn [] (docker/invoke containers {:op :ContainerStats :params {:id container-name :stream false}}))]
(stats-f)
stats-f))
(defn- get-cpu-pct
[stats]
(let [old (:precpu_stats stats)
new (:cpu_stats stats)
container-use (- (get-in new [:cpu_usage :total_usage]) (get-in old [:cpu_usage :total_usage]))
system-use (- (:system_cpu_usage new) (:system_cpu_usage old))]
(double (* 100 (/ container-use system-use)))))
(defn- get-mem-mib
[stats]
(double (/
(- (get-in stats [:memory_stats :usage])
(get-in stats [:memory_stats :stats :total_cache]))
bytes-in-mb)))
(defn- get-usage
[stats-f]
(let [stats (stats-f)
cpu-pct (get-cpu-pct stats)
mem-mib (get-mem-mib stats)]
{:cpu-pct cpu-pct :mem-mib mem-mib}))
(defn safe-get-usage
[stats-f]
(try
(get-usage stats-f)
(catch Exception error
(println "An error occurred" (.toString error))
{})))