-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.clj
More file actions
166 lines (136 loc) · 4.78 KB
/
stats.clj
File metadata and controls
166 lines (136 loc) · 4.78 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(ns re-mote.zero.stats
"General machine stats"
(:require
re-mote.repl.base
[re-cog.scripts.stats :refer (net-script cpu-script free-script load-script du-script entropy-script)]
[re-cog.zero.scheduled :refer (get-scheduled-result)]
[clojure.core.strint :refer (<<)]
[clojure.string :refer (split split-lines)]
[re-mote.zero.pipeline :refer (run-hosts schedule-hosts)]
[taoensso.timbre :refer (refer-timbre)]
[com.rpl.specter :as s :refer (transform select MAP-VALS ALL pred ATOM keypath multi-path)]
[re-share.time :refer (local-now)]
[re-cog.scripts.common :refer (shell-args shell)]
[re-share.schedule :refer (watch seconds)])
(:import re_mote.repl.base.Hosts))
(refer-timbre)
(defn space [line]
(split line #"\s"))
(defn comma [line]
(split line #","))
(defn zipped [parent k ks by {:keys [result] :as m}]
(let [lines (split-lines (result :out))
ms (mapv (fn [line] (zipmap ks (by line))) lines)]
(assoc-in m [parent k]
(if (> (count ms) 1) ms (first ms)))))
(defn zip
"Collecting output into a hash, must be defined outside protocoal because of var args"
[this [parent k & ks] {:keys [success failure] :as res}]
(let [by (or (first (filter fn? ks)) space)
success' (map (partial zipped parent k (filter keyword? ks) by) success)]
[this (assoc (assoc res :success success') :failure failure)]))
(def readings (atom {}))
(defn safe-dec [v]
(try
(bigdec v)
(catch Throwable e
(error (<< "failed to convert ~{v} into big decimal") e))))
(def single-nav
[:success ALL :stats MAP-VALS MAP-VALS])
(defn multi-nav [& ks]
[:success ALL :stats MAP-VALS ALL (apply multi-path ks)])
(defn into-dec
([v]
(into-dec single-nav v))
([nav [this readings]]
[this (transform nav safe-dec readings)]))
(defn reset
"reset a key in readings"
[k]
(transform [ATOM MAP-VALS MAP-VALS] (fn [m] (dissoc m k)) readings))
(defn select-
"select a single key from readings"
[k]
(select [ATOM MAP-VALS MAP-VALS (keypath k)] readings))
(defn last-n
"keep last n items of a sorted map"
[n m]
(let [v (vec (into (sorted-map) m)) c (count v)]
(if (< c n) m (into (sorted-map) (subvec v (- c n) c)))))
(def timeout [5 :second])
(def scripts {:free free-script
:net net-script
:cpu cpu-script
:entropy entropy-script
:du du-script
:load load-script})
(defprotocol Stats
(du [this] [this m])
(entropy [this] [this m])
(net [this] [this m])
(cpu [this] [this m])
(free [this] [this m])
(load-avg [this] [this m])
(collect [this m])
(sliding [this m f k])
(schedule [this k n capacity]))
(defn split-results [{:keys [success] :as m}]
(assoc m :success
(flatten (map (fn [{:keys [result] :as r}]
(map (fn [{:keys [exit] :as v}] (assoc r :result v :code exit)) result)) success))))
(defn normalize [this m & ks]
(->> m
split-results
(zip this ks)
into-dec))
(extend-type Hosts
Stats
(du
([this]
(into-dec
(multi-nav :blocks :used :available)
(zip this [:stats :du :filesystem :type :blocks :used :available :perc :mount]
(split-results (run-hosts this get-scheduled-result [:du] timeout)))))
([this _]
(du this)))
(entropy
([this]
(normalize this (run-hosts this get-scheduled-result [:entropy] timeout) :stats :entropy :available))
([this _]
(entropy this)))
(net
([this]
(normalize this (run-hosts this get-scheduled-result [:net] timeout)
:stats :net :rxpck/s :txpck/s :rxkB/s :txkB/s :rxcmp/s :txcmp/s :rxmcst/s :ifutil))
([this _]
(net this)))
(cpu
([this]
(normalize this (run-hosts this get-scheduled-result [:cpu] timeout) :stats :cpu :usr :sys :idle))
([this _]
(cpu this)))
(free
([this]
(normalize this (run-hosts this get-scheduled-result [:free] timeout) :stats :free :total :used :free :shared :buff-cache :available))
([this _]
(free this)))
(load-avg
([this]
(normalize this (run-hosts this get-scheduled-result [:load] timeout) :stats :load :one :five :fifteen :cores))
([this _]
(load-avg this)))
(collect [this {:keys [success] :as m}]
(doseq [{:keys [host stats]} success]
(doseq [[k v] stats]
(swap! readings update-in [host k :timeseries]
(fn [m] (if (nil? m) (sorted-map (local-now) v) (assoc m (local-now) v))))))
[this m])
(schedule
([this k n capacity]
(schedule-hosts this shell (shell-args (scripts k) :cached? true) [k n capacity]))))
(defn purge [n]
(transform [ATOM MAP-VALS MAP-VALS MAP-VALS] (partial last-n n) readings))
(defn refer-stats []
(require '[re-mote.zero.stats :as stats :refer (load-avg net cpu free du entropy collect)]))
(comment
(reset! readings {}))