diff --git a/src/helpers/build-node-file.ts b/src/helpers/build-node-file.ts index 7836eea..81451a6 100644 --- a/src/helpers/build-node-file.ts +++ b/src/helpers/build-node-file.ts @@ -1,6 +1,13 @@ import fs from "fs"; import path from "path"; +import { niceBytes } from "./nice-bytes"; + +interface Disk { + size: string; + type: string; + format: string; +} interface BuildNodeFileParams { name: string; cpuCores: number; @@ -8,40 +15,30 @@ interface BuildNodeFileParams { cpuName: string; sockets: number; memory: number; + disks: Disk[]; } -export const buildNodeFile = ( - params: BuildNodeFileParams -): Promise => { - const { name, cpuCores, cpuThreads, cpuName, sockets, memory } = params; +export const buildNodeFile = ({ + name, + cpuCores, + cpuThreads, + cpuName, + sockets, + memory, + disks, +}: BuildNodeFileParams): string => `# Cluster Node - ${name} - const content = `# Node: ${name} -# CPU: ${cpuCores} cores, ${cpuThreads} threads - ${cpuName} -# Sockets: ${sockets} -# Memory: ${memory} MB -# Generated by Proxmox API Helper +## Hardware +##### CPU +${cpuCores} Cores, ${cpuThreads} Threads - ${cpuName} (${sockets} Sockets) +##### Memory +${niceBytes(memory)} +##### Disk +| Size | Type | Format | +| ---- | ---- | ------ | +${disks + .map((disk) => `| ${disk.size} | ${disk.type} | ${disk.format} |`) + .join("\n")} + +#### Generated by Proxmox API Helper `; - - const filePath = path.join( - __dirname, - "../../", - "build", - `${name}.nodeinfo.md` - ); - return new Promise((resolve, reject) => { - fs.writeFile( - filePath, - content, - { flag: "wx", encoding: "utf8" }, - (err: NodeJS.ErrnoException | null) => { - if (err) { - console.error(`Error writing file ${filePath}:`, err); - reject(err); - } else { - console.info(`File ${filePath} created successfully.`); - resolve(true); - } - } - ); - }); -}; diff --git a/src/helpers/nice-bytes.ts b/src/helpers/nice-bytes.ts new file mode 100644 index 0000000..028f3f2 --- /dev/null +++ b/src/helpers/nice-bytes.ts @@ -0,0 +1,11 @@ +const units = ["bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; + +export function niceBytes(n: number): string { + let l = 0; + + while (n >= 1024 && ++l) { + n = n / 1024; + } + + return n.toFixed(n < 10 && l > 0 ? 1 : 0) + " " + units[l]; +} diff --git a/src/helpers/write-file.ts b/src/helpers/write-file.ts new file mode 100644 index 0000000..ee1fe42 --- /dev/null +++ b/src/helpers/write-file.ts @@ -0,0 +1,22 @@ +import fs from "fs"; +import path from "path"; + +export const writeFile = (content, fileName): Promise => { + const filePath = path.join(__dirname, "../../", "build", fileName); + return new Promise((resolve, reject) => { + fs.writeFile( + filePath, + content, + { flag: "w", encoding: "utf8" }, + (err: NodeJS.ErrnoException | null) => { + if (err) { + console.error(`Error writing file ${filePath}:`, err); + reject(err); + } else { + console.info(`File ${filePath} created successfully.`); + resolve(true); + } + } + ); + }); +}; diff --git a/src/index.ts b/src/index.ts index a074734..39050f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import proxmoxApi from "proxmox-api"; import { buildNodeFile } from "./helpers/build-node-file"; +import { writeFile } from "./helpers/write-file"; // authorize self signed cert if you do not use a valid SSL certificat process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; @@ -25,20 +26,45 @@ async function test() { if (node.status === "online") { const status = await theNode.status.$get(); - console.log(`##### Status: ${JSON.stringify(status)}`); const cpuName = status.cpuinfo.model; const cpuCores = status.cpuinfo.cores; const cpuThreads = status.cpuinfo.cpus; const sockets = status.cpuinfo.sockets; - await buildNodeFile({ + const diskTypes = await theNode.disks.$get(); + const disks = diskTypes.map((diskType) => { + return theNode.disks[diskType.name] + .$get() + .then((diskList) => { + if (!diskList) return []; + + return diskList.map((disk) => ({ + size: disk.size, + type: disk.type, + format: disk.format, + })); + }) + .catch((error) => { + console.error(`Error fetching disk type ${diskType.name}:`, error); + return []; + }); + }); + console.log(`##### Directory disks: ${JSON.stringify(disks)}`); + + const nodeFile = buildNodeFile({ name: node.node, cpuCores, cpuThreads, cpuName, sockets, memory: status.memory.total, + disks: disks.map((disk) => ({ + size: disk.size, + type: disk.type, + format: disk.format, + })), }); + await writeFile(nodeFile, `${node.node}.nodeinfo.md`); console.log(`CPU: ${cpuCores} cores, ${cpuThreads} threads - ${cpuName}`);