Skip to content

Commit

Permalink
fix(api): read type and interface speed from host instead of container
Browse files Browse the repository at this point in the history
for #105
  • Loading branch information
MauriceNino committed Jun 8, 2022
1 parent 0da0744 commit 2f0eea4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/dynamic-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export const netowrkObs = createBufferedInterval(
async (): Promise<NetworkLoad> => {
if (NET_INTERFACE !== 'unknown') {
const { stdout } = await exec(
`cat /mnt/host_sys/class/net/${NET_INTERFACE}/statistics/rx_bytes;` +
`cat /mnt/host_sys/class/net/${NET_INTERFACE}/statistics/tx_bytes;`
`cat /internal_mnt/host_sys/class/net/${NET_INTERFACE}/statistics/rx_bytes;` +
`cat /internal_mnt/host_sys/class/net/${NET_INTERFACE}/statistics/tx_bytes;`
);
const [rx, tx] = stdout.split('\n').map(Number);

Expand Down
7 changes: 3 additions & 4 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CONFIG } from './config';
import { cpuObs, netowrkObs, ramObs, storageObs } from './dynamic-info';
import { environment } from './environments/environment';
import { setupNetworking } from './setup-networking';
import { getStaticServerInfo, runSpeedTest } from './static-info';
import { gatherStaticNetworkInfo, getStaticServerInfo } from './static-info';

const app = express();
const server = http.createServer(app);
Expand Down Expand Up @@ -74,11 +74,10 @@ server.listen(CONFIG.port, async () => {
);

await setupNetworking();

console.log('Running speed-test (this may take a few minutes)...');
try {
console.log(
inspect(await runSpeedTest(), {
inspect(await gatherStaticNetworkInfo(), {
showHidden: false,
depth: null,
colors: true,
Expand All @@ -90,7 +89,7 @@ server.listen(CONFIG.port, async () => {

// Run speed test every CONFIG.speed_test_interval minutes
interval(CONFIG.speed_test_interval * 60 * 1000)
.pipe(mergeMap(async () => await runSpeedTest()))
.pipe(mergeMap(async () => await gatherStaticNetworkInfo()))
.subscribe({
error: e => console.error(e),
});
Expand Down
17 changes: 13 additions & 4 deletions apps/api/src/setup-networking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export let NET_INTERFACE = 'unknown';
export const setupNetworking = async () => {
if (fs.existsSync('/mnt/host_ns_net')) {
try {
await exec('mkdir -p /mnt/host_sys');
await exec('mkdir -p /internal_mnt/host_sys');
await exec(
'mountpoint -q /mnt/host_sys || nsenter --net=/mnt/host_ns_net mount -t sysfs nodevice /mnt/host_sys'
'mountpoint -q /internal_mnt/host_sys || nsenter --net=/mnt/host_ns_net mount -t sysfs nodevice /internal_mnt/host_sys'
);
} catch (e) {
console.warn(e);
Expand All @@ -21,9 +21,18 @@ export const setupNetworking = async () => {
const { stdout } = await exec(
"nsenter --net=/mnt/host_ns_net route | grep default | awk '{print $8}'"
);
NET_INTERFACE = stdout.trim();

console.log(`Using network interface "${NET_INTERFACE}"`);
const iface = stdout.trim();

if (iface !== '') {
NET_INTERFACE = iface;

console.log(`Using network interface "${NET_INTERFACE}"`);
} else {
console.warn(
'Unable to determine network interface, using default container network interface'
);
}
} catch (e) {
console.warn(e);
}
Expand Down
60 changes: 45 additions & 15 deletions apps/api/src/static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
StorageInfo,
} from '@dash/common';
import { exec as cexec } from 'child_process';
import * as fs from 'fs';
import * as si from 'systeminformation';
import { SpeedUnits, UniversalSpeedtest } from 'universal-speedtest';
import { promisify } from 'util';
import { CONFIG } from './config';
import { NET_INTERFACE } from './setup-networking';

const exec = promisify(cexec);

Expand All @@ -26,15 +28,9 @@ let INFO_SAVE: HardwareInfo | null = null;

export const getStaticServerInfo = async (): Promise<ServerInfo> => {
if (!INFO_SAVE) {
const [osInfo, cpuInfo, memInfo, memLayout, diskLayout, networkInfo] =
await Promise.all([
si.osInfo(),
si.cpu(),
si.mem(),
si.memLayout(),
si.diskLayout(),
si.networkInterfaces(),
]);
const [osInfo, cpuInfo, memInfo, memLayout, diskLayout] = await Promise.all(
[si.osInfo(), si.cpu(), si.mem(), si.memLayout(), si.diskLayout()]
);

const os: OsInfo = {
arch: osInfo.arch,
Expand Down Expand Up @@ -70,14 +66,11 @@ export const getStaticServerInfo = async (): Promise<ServerInfo> => {
})),
};

//@ts-ignore
const defaultNet = networkInfo.find(net => net.default)!;

const network: NetworkInfo = {
interfaceSpeed: defaultNet.speed,
interfaceSpeed: 0,
speedDown: 0,
speedUp: 0,
type: defaultNet.type,
type: '',
publicIp: '',
};

Expand All @@ -100,7 +93,44 @@ export const getStaticServerInfo = async (): Promise<ServerInfo> => {
};
};

export const runSpeedTest = async () => {
export const gatherStaticNetworkInfo = async () => {
if (NET_INTERFACE !== 'unknown') {
const NET_PATH = `/internal_mnt/host_sys/class/net/${NET_INTERFACE}`;
const isWireless = fs.existsSync(`${NET_PATH}/wireless`);
const isBridge = fs.existsSync(`${NET_PATH}/bridge`);
const isBond = fs.existsSync(`${NET_PATH}/bonding`);
const isTap = fs.existsSync(`${NET_PATH}/tun_flags`);

INFO_SAVE!.network.type = isWireless
? 'Wireless'
: isBridge
? 'Bridge'
: isBond
? 'Bond'
: isTap
? 'TAP'
: 'Wired';

// Wireless networks have no fixed Interface speed
if (!isWireless) {
const { stdout } = await exec(`cat ${NET_PATH}/speed`);
const numValue = Number(stdout.trim());

if (isNaN(numValue) || numValue === -1) {
INFO_SAVE!.network.interfaceSpeed = 0;
} else {
INFO_SAVE!.network.interfaceSpeed = numValue * 1000 * 1000;
}
}
} else {
const networkInfo = await si.networkInterfaces();
//@ts-ignore
const defaultNet = networkInfo.find(net => net.default)!;

INFO_SAVE!.network.type = defaultNet.type;
INFO_SAVE!.network.interfaceSpeed = defaultNet.speed;
}

const { stdout, stderr } = await exec('which speedtest');

if (stderr === '' && stdout.trim() !== '') {
Expand Down

0 comments on commit 2f0eea4

Please sign in to comment.