Logic Machine Forum
CPU Load Log as Trend - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: CPU Load Log as Trend (/showthread.php?tid=2233)



CPU Load Log as Trend - manos@dynamitec - 10.09.2019

Hello everyone,

Does anyone know how could I get the CPU load as a 2byte value (virtual object) and then make a Trend Log with a resolution of 1sec or log by delta change = 1% of CPU usage?

What I need to achieve is a historic data log from the CPU usage.

Kind regards and thank you for any help in advance


RE: CPU Load Log as Trend - Daniel - 10.09.2019

This will give you load and memory
Code:
data = io.readfile('/proc/meminfo')
load = io.readfile('/proc/loadavg'):split(' ')[ 1 ]

memtotal = data:match('MemTotal:%s+(%d+)')
memavail = data:match('MemAvailable:%s+(%d+)')
memusage = math.floor((memtotal - memavail) / memtotal * 1000) / 10

log(tonumber(load), memusage)



RE: CPU Load Log as Trend - manos@dynamitec - 10.09.2019

(10.09.2019, 13:37)Daniel. Wrote: This will give you load and memory
Code:
data = io.readfile('/proc/meminfo')
load = io.readfile('/proc/loadavg'):split(' ')[ 1 ]

memtotal = data:match('MemTotal:%s+(%d+)')
memavail = data:match('MemAvailable:%s+(%d+)')
memusage = math.floor((memtotal - memavail) / memtotal * 1000) / 10

log(tonumber(load), memusage)


Hello Daniel.

Thank you for your reply.

I am getting values 0.24 , 0.25 ... Does this mean the CPU load is 0.24 %?


RE: CPU Load Log as Trend - Daniel - 10.09.2019

No it is not percentage, it is bit more complicated in linux. Try reading this
https://www.tecmint.com/understand-linux-load-averages-and-monitor-performance/

To write to object just do it like that:
grp.update('1/1/1', tonumber(load))


RE: CPU Load Log as Trend - manos@dynamitec - 10.09.2019

(10.09.2019, 14:02)Daniel. Wrote: No it is not percentage, it is bit more complicated in linux. Try reading this
https://www.tecmint.com/understand-linux-load-averages-and-monitor-performance/

To write to object just do it like that:
grp.update('1/1/1', tonumber(load))

Yes I figured out how to write the value to the object but the Load average is indeed complicated. So then it will require a script to calculate properly the % of CPU load average for the last 1min.

Does anyone have a solution for this?


RE: CPU Load Log as Trend - admin - 11.09.2019

Use this script to log CPU load to an object. Resident script sleep time controls how often load is calculated.
Code:
data = io.readfile('/proc/uptime'):split(' ')
total, idle = tonumber(data[ 1 ]), tonumber(data[ 2 ])

if prevtotal then
  deltatotal = total - prevtotal
  deltaidle = idle - previdle

  load = math.floor((1 - (deltaidle / deltatotal)) * 100 + 0.5)
  grp.update('32/0/0', load)
end

prevtotal, previdle = total, idle



RE: CPU Load Log as Trend - manos@dynamitec - 11.09.2019

Thank you Admin. As always works perfectly.  Smile


RE: CPU Load Log as Trend - Jørn - 16.04.2020

(11.09.2019, 06:33)admin Wrote: Use this script to log CPU load to an object. Resident script sleep time controls how often load is calculated.
Code:
data = io.readfile('/proc/uptime'):split(' ')
total, idle = tonumber(data[ 1 ]), tonumber(data[ 2 ])

if prevtotal then
  deltatotal = total - prevtotal
  deltaidle = idle - previdle

  load = math.floor((1 - (deltaidle / deltatotal)) * 100 + 0.5)
  grp.update('32/0/0', load)
end

prevtotal, previdle = total, idle

What datatype would appropiate for this output`?


RE: CPU Load Log as Trend - admin - 16.04.2020

5.001 - 0..100%