HomeGenie Forum

Automation Program Plugins and Wizard Scripting => APP Contributions => Topic started by: dani on November 11, 2015, 10:28:18 AM

Title: CPU Load Sensor
Post by: dani on November 11, 2015, 10:28:18 AM
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.
Title: Re: CPU Load Sensor
Post by: KaZe on November 11, 2015, 12:59:27 PM
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!
Title: Re: CPU Load Sensor
Post by: dani on November 11, 2015, 03:44:20 PM
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
Title: Re: CPU Load Sensor
Post by: bkenobi on November 11, 2015, 04:32:20 PM
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.
Title: Re: CPU Load Sensor
Post by: dani on November 11, 2015, 04:39:18 PM
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.
Title: Re: CPU Load Sensor
Post by: dani on November 11, 2015, 04:41:58 PM
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.
Title: Re: CPU Load Sensor
Post by: KaZe on November 11, 2015, 04:56:50 PM
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.
Title: Re: CPU Load Sensor
Post by: dani on November 11, 2015, 05:08:34 PM
In that case you can move the 3 lines concerned in the loop
Title: Re: CPU Load Sensor
Post by: KaZe on November 11, 2015, 11:23:02 PM
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.
Title: Re: CPU Load Sensor
Post by: dani on November 12, 2015, 09:00:43 AM
Hi Kaze, carrefull your password is in clear in one file. I take off that file from your post.
Title: Re: CPU Load Sensor
Post by: KaZe on November 12, 2015, 09:42:36 AM
Hi Kaze, carrefull your password is in clear in one file. I take off that file from your post.
Thx, I totally forgot.
Title: Re: CPU Load Sensor
Post by: funworld on November 12, 2015, 07:57:17 PM
Hello Dani

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

thanks
funworld
Title: Re: CPU Load Sensor
Post by: dani on November 12, 2015, 09:20:34 PM
Hello FunWorld,

Following src is the version with CPU Temperature
Title: Re: CPU Load Sensor
Post by: funworld on November 13, 2015, 06:36:45 PM
THX Dani,work´s great  ;D
how I can change the intervall time!

funworld
Title: Re: CPU Load Sensor
Post by: dani on November 13, 2015, 10:17:04 PM
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
Title: Re: CPU Load Sensor
Post by: DavZero on November 22, 2015, 04:06:19 PM
Hi,

I've made something similar in C# integrated in a HG program but don't find a way to get the memory usage with mono. But it works for cpu usage
If someone want to improve this program I attach it.

Bye
Title: Re: CPU Load Sensor
Post by: bkenobi on November 22, 2015, 07:18:31 PM
Nice, thanks for sharing!
Title: Re: CPU Load Sensor
Post by: dani on November 22, 2015, 07:19:43 PM
Thank's DavZero, I will test.

Cheers Dani

I have made some tests but need to stay in the front of the screen. And the 2 nde RAM Avalaible value is what ? I got only 3.26 MB.
What I need is to see the memory evolution of the system in time, during several days. 

Cheers Dani
Title: Re: CPU Load Sensor
Post by: DavZero on November 23, 2015, 08:27:06 PM
Yes, you should stay logged to see the message because it'a a test program and I only set a notification instead of create a widget for display the result but if needed you can create a virtual sensor and put all the value needed on the sensor widget. Please find attach a new version which do this.

But as I say in my other post, it does NOT works for the memory usage, because I haven't find the correct performance counter for get this value with mono. I'm not sure it exist

So actually, it works only for the CPU usage
Title: Re: CPU Load Sensor
Post by: DavZero on November 23, 2015, 09:21:50 PM
I've just made a little improvement to get the memory usage of the HG process.

If you need total memory instead of HG process memory, use :

   double memoryUsage = 0.0;
    System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcesses();
    for (int i = 0 ; i < proc.Length ; i++)
    {   
      memoryUsage += proc.WorkingSet64/1024/1024;
    }
    Program.RaiseEvent(systemSensor, "Sensor.System.Memory", (memoryUsage).ToString(), "update Memory usage");


Tell me if something else is needed.

Bye
Title: Re: CPU Load Sensor
Post by: DavZero on November 24, 2015, 09:49:35 PM
Hi (again),

I've made a new modification. The program now extract CPU usage in %, RAM usage in % and Disk Usage in %.

If you want to modify the refresh time, just change this line :
var checkTimer = new System.Timers.Timer(10000);

put the time value in ms.

For the disk it's the total of all the disk, may be it's not what all the user want ?

Bye
Title: Re: CPU Load Sensor
Post by: DavZero on November 28, 2015, 01:36:31 PM
Hi,

I've updated the program in order to avoid notification flood :
 - Check if values need to be update instead of update it each loop
 - Use the new "Program.RaiseEvent("Program.UIRefresh",...)


In order to use this version, you must be in HG 1.1 499 or a newer version.

Bye
Title: Re: CPU Load Sensor
Post by: bkenobi on November 28, 2015, 08:21:19 PM
This version seems to work pretty well.

One question though...  What CPU and memory loads are people seeing reported?  I was seeing 18% cpu and 42% memory for a couple hours unchanged.  After forcing a page refresh, it displayed 29% cpu.  This seems WAY higher than I expected so I monitored the top command and I'm seeing typically 7-8% with spikes up to 25% cpu or so.  I do have a few programs that I've written so I suppose one/some of them are not ideal with resources.
Title: Re: CPU Load Sensor
Post by: dani on November 29, 2015, 01:05:55 PM
My batch report reel values because it's base on free and ps of linux.
I don't know about values report by C# Mono.
Title: Re: CPU Load Sensor
Post by: DavZero on November 29, 2015, 04:11:26 PM
Oups,
you're rigth, the widget doesn't update itself.
I think it's cause by the new raise event "Program.RaiseEvent("Program.UiRefresh","Data updated","")", I probably don't uderstand corectly how it works.
I join a version with the "old style" parameter update but all updated value generate a notification.

For the value :
CPU ==> It's the average value on the loop time (in %). So if you the the loop time every second, it should be like the task manager value, if you set the loop time every 60 secondes (as it is by default), it should be different from the task manager.
Memory ==> It's the actual physical memory use at the time the loop was executed (in %). It's the addition of the physical memory used by all process divide by the total physical memory
Disk ==> It's the actual Disk usage (in %). It's the addition of disk usage of all "fixed" disk on the computer divide by the total size of all "fixed" disk.
Title: Re: CPU Load Sensor
Post by: Gene on November 29, 2015, 07:56:32 PM
Hi DavZero,

since you're using a virtual module, you've to address the refresh event on that module rather than on the program module.

Code: [Select]
Program.RaiseEvent(yourVirtualModule, "Program.UiRefresh","Data updated","");

g.
Title: Re: CPU Load Sensor
Post by: DavZero on November 29, 2015, 08:43:17 PM
Hi Gene,

I've made the modification but the widget doesn't refresh itself, I must reload the page or change the page and go again to the page that display this widget in order to see the value updated.

This is an extract of the log file :
Code: [Select]
2015-11-29 20:33:26.9178 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:31.9175 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:36.9175 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:37.3154 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [CLOSED AFTER 0 seconds]
2015-11-29 20:33:37.3154 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [OPEN]
2015-11-29 20:33:41.9185 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:46.9175 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:47.3194 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [CLOSED AFTER 0 seconds]
2015-11-29 20:33:47.3189 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [OPEN]
2015-11-29 20:33:51.9176 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:54.3640 Info HomeAutomation.PhilipsHue 1 - Meter.Watts 0.0
2015-11-29 20:33:54.3831 Info HomeAutomation.PhilipsHue 2 - Meter.Watts 0.0
2015-11-29 20:33:54.3943 Info HomeAutomation.PhilipsHue 3 - Meter.Watts 0.0
2015-11-29 20:33:56.9175 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:33:58.3242 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [CLOSED AFTER 0 seconds]
2015-11-29 20:33:58.3242 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [OPEN]
2015-11-29 20:34:01.9183 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:34:06.9177 Info HomeAutomation.SystemChecker 1 Virtual Module Program.UiRefresh Data Updated
2015-11-29 20:34:09.3224 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [CLOSED AFTER 0 seconds]
2015-11-29 20:34:09.3224 Info WebServiceGateway 192.168.0.1 HTTP GET 200 /api/HomeAutomation.HomeGenie/Config/Interfaces.List/ [OPEN]
We can see that the refresh event appear every 5 secondes and it's what I ask but the widget never update if I stay on the page which display it.

I update value like that :
Code: [Select]
var UpdateParamterValue = new Func<ModuleHelper,string,string,bool> ((module,param,val) => {
  if (module.Parameter(param).Value != val)
  {
    //Program.Notify("SystemChecker", param + " : " + module.Parameter(param).Value + "->" + val);
    module.Parameter(param).Value = val;
    //Program.RaiseEvent(module, param, val, "Update " + param);
    return true;
  }
  return false; 
});

...
...
...
//Check for update only if needed
    var needUpdate = UpdateParamterValue(systemSensor,"Sensor.System.Disk",newDiskVal);
    needUpdate |= UpdateParamterValue(systemSensor,"Sensor.System.CPU",newCPUVal);
    needUpdate |= UpdateParamterValue(systemSensor,"Sensor.System.Memory",newMemoryVal);

    if (needUpdate)
      Program.RaiseEvent(systemSensor,"Program.UiRefresh", "Data Updated", "");
...
...
...

Thanks
Title: Re: CPU Load Sensor
Post by: Gene on November 29, 2015, 09:19:44 PM
Hi DavZero,

the raise event instruction is correct, though there's another thing to consider: the sensor widget only take into account events such as Sensor.*, Status.Level and few others. So it simply ignore the UiRefresh event.
What you can do is using another widget like the "homegenie/generic/status" or write your own widget that perhaps will also have some nice charts.

Cheers,
g.
Title: Re: CPU Load Sensor
Post by: DavZero on November 29, 2015, 11:23:05 PM
Ok I will sop this discution on this thread and continu in the dedicated thread in order to leave this one for CPU monitoring :
http://www.homegenie.it/forum/index.php?topic=1232.0 (http://www.homegenie.it/forum/index.php?topic=1232.0)

I will post as as I can a new program with a dedicated widget.

Thanks
Title: Re: CPU Load Sensor
Post by: DavZero on December 05, 2015, 02:04:57 PM
Hi,

Please find attach a new release of the systemChecker program which use the last capability of HomeGenie in order to don't display popup on each update.

I've made to a new widget in order to add the "%" symbole after each value. I don't really like made widget (I'm a neewbie with html and javascript and I'm really not an artist ^^)

I've add too a parameter to the SystemChecker program in order to define the resfresh time (60 seconde by default)

I will now try to understand how to add these values in the statistic graph. I think that value parameter should be like Sensor.* but I'm not sure

Bye
Title: Re: CPU Load Sensor
Post by: bkenobi on December 06, 2015, 01:38:47 AM
I haven't used custom widgets before.  Do I unzip this into somewhere on my RPi or is there a way in HG to install a widget?

I found the import tool in HG under the widgets configuration menu.  I updated to the latest version and everything looks good.
Title: Re: CPU Load Sensor
Post by: enterprised on December 06, 2015, 07:09:52 PM
One tiny issue with the custom widget, It does not show up in HG Plus for Android.
Title: Re: CPU Load Sensor
Post by: Gene on December 06, 2015, 08:07:10 PM
Hi DavZero,

you can switch back using standard HG widget since the ui refresh and notification issues are solved, unless you're looking for a different design or layout.

Cheers,
g.
Title: Re: CPU Load Sensor
Post by: DavZero on December 06, 2015, 09:16:33 PM
Hi,

@Gene, I've custom a little the widget in order to show the '%' symbol after the value so I will continue to use it. I've made feature request in order to manage which property to show in the statistic. Is there a way to make a custom widget compatible with HG Plus app?

@enterprised, I'm sorry but I don't know how to make a widget compatible with the HG Plus android app. Depending on the response of Gene, If you need it to be show in the Android App, I could change in the way Gene tell me or if it's not possible to make a widget compatible with HG plus, I could use the standard widget but without the '%' symbol.

Bye
Title: Re: CPU Load Sensor
Post by: kevin1 on December 07, 2015, 03:21:02 AM
this app gives me a runtime error on win7... "category doesn't exist"
any ideas what this is?  it happened on older version too.  tried recompiling
Title: Re: CPU Load Sensor
Post by: DavZero on December 07, 2015, 09:54:01 PM
Yes it's because of this line :
Code: [Select]
var memCounter = new System.Diagnostics.PerformanceCounter("Mono Memory", "Total Physical Memory");
It only works when using Mono. I don't find a way to do it in a portable way. For get this information on a Windows plateform, you should check the WMI capability. I will try to add a windows compatibiliy if I find the time to do it. For now just comment the line about the memory usage en the counter memCounter.

Bye
Title: Re: CPU Load Sensor
Post by: DavZero on December 08, 2015, 09:49:32 PM
Hi,

sorry @Kevin1 but I can't find a simple way to get the physical memory size installed in a comptuer under Windows.

Please find the attached code which get the physical memory size with Mono if the counter exist or if not, it use the default size which is 2Go. If you need to change this value, for example if your computer has 4 Go, juste change this line :
Code: [Select]
double totalPhysicalMemory = 2.0*1024*1024*1024;and replace the "2.0" bye the correct value on your computer (for exemple 4.0 or 8.0)

I can't test so if there's an other problem tell me and I will try to correct it.
Title: Re: CPU Load Sensor
Post by: kevin1 on December 09, 2015, 01:36:48 PM
Thanks for the help!  I haven't been able to get the widget to show the data for some reason.  I stuck in a Notify and it was showing CPU values, just not showing in the widget.  I'll look at it again tonight.  My HG appears to be down now :(
Title: Re: CPU Load Sensor
Post by: bkenobi on December 27, 2015, 04:26:08 PM
I had this working great last month but when I just checked to see some other statistics data I found that I didn't have any info for my CPU, memory, disk usage.  Does anyone else find a similar issue?  I assume this is either too much data is being stored for that specific data set (since I do have other data that is more current for other statistics items) or something broke in the CPU Load Sensor code.

I initially thought that perhaps the :nopopup: code might have affected the statistics, but removing it and recompiling didn't change anything.  I still have up to date widget values, though.  This is strictly a statistics issue I believe.
Title: Re: CPU Load Sensor
Post by: bkenobi on December 28, 2015, 09:33:25 PM
I have been working with my MQTT sensor code which pushes data into several sensors (temperature, humidity, watts, etc) and all of those statistics are updating as expected.  For some reason, the widget for CPU Load is updating but the statistics are not.  Maybe it's because I switched it over to use the custom widget rather than the built-in sensor widget.
Title: Re: CPU Load Sensor
Post by: dani on December 29, 2015, 11:41:00 AM
Hi bkenobi,

Same you at the fisrt the CPU statistics were recorded. And now no. For it is recorded we must prefix  the parameters name with "Meter." instead of "System." or look at that post :
http://www.homegenie.it/forum/index.php?topic=1248.msg8294#msg8294 (http://www.homegenie.it/forum/index.php?topic=1248.msg8294#msg8294)
from Gene for add "System." prefix to the statistics handler.
Title: Re: CPU Load Sensor
Post by: bkenobi on December 29, 2015, 12:17:34 PM
I remembered seeing that post but couldn't find it.  I'll add the prefix, thanks.
Title: Re: CPU Load Sensor
Post by: bkenobi on January 02, 2016, 06:13:23 AM
Is the CPU meter working for others on RPi?  Memory and disk are working but the CPU isn't any more.
Title: Re: CPU Load Sensor
Post by: dani on January 02, 2016, 06:44:22 AM
For me raspi with r508a CPU load works, it is clearly updated
Title: Re: CPU Load Sensor
Post by: bkenobi on January 02, 2016, 07:12:59 AM
Good to know.  I'll look around in the code to see if I messed something up then.
Title: Re: CPU Load Sensor
Post by: bkenobi on January 03, 2016, 10:52:41 PM
I updated to the latest version and changed "System.*" to "Sensor.*" as well as switching to the standard widget.  After this change, everything seems to be working again.

That said, it's very good that I got it working again since I just found that mono is using 98.8% CPU!
Title: Re: CPU Load Sensor
Post by: dani on January 03, 2016, 11:13:52 PM
On raspberry pi ?  You have some prgrams loop. For me I get between 1 and 6% on standard conditions.
I got high value during last update to 509.
CPUload show around 30% and shell top show 109% !!! I think CPUload doesn't give reel value
Title: Re: CPU Load Sensor
Post by: bkenobi on January 04, 2016, 02:12:22 AM
Top showed 98%+ + 1% for something else so 100% seemed pretty accurate.  I restarted the RPi and I'm down to ~10%.  I'm updating now and it's at 100%, but that seems accurate too.
Title: Re: CPU Load Sensor
Post by: dani on January 04, 2016, 07:15:03 PM
Those what I obtain before yesterday during update HG from r508 to r509.
CPULoad are not very identical ?
And yes linux top function  give more than 100% of cpu load for mono. I think there was some erros because Raspberry PI B2 has 4 hearts !!!
Those screenshots were taken at 1 or 2 seconds interval.
Title: Re: CPU Load Sensor
Post by: KaZe on January 11, 2016, 10:13:10 AM
Hi!

I modify the first program. Now the HG run the query, not the linux, and no need HG user and password in the bash file. You just copy the CPUData.sh file to /usr/local/bin/homegenie/ folder, and then inport the program. I modify the sensor.js to show "%" and "MB" data.
I try to run directly the query from HG program - without CPUData.sh  (http://www.homegenie.it/forum/index.php?topic=352.0 (http://www.homegenie.it/forum/index.php?topic=352.0)) but I can't figure out how can I do.  :-\
In the program options, you can set the Query delay interval. The default is 60s.