From aa091723666c4ff7a82deff84b06686ac965c9a8 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Fri, 24 Jun 2022 18:43:22 +0200 Subject: [PATCH] processes() improved start time parsing --- lib/processes.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/processes.js b/lib/processes.js index 6c41707..79af85d 100644 --- a/lib/processes.js +++ b/lib/processes.js @@ -98,13 +98,12 @@ function parseElapsedTime(etime) { const days = timeIndex > 0 ? parseInt(elapsed[timeIndex - 1]) : 0; const timeStr = elapsed[timeIndex].split(':'); - const hours = parseInt(timeStr[0] || 0); - const mins = parseInt(timeStr[1] || 0); - const secs = parseInt(timeStr[2] || 0); - + const hours = timeStr.length === 3 ? parseInt(timeStr[0] || 0) : 0; + const mins = parseInt(timeStr[timeStr.length === 3 ? 1 : 0] || 0); + const secs = parseInt(timeStr[timeStr.length === 3 ? 2 : 1] || 0); const ms = (((((days * 24 + hours) * 60) + mins) * 60 + secs) * 1000); - const res = new Date(current.getTime() - (((((days * 24 + hours) * 60) + mins) * 60 + secs) * 1000)); + const res = new Date(current.getTime() - ms); return res.toISOString().substring(0, 10) + ' ' + res.toISOString().substring(11, 19); } @@ -570,7 +569,7 @@ function processes(callback) { checkColumn(7); const nice = parseInt(line.substring(parsedhead[7].from + offset, parsedhead[7].to + offset2)) || 0; checkColumn(8); - const started = _darwin ? parseElapsedTime(line.substring(parsedhead[8].from + offset, parsedhead[8].to + offset2).trim()) : parseTimeUnix(line.substring(parsedhead[8].from + offset, parsedhead[8].to + offset2).trim()); + const started = !_sunos ? parseElapsedTime(line.substring(parsedhead[8].from + offset, parsedhead[8].to + offset2).trim()) : parseTimeUnix(line.substring(parsedhead[8].from + offset, parsedhead[8].to + offset2).trim()); checkColumn(9); let state = line.substring(parsedhead[9].from + offset, parsedhead[9].to + offset2).trim(); state = (state[0] === 'R' ? 'running' : (state[0] === 'S' ? 'sleeping' : (state[0] === 'T' ? 'stopped' : (state[0] === 'W' ? 'paging' : (state[0] === 'X' ? 'dead' : (state[0] === 'Z' ? 'zombie' : ((state[0] === 'D' || state[0] === 'U') ? 'blocked' : 'unknown'))))))); @@ -713,8 +712,8 @@ function processes(callback) { if ((_processes_cpu.ms && Date.now() - _processes_cpu.ms >= 500) || _processes_cpu.ms === 0) { if (_linux || _freebsd || _openbsd || _netbsd || _darwin || _sunos) { - if (_linux) { cmd = 'export LC_ALL=C; ps -axo pid:11,ppid:11,pcpu:6,pmem:6,pri:5,vsz:11,rss:11,ni:5,lstart:30,state:5,tty:15,user:20,command; unset LC_ALL'; } - if (_freebsd || _openbsd || _netbsd) { cmd = 'export LC_ALL=C; ps -axo pid,ppid,pcpu,pmem,pri,vsz,rss,ni,lstart,state,tty,user,command; unset LC_ALL'; } + if (_linux) { cmd = 'export LC_ALL=C; ps -axo pid:11,ppid:11,pcpu:6,pmem:6,pri:5,vsz:11,rss:11,ni:5,etime:30,state:5,tty:15,user:20,command; unset LC_ALL'; } + if (_freebsd || _openbsd || _netbsd) { cmd = 'export LC_ALL=C; ps -axo pid,ppid,pcpu,pmem,pri,vsz,rss,ni,etime,state,tty,user,command; unset LC_ALL'; } if (_darwin) { cmd = 'ps -axo pid,ppid,pcpu,pmem,pri,vsz=temp_title_1,rss=temp_title_2,nice,etime=temp_title_3,state,tty,user,command -r'; } if (_sunos) { cmd = 'ps -Ao pid,ppid,pcpu,pmem,pri,vsz,rss,nice,stime,s,tty,user,comm'; } exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {