Issue with ram usage node-red 1.1.3

Hi guys, im trying to do a dashboard that monitor all values of a Raspberry Pi 4 (cpu, ram, temp..)

I used at first a specific node called: node-red-contrib-os
then i try with simply command like this: free | grep Mem | awk '{print 100($4+$6+$7)/$2}'*

For a trusted comparison i used task manager on rpi.

No one of this command/node works fine. Because those commands returns different results.
I try with a stress test for ram memory.
Below i try to explain:

TASK MANAGER SAYS:
RPI was at 85% of ram
Total: 1815 MB
Used: 1374 MB
Free: 73 MB

free | grep Mem | awk '{print 100*($4+$6+$7)/$2}' ---> RETURN:
40.5442
i dont know what is this value. 40.54 what is? %? int? also it dont look like any values on taskmanager.

Then i try with specific node: node-red-contrib-os
but it return: 95%

Mybe i have to correct some data, or some format. I really dont know.
Mybe version of node-red (i dont think because i used script for auto-install)
Below screenshot of my nodes

Im available for more explain because i didnt find on internet a right command for ram usage for RPI 4

Many thanks if someone try to help me.
Fabio :slight_smile:

Hi Fabio,

I have not used that command myself, but I assume it works like this:

  1. With free you get some info about the memory (physical and swap memory, as well as the free and used memory):

    image

  2. That output is used as input (via the | pipe symbol) as input into the grep command, which finds the line with "Mem":

    image

  3. And that output is used (again via the |) as input for awk, which enables you to 'print' the input fields (first input field is $1, second $2 ...). You can see the values of the input fields like this:

    image

    So you can do a calculation with that numbers like in your example statement (which contains a sum $4+$6+$7 divided by $2):

    image

Hopefully it is now clear how your command calculates that 40,54 number...

Bart

Hi Bart,
your reply was very helpfull. Trought your answer i built a command to print the RAM usage.
Now works fine and it's all fine.

I think it can be definied a solution.
If you want i can share with you template and dashboard for ndoe-red

Many thanks,
Fabio

Evening Fabio,
I could have given you the final result. But now that you understand how it works, you will be able to do much more cool stuff with linux commands on your own...
Yes please share you final result, so others can learn from it...
Bart

Even with the node red modules that provide such information, I find it better, usually to use exec nodes and select the commands directly. Is not as elegant as some other solutions but it does give you explicit control on what you ask for and get in return.

Also worth noting that the "free" memory on Linux is probably not what you think it is. Since "spare" memory on Linux will be commonly occupied by things like file caches.

Depending on what you are trying to actually measure, the SWAP usage may be more important, especially on a Pi where swap access is horribly slow. Use of swap means that the system could not allocate a sufficiently large block of memory for an application and so moved anothers application memory to disk temporarily. Once swap use starts to climb you will notice a drop in performance on the device. On a Pi that can be a very dramatic drop.

@TotallyInformation,

Very good point(s).

Good information.

i understood what u say. I think that i can build a dashboard with free ram available, and Swap so i can monitor all Rpi's behavior about ram memory.

Btw i found your reply very helpfull to understand better rpi behavior.

Many thanks,
Fabio

Ok, below i paste the solution that i found.

At first i used this command free | grep Mem | awk '{print (($3)/1024)}'
$3 -> because its the third value and it is the column of used memory in rpi
/1024 -> because i have to convert Byte in MegaByte

I compared the result with rpi task manager and datas are the same

Now i think i will add an indicator about SWAP memory.

PS if someone is interested for my dashboard this is the tempalte:

Thank you so much to every one,
Fabio

1 Like

Yeah, the suggestion of using/tracking SWAP is a great one.

Hi Fabio,
Thanks for sharing your solution!!
I will add your flow here again directly in the forum, in case your pastebin link will get broken in the future:

[{"id":"f1aa2c89.23dbb","type":"tab","label":"Rpi monitor","disabled":false,"info":""},{"id":"557fe3aa.4f498c","type":"inject","z":"f1aa2c89.23dbb","name":"5 min tick","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"15","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":110,"y":460,"wires":[["295c57f6.ed33b8","470c70b3.79679","7d2681d9.2b7b8","8d61a3c5.6f32b","c106418d.50f6"]]},{"id":"f107291c.2887d8","type":"ui_gauge","z":"f1aa2c89.23dbb","name":"","group":"f4451afa.e37488","order":1,"width":"3","height":"3","gtype":"gage","title":"CPU load","label":"%","format":"{{ msg.payload }}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"60","seg2":"80","x":760,"y":600,"wires":[]},{"id":"450cb3c3.a34a2c","type":"ui_gauge","z":"f1aa2c89.23dbb","name":"","group":"cefd6796.55a8e8","order":2,"width":"6","height":"4","gtype":"wave","title":"Used","label":"%","format":"{{msg.payload}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"60","seg2":"80","x":870,"y":300,"wires":[]},{"id":"def108d6.169f28","type":"ui_chart","z":"f1aa2c89.23dbb","name":"","group":"f4451afa.e37488","order":3,"width":"6","height":"4","label":"Load, %","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"bezier","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"604800","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#ff8000","#aec7e8","#0080ff","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":780,"y":640,"wires":[[]]},{"id":"6ad433b.9abe7cc","type":"ui_chart","z":"f1aa2c89.23dbb","name":"","group":"cefd6796.55a8e8","order":1,"width":"6","height":"6","label":"Storage","chartType":"line","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":940,"y":240,"wires":[[]]},{"id":"eba70268.0aa44","type":"ui_gauge","z":"f1aa2c89.23dbb","name":"","group":"d12443d7.67236","order":2,"width":"0","height":"0","gtype":"donut","title":"RAM usage","label":"MB","format":"{{ value | number: 0}}","min":0,"max":"2048","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1024","seg2":"1550","x":690,"y":460,"wires":[]},{"id":"4f753f45.ae9c","type":"ui_chart","z":"f1aa2c89.23dbb","name":"","group":"d12443d7.67236","order":3,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":670,"y":500,"wires":[[]]},{"id":"409b73c9.519dac","type":"ui_gauge","z":"f1aa2c89.23dbb","name":"","group":"f4451afa.e37488","order":2,"width":"3","height":"3","gtype":"gage","title":"CPU Temp","label":"°C","format":"{{ msg.payload }}","min":0,"max":"100","colors":["#0080ff","#ffffff","#ca3838"],"seg1":"65","seg2":"85","x":760,"y":680,"wires":[]},{"id":"22fa5d26.75c002","type":"function","z":"f1aa2c89.23dbb","name":"convert_uptime","func":"var d = Math.floor(msg.payload.uptime / 86400);\nvar h = Math.floor((msg.payload.uptime % 86400) / 3600);\nvar m = Math.floor(((msg.payload.uptime % 86400) % 3600) / 60);\nvar s = ((msg.payload.uptime % 86400) % 3600) % 60;\nmsg.payload = d + ':' + h + ':' + m + ':' + s;\nreturn msg;","outputs":1,"noerr":0,"x":560,"y":400,"wires":[["834bc7c6.b2ae58"]]},{"id":"295c57f6.ed33b8","type":"exec","z":"f1aa2c89.23dbb","command":"/opt/vc/bin/vcgencmd measure_temp | egrep -o \"[0-9]+\\.[0-9]+?\"","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"read_T","x":420,"y":680,"wires":[["409b73c9.519dac","23a7c2a8.1388fe","43f6c84f.769938"],[],[]]},{"id":"37301279.f50ace","type":"Memory","z":"f1aa2c89.23dbb","name":"","x":80,"y":560,"wires":[[]]},{"id":"470c70b3.79679","type":"Uptime","z":"f1aa2c89.23dbb","name":"Uptime","x":340,"y":400,"wires":[["22fa5d26.75c002"]]},{"id":"23a7c2a8.1388fe","type":"ui_chart","z":"f1aa2c89.23dbb","name":"","group":"f4451afa.e37488","order":4,"width":"6","height":"3","label":"Temperature, °C","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"bezier","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"604800","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":800,"y":719,"wires":[[]]},{"id":"43f6c84f.769938","type":"debug","z":"f1aa2c89.23dbb","name":"","active":true,"tosidebar":false,"console":true,"tostatus":false,"complete":"payload","x":760,"y":760,"wires":[]},{"id":"834bc7c6.b2ae58","type":"ui_text","z":"f1aa2c89.23dbb","group":"d12443d7.67236","order":1,"width":0,"height":0,"name":"","label":"Uptime, DD:HH:MM:SS","format":"{{ msg.payload }}","layout":"row-spread","x":790,"y":400,"wires":[]},{"id":"12395fa5.f6274","type":"debug","z":"f1aa2c89.23dbb","name":"Voyer","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","x":300,"y":80,"wires":[]},{"id":"7d2681d9.2b7b8","type":"exec","z":"f1aa2c89.23dbb","command":"top -d 0.5 -b -n2 | grep Cpu | tail -1 | awk '{print 100-$8}'","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"cpu_load","x":420,"y":600,"wires":[["f107291c.2887d8","def108d6.169f28"],[],[]]},{"id":"8d61a3c5.6f32b","type":"exec","z":"f1aa2c89.23dbb","command":"df -m | grep root","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"read_sd_usage","x":320,"y":300,"wires":[["c1bb3264.3715c"],[],[]]},{"id":"c1bb3264.3715c","type":"function","z":"f1aa2c89.23dbb","name":"convert","func":"var msg1,msg2,msg3;\n\nl = msg.payload.split(/(\\s+)/).filter( function(e) { return e.trim().length > 0; } );\nmsg1 = {topic: 'Total', payload: Math.round(l[1] / 1000)};\nmsg2 = {topic: 'Free', payload: Math.round(l[3] / 1000)};\nmsg3 = {topic: 'Used%', payload: l[4].slice(0, -1)};\n\nreturn [msg1, msg2, msg3];","outputs":3,"noerr":0,"x":540,"y":280,"wires":[["6ad433b.9abe7cc"],["6ad433b.9abe7cc"],["6ad433b.9abe7cc","450cb3c3.a34a2c"]]},{"id":"fa8f4d6c.b165d","type":"ui_button","z":"f1aa2c89.23dbb","name":"Next","group":"abd9f568.55e3e8","order":3,"width":"1","height":"1","passthru":false,"label":"","color":"","bgcolor":"","icon":"arrow_forward","payload":"+1","payloadType":"str","topic":"","x":530,"y":60,"wires":[["b4cbf320.4a577"]]},{"id":"b4cbf320.4a577","type":"ui_ui_control","z":"f1aa2c89.23dbb","name":"Control tabs","x":730,"y":80,"wires":[[]]},{"id":"d5ae9337.fe66","type":"ui_button","z":"f1aa2c89.23dbb","name":"Prev","group":"abd9f568.55e3e8","order":1,"width":"1","height":"1","passthru":false,"label":"","color":"","bgcolor":"","icon":"arrow_back","payload":"-1","payloadType":"str","topic":"","x":530,"y":100,"wires":[["b4cbf320.4a577"]]},{"id":"714f2cd.dc2fcd4","type":"ui_button","z":"f1aa2c89.23dbb","name":"Refresh","group":"abd9f568.55e3e8","order":2,"width":"1","height":"1","passthru":false,"label":"","color":"","bgcolor":"","icon":"replay","payload":"{tab:\"\"}","payloadType":"str","topic":"","x":520,"y":20,"wires":[["b4cbf320.4a577"]]},{"id":"4732572b.c98cb8","type":"inject","z":"f1aa2c89.23dbb","name":"1 min tick","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":"60","topic":"","payload":"","payloadType":"date","x":290,"y":200,"wires":[["986c4bb0.fd3058"]]},{"id":"986c4bb0.fd3058","type":"function","z":"f1aa2c89.23dbb","name":"date_time","func":"var d_obj = new Date().toString().slice(0, -18); \nmsg.payload = d_obj.slice(0, -6) + '<br>' + d_obj.slice(-5); \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":200,"wires":[["5574af15.00435"]]},{"id":"5574af15.00435","type":"ui_text","z":"f1aa2c89.23dbb","group":"abd9f568.55e3e8","order":0,"width":"3","height":"4","name":"Date time","label":"","format":"{{ msg.payload }}","layout":"col-center","x":780,"y":160,"wires":[]},{"id":"b0bccafb.97d4b8","type":"comment","z":"f1aa2c89.23dbb","name":"UI controls","info":"","x":880,"y":40,"wires":[]},{"id":"c106418d.50f6","type":"exec","z":"f1aa2c89.23dbb","command":"free | grep Mem | awk '{print (($3)/1024)}'","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"ram-test1","x":380,"y":480,"wires":[["eba70268.0aa44","4f753f45.ae9c"],[],[]]},{"id":"f4451afa.e37488","type":"ui_group","z":"","name":"CPU","tab":"2a7432a1.e8fe5e","order":2,"disp":true,"width":"6","collapse":true},{"id":"cefd6796.55a8e8","type":"ui_group","z":"","name":"SD card","tab":"2a7432a1.e8fe5e","order":4,"disp":true,"width":"6","collapse":true},{"id":"d12443d7.67236","type":"ui_group","z":"","name":"Memory","tab":"2a7432a1.e8fe5e","order":3,"disp":true,"width":"6","collapse":true},{"id":"abd9f568.55e3e8","type":"ui_group","z":"","name":"Buttons","tab":"2a7432a1.e8fe5e","order":1,"disp":false,"width":"3","collapse":false},{"id":"2a7432a1.e8fe5e","type":"ui_tab","z":"","name":"Monitor RPI","icon":"settings","order":2,"disabled":false,"hidden":false}]

I have one more item than typical resource boards for Pi, I added a Node Red specific uptime item. Decided I wanted to know how long since NR was restarted not just system up time.

As the Pi is running Linux, nearly all of this operational data is available in "files" in the /proc and /sys folders.

pi@pi3:~ $ cat /proc/uptime
65134.41 243734.02

Also, if you want a full set of operational measures and history as well, consider using InfluxDB, and Telegraf. Maybe also Grafana for a nice dashboard.

Here is the swap use on my Pi3 - from which you can easily see that it is somewhat overloaded (thanks to running the controller for my Unifi WiFi access points - Java based :poop:)

image

The first NR custom node I wrote used the sys and proc methods to grab data, just was an interesting way to learn now to write custom nodes for NR. Usually with python I got for the sys and proc route to get system data to avoid dependency on 3rd party modules, doing the same in JavaScript was learning experience since at the time I had almost no time in with JS. Using exec nodes versus other methods always has trade offs.

I am at the point where I think I want a better dashboard, you gave me another idea to review... cool.

The tool set of InfluxDB, Telegraf and Grafana is amazing. So easy to capture the data, save it and analyse it. There are also plenty of pre-canned Grafana dashboards that you can use directly or filtch parts of.

It will all run easily on a Pi as well. Though be careful not to try and capture too much information in InfluxDB or your Pi will get very slow eventually.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.