mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-03 09:20:10 +00:00
Implements CPU states estimator as a system user script
This commit is contained in:
parent
7b6ce10940
commit
91ad5f05cd
1 changed files with 51 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ local ts_utils = require("ts_utils_core")
|
|||
local user_scripts = require("user_scripts")
|
||||
local ts_utils = require("ts_utils_core")
|
||||
local storage_utils = require("storage_utils")
|
||||
local alerts_api = require("alerts_api")
|
||||
|
||||
local script = {
|
||||
-- Script category
|
||||
|
|
@ -37,4 +38,54 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
-- Computes the CPU states in percentage
|
||||
function script.hooks.min(params)
|
||||
if not ntop.isWindows() then
|
||||
local f = io.open("/proc/stat", "r")
|
||||
local res
|
||||
|
||||
if f then
|
||||
-- The first line of the file contains cpu jiffies
|
||||
-- See http://man7.org/linux/man-pages/man5/proc.5.html for the meaning of each column
|
||||
local cpu = f:read("*line")
|
||||
|
||||
if not isEmptyString(cpu) then
|
||||
-- Parse the cpu string using an sscanf-like pattern
|
||||
local user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice = cpu:match("cpu%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)")
|
||||
local states = {user = user, nice = nice, system = system, idle = idle, iowait = iowait, softirq = softirq, steal = steal, guest = guest, guest_nice = guest_nice}
|
||||
|
||||
-- Compute the deltas with reference to the previous values
|
||||
local delta_total = 0
|
||||
local delta_states = {}
|
||||
for state, state_total in pairs(states) do
|
||||
local delta = alerts_api.interface_delta_val(script.key..state, params.granularity, state_total or 0)
|
||||
delta_states[state] = delta
|
||||
delta_total = delta_total + delta
|
||||
end
|
||||
|
||||
-- Compute the deltas in percent
|
||||
local delta_total_pct = 0
|
||||
local delta_states_pct = {}
|
||||
for state, state_delta in pairs(delta_states) do
|
||||
local delta_pct = state_delta / delta_total * 100
|
||||
delta_states_pct[state] = delta_pct
|
||||
delta_total_pct = delta_total_pct + delta_pct
|
||||
end
|
||||
|
||||
-- tprint(cpu)
|
||||
-- tprint(states)
|
||||
-- tprint(delta_states)
|
||||
-- tprint(delta_states_pct)
|
||||
-- tprint(delta_total_pct)
|
||||
|
||||
-- Cache the cpu value so it can be re-read during next call
|
||||
end
|
||||
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
return(script)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue