68 lines
2.8 KiB
JavaScript
68 lines
2.8 KiB
JavaScript
import os from 'node:os';
|
|
|
|
console.log(os.platform());
|
|
console.log(os.cpus().length);
|
|
|
|
console.log(`I am running on a machine with ${os.platform()} and ${os.cpus().length} cores.`);
|
|
console.log(`I am running on a machine with ${os.type()} and ${os.cpus().length} cores.`);
|
|
console.log(`I am running on a machine with ${os.version()} and ${os.cpus().length} cores.`);
|
|
|
|
// Weitere Informationen zum Betriebssystem
|
|
console.log('\n--- Betriebssystem ---');
|
|
console.log('Systemtyp:', os.type());
|
|
console.log('Systemversion:', os.release());
|
|
console.log('Architektur von Node.js:', os.arch());
|
|
console.log('Computername:', os.hostname());
|
|
|
|
// os.cpus() liefert Informationen zu den logischen CPU-Kernen.
|
|
const cpus = os.cpus();
|
|
console.log('\n--- Prozessor ---');
|
|
console.log('Logische CPU-Kerne:', cpus.length);
|
|
console.log('CPU-Modell:', cpus[0]?.model ?? 'Unbekannt');
|
|
|
|
// Die Speicherangaben kommen in Bytes. 1024 ** 3 Bytes entsprechen einem GiB.
|
|
const totalMemory = os.totalmem();
|
|
const freeMemory = os.freemem();
|
|
const usedMemory = totalMemory - freeMemory;
|
|
|
|
function formatMemory(bytes) {
|
|
return `${(bytes / 1024 ** 3).toFixed(2)} GiB`;
|
|
}
|
|
|
|
console.log('\n--- Arbeitsspeicher ---');
|
|
console.log('Gesamt:', formatMemory(totalMemory));
|
|
console.log('Frei:', formatMemory(freeMemory));
|
|
console.log('Belegt:', formatMemory(usedMemory));
|
|
|
|
// os.uptime() gibt die Laufzeit des Betriebssystems in Sekunden zurück.
|
|
const uptime = os.uptime();
|
|
const hours = Math.floor(uptime / 3600);
|
|
const minutes = Math.floor((uptime % 3600) / 60);
|
|
console.log(`\nSystemlaufzeit: ${hours} Stunden und ${minutes} Minuten`);
|
|
|
|
console.log('\n--- Benutzer und Verzeichnisse ---');
|
|
console.log('Benutzername:', os.userInfo().username);
|
|
console.log('Home-Verzeichnis:', os.homedir());
|
|
console.log('Temporäres Verzeichnis:', os.tmpdir());
|
|
|
|
// Jede Netzwerkschnittstelle kann mehrere IPv4- und IPv6-Adressen haben.
|
|
console.log('\n--- Netzwerkschnittstellen ---');
|
|
for (const [name, addresses] of Object.entries(os.networkInterfaces())) {
|
|
for (const address of addresses ?? []) {
|
|
console.log(`${name}: ${address.address} (${address.family}${address.internal ? ', intern' : ''})`);
|
|
}
|
|
}
|
|
|
|
// Übung 32: Information zum Betriebssystem und der Anzahl der vorhandenen CPUs
|
|
|
|
// Im Kontext einer Monitoring-Lösung benötigt ein Kunde eine kleine CLI (Command Line Interface)-Anwendung, die es erlaubt, das aktuelle Betriebssystem und die Anzahl der CPUs auf dem jeweiligen Rechner zu ermitteln.
|
|
|
|
// Vorgehensweise
|
|
|
|
// 1.Schau dir die Online-Dokumentation zum os-Modul der Standardbibliothek an.
|
|
// 2.Erstelle dann ein kurzes Node.js-Programm, das ausgibt, auf welcher Plattform es gerade läuft (also etwa »linux«, »darwin« (OS X) oder »win32«) und wie viele Kerne der Rechner hat.
|
|
|
|
// Hier eine mögliche Beispielausgabe:
|
|
|
|
// I am running on a machine with linux and 2 cores.
|