system: merge info and dmi routes

also return uptimeSecs instead of abstract date
This commit is contained in:
Girish Ramakrishnan
2023-12-04 01:09:42 +01:00
parent eb64bd296a
commit cbf1b47332
5 changed files with 21 additions and 31 deletions

View File

@@ -12,7 +12,6 @@ exports = module.exports = {
getSystemGraphs,
getBlockDevices,
getCpus,
getDmi
};
const assert = require('assert'),
@@ -152,10 +151,3 @@ async function getCpus(req, res, next) {
next(new HttpSuccess(200, { cpus }));
}
async function getDmi(req, res, next) {
const [error, dmi] = await safe(system.getDmi());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { dmi }));
}

View File

@@ -33,16 +33,18 @@ describe('System', function () {
});
});
describe('dmi', function () {
describe('info', function () {
it('succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/system/dmi`)
const response = await superagent.get(`${serverUrl}/api/v1/system/info`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.dmi).to.be.ok();
expect(response.body.info).to.be.ok();
expect(response.body.dmi.sysVendor).to.be.a('string');
expect(response.body.dmi.productName).to.be.a('string');
expect(response.body.info.sysVendor).to.be.a('string');
expect(response.body.info.productName).to.be.a('string');
expect(response.body.info.uptimeSecs).to.be.a('number');
expect(response.body.info.rebootRequired).to.be.a('boolean');
});
});

View File

@@ -115,7 +115,6 @@ async function initializeExpressSync() {
router.get ('/api/v1/system/graphs', token, authorizeAdmin, routes.system.getSystemGraphs);
router.get ('/api/v1/system/disks', token, authorizeAdmin, routes.system.getDisks);
router.get ('/api/v1/system/cpus', token, authorizeAdmin, routes.system.getCpus);
router.get ('/api/v1/system/dmi', token, authorizeAdmin, routes.system.getDmi);
router.get ('/api/v1/system/disk_usage', token, authorizeAdmin, routes.system.getDiskUsage);
router.post('/api/v1/system/disk_usage', token, authorizeAdmin, routes.system.updateDiskUsage);
router.get ('/api/v1/system/block_devices', token, authorizeAdmin, routes.system.getBlockDevices);

View File

@@ -17,7 +17,6 @@ exports = module.exports = {
runSystemChecks,
getProvider,
getCpus,
getDmi
};
const apps = require('./apps.js'),
@@ -282,10 +281,16 @@ async function reboot() {
async function getInfo() {
// https://serverfault.com/questions/92932/how-does-ubuntu-keep-track-of-the-system-restart-required-flag-in-motd
const rebootRequired = fs.existsSync('/var/run/reboot-required');
const uptime = safe.child_process.execSync('uptime -s', { encoding: 'utf8' });
const uptime = safe.fs.readFileSync('/proc/uptime', 'utf8');
const uptimeSecs = parseInt(uptime.split(' ')[0], 10);
const sysVendor = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/sys_vendor', 'utf8');
const productName = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/product_name', 'utf8');
return {
uptime,
sysVendor,
productName,
uptimeSecs,
rebootRequired
};
}
@@ -376,13 +381,3 @@ function getProvider() {
async function getCpus() {
return os.cpus();
}
async function getDmi() {
const sysVendor = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/sys_vendor', 'utf8');
const productName = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/product_name', 'utf8');
return {
sysVendor,
productName
};
}

View File

@@ -53,11 +53,13 @@ describe('System', function () {
expect(cpus[0].model).to.be.a('string');
});
it('can get DMI', async function () {
const dmi = await system.getDmi();
it('can get info', async function () {
const info = await system.getInfo();
expect(dmi.sysVendor).to.be.a('string');
expect(dmi.productName).to.be.a('string');
expect(info.sysVendor).to.be.a('string');
expect(info.productName).to.be.a('string');
expect(info.uptimeSecs).to.be.a('number');
expect(info.rebootRequired).to.be.a('boolean');
});
it('can get diskUsage', async function () {