Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sleep(sleep_duration)
- local t0 = os.time() + sleep_duration
- repeat until os.time() > t0
- end
- sleep_duration = 0.2
- stat = io.open('/proc/stat', 'r')
- stat:setvbuf('no')
- stat:seek('set', 5)
- previousStats = stat:read()
- sleep(sleep_duration)
- stat:seek('set', 5)
- currentStats = stat:read()
- user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice = string.match(currentStats, '(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*)')
- prevuser, prevnice, prevsystem, previdle, previowait, previrq, prevsoftirq, prevsteal, prevguest, prevguest_nice = string.match(previousStats, '(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*)')
- user = tonumber(user)
- nice = tonumber(nice)
- system = tonumber(system)
- idle = tonumber(idle)
- iowait = tonumber(iowait)
- irq = tonumber(irq)
- softirq = tonumber(softirq)
- steal = tonumber(steal)
- guest = tonumber(guest)
- guest_nice = tonumber(guest_nice)
- prevuser = tonumber(prevuser)
- prevnice = tonumber(prevnice)
- prevsystem = tonumber(prevsystem)
- previdle = tonumber(previdle)
- previowait = tonumber(previowait)
- previrq = tonumber(previrq)
- prevsoftirq = tonumber(prevsoftirq)
- prevsteal = tonumber(prevsteal)
- prevguest = tonumber(prevguest)
- prevguest_nice = tonumber(prevguest_nice)
- PrevIdle = previdle + previowait
- Idle = idle + iowait
- PrevNonIdle = (prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevsteal)
- NonIdle = (user + nice + system + irq + softirq + steal)
- PrevTotal = (PrevIdle + PrevNonIdle)
- Total = (Idle + NonIdle)
- totald = (Total - PrevTotal)
- idled = (Idle - PrevIdle)
- CPU_Percentage = (totald - idled) / totald * 100
- print(tostring(CPU_Percentage))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement