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
-8
View File
@@ -12,7 +12,6 @@ exports = module.exports = {
getSystemGraphs, getSystemGraphs,
getBlockDevices, getBlockDevices,
getCpus, getCpus,
getDmi
}; };
const assert = require('assert'), const assert = require('assert'),
@@ -152,10 +151,3 @@ async function getCpus(req, res, next) {
next(new HttpSuccess(200, { cpus })); 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 }));
}
+7 -5
View File
@@ -33,16 +33,18 @@ describe('System', function () {
}); });
}); });
describe('dmi', function () { describe('info', function () {
it('succeeds', async 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 }); .query({ access_token: owner.token });
expect(response.statusCode).to.equal(200); 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.info.sysVendor).to.be.a('string');
expect(response.body.dmi.productName).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');
}); });
}); });
-1
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/graphs', token, authorizeAdmin, routes.system.getSystemGraphs);
router.get ('/api/v1/system/disks', token, authorizeAdmin, routes.system.getDisks); 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/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.get ('/api/v1/system/disk_usage', token, authorizeAdmin, routes.system.getDiskUsage);
router.post('/api/v1/system/disk_usage', token, authorizeAdmin, routes.system.updateDiskUsage); router.post('/api/v1/system/disk_usage', token, authorizeAdmin, routes.system.updateDiskUsage);
router.get ('/api/v1/system/block_devices', token, authorizeAdmin, routes.system.getBlockDevices); router.get ('/api/v1/system/block_devices', token, authorizeAdmin, routes.system.getBlockDevices);
+8 -13
View File
@@ -17,7 +17,6 @@ exports = module.exports = {
runSystemChecks, runSystemChecks,
getProvider, getProvider,
getCpus, getCpus,
getDmi
}; };
const apps = require('./apps.js'), const apps = require('./apps.js'),
@@ -282,10 +281,16 @@ async function reboot() {
async function getInfo() { async function getInfo() {
// https://serverfault.com/questions/92932/how-does-ubuntu-keep-track-of-the-system-restart-required-flag-in-motd // 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 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 { return {
uptime, sysVendor,
productName,
uptimeSecs,
rebootRequired rebootRequired
}; };
} }
@@ -376,13 +381,3 @@ function getProvider() {
async function getCpus() { async function getCpus() {
return os.cpus(); 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
};
}
+6 -4
View File
@@ -53,11 +53,13 @@ describe('System', function () {
expect(cpus[0].model).to.be.a('string'); expect(cpus[0].model).to.be.a('string');
}); });
it('can get DMI', async function () { it('can get info', async function () {
const dmi = await system.getDmi(); const info = await system.getInfo();
expect(dmi.sysVendor).to.be.a('string'); expect(info.sysVendor).to.be.a('string');
expect(dmi.productName).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 () { it('can get diskUsage', async function () {