more G-Labs products

Author Topic: CPU Load Sensor  (Read 9940 times)

November 11, 2015, 10:28:18 AM
Read 9940 times

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
Here is a CPU Load Sensor avalaible for Linux system.
By changing the bash you can probably use it with Windows.

Control that nobody hack the sh file, it must contain the following text :

Code: [Select]
#!/bin/bash
TEMPO=600

sleep 30
while true
 do
   tpscpu=`ps aux | awk '{ print $3,$11 }' | grep mono | cut -d' ' -f1`
   memcpu=`ps aux | awk '{ print $4,$11 }' | grep mono | cut -d' ' -f1`
   vszmem=`ps aux | awk '{ print $5,$11 }' | grep mono | cut -d' ' -f1`
   rssmem=`ps aux | awk '{ print $6,$11 }' | grep mono | cut -d' ' -f1`
   curl "http://127.0.0.1/api/HomeAutomation.HomeGenie.Automation/CPUData/Sensor.LoadMem/$memcpu"
   echo " LoadMem=$memcpu"
   curl "http://127.0.0.1/api/HomeAutomation.HomeGenie.Automation/CPUData/Sensor.LoadTime/$tpscpu"
   echo " LoadTime=$tpscpu"
   curl "http://127.0.0.1/api/HomeAutomation.HomeGenie.Automation/CPUData/Sensor.RSSMem/$rssmem"
   echo " RSSMem=$rssmem"
   curl "http://127.0.0.1/api/HomeAutomation.HomeGenie.Automation/CPUData/Sensor.VSZMem/$vszmem"
   echo " VSZMem=$vszmem"
   sleep $TEMPO
 done

If you want to activate that bash automatically you can use with some modifications of the bash :
sudo update-rc.d CPUDtata.sh defaults // to put it on debian runlevel.
« Last Edit: November 11, 2015, 05:10:08 PM by dani »

November 11, 2015, 12:59:27 PM
Reply #1

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
Hi!

It's good idea.
I think you must show "MB", "KB","%"  at the end of the line.
If somebody use other port then 80, her must change the port number in CPUData.sh. If use password, the code must change to http://admin:password@127.0.0.1:port/api/HomeAutomation.HomeGenie.Automation/CPUData/Sensor.LoadMem/$memcpu
With this little modification works to me. Thanks!

November 11, 2015, 03:44:20 PM
Reply #2

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
Thank's Kaze for complete information of curl.
Actually the used spaces are in KBytes. If you want to change the scale you have to modify the sensor widget, because the Average Value in the database is a float, or modify the program to divide the value  by 1000 if you want Mega or 1000000 if you want Giga before to Send the RaiseEvent.

The timer at the begining of the bash is in secondes. By default I put 10 minutes. Carrefull to not put a too short time because at every timeout you get 4 values in the Statistics database. Interresting to reduce it for specific test but not at all.

Of course all values concern the mono Homegenie process.
If somebody need I can add total used memory of the system.

Some Spelling Mistake in the code : Replace Unavalaible by Unavailable
« Last Edit: November 11, 2015, 04:16:46 PM by dani »

November 11, 2015, 04:32:20 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
This is great!  I'll check it out tonight but this sounds like it should help people understand their system better.

Out of curiosity, was there a reason not to use C# methods to obtain system resource use rather than using "ps"?  It looks like "ps" works fine, but by using the internal code you wouldn't have to send the data via API calls.

November 11, 2015, 04:39:18 PM
Reply #4

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
Version with Total Memory of the System and Total Used Memory in the system.
Adding a pause of 30 secondes at the begining of the bash fot let's homegenie started before to send the first data.
The total memory is sent only one time at the started of the bash because it will not move.

November 11, 2015, 04:41:58 PM
Reply #5

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
I agree with you bkenobi, but I am not sure that Mono can get data system of linux. If you know the class you can help me to modify that contrib.

And my sample of bash will help people to understand some functions of bash code, which is not easy at the first look. Even for me, I have make many tries to get that result. Man linux function is good but no samples provided, and the net is not alxways clear for that.
« Last Edit: November 11, 2015, 04:47:52 PM by dani »

November 11, 2015, 04:56:50 PM
Reply #6

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
Version with Total Memory of the System and Total Used Memory in the system.
Adding a pause of 30 secondes at the begining of the bash fot let's homegenie started before to send the first data.
The total memory is sent only one time at the started of the bash because it will not move.

Hi Dani!

Total memory can change under virtualized environments. In my case I use Hyper-V with dinamic memory usage to my linux server.

November 11, 2015, 05:08:34 PM
Reply #7

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
In that case you can move the 3 lines concerned in the loop

November 11, 2015, 11:23:02 PM
Reply #8

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
In that case you can move the 3 lines concerned in the loop
Hi dani!

I modify the program little. I 'convert' memory data to MB. And I modify the sensor widget to show atributes. Thank you for sharing this program! Very usefull for me.
« Last Edit: November 12, 2015, 09:01:11 AM by dani »

November 12, 2015, 09:00:43 AM
Reply #9

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
Hi Kaze, carrefull your password is in clear in one file. I take off that file from your post.

November 12, 2015, 09:42:36 AM
Reply #10

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
Hi Kaze, carrefull your password is in clear in one file. I take off that file from your post.
Thx, I totally forgot.

November 12, 2015, 07:57:17 PM
Reply #11

funworld

  • *
  • Information
  • Newbie
  • Posts: 20
Hello Dani

It´s a great app,
how I can read the cpu temp (vcgencmd measure_temp)

thanks
funworld

November 12, 2015, 09:20:34 PM
Reply #12

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
Hello FunWorld,

Following src is the version with CPU Temperature
« Last Edit: November 12, 2015, 10:41:51 PM by dani »

November 13, 2015, 06:36:45 PM
Reply #13

funworld

  • *
  • Information
  • Newbie
  • Posts: 20
THX Dani,work´s great  ;D
how I can change the intervall time!

funworld
« Last Edit: November 13, 2015, 06:44:11 PM by funworld »

November 13, 2015, 10:17:04 PM
Reply #14

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
The timeout= 600 means 10 mns. You can change 600 by every value you want
This parameter is in the file CPUData.sh that you can edit with nano or gedit
« Last Edit: November 14, 2015, 02:57:32 PM by dani »