From 68c5fd91cedd5afd214ee2ea1ec9298f87cee014 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 7 Nov 2017 13:22:09 +0100 Subject: [PATCH] added bios and main board information --- .editorconfig | 2 +- CHANGELOG.md | 3 +- README.md | 48 +++++---- docs/ISSUE_TEMPLATE.md | 6 ++ lib/cpu.js | 14 +-- lib/docker.js | 10 -- lib/index.js | 26 +++-- lib/system.js | 227 ++++++++++++++++++++++++++++++++++------- lib/users.js | 38 +------ lib/util.js | 36 +++++++ 10 files changed, 289 insertions(+), 121 deletions(-) diff --git a/.editorconfig b/.editorconfig index 3087260..b6ea430 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,7 +11,7 @@ end_of_line = lf indent_style = space indent_size = 2 insert_final_newline = true -trim_trailing_whitespace = true +# trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = true diff --git a/CHANGELOG.md b/CHANGELOG.md index e829d7b..7857e86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,7 @@ New Functions Bug Fixes / improvements -- improvement `cpuTemperature` - works now also on Raspberry Pi +- improvement `cpuTemperature` - works now also on Raspberry Pi - bugfix `disksIO` - on OSX read and write got mixed up - several bug fixes (like assess errors in `cpuCurrentspeed`, potentially incorrect results in `users`, ...) - testet on even more platforms and linux distributions @@ -99,6 +99,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.33.0 | 2017-11-07 | added bios and main board information | | 3.32.4 | 2017-11-02 | AMD cpu base frequencies table also for windows | | 3.32.3 | 2017-11-02 | code cleanup, AMD cpu base frequencies table | | 3.32.2 | 2017-11-01 | bugfix JSON.parse error `blockDevices()` | diff --git a/README.md b/README.md index 9e5b9be..98c18f1 100644 --- a/README.md +++ b/README.md @@ -52,14 +52,14 @@ async function cpu() { ### Latest Activity -(last 7 major and minor version releases) +(last 7 major and minor version releases) +- Version 3.33.0: added bios `bios()` and main board `baseboard()` information - Version 3.32.0: extended `memLayout()` - added manufacturer - Version 3.31.0: extended windows support `cpuFlags()` (partially) - Version 3.30.0: extended `versions()` (added `yarn`, `gulp`, `grunt`, `tsc`, `git`) - Version 3.29.0: extended windows support `services()` - Version 3.28.0: extended windows support `processes()` - Version 3.27.0: added raw data to `currentLoad()`, fixed `networkInterfaces()` MAC problem node 8.x -- Version 3.26.0: improved windows support `getDynamicData()`, updated docs - ... You can find all changes here: [detailed changelog][changelog-url] @@ -103,8 +103,20 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | version | X | X | X | version e.g. '1.0' | | | serial | X | X | X | serial number | | | uuid | X | X | X | UUID | +| | sku | X | | X | SKU number | +| si.bios(cb) | {...} | X | X | X | hardware information | +| | vendor | X | X | X | e.g. 'AMI' | +| | version | X | | X | Version | +| | releaseDate | X | | X | Release Date | +| | revision | X | | X | Revision | +| si.baseboard(cb) | {...} | X | X | X | hardware information | +| | manufacturer | X | X | X | e.g. 'ASUS' | +| | model | X | X | X | Model / Product Name | +| | version | X | X | X | Version | +| | serial | X | X | X | Serial Number | +| | assetTag | X | X | X | Asset Tag | -#### 3. CPU, Memory, Disks, Battery, Graphics +#### 3. CPU, Memory, Disks, Battery, Graphics | Function | Result object | Linux | OSX | Win | Comments | | --------------- | ----- | ----- | ---- | ------- | -------- | @@ -298,7 +310,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | ms | X | X | X | response time in ms | | si.inetLatency(host, cb) | : number | X | X | X | response-time (ms) to external resource
host parameter is optional (default 8.8.8.8)| -#### 7. Current Load, Processes & Services +#### 7. Current Load, Processes & Services | Function | Result object | Linux | OSX | Win | Comments | | --------------- | ----- | ----- | ---- | ------- | -------- | @@ -346,7 +358,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | [0].pcpu | X | X | | process % CPU | | | [0].pmem | X | X | | process % MEM | -#### 8. Docker +#### 8. Docker | Function | Result object | Linux | OSX | Win | Comments | | --------------- | ----- | ----- | ---- | ------- | -------- | @@ -467,31 +479,31 @@ async function network() { ## Known Issues -#### OSX - Temperature Sensor - -To be able to measure temperature on OSX I created a litte additional package. Due to some difficulties -in NPM with `optionalDependencies` I unfortunately was getting unexpected warnings on other platforms. -So I decided to drop this optional dependencies for OSX - so by default, you will not get correct values. +#### OSX - Temperature Sensor -But if you need to detect OSX temperature just run the following additional +To be able to measure temperature on OSX I created a litte additional package. Due to some difficulties +in NPM with `optionalDependencies` I unfortunately was getting unexpected warnings on other platforms. +So I decided to drop this optional dependencies for OSX - so by default, you will not get correct values. + +But if you need to detect OSX temperature just run the following additional installation command: ```bash $ npm install osx-temperature-sensor --save ``` - + `systeminformation` will then detect this additional library and return the temperature when calling systeminformations standard function `cpuTemperature()` #### Windows Temperature, Battery, ... -`wmic` - which is used to determine temperature and battery sometimes needs to be run with admin -privileges. So if you do not get any values, try to run it again with according -privileges. If you still do not get any values, your system might not support this feature. -In some cases we also discovered that `wmic` returned incorrect temperature values. +`wmic` - which is used to determine temperature and battery sometimes needs to be run with admin +privileges. So if you do not get any values, try to run it again with according +privileges. If you still do not get any values, your system might not support this feature. +In some cases we also discovered that `wmic` returned incorrect temperature values. #### Linux Temperature -In some cases you need to install the linux `sensors` package to be able to measure temperature +In some cases you need to install the linux `sensors` package to be able to measure temperature e.g. on DEBIAN based systems by running `sudo apt-get install lm-sensors` #### *: Additional Notes @@ -532,7 +544,7 @@ Written by Sebastian Hildebrandt [sebhildebrandt](https://github.com/sebhildebra - Adam Reis [adamreisnz](https://github.com/adamreisnz) OSX Temperature: Credits here are going to: - + - Massimiliano Marcon [mmarcon](https://github.com/mmarcon) for his work on [smc-code][smc-code-url] - Sébastien Lavoie [lavoiesl](https://github.com/lavoiesl) for his work on [osx-cpu-temp][osx-cpu-temp-url] code. diff --git a/docs/ISSUE_TEMPLATE.md b/docs/ISSUE_TEMPLATE.md index a66f549..52ad26e 100644 --- a/docs/ISSUE_TEMPLATE.md +++ b/docs/ISSUE_TEMPLATE.md @@ -1,3 +1,9 @@ +### Issue Type + +* [ ] Bug +* [ ] Feature Request +* [ ] Question + ### Expected behavior - diff --git a/lib/cpu.js b/lib/cpu.js index f97fb64..88252f3 100644 --- a/lib/cpu.js +++ b/lib/cpu.js @@ -17,11 +17,13 @@ const exec = require('child_process').exec; const fs = require('fs'); const util = require('./util'); -let _platform = os.type(); +let _platform = process.platform; -const _linux = (_platform === 'Linux'); -const _darwin = (_platform === 'Darwin'); -const _windows = (_platform === 'Windows_NT'); +const _linux = (_platform === 'linux'); +const _darwin = (_platform === 'darwin'); +const _windows = (_platform === 'win32'); +const _freebsd = (_platform === 'freebsd'); +const _sunos = (_platform === 'sunos'); let _cpu_speed = '0.00'; let _current_cpu = { @@ -199,7 +201,7 @@ function getCpu() { result.speed = modelline.split('@')[1] ? parseFloat(modelline.split('@')[1].trim()).toFixed(2) : '0.00'; if (result.speed === '0.00' && result.brand.indexOf('AMD') > -1) { result.speed = getAMDSpeed(result.brand); - } + } if (result.speed === '0.00') { let current = getCpuCurrentSpeedSync(); if (current !== '0.00') result.speed = current; @@ -258,7 +260,7 @@ function getCpu() { result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; if (!result.speed && result.brand.indexOf('AMD') > -1) { result.speed = getAMDSpeed(result.brand); - } + } if (!result.speed) { result.speed = result.speedmax; } diff --git a/lib/docker.js b/lib/docker.js index 9ac3a7f..dacf4a5 100644 --- a/lib/docker.js +++ b/lib/docker.js @@ -51,12 +51,7 @@ function dockerContainers(all, callback) { _docker_socket.listContainers(all, data => { let docker_containers = {}; - // let cmd = "curl --unix-socket /var/run/docker.sock http:/containers/json" + (all ? "?all=1" : ""); - // exec(cmd, function (error, stdout) { - // if (!error) { try { - // let jsonString = stdout.toString(); - // let docker_containers = JSON.parse(jsonString); docker_containers = data; if (docker_containers && Object.prototype.toString.call(docker_containers) === '[object Array]' && docker_containers.length > 0) { docker_containers.forEach(function (element) { @@ -221,12 +216,7 @@ function dockerContainerStats(containerID, callback) { } _docker_socket.getStats(containerID, data => { - // let cmd = "curl --unix-socket /var/run/docker.sock http:/containers/" + containerID + "/stats?stream=0"; - // exec(cmd, function (error, stdout) { - // if (!error) { - // let jsonString = stdout.toString(); try { - // let stats = JSON.parse(jsonString); let stats = data; /** * @namespace diff --git a/lib/index.js b/lib/index.js index 845b2bb..c1d7984 100644 --- a/lib/index.js +++ b/lib/index.js @@ -128,7 +128,9 @@ function getStaticData(callback) { data.version = version(); Promise.all([ - system(), + system.system(), + system.bios(), + system.baseboard(), osInfo.osInfo(), osInfo.versions(), cpu.cpu(), @@ -139,14 +141,16 @@ function getStaticData(callback) { filesystem.diskLayout() ]).then(res => { data.system = res[0]; - data.os = res[1]; - data.versions = res[2]; - data.cpu = res[3]; - data.cpu.flags = res[4]; - data.graphics = res[5]; - data.net = res[6]; - data.memLayout = res[7]; - data.diskLayout = res[8]; + data.bios = res[1]; + data.baseboard = res[2]; + data.os = res[3]; + data.versions =res[4]; + data.cpu = res[5]; + data.cpu.flags = res[6]; + data.graphics = res[7]; + data.net = res[8]; + data.memLayout = res[9]; + data.diskLayout = res[10]; if (callback) { callback(data); } resolve(data); }); @@ -330,7 +334,9 @@ function getAllData(srv, iface, callback) { // ---------------------------------------------------------------------------------- exports.version = version; -exports.system = system; +exports.system = system.system; +exports.bios = system.bios; +exports.baseboard = system.baseboard; exports.time = osInfo.time; exports.osInfo = osInfo.osInfo; diff --git a/lib/system.js b/lib/system.js index 2759635..713b0fa 100644 --- a/lib/system.js +++ b/lib/system.js @@ -9,20 +9,22 @@ // ---------------------------------------------------------------------------------- // License: MIT // ================================================================================== -// 2. System (Hardware) +// 2. System (Hardware, BIOS, Base Board) // ---------------------------------------------------------------------------------- -const os = require('os'); const exec = require('child_process').exec; const fs = require('fs'); +const util = require('./util'); -let _platform = os.type(); +let _platform = process.platform; -const _linux = (_platform === 'Linux'); -const _darwin = (_platform === 'Darwin'); -const _windows = (_platform === 'Windows_NT'); +const _linux = (_platform === 'linux'); +const _darwin = (_platform === 'darwin'); +const _windows = (_platform === 'win32'); +const _freebsd = (_platform === 'freebsd'); +const _sunos = (_platform === 'sunos'); -module.exports = function (callback) { +function system(callback) { return new Promise((resolve) => { process.nextTick(() => { @@ -32,27 +34,26 @@ module.exports = function (callback) { model: 'Computer', version: '', serial: '-', - uuid: '-' + uuid: '-', + sku: '-', }; if (_linux) { exec('dmidecode -t system', function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); - lines.forEach(function (line) { - if (line.indexOf(':') !== -1) { - if (line.toLowerCase().indexOf('manufacturer') !== -1) result.manufacturer = result.manufacturer || line.split(':')[1].trim(); - if (line.toLowerCase().indexOf('product name') !== -1) result.model = line.split(':')[1].trim(); - if (line.toLowerCase().indexOf('version') !== -1) result.version = result.version || line.split(':')[1].trim(); - if (line.toLowerCase().indexOf('serial number') !== -1) result.serial = line.split(':')[1].trim(); - if (line.toLowerCase().indexOf('uuid') !== -1) result.uuid = line.split(':')[1].trim(); - } - }); + result.manufacturer = util.getValue(lines, 'manufacturer'); + result.model = util.getValue(lines, 'product name'); + result.version = util.getValue(lines, 'version'); + result.serial = util.getValue(lines, 'serial number'); + result.uuid = util.getValue(lines, 'uuid'); + result.sku = util.getValue(lines, 'sku number'); if (result.serial.toLowerCase().indexOf('o.e.m.') !== -1) result.serial = '-'; if (result.manufacturer.toLowerCase().indexOf('o.e.m.') !== -1) result.manufacturer = ''; if (result.model.toLowerCase().indexOf('o.e.m.') !== -1) result.model = 'Computer'; if (result.version.toLowerCase().indexOf('o.e.m.') !== -1) result.version = '-'; - + if (result.sku.toLowerCase().indexOf('o.e.m.') !== -1) result.sku = '-'; + if (result.manufacturer === '' && result.model === 'Computer' && result.version === '-') { // Check Raspberry Pi exec('grep Hardware /proc/cpuinfo; grep Serial /proc/cpuinfo; grep Revision /proc/cpuinfo', function (error, stdout) { @@ -64,7 +65,7 @@ module.exports = function (callback) { if (line.toLowerCase().indexOf('revision') !== -1) result.version = line.split(':')[1].trim(); if (line.toLowerCase().indexOf('serial') !== -1) result.serial = line.split(':')[1].trim(); } - }); + }); if (result.model === 'BCM2835') { // Pi 3 result.manufacturer = 'Raspberry Pi Foundation'; result.model = result.model + ' - Pi 3 Model B'; @@ -119,6 +120,7 @@ module.exports = function (callback) { } } } + if (callback) { callback(result); } resolve(result); }); @@ -127,7 +129,7 @@ module.exports = function (callback) { resolve(result); } } else { - exec("dmesg | grep -i virtual | grep -iE 'vmware|qemu|kvm|xen'", function (error, stdout) { + exec('dmesg | grep -i virtual | grep -iE "vmware|qemu|kvm|xen"', function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); if (lines.length > 0) result.model = 'Virtual machine'; @@ -144,31 +146,31 @@ module.exports = function (callback) { if (_darwin) { exec('ioreg -c IOPlatformExpertDevice -d 2', function (error, stdout) { if (!error) { - let lines = stdout.toString().split('\n'); - lines.forEach(function (line) { - line = line.replace(/[<>"]/g, ''); - if (line.indexOf('=') !== -1) { - if (line.toLowerCase().indexOf('manufacturer') !== -1) result.manufacturer = line.split('=')[1].trim(); - if (line.toLowerCase().indexOf('model') !== -1) result.model = line.split('=')[1].trim(); - if (line.toLowerCase().indexOf('version') !== -1) result.version = line.split('=')[1].trim(); - if (line.toLowerCase().indexOf('ioplatformserialnumber') !== -1) result.serial = line.split('=')[1].trim(); - if (line.toLowerCase().indexOf('ioplatformuuid') !== -1) result.uuid = line.split('=')[1].trim(); - } - }); + let lines = stdout.toString().replace(/[<>"]/g, '').split('\n'); + result.manufacturer = util.getValue(lines, 'manufacturer', '=', true); + result.model = util.getValue(lines, 'model', '=', true); + result.version = util.getValue(lines, 'version', '=', true); + result.serial = util.getValue(lines, 'ioplatformserialnumber', '=', true); + result.uuid = util.getValue(lines, 'ioplatformuuid', '=', true); + result.sku = util.getValue(lines, 'board-id', '=', true); } if (callback) { callback(result); } resolve(result); }); } if (_windows) { - exec('wmic csproduct get', function (error, stdout) { + // exec('wmic csproduct get', function (error, stdout) { + // ToDo: refactor /value + exec('wmic csproduct get /value', function (error, stdout) { if (!error) { - let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/); - result.manufacturer = lines[5]; - result.model = lines[3]; - result.version = lines[6]; - result.serial = lines[2]; - result.uuid = lines[4]; + // let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/); + let lines = stdout.split('\r\n'); + result.manufacturer = util.getValue(lines, 'vendor', '='); + result.model = util.getValue(lines, 'name', '='); + result.version = util.getValue(lines, 'version', '='); + result.serial = util.getValue(lines, 'identifyingnumber', '='); + result.uuid = util.getValue(lines, 'uuid', '='); + result.sku = util.getValue(lines, 'skunumber', '='); } if (callback) { callback(result); } resolve(result); @@ -176,4 +178,151 @@ module.exports = function (callback) { } }); }); -}; +} + +exports.system = system; + +function bios(callback) { + + return new Promise((resolve) => { + process.nextTick(() => { + + let result = { + vendor: '', + version: '', + releaseDate: '', + revision: '', + }; + let cmd = ''; + if (_linux) { + if (process.arch === 'arm') { + cmd = 'cat /proc/cpuinfo | grep Serial'; + + } else { + cmd = 'dmidecode --type 0'; + } + exec(cmd, function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + result.vendor = util.getValue(lines, 'Vendor'); + result.version = util.getValue(lines, 'Version'); + const datetime = util.getValue(lines, 'Release Date'); + result.releaseDate = datetime.date; + result.revision = util.getValue(lines, 'BIOS Revision'); + } + + if (callback) { callback(result); } + resolve(result); + }); + } + if (_darwin) { + result.vendor = 'Apple Inc.'; + if (callback) { callback(result); } + resolve(result); + } + if (_windows) { + // ToDo: check BIOS windows + exec('wmic bios get BIOSversion, BuildNumber, Caption, Description, IdentificationCode, Manufacturer, Name, ReleaseDate, Version /value', function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\r\n'); + const description = util.getValue(lines, 'description', '='); + if (description.indexOf(' Version ') !== -1) { + // ... Phoenix ROM BIOS PLUS Version 1.10 A04 + result.vendor = description.split(' Version ')[0].trim(); + result.version = description.split(' Version ')[1].trim(); + } else { + result.vendor = util.getValue(lines, 'manufacturer', '='); + result.version = util.getValue(lines, 'version', '='); + } + result.releaseDate = util.getValue(lines, 'releasedate', '='); + if (result.releaseDate.length >= 10) { + result.releaseDate = result.releaseDate.substr(0,4) + '-' + result.releaseDate.substr(4,2) + '-' + result.releaseDate.substr(6,2); + } + result.revision = util.getValue(lines, 'buildnumber', '='); + } + + if (callback) { callback(result); } + resolve(result); + }); + } + }); + }); +} + +exports.bios = bios; + +function baseboard(callback) { + + return new Promise((resolve) => { + process.nextTick(() => { + + let result = { + manufacturer: '', + model: '', + version: '', + serial: '-', + assetTag: '-', + }; + let cmd = ''; + if (_linux) { + if (process.arch === 'arm') { + cmd = 'cat /proc/cpuinfo | grep Serial'; + // 'BCM2709', 'BCM2835', 'BCM2708' --> + } else { + cmd = 'dmidecode -t 2'; + } + exec(cmd, function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + result.manufacturer = util.getValue(lines, 'Manufacturer'); + result.model = util.getValue(lines, 'Product Name'); + result.version = util.getValue(lines, 'Version'); + result.serial = util.getValue(lines, 'Serial Number'); + result.assetTag = util.getValue(lines, 'Asset Tag'); + if (result.serial.toLowerCase().indexOf('o.e.m.') !== -1) result.serial = '-'; + if (result.assetTag.toLowerCase().indexOf('o.e.m.') !== -1) result.assetTag = '-'; + } + + if (callback) { callback(result); } + resolve(result); + }); + } + if (_darwin) { + exec('ioreg -c IOPlatformExpertDevice -d 2', function (error, stdout) { + if (!error) { + let lines = stdout.toString().replace(/[<>"]/g, '').split('\n'); + result.manufacturer = util.getValue(lines, 'manufacturer', '=', true); + result.model = util.getValue(lines, 'model', '=', true); + result.version = util.getValue(lines, 'version', '=', true); + result.serial = util.getValue(lines, 'ioplatformserialnumber', '=', true); + result.assetTag = util.getValue(lines, 'board-id', '=', true); + } + + if (callback) { callback(result); } + resolve(result); + }); + } + if (_windows) { + // ToDo: check BIOS windows + exec('wmic baseboard get manufacturer, model, partnumber, product, serialnumber, sku, version /value', function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\r\n'); + + result.manufacturer = util.getValue(lines, 'manufacturer', '='); + result.model = util.getValue(lines, 'model', '='); + result.version = util.getValue(lines, 'version', '='); + result.serial = util.getValue(lines, 'serialnumber', '='); + result.assetTag = util.getValue(lines, 'partnumber', '='); + } + + if (callback) { callback(result); } + resolve(result); + }); + } + }); + }); +} + +exports.baseboard = baseboard; + + diff --git a/lib/users.js b/lib/users.js index aa39d23..8a2af14 100644 --- a/lib/users.js +++ b/lib/users.js @@ -14,6 +14,7 @@ const os = require('os'); const exec = require('child_process').exec; +const util = require('./util'); let _platform = os.type(); @@ -134,41 +135,6 @@ function parseUsersDarwin(lines) { function parseUsersWin(lines) { - function parseDateTime(dt) { - const result = { - date: '', - time: '' - }; - const parts = dt.split(' '); - if (parts[0]) { - if (parts[0].indexOf('/') >= 0) { - // Dateformat: mm/dd/yyyy - const dtparts = parts[0].split('/'); - if (dtparts.length === 3) { - result.date = dtparts[2] + '-' + ('0' + dtparts[0]).substr(-2) + '-' + ('0' + dtparts[1]).substr(-2); - } - } - if (parts[0].indexOf('.') >= 0) { - // Dateformat: dd.mm.yyyy - const dtparts = parts[0].split('.'); - if (dtparts.length === 3) { - result.date = dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2); - } - } - if (parts[0].indexOf('-') >= 0) { - // Dateformat: yyyy-mm-dd - const dtparts = parts[0].split('-'); - if (dtparts.length === 3) { - result.date = dtparts[0] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[2]).substr(-2); - } - } - } - if (parts[1]) { - result.time = parts[1]; - } - return result; - } - let result = []; const header = lines[0]; const headerDelimiter = []; @@ -191,7 +157,7 @@ function parseUsersWin(lines) { if (lines[i].trim()) { const user = lines[i].substring(headerDelimiter[0]+1, headerDelimiter[1]).trim() || ''; const tty = lines[i].substring(headerDelimiter[1]+1, headerDelimiter[2] - 2).trim() || ''; - const dateTime = parseDateTime(lines[i].substring(headerDelimiter[5]+1, 2000).trim()) || ''; + const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5]+1, 2000).trim()) || ''; result.push({ user: user, tty: tty, diff --git a/lib/util.js b/lib/util.js index 336fd18..7ff364e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -86,9 +86,45 @@ function decodeEscapeSequence(str, base) { }); } +function parseDateTime(dt) { + const result = { + date: '', + time: '' + }; + const parts = dt.split(' '); + if (parts[0]) { + if (parts[0].indexOf('/') >= 0) { + // Dateformat: mm/dd/yyyy + const dtparts = parts[0].split('/'); + if (dtparts.length === 3) { + result.date = dtparts[2] + '-' + ('0' + dtparts[0]).substr(-2) + '-' + ('0' + dtparts[1]).substr(-2); + } + } + if (parts[0].indexOf('.') >= 0) { + // Dateformat: dd.mm.yyyy + const dtparts = parts[0].split('.'); + if (dtparts.length === 3) { + result.date = dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2); + } + } + if (parts[0].indexOf('-') >= 0) { + // Dateformat: yyyy-mm-dd + const dtparts = parts[0].split('-'); + if (dtparts.length === 3) { + result.date = dtparts[0] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[2]).substr(-2); + } + } + } + if (parts[1]) { + result.time = parts[1]; + } + return result; +} + exports.isFunction = isFunction; exports.unique = unique; exports.sortByKey= sortByKey; exports.cores = cores; exports.getValue = getValue; exports.decodeEscapeSequence = decodeEscapeSequence; +exports.parseDateTime = parseDateTime;