-
Notifications
You must be signed in to change notification settings - Fork 1
/
reportingReason-gui.lua
321 lines (239 loc) · 8.56 KB
/
reportingReason-gui.lua
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
-- Generates percentage statistics of 3GPP-Reporting-Reason, for example
--
-- FINAL (2) - 381 (8.2129769346842 %)
-- QHT (1) - 267 (5.7555507652511 %)
-- VALIDITY_TIME (4) - 3632 (78.292735503341 %)
-- QUOTA_EXHAUSTED (3) - 359 (7.7387367967234 %)
--
-- Lua functions for the Plugin.
--
plugin_version = "1.0.0"
year = "2020"
--
-- Display version info for Wireshark
--
local plugin_info = {
version = plugin_version,
author = "Jarek Hartman",
repository = "https://jhartman.pl"
}
set_plugin_info(plugin_info)
--
-- Configuration
--
local histogramSteps = 25
local win = nil
--
-- Display plugin message. This will either go to the
-- GUI text window or just print to stdout.
--
function message(message)
-- handle either gui or non gui mode
if gui_enabled() == true and win ~= nil then
-- declare window if not yet there
if win == nil then
statistics_win()
end
win:append(message .. '\n')
else
print(message)
end
end
function median (numlist)
if type(numlist) ~= 'table' then return numlist end
if #numlist == 0 then return 0 end
table.sort(numlist)
if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end
return numlist[math.ceil(#numlist/2)]
end
function average (numlist)
if type(numlist) ~= 'table' then return numlist end
if #numlist == 0 then return 0 end
local total = 0
for index, value in ipairs(numlist) do
total = total + value
end
return total / #numlist
end
function min(numlist)
if type(numlist) ~= 'table' then return numlist end
if #numlist == 0 then return 0 end
local number = nil
for index, value in ipairs(numlist) do
if number == nil then
number = value
end
if value <= number then
number = value
end
end
return number
end
function max(numlist)
if type(numlist) ~= 'table' then return numlist end
if #numlist == 0 then return 0 end
local number = 0
for index, value in ipairs(numlist) do
if value >= number then
number = value
end
end
return number
end
function histogram(numlist)
local histo = {}
local maximum = max(numlist)
local factor = 1024*1024
local stepSize = maximum / histogramSteps
for i = 1, histogramSteps do
histo[i] = 0
end
for index, value in ipairs(numlist) do
local step = math.ceil(value / stepSize)
histo[step] = histo[step] + 1 or 0
end
message("Histogram of USU (Validity-Time)")
local currentStep = 0
for index, value in ipairs(histo) do
message(string.format("%2d: %9d MB - %9d MB : %6d (%3.2f %%)", index, currentStep/factor, (currentStep + stepSize+1)/factor, value , value/(#numlist)*100))
currentStep = currentStep + stepSize
end
end
--
-- Output window
--
local function statistics_win()
-- Declare the window we will use
win = TextWindow.new("Diameter Statistics")
local function remove()
-- this way we remove the listener that otherwise will remain running indefinitely
tap:remove();
end
win:set_atclose(remove)
end
local function report(reportingReasons, totalOctets, total)
if (totalOctets == nil) then
message('')
message('--------------------------------------')
message("Result Codes:")
else
message('--------------------------------------')
message("Reporting Reasons:")
end
for k, v in pairs(reportingReasons) do
local percentage = string.format("%.3f", (tostring(v)/total*100))
message(string.format("%-30s", k) .. " - " .. tostring(v) .. ' \t\t(' .. percentage .. ' %)')
end
message('')
message(string.format("%-30s", 'Total') .. " - " .. tostring(total) .. ' \t\t(' .. 100 .. ' %)')
message('')
if not (totalOctets == nil) then
message("USU (octets) when VALIDITY_TIME (4)")
local median = median(totalOctets)
local average = average(totalOctets)
local min = min(totalOctets)
local max = max(totalOctets)
message(string.format("Median : %d octets, %.2f kB", median, median / 1024))
message(string.format("Average : %d octets, %.2f kB", average, average / 1024))
message(string.format("Min : %d octets", min))
message(string.format("Max : %d octets, %.2f kB", max, max / 1024))
end
-- histogram(totalOctets)
end
------------------------------------
--
-- Here start of the plugin
--
------------------------------------
do
local rrField = Field.new("diameter.3GPP-Reporting-Reason")
local toField = Field.new("diameter.CC-Total-Octets")
local rcField = Field.new("diameter.Result-Code")
local total = 0
local totalRC = 0
local function init_listener()
-- Hash of reportingReasons
local reportingReasons = {}
local resultCodes = {}
local totalOctets = {}
local filter = 'diameter.3GPP-Reporting-Reason'
if gui_enabled() then
statistics_win()
end
message("Registering Listener")
tap = Listener.new("diameter")
-- this function will be called once for each packet
function tap.packet(pinfo, tvb, tapdata)
local rrFields = {rrField()}
local toFields = {toField()}
-- print(string.format("Loop begins for #rrFields: %d, #toFields %d", #rrFields, #toFields))
for index, _ in pairs(rrFields) do
local to = 0
-- Retrieve Reporting Reason and corresponing Total-Octets
-- Note, this may *not* always work
local rr = rrFields[index].display
if index <= #toFields then
to = toFields[index].display
end
-- print(string.format(" %s - %s - %s (octets)", index, rr, to))
reportingReasons[rr] = (reportingReasons[rr] or 0 ) + 1
if rr == 'VALIDITY_TIME (4)' and to ~= nil then
-- print(string.format("%d - %d", pinfo.number, to))
-- print("RR:" .. rr)
table.insert(totalOctets, tonumber(to))
end
total = total + 1
end
end
--------------------------------------
local filterRC = 'diameter.Result-Code'
tap = Listener.new("diameter", filterRC)
-- this function will be called once for each packet
function tap.packet(pinfo, tvb, tapdata)
local rcFields = {rcField()}
for indexRC, _ in pairs(rcFields) do
local to = 0
local rc = rcFields[indexRC].display
if indexRC <= #rcFields then
to = rcFields[indexRC].display
end
resultCodes[rc] = (resultCodes[rc] or 0 ) + 1
totalRC = totalRC + 1
end
end
-----------------------------
function tap.draw(t)
if gui_enabled() then
win:clear()
if total > 0 then
report(reportingReasons, totalOctets, total)
report(resultCodes, nil, totalRC)
end
end
end
-- a listener tap's reset function is called at the end of a live capture run,
-- when a file is opened, or closed. TShark never appears to call it.
function tap.reset()
if gui_enabled() and win ~= nil then
win:clear()
end
report(reportingReasons, totalOctets, total)
report(resultCodes, nil, totalRC)
reportingReasons = {}
totalOctets = {}
total = 0
end
if gui_enabled() then
retap_packets()
end
end
if gui_enabled() == true then
-- Starting in GUI mode
register_menu("Statistics/Diameter Statistics", init_listener, MENU_TOOLS_UNSORTED)
-- Call the init function to get things started.
else
message("Starting in command-line mode")
-- Call the init function to get things started.
init_listener()
end
end