trying to get cpu temperature systeminformation npm library - node.js

im trying to get CPU temperature using systeminformation in node.js, but getting null values in the output.
i also installed sensors before getting cpu information
this application is running on AWS EC2 instance
import si from 'systeminformation'
import { execSync } from 'child_process'
let output=execSync('sudo apt-get install lm-sensors')
const temperature = await si.cpuTemperature()
console.log(temperature)
output:
{ main: null, cores: [], max: null, socket: [], chipset: null }

AWS EC2 won't disclose its CPU temperature. But this should work in any local machine.

Related

How to get CPU utilization in % of a NodeJS process, on EC2 instance?

I have a express app which runs on local EC2 instance. When I run my express app in the localhost I am able to get CPU % utilization with the following code -
import os from "os";
import { memoryUsage } from "process";
import osu from "node-os-utils";
import { usageMetrics } from "../utils/usagemetric";
import * as osUtil from "os-utils";
// in the controller
const cpu = osu.cpu;
usageMetrics(cpu, process, os, osUtil);
export const usageMetrics = (cpu: any, process: any, os: any, osUtil: any) => {
const totalMemory = os.totalmem();
const rss = process.memoryUsage().rss;
const totalUnusedMemory = totalMemory - rss;
const percentageMemoryUnUsed = totalUnusedMemory / totalMemory;
console.log("system memory", totalMemory);
console.log("node process memory usage", rss);
console.log("Memory consumed in %:", 100 - percentageMemoryUnUsed * 100);
cpu.usage().then((info) => {
console.log("Node-OS-utils-CPU Usage(%):", info);
});
osUtil.cpuUsage(function (v) {
console.log(" OS-Util-CPU-Usage(%): " + v);
});
os.cpuUsage(function (v) {
console.log("native OS-CPU Usage(%):", +v);
});
};
The above code work well in the localhost giving value in 43,15,20,etc in %. But when I run in the EC2 instance it is showing me 1% or 2% sometimes. The real question is both the libraries os-utils and node-os-utils are giving 0 % as CPU utilization. Any help on how do I get actual CPU utilization with help of libraries or any native NodeJS methods?
While Running on EC2
While running on localhost

Serverless offline child process - spawn serverless ENOENT

I am using GitLab's serverless framework template and trying to run tests locally first, by creating a child serverless offline process. I am getting the error 'spawn serverless ENOENT' when executing the jest tests. Any idea why?
let useLocalService = true
...
let serverlessProcess = {
serverless_process: null,
start: async () => {
if(useLocalService) {
this.serverless_process = spawn('serverless', ['offline', '--port', 3000])
}
},
stop: () => {
if(useLocalService) {
this.serverless_process.kill('SIGINT')
}
}
}
describe('hello', () => {
beforeAll(async () => {
// serverlessProcess.start starts serverless offline in a child process
// unless the test is pointed at a deployed service
await serverlessProcess.start()
})
afterAll(() => {
// serverlessProcess.stop kills the child process at the end of the test
// unless the test is pointed at a deployed service
serverlessProcess.stop()
})
it(...
The only error message I recieve:
● Test suite failed to run
spawn serverless ENOENT
Are your trying to initiate any AWS service locally?
If you are trying to use any AWS plugins that is causing the error.
Make sure Java is installed
setup AWS service which you are trying locally eg: if dynamodb then
sls dynamodb install
If you still face issue then check serverless local issue by enabling DEBUG
export SLS_DEBUG=true

nodejs abort fetch in lambda AWS environment

I managed to have a working abort controller in nodejs on a local environment, something as simple as this:
const signal = controller.signal;
setTimeout(() => controller.abort(), 5000);
fetch(url, { signal }).then(response => {
return response.text();
}).then(text => {
console.log(text);
});
So I am able to abort the request after n seconds.It works without issues in local environment, however when deployed to AWS in a Lambda I endup with and error
TypeError: Expected signal to be an instanceof AbortSignal
After digging in the fetch module source it seems that this line is the culprit:
!isAbortSignal(signal)
From this function::
function isAbortSignal(signal) {
const proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
return !!(proto && proto.constructor.name === 'AbortSignal');
}
The proto.constructor.name in local is correct as "AbortSignal" while in AWS lambda it ends up being just 'a'.
I searched around but I could not find anything specific about why this is not working. Any tips on that?
Are you using the node-fetch module? fetch and AbortController don't exist in native nodejs. You'll need to use the nodejs polyfill for each.
https://www.npmjs.com/package/node-abort-controller
Looks like it should be fixed in node-fetch v3 (currently in beta)
In my case, I'm using Webpack to bundle before uploading the code to AWS, after upgrading to 3.0.0-beta.4 it worked.
If you cant upgrade and using webpack try:
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
keep_classnames: true,
keep_fnames: true,
},
}),
],
},
and if it doesn't work try to config optimization.minimize to false

Get GPU temperature NODEJS

I'm trying to get gpu temperature using nodeJS.
I found one package on npm called "systeminformation" but I cant get gpu temperature from it.
If there is no package/module for it I would like to know a way how to do it from NodeJS.
There are not Node.js packages with C/C++ submodules for checking GPU temperature, but you can use CLI for that.
Pros and cons:
👍 Easy
👍 You need to know only the CLI command for your OS
👎 performance can be slow
👎 maybe you need run your app with sudo
For Ubuntu the CLI command looks like:
nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader
Any CLI command execution is async operation so you need callbacks or promises or generators. I prefer async/await approach.
Example with async/await for 8.9.x Node.js:
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
const gpuTempeturyCommand = 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader'; // change it for your OS
async function getGPUTemperature() {
try {
const result = await execAsync(gpuTempeturyCommand);
return result.stdout;
} catch (error) {
console.log('Error during getting GPU temperature');
return 'unknown';
}
}

firmatajs, multiple Arduinos give timeout (johnny-five, cylonjs)

I have two Arduino unos flashed with the standard StandardFirmata and i'm trying the multi board demo with a simple node project (johnny-five npm package). Both arduinos work when I try them separately. with the following code:
var five = require("johnny-five");
var boardOne = new five.Board({ id: "A", port: "/dev/cu.usbmodem1d1141" });
boardOne.on("ready", function(){
var led = new five.Led({
pin: 13,
board: this
});
led.on();
});
node index.js
1418288836782 Connected /dev/cu.usbmodem1d1141
1418288836784 Repl Initialized
>>
When trying the multi board example I get:
Device or Firmware Error A timeout occurred while connecting to the Board.
Please check that you've properly flashed the board with the correct firmware.
var five = require("johnny-five");
var ports = [
{ id: "A", port: "/dev/cu.usbmodem1d1141" },
{ id: "B", port: "/dev/cu.usbmodem1d1131" }
];
new five.Boards(ports).on("ready", function(){
var led = new five.Led({
pin: 13,
board: this[0]
});
led.on();
});
Update #1:
Out of curiosity I tried to switch around the usb cables and got some different results:
1) Only one arduino seems to connect:
1418318698635 Device(s) /dev/cu.usbmodem1a1231,/dev/cu.usbmodem1a1241
1418318698642 Device(s) /dev/cu.usbmodem1a1241
1418318701849 Connected /dev/cu.usbmodem1a1231
1418318701850 Board ID: A
or 2) I get an error:
.../johnny-five-master/node_modules/firmata/lib/firmata.js:246
board.pins[pin].analogChannel = currentValue;
^
TypeError: Cannot set property 'analogChannel' of undefined
at Object.SYSEX_RESPONSE.(anonymous function) [as 106]
(.../johnny-five-master/node_modules/firmata/lib/firmata.js:246:35)
Update #2:
I did the above test with cylon.js and got the same results. Still no clue how to fix this :(
One arduino works fine, multiple do nothing. (Maybe an osx related problem?)
Update #3:
I added some logs in the johnny-five code and it's definitely a connection problem(I think!?). The second Arduino never responds. I switched the order of the arduinos and get the same result (first one connects, the other fails to respond). The connection is asynchronous, so maybe it gets blocked somewhere. The lights on both arduinos definitely show some action is going on.
node index.js
err: undefined --- type: connect --- io: /dev/tty.usbmodem1d1111
err: undefined --- type: connect --- io: /dev/tty.usbmodem1d1121
err: undefined --- type: ready --- io: /dev/tty.usbmodem1d1111
1418467187527 Connected /dev/tty.usbmodem1d1111
1418467187527 Board ID: A
1418467284327 Device or Firmware Error A timeout occurred while connecting to the Board.
Please check that you've properly flashed the board with the correct firmware.
Thanks to #izar for posting this and then bringing the question to us in the Johnny-Five gitter channel. From there, Divan Visagie (from Johnny-Five core team) worked to triage the bug and was able to confirm via reproduction. This revealed a bug in Firmata.js, where the options passed to Serialport were being extended by that class. Since the defaults object was reused and Object.assign is not a "deep" operation, the changes were being made to a reference, not a copy. The result was that the second initialization was getting a set of "defaults" that were loaded up with the first instance's own data. The issue was fixed by changing Firmata to use fresh defaults for every instance. Here's the patch

Resources