How to get current CPU and memory usage of the system?

Hi,

I need to get info about current CPU and memory usage of the system.
For example: Cpu usage is %15, Memory usage is %60
I’ll show that info on a specific web page. Can you help me how can I that get these info?

From a webextension, the only way is to use native messaging.

Is this possible in Google Chrome?

it’s possible in Chrome. I got an extension working just as I expected. But I am not sure it is web extension or not. I can post the Chrome extension’s if you’d like.

Can you please paste the code that you used to do this in Chrome. Maybe I was mistaken and there is a non-nativemessaging way.

background.js

var previousCpuInfo;
chrome.runtime.onMessageExternal.addListener(function (message, sender, callback) {
console.log(message)

if (message.openUrlInEditor){
    openUrl(message.openUrlInEditor);
}


switch(message.type) {
    case 'getScreen':

        var pending = chrome.desktopCapture.chooseDesktopMedia(message.options || ['screen', 'window'],
                                                               sender.tab, function (streamid) {
            // communicate this string to the app so it can call getUserMedia with it
            message.type = 'gotScreen';
            message.sourceId = streamid;
            callback(message);
            return false;
        });
        return true; // retain callback for chooseDesktopMedia result
    case 'cancelGetScreen':
        chrome.desktopCapture.cancelChooseDesktopMedia(message.request);
        message.type = 'canceledGetScreen';
        callback(message);
        return false; //dispose callback
    case 'systemInfo':
        console.log('systemInfo');

        chrome.system.cpu.getInfo(function(cpuInfo) {
            console.log(cpuInfo);
            cpuName = cpuInfo.modelName.replace(/\(R\)/g, '®').replace(/\(TM\)/, '™');
            cpuArch = cpuInfo.archName.replace(/_/g, '-');
            cpuFeatures = cpuInfo.features.join(', ').toUpperCase().replace(/_/g, '.') || '-';

            var sumOfUsage = 0;

            for (var i = 0; i < cpuInfo.numOfProcessors; i++) {
                var usage = cpuInfo.processors[i].usage;
                var usedSectionWidth = 0;

                if (previousCpuInfo) {
                    var oldUsage = previousCpuInfo.processors[i].usage;
                    usedSectionWidth = Math.floor((usage.kernel + usage.user - oldUsage.kernel - oldUsage.user) / (usage.total - oldUsage.total) * 100);
                } else {
                    usedSectionWidth = Math.floor((usage.kernel + usage.user) / usage.total * 100);
                }
                console.log(usedSectionWidth);
                sumOfUsage = sumOfUsage + usedSectionWidth;

            }
            previousCpuInfo = cpuInfo;
            console.log(Math.floor(100-sumOfUsage/4));

            callback(Math.floor(100-sumOfUsage/4));
        });

        return true;

    case 'memory':

        console.log('memory');
        chrome.system.memory.getInfo(function(memoryInfo) {
            var usedMemory = Math.floor(memoryInfo.availableCapacity / memoryInfo.capacity * 100);
            callback(usedMemory);
        });

        return true;
}

});

With this script, html page is getting CPU and memory usage information.

function systemInfo() {
    chrome.runtime.sendMessage("chrome extension id",
            {type: 'systemInfo', id: 1}, null,
            function (data) {
                console.log(data);
            }
    );
}

function networkInfo() {
    chrome.runtime.sendMessage("chrome extension id",
            {type: 'memory', id: 1}, null,
            function (data) {
                console.log(data);
            }
    );
}

Oh interesting. I didn’t know of the chrome.system APIs. It seems this is for Chrome Apps only though. Do Chrome extensions support this? If they do, I’m sure we can file some bugs for it.

The MDN documentation on WebExtensions makes no mention of system.***

Can we do it without using web extension. with addon sdk? I am a bit familiar with addon sdk.