Skip to content

Commit 6c084b3

Browse files
committed
Added OS info, updated CPU info
1 parent 006689b commit 6c084b3

File tree

9 files changed

+212
-64
lines changed

9 files changed

+212
-64
lines changed

.github/workflows/on-push.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: On Push
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Building
6+
runs-on: Linux
7+
steps:
8+
- name: Checking out repository
9+
uses: actions/checkout@v4
10+
- name: "Setup Node"
11+
uses: actions/setup-node@v4
12+
with:
13+
node-version: '22'
14+
cache: 'npm'
15+
- name: Installing
16+
run: npm install
17+
- name: Building
18+
run: npm run build
19+
20+
testing:
21+
name: Testing
22+
needs: build
23+
runs-on: Linux
24+
steps:
25+
- name: "Setup Node"
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: '22'
29+
cache: 'npm'
30+
- name: Installing
31+
run: npm install
32+
- name: Running linter
33+
run: npm run lint
34+
- name: Running tests
35+
run: npm run test

README.md

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,56 @@ Node module that provides utilities for interacting with the operating system an
1010

1111
JavaScript:
1212
```javascript
13-
const osUtils = require('lup-system');
13+
const lupSystem = require('lup-system');
1414

15-
osUtils.getCpuUtilization().then(utilization => console.log("CPU Utilization: " + utilization));
16-
osUtils.getDrives().then(drives => console.log("Drives: " + drives)); // Array of drive objects
17-
osUtils.getNetworkInterfaces().then(interfaces => console.log("Network Interfaces: " + interfaces));
18-
osUtils.getGPUs().then(gpus => console.log("GPU Info: " + gpus));
19-
osUtils.getTemperatures().then(temps => console.log("Temperatures: " + temps));
15+
lupSystem.getCpuInfo().then(cpuInfo => console.log("CPU Info: " + cpuInfo));
16+
lupSystem.getDrives().then(drives => console.log("Drives: " + drives)); // Array of drive objects
17+
lupSystem.getGPUs().then(gpus => console.log("GPU Info: " + gpus));
18+
lupSystem.getNetworkInterfaces().then(interfaces => console.log("Network Interfaces: " + interfaces));
19+
lupSystem.getOSInfo().then(osInfo => console.log("OS Info: " + osInfo));
20+
lupSystem.getTemperatures().then(temps => console.log("Temperatures: " + temps));
2021
```
2122

2223
TypeScript:
2324
```typescript
24-
import osUtils from 'lup-system';
25+
import lupSystem from 'lup-system';
2526

2627
(async () => {
27-
console.log("CPU Utilization: " + await osUtils.getCpuUtilization());
28-
console.log("Drives: ", await osUtils.getDrives()); // Array of drive objects
29-
console.log("Network Interfaces: ", await osUtils.getNetworkInterfaces());
30-
console.log("GPU Info: ", await osUtils.getGPUs());
31-
console.log("Temperatures: ", await osUtils.getTemperatures());
28+
console.log("CPU Info: ", await lupSystem.getCpuInfo());
29+
console.log("Drives: ", await lupSystem.getDrives()); // Array of drive objects
30+
console.log("GPU Info: ", await lupSystem.getGPUs());
31+
console.log("Network Interfaces: ", await lupSystem.getNetworkInterfaces());
32+
console.log("OS Info: ", await lupSystem.getOSInfo());
33+
console.log("Temperatures: ", await lupSystem.getTemperatures());
3234
})();
3335
```
3436

3537
Output:
3638
```
37-
CPU Utilization: 0.2313135420902806
39+
CPU Info: {
40+
architecture: 'x64',
41+
coreCount: 12,
42+
endian: 'LE',
43+
name: '11th Gen Intel(R) Core(TM) i5-11600K @ 3.90GHz',
44+
speed: 3912,
45+
utilization: {
46+
overall: 0.20021299254526093,
47+
cores: [
48+
0.20253164556962025,
49+
0.19230769230769232,
50+
0.6025641025641025,
51+
0,
52+
0.3974358974358974,
53+
0.20253164556962025,
54+
0,
55+
0,
56+
0.20253164556962025,
57+
0,
58+
0.19480519480519481,
59+
0.4050632911392405
60+
]
61+
}
62+
}
3863
Drives: [
3964
{
4065
filesystem: 'C:',
@@ -55,6 +80,24 @@ Drives: [
5580
utilization: 0.08308232033236287
5681
}
5782
]
83+
GPU Info: [
84+
{
85+
name: 'NVIDIA GeForce RTX 3060 Ti',
86+
status: 'ok',
87+
id: 'PCI\\VEN_10DE&DEV_2489&SUBSYS_884F1043&REV_A1\\4&2130FF93&0&0008',
88+
processor: 'NVIDIA GeForce RTX 3060 Ti',
89+
memory: 8589934592,
90+
driverDate: '14.05.2025 02:00:00',
91+
driverVersion: '32.0.15.7652',
92+
displayAttached: true,
93+
displayActive: true,
94+
fanSpeed: 0.53,
95+
utilization: 0.03,
96+
memoryUtilization: 0.01,
97+
temperature: 52,
98+
powerDraw: 48.32
99+
}
100+
]
58101
Network Interfaces: [
59102
{
60103
name: 'Loopback Pseudo-Interface 1',
@@ -74,24 +117,16 @@ Network Interfaces: [
74117
}
75118
}
76119
]
77-
GPU Info: [
78-
{
79-
name: 'NVIDIA GeForce RTX 3060 Ti',
80-
status: 'ok',
81-
id: 'PCI\\VEN_10DE&DEV_2489&SUBSYS_884F1043&REV_A1\\4&2130FF93&0&0008',
82-
processor: 'NVIDIA GeForce RTX 3060 Ti',
83-
memory: 8589934592,
84-
driverDate: '14.05.2025 02:00:00',
85-
driverVersion: '32.0.15.7652',
86-
displayAttached: true,
87-
displayActive: true,
88-
fanSpeed: 0.53,
89-
utilization: 0.03,
90-
memoryUtilization: 0.01,
91-
temperature: 52,
92-
powerDraw: 48.32
93-
}
94-
]
120+
OS Info: {
121+
name: 'Windows',
122+
version: '10.0.26100',
123+
architecture: 'x64',
124+
machine: 'x86_64',
125+
platform: 'win32',
126+
bits: 64,
127+
hostname: 'my-pc',
128+
uptime: 2025-07-25T09:38:27.126Z
129+
}
95130
Temperatures: {
96131
cpu: 45.2,
97132
gpu: 60.8,

TODO.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
memory.ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lup-system",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "NodeJS library to retrieve system information and utilization.",
55
"files": [
66
"lib/**/*"

src/__tests__/CPU.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getCpuUtilization, stopCpuUtilizationComputation } from '../cpu';
1+
import { getCpuInfo, stopCpuUtilizationComputation } from '../cpu';
22

3-
test('getCpuUtilization', async () => {
4-
const utilization = await getCpuUtilization();
5-
console.log('CPU utilization:', utilization); // TODO REMOVE
3+
test('getCpuInfo', async () => {
4+
const cpuInfo = await getCpuInfo();
5+
console.log('CPU Info:', cpuInfo); // TODO REMOVE
66
});
77

88
afterAll(() => {

src/__tests__/OS.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getOSInfo } from '../os';
2+
3+
test('getOSInfo', async () => {
4+
const osInfo = await getOSInfo();
5+
console.log(osInfo); // TODO REMOVE
6+
});

src/cpu.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export type CPU = {
2323
/** CPU architecture (e.g., x64, arm64). */
2424
architecture: string;
2525

26-
/** CPU model (e.g., Intel Core i7-9700K). */
27-
model: string;
26+
/** Endianness of the CPU (e.g., LE for little-endian, BE for big-endian). */
27+
endian: 'LE' | 'BE';
2828

2929
/** CPU speed in MHz. */
3030
speed: number;
@@ -43,17 +43,19 @@ export let CPU_COMPUTE_UTILIZATION_INTERVAL = 1000;
4343
const CPU_COMPUTE_UTILIZATION_INITIAL_DELAY = 50;
4444

4545
let PREV_CPU_CORES: os.CpuInfo[] = [];
46-
let CPU_CORE_UTILIZATIONS: number[] = [];
47-
let CPU_UTILIZATION: number = 0;
46+
const CPU_UTILIZATION: CPUUtilization = {
47+
overall: 0,
48+
cores: []
49+
};
4850
let CPU_COMPUTE_RUNNING = false;
4951
let CPU_COMPUTE_TIMEOUT: NodeJS.Timeout | null = null;
5052

5153
async function computeCpuUtilization() {
5254
const cpuCores = os.cpus();
5355
const min = Math.min(cpuCores.length, PREV_CPU_CORES.length);
54-
if (CPU_CORE_UTILIZATIONS.length < min)
55-
for (let i = CPU_CORE_UTILIZATIONS.length; i < min; i++) CPU_CORE_UTILIZATIONS.push(0);
56-
else if (CPU_CORE_UTILIZATIONS.length > min) CPU_CORE_UTILIZATIONS = CPU_CORE_UTILIZATIONS.slice(0, min);
56+
if (CPU_UTILIZATION.cores.length < min)
57+
for (let i = CPU_UTILIZATION.cores.length; i < min; i++) CPU_UTILIZATION.cores.push(0);
58+
else if (CPU_UTILIZATION.cores.length > min) CPU_UTILIZATION.cores = CPU_UTILIZATION.cores.slice(0, min);
5759

5860
let totalUsage = 0;
5961
let totalTotal = 0;
@@ -69,11 +71,11 @@ async function computeCpuUtilization() {
6971
const prevTotal = prevUsage + prev.times.idle;
7072
const total = nextTotal - prevTotal;
7173

72-
CPU_CORE_UTILIZATIONS[i] = usage / total;
74+
CPU_UTILIZATION.cores[i] = usage / total;
7375
totalUsage += usage;
7476
totalTotal += total;
7577
}
76-
CPU_UTILIZATION = totalTotal !== 0 ? totalUsage / totalTotal : 0;
78+
CPU_UTILIZATION.overall = totalTotal !== 0 ? totalUsage / totalTotal : 0;
7779
PREV_CPU_CORES = cpuCores;
7880
}
7981

@@ -95,39 +97,48 @@ export function stopCpuUtilizationComputation() {
9597
}
9698

9799

100+
98101
/**
99-
* Returns the number of CPU cores available on the system.
100-
*
101-
* @returns Number of CPU cores.
102+
* Returns information about the CPU.
103+
*
104+
* @returns CPU information.
102105
*/
103-
export function getCpuCoreCount(): number {
104-
return os.cpus().length;
106+
export async function getCpuInfo(): Promise<CPU> {
107+
const cpuCores = os.cpus();
108+
return {
109+
architecture: os.arch(),
110+
coreCount: cpuCores.length,
111+
endian: os.endianness() as 'LE' | 'BE',
112+
name: cpuCores[0].model,
113+
speed: cpuCores[0].speed,
114+
utilization: await getCpuUtilization(),
115+
}
105116
}
106117

118+
107119
/**
108-
* Returns the utilization of each CPU core as a percentage (0.0-1.0).
109-
*
110-
* @returns List of CPU core utilizations as percentages (0.0-1.0).
120+
* Returns the current CPU utilization.
121+
* If the computation is not running, it will start the computation and return the initial values.
122+
*
123+
* @returns CPU utilization data.
111124
*/
112-
export async function getCpuCoreUtilization(): Promise<number[]> {
125+
export async function getCpuUtilization(): Promise<CPUUtilization> {
113126
if (!CPU_COMPUTE_RUNNING) {
114127
await runCpuComputeInterval(); // runs the first computation immediately
115128
await sleep(CPU_COMPUTE_UTILIZATION_INITIAL_DELAY); // wait a bit to get initial values
116129
await computeCpuUtilization(); // run second computation immediately to get initial values
117130
}
118-
return CPU_CORE_UTILIZATIONS;
131+
return CPU_UTILIZATION;
119132
}
120133

134+
135+
121136
/**
122-
* Returns the overall CPU utilization as a percentage (0.0-1.0).
137+
* Returns the number of CPU cores available on the system.
123138
*
124-
* @returns Overall CPU utilization as a percentage (0.0-1.0).
139+
* @returns Number of CPU cores.
125140
*/
126-
export async function getCpuUtilization(): Promise<number> {
127-
if (!CPU_COMPUTE_RUNNING) {
128-
await runCpuComputeInterval(); // runs the first computation immediately
129-
await sleep(CPU_COMPUTE_UTILIZATION_INITIAL_DELAY); // wait a bit to get initial values
130-
await computeCpuUtilization(); // run second computation immediately to get initial values
131-
}
132-
return CPU_UTILIZATION;
141+
export function getCpuCoreCount(): number {
142+
return os.cpus().length;
133143
}
144+

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as cpu from './cpu';
22
import * as drive from './drive';
33
import * as gpu from './gpu';
44
import * as net from './net';
5+
import * as os from './os';
56
import * as temperatures from './temperature';
67
import * as utils from './utils';
78

@@ -13,6 +14,7 @@ const osUtils = {
1314
...drive,
1415
...gpu,
1516
...net,
17+
...os,
1618
...temperatures,
1719
...utils,
1820
};

src/os.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import os from 'os';
2+
3+
export type OSInfo = {
4+
5+
/** Name of the operating system (e.g., Windows, Linux, macOS). */
6+
name: string;
7+
8+
/** Version of the operating system (e.g., 10.0.19042 for Windows 10). */
9+
version: string;
10+
11+
/**
12+
* Architecture of the operating system (e.g., x64, arm64).
13+
* This refers to the CPU architecture, which indicates the type of processor and instruction set used by the operating system.
14+
* For example, x64 is used for 64-bit Intel/AMD processors, while arm64 is used for 64-bit ARM processors.
15+
*/
16+
architecture: string;
17+
18+
/**
19+
* Machine type (e.g., x86_64, armv7l).
20+
* The difference between architecture and machine is that architecture refers to the CPU architecture, while machine refers to the specific hardware platform
21+
* like x86_64 for 64-bit Intel/AMD processors or armv7l for 32-bit ARM processors.
22+
*/
23+
machine: string;
24+
25+
/**
26+
* Platform of the operating system (e.g., win32, linux, darwin).
27+
* This indicates the underlying platform on which the operating system is running.
28+
* For example, win32 is used for Windows, linux for Linux distributions, and darwin for macOS.
29+
*/
30+
platform: string;
31+
32+
/** Number of bits in the operating system (e.g., 32 or 64). */
33+
bits: number;
34+
35+
/** Hostname of the machine. */
36+
hostname: string;
37+
38+
/** Date since when the system has been booted. */
39+
uptime?: Date;
40+
};
41+
42+
/**
43+
* Returns information about the operating system.
44+
*
45+
* @returns Operating system information.
46+
*/
47+
export async function getOSInfo(): Promise<OSInfo> {
48+
return {
49+
name: os.type().replaceAll('_NT', ''), // normalize name
50+
version: os.release(),
51+
architecture: os.arch(),
52+
machine: os.machine(),
53+
platform: os.platform(),
54+
bits: os.arch().includes('64') ? 64 : 32,
55+
hostname: os.hostname(),
56+
uptime: new Date(Date.now() - os.uptime() * 1000)
57+
};
58+
}

0 commit comments

Comments
 (0)