return swap listing in the disk route

This commit is contained in:
Girish Ramakrishnan
2022-11-04 15:09:37 +01:00
parent c4c90cfaf9
commit 549cb92ce7
8 changed files with 50 additions and 20 deletions

View File

@@ -2,6 +2,7 @@
exports = module.exports = {
getDisks,
getSwaps,
checkDiskSpace,
getMemory,
getMemoryAllocation,
@@ -35,6 +36,25 @@ async function du(file) {
return parseInt(stdoutResult.trim(), 10);
}
async function getSwaps() {
const stdout = safe.child_process.execSync('swapon --noheadings --raw --bytes --show=type,size,used,name', { encoding: 'utf8' });
if (!stdout) return {};
const swaps = {};
for (const line of stdout.trim().split('\n')) {
const parts = line.split(' ', 4);
const name = parts[3];
swaps[name] = {
name: parts[3],
type: parts[0], // partition or file
size: parseInt(parts[1]),
used: parseInt(parts[2]),
};
}
return swaps;
}
async function getDisks() {
let [dfError, dfEntries] = await safe(df.disks());
if (dfError) throw new BoxError(BoxError.FS_ERROR, `Error running df: ${dfError.message}`);
@@ -120,25 +140,23 @@ async function checkDiskSpace() {
await notifications.alert(notifications.ALERT_DISK_SPACE, 'Server is running out of disk space', markdownMessage);
}
function getSwapSize() {
const stdout = safe.child_process.execSync('swapon --noheadings --raw --bytes --show=SIZE', { encoding: 'utf8' });
const swap = !stdout ? 0 : stdout.trim().split('\n').map(x => parseInt(x, 10) || 0).reduce((acc, cur) => acc + cur);
return swap;
async function getSwapSize() {
const swaps = await getSwaps();
return Object.keys(swaps).map(n => swaps[n].size).reduce((acc, cur) => acc + cur);
}
async function getMemory() {
return {
memory: os.totalmem(),
swap: getSwapSize()
swap: await getSwapSize()
};
}
function getMemoryAllocation(limit) {
async function getMemoryAllocation(limit) {
let ratio = parseFloat(safe.fs.readFileSync(paths.SWAP_RATIO_FILE, 'utf8'), 10);
if (!ratio) {
const pc = os.totalmem() / (os.totalmem() + getSwapSize());
const pc = os.totalmem() / (os.totalmem() + await getSwapSize());
ratio = Math.round(pc * 10) / 10; // a simple ratio
}