Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. function sleep(sleep_duration)
  2. local t0 = os.time() + sleep_duration
  3. repeat until os.time() > t0
  4. end
  5.  
  6. sleep_duration = 0.2
  7.  
  8. stat = io.open('/proc/stat', 'r')
  9. stat:setvbuf('no')
  10.  
  11. stat:seek('set', 5)
  12. previousStats = stat:read()
  13.  
  14. sleep(sleep_duration)
  15.  
  16. stat:seek('set', 5)
  17. currentStats = stat:read()
  18.  
  19.  
  20. user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice = string.match(currentStats, '(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*)')
  21.  
  22. prevuser, prevnice, prevsystem, previdle, previowait, previrq, prevsoftirq, prevsteal, prevguest, prevguest_nice = string.match(previousStats, '(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*) +(.*)')
  23.  
  24. user = tonumber(user)
  25. nice = tonumber(nice)
  26. system = tonumber(system)
  27. idle = tonumber(idle)
  28. iowait = tonumber(iowait)
  29. irq = tonumber(irq)
  30. softirq = tonumber(softirq)
  31. steal = tonumber(steal)
  32. guest = tonumber(guest)
  33. guest_nice = tonumber(guest_nice)
  34.  
  35. prevuser = tonumber(prevuser)
  36. prevnice = tonumber(prevnice)
  37. prevsystem = tonumber(prevsystem)
  38. previdle = tonumber(previdle)
  39. previowait = tonumber(previowait)
  40. previrq = tonumber(previrq)
  41. prevsoftirq = tonumber(prevsoftirq)
  42. prevsteal = tonumber(prevsteal)
  43. prevguest = tonumber(prevguest)
  44. prevguest_nice = tonumber(prevguest_nice)
  45.  
  46.  
  47. PrevIdle = previdle + previowait
  48. Idle = idle + iowait
  49.  
  50. PrevNonIdle = (prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevsteal)
  51. NonIdle = (user + nice + system + irq + softirq + steal)
  52.  
  53. PrevTotal = (PrevIdle + PrevNonIdle)
  54. Total = (Idle + NonIdle)
  55.  
  56. totald = (Total - PrevTotal)
  57. idled = (Idle - PrevIdle)
  58.  
  59. CPU_Percentage = (totald - idled) / totald * 100
  60.  
  61.  
  62.  
  63. print(tostring(CPU_Percentage))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement