Skip to content

Commit

Permalink
Issue #5 Plugin doesn't work properly in non-US locales. (#8)
Browse files Browse the repository at this point in the history
* Switch comma to period as decimal separator

* Changed memory utilization to use /proc/meminfo as it is less sensitive to different locales

* Fix to work correctly in other locales
  • Loading branch information
sberl authored Mar 30, 2023
1 parent 4490400 commit 4015db1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
56 changes: 38 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const spawn = require('child_process').spawn
const gpu_temp_command = 'vcgencmd measure_temp'
const cpu_temp_command = 'cat /sys/class/thermal/thermal_zone0/temp'
const cpu_util_mpstat_command = 'S_TIME_FORMAT=\'ISO\' mpstat -P ALL 5 1 | sed -n 4,8p'
const mem_util_command = 'free'
const mem_util_command = 'cat /proc/meminfo'
const sd_util_command = 'df --output=pcent \/\| tail -1 \| awk \'gsub\(\"\%\",\"\"\)\''

module.exports = function(app) {
Expand Down Expand Up @@ -204,7 +204,7 @@ module.exports = function(app) {
}
newPath = newPath + "core." + (Number(spl_line[1])+1).toString()
newPath = newPath + "." + pathArray[(pathArray.length-1)]
var cpu_util_core = ((100 - Number(spl_line[11]))/100).toFixed(2)
var cpu_util_core = ((100 - Number(spl_line[11].replace(/,/, '.')))/100).toFixed(2)
app.handleMessage(plugin.id, {
updates: [
{
Expand All @@ -218,7 +218,7 @@ module.exports = function(app) {
}
else {
debug(`cpu utilisation is ${spl_line[11]}`)
cpu_util_all = ((100 - Number(spl_line[11]))/100).toFixed(2)
cpu_util_all = ((100 - Number(spl_line[11].replace(/,/, '.')))/100).toFixed(2)
app.handleMessage(plugin.id, {
updates: [
{
Expand Down Expand Up @@ -249,24 +249,44 @@ module.exports = function(app) {
memutil.stdout.on('data', (data) => {
debug(`got memory ${data}`)
var mem_util = data.toString().replace(/(\n|\r)+$/, '').split('\n')
mem_util.forEach(function(mem_util_line){
var mem_total
var mem_free
var buffers
var cached
var slab
mem_util.forEach(function(mem_util_line) {
var splm_line = mem_util_line.replace(/ +/g, ' ').split(' ')
if (splm_line[0].toString() === "Mem:"){
var mem_util_per = (Number(splm_line[2])/Number(splm_line[1])).toFixed(2)
app.handleMessage(plugin.id, {
updates: [
{
values: [ {
path: options.path_mem_util,
value: Number(mem_util_per)
}]
}
]
})
}
if (splm_line[0].toString() === "MemTotal:") {
mem_total = Number(splm_line[1])
debug(`got mem_total = ${mem_total}`)
} else if (splm_line[0].toString() === "MemFree:") {
mem_free = Number(splm_line[1])
debug(`got mem_free = ${mem_free}`)
} else if (splm_line[0].toString() === "Buffers:") {
buffers = Number(splm_line[1])
debug(`got buffers = ${buffers}`)
} else if (splm_line[0].toString() === "Cached:") {
cached = Number(splm_line[1])
debug(`got cached = ${cached}`)
} else if (splm_line[0].toString() === "Slab:") {
slab = Number(splm_line[1])
debug(`got slab = ${slab}`)
}
})
var mem_util_per = ((mem_total - (mem_free + buffers + cached + slab))/mem_total).toFixed(2)
debug(`mem_util_per: ${mem_util_per}`)

app.handleMessage(plugin.id, {
updates: [
{
values: [ {
path: options.path_mem_util,
value: Number(mem_util_per)
}]
}
]
})
})

memutil.on('error', (error) => {
console.error(error.toString())
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "signalk-rpi-monitor",
"version": "1.1.0",
"version": "1.1.1",
"description": "Signal K Node Server Plugin for Raspberry PI monitoring",
"homepage": "https://github.com/sberl/signalk-rpi-monitor#readme",
"keywords": [
Expand Down

0 comments on commit 4015db1

Please sign in to comment.