/
core.clj
191 lines (170 loc) · 6.07 KB
/
core.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(ns ^{:doc "Access log to json converter"
:author "Jitendra Takalkar"}
log2json.core
(:gen-class)
(:use [clojure.data.json :only (json-str write-json read-json)])
(:import [java.io BufferedReader FileReader BufferedWriter FileWriter PrintWriter]))
(def re-expn #"^([\d.]+) (\S+) (\S+) \[([\w:/]+\s[+\-]\d{4})\] \"(.+?)\" (\d{3}) (\d+) \"(.+?)\" \"(.+?)\" (\d+) (\d+) (\d+)")
(def servers (atom '()))
(def module-names '("abonnement"
"adresse"
"afsaetning"
"kunde"
"logistik"
"ordre"
"produkt"
"provisionering"))
(defn init-servers
"Initialize the servers list"
[^Integer count]
(loop [i count]
(if (= i 0)
@servers
(recur (do (swap! servers conj i) (dec i))))))
(def status-codes '("200" "201" "204" "207" "400" "403" "404" "406" "500" "503"))
(def json-map {:chart {:renderTo "container"
:plotBackgroundColor "none"
:backgroundColor "none"
:defaultSeriesType "column"}
:credits {:enabled false}
:xAxis {:categories (vec status-codes)}
:yAxis {:gridLineColor "rgba(255,255,255,0.05)"
:title {:text "SERVER HITS"}}
:tooltip {:crosshairs true
:shared true}
:subtitle {:text "KASIA2"}
:plotOptions {:column {:pointPadding 0.2
:borderWidth 0}}})
(defn init-status-code-map
"Returns the initial map for status codes"
[]
(loop [s-map {} s-codes status-codes]
(if (empty? s-codes)
s-map
(recur (assoc s-map (first s-codes) 0) (rest s-codes)))))
(defn init-server-keys
[^String module-name]
(loop [rvec [] s @servers]
(if (empty? s)
rvec
(recur (conj rvec (str module-name ":" (first s)))
(rest s)))))
(defn init-map-keys
"Initialize the map keys"
[]
(loop [rvec [] modules module-names]
(if (empty? modules)
rvec
(recur (into rvec (init-server-keys (first modules)))
(rest modules)))))
(defn init-column-map
"This return the initial data structure for column graph"
[]
(let [s-counts (init-status-code-map)]
(loop [rmap {} keys (init-map-keys)]
(if (empty? keys)
rmap
(recur (assoc rmap (first keys) s-counts)
(rest keys))))))
(defn skip-line?
[^String line]
(or (= "" (.trim line))
(.contains line "alive.html")
(.contains line "alive?")
(.contains line "/docs")
(not (or (.contains line "abonnement")
(.contains line "adresse")
(.contains line "afsaetning")
(.contains line "kunde")
(.contains line "logistik")
(.contains line "ordre")
(.contains line "produkt")
(.contains line "provisionering")))
(nil? (re-matches re-expn line))))
(defn get-module-name
"Return module name"
[^String token]
(let [tokens1 (.split token " ")
token (get tokens1 1)
tokens2 (.split token "/")
cnt (count tokens2)]
(if (== 0 cnt)
token
(.replace (get tokens2 1) "-v1" ""))))
(defn parse-log-line
"Returns map object after parsing the line"
[^Integer i ^String line]
(let [tokens (re-find re-expn line)
request-uri (get tokens 5)
status-code (get tokens 6)
module-name (get-module-name request-uri)
key (str (.toLowerCase module-name) ":" i)]
[key status-code]))
(defn parse-log-file
"Returns an array of Lines of log file"
[^Integer i r-map ^String file-path]
(with-open [fr (FileReader. file-path)
br (BufferedReader. fr)]
(loop [rmap r-map
lines (line-seq br)]
(if (empty? lines)
rmap
(if (skip-line? (first lines))
(recur rmap (rest lines))
(let [line-vec (parse-log-line i (first lines))
tcount (get-in rmap line-vec)
acount (if (nil? tcount) 0 tcount)]
(recur (assoc-in rmap line-vec (inc acount))
(rest lines))))))))
(defn parse-log-files
"Returns the map datastructure"
[^String base-path file-names]
(loop [r-map (init-column-map) f-names file-names i 1]
(if (empty? f-names)
r-map
(recur (parse-log-file i r-map (str base-path (first f-names)))
(rest f-names)
(inc i)))))
(defn get-status-code-data
"Return vector"
[m-map]
(loop [rvec [] s-codes status-codes]
(if (empty? s-codes)
rvec
(recur (conj rvec (get m-map (first s-codes)))
(rest s-codes)))))
(defn get-module-data
""
[^String module-name d-map]
(loop [r-vec [] s @servers]
(if (empty? s)
(merge json-map
{:title {:text (.toUpperCase module-name)}
:series r-vec})
(recur (conj r-vec {:name (str "SERVER-" (first s))
:data (get-status-code-data (get d-map (str module-name ":" (first s))))})
(rest s)))))
(defn write-to-file
"Create new File and write the string content."
[^String file-path ^String data]
(with-open [file-writer (FileWriter. file-path)
bf-writer (BufferedWriter. file-writer)
out (PrintWriter. bf-writer)]
(.write out data)))
(defn for-each-module
"Process for each module"
[^String base-path file-names]
(let [d-map (parse-log-files base-path file-names)
out-file-name (str "access_log.json")]
(loop [rpath [] modules module-names]
(if (empty? modules)
rpath
(recur (conj rpath (str base-path (first modules) "_" out-file-name))
(do (write-to-file (str base-path (first modules) "_" out-file-name)
(json-str (get-module-data (first modules) d-map)))
(rest modules)))))))
(defn -main
"Main function"
[^String base-path & file-names]
(init-servers (count file-names))
(for-each-module base-path file-names))