44--
55--
66
7+ -- Lua time recordings config
8+ g_LuaTimingRecordings = {
9+ Enabled = true ,
10+ Frequency = 2000 , -- in milliseconds
11+ HistoryLength = 300 , -- number of records to keep
12+ HighCPUResourcesAmount = 10 , -- percentage threshold
13+ }
14+
15+ -- Global variable to store high usage resources similar to IPB
16+ g_HighUsageResources = {}
17+
718-- Browser update
819function setQuery ( counter , user , target , category , options , filter , showClients )
920 local viewer = getViewer (user )
1021 return viewer :setQuery ( counter , target , category , options , filter , showClients )
1122end
23+
24+ -- Date/time formatting function
25+ local function getDateTimeString ()
26+ local time = getRealTime ()
27+ local weekday = ({" Sunday" , " Monday" , " Tuesday" , " Wednesday" , " Thursday" , " Friday" , " Saturday" })[time .weekday + 1 ]
28+ -- Weekday, DD.MM.YYYY, hh:mm:ss
29+ return (" %s, %02d.%02d.%d, %02d:%02d:%02d" ):format (weekday , time .monthday , time .month + 1 , time .year + 1900 , time .hour , time .minute , time .second )
30+ end
31+
32+ -- Save high CPU resources function (based on IPB alarm.lua)
33+ function saveHighCPUResources ()
34+ local columns , rows = getPerformanceStats (" Lua timing" )
35+
36+ if not rows then
37+ return
38+ end
39+
40+ for index , row in pairs (rows ) do
41+ local usageText = row [2 ]:gsub (" [^0-9%.]" , " " )
42+ local usage = math.floor (tonumber (usageText ) or 0 )
43+
44+ if (usage > g_LuaTimingRecordings .HighCPUResourcesAmount ) then
45+ -- Record this high usage to table
46+ table.insert (g_HighUsageResources , 1 , {row [1 ], row [2 ], getDateTimeString ()})
47+
48+ -- Make sure it won't get too big
49+ if # g_HighUsageResources > g_LuaTimingRecordings .HistoryLength then
50+ table.remove (g_HighUsageResources , g_LuaTimingRecordings .HistoryLength )
51+ end
52+ end
53+ end
54+ end
55+
56+ -- Start monitoring timer
57+ if (g_LuaTimingRecordings .Enabled ) then
58+ setTimer (saveHighCPUResources , g_LuaTimingRecordings .Frequency , 0 )
59+ end
0 commit comments