Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions sensors/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
const os = require('os')
const childProcess = require('child_process')

const user = os.userInfo()
//console.log(user)

const plugin = {
/**
* * This appears in the title of the graph
*/
title: 'Process List',
mem1: 1,
description: `
This returns a process list, grouped by executable name. CPU % is divided by the number of cores.
100% CPU Usage is all cores being maxed out. Unlike other tools that define the maximum as 800% for 8 cores for example.`,
Expand All @@ -31,19 +35,9 @@ const plugin = {
initialized: false,

sort: 'cpu',

columns: ['Command', 'CPU %', 'Count', 'Memory %'],
currentValue: [{
'Command': 'Google Chrome',
'Count': '4',
'CPU %': '0.4',
'Memory %': '1'
}, {
'Command': 'Sublime Text 2',
'Count': '1',
'CPU %': '0.1',
'Memory': '5'
}],
flag: 0,
columns: ['core Command','CPU%', 'Count','Memory'],
currentValue: [], // Default processes are non-empty

/**
* Grab the current value for the table
Expand All @@ -53,7 +47,7 @@ const plugin = {
// @todo If you can think of a better way of getting process stats,
// then please feel free to send me a pull request. This is version 0.1
// and needs some love.
childProcess.exec('ps -ewwwo %cpu,%mem,comm', (error, stdout, stderr) => {
childProcess.exec('ps -ewwwo %cpu,%mem,psr,comm', (error, stdout, stderr) => { // used psr command to acquire details of core
if (error) {
console.error(error)
}
Expand All @@ -63,15 +57,19 @@ const plugin = {
for (const line in lines) {
const currentLine = lines[line].trim().replace(' ', ' ')
const words = currentLine.split(' ')
if (typeof words[0] !== 'undefined' && typeof words[1] !== 'undefined') {
if (typeof words[0] !== 'undefined' && typeof words[1] !== 'undefined' && typeof words[2] !== 'undefined') {
let core = words[2].replace(',','.') // preprocessing
const cpu = words[0].replace(',', '.')
const mem = words[1].replace(',', '.')

const offset = cpu.length + mem.length + 2
let comm = currentLine.slice(offset)
// If we're on Mac then remove the path
if (/^darwin/.test(process.platform)) {
var a = comm.slice(0,6)
comm = comm.split('/')
comm = comm[comm.length - 1]
comm = a.concat(comm)
} else {
// Otherwise assume linux and remove the unnecessary /1 info like
// you get on kworker
Expand All @@ -81,6 +79,7 @@ const plugin = {
// If already exists, then add them together
if (typeof stats[comm] !== 'undefined') {
stats[comm] = {
core: parseFloat(stats[comm].core, 10) + parseFloat(core), // calculating the value of cores
cpu: parseFloat(stats[comm].cpu, 10) + parseFloat(cpu),
mem: parseFloat(stats[comm].mem, 10) + parseFloat(mem),
comm,
Expand All @@ -89,6 +88,7 @@ const plugin = {
} else {
stats[comm] = {
cpu,
core,
mem,
comm,
count: 1
Expand All @@ -102,16 +102,20 @@ const plugin = {
const cpuRounded = parseFloat(stats[stat].cpu / os.cpus().length).toFixed(1)
const memRounded = parseFloat(stats[stat].mem).toFixed(1)
statsArray.push({
'Command': stats[stat].comm,
'core Command': stats[stat].comm, //pushing the core details in the statsarray
'Count': stats[stat].count,
'CPU %': cpuRounded,
'Memory %': memRounded,
'core': stats[stat].comm.slice(0,6),
'CPU%': cpuRounded,
'Memory': memRounded*plugin.mem1,
'cpu': stats[stat].cpu,
'mem': stats[stat].mem // exact cpu for comparison
})
}
statsArray.sort((a, b) => parseFloat(b[plugin.sort]) - parseFloat(a[plugin.sort]))

//plugin.flag = 0
if(plugin.flag == 1){
statsArray.reverse()
}
plugin.currentValue = statsArray
plugin.initialized = true
})
Expand Down