Skip to content

Commit

Permalink
feat(api): change volume mount for networking to a simpler one
Browse files Browse the repository at this point in the history
makes the old volume mount invalid and needs a new one to make the networking graph work correctly
again

BREAKING CHANGE: volume mount for networking graph is different now
close #58
  • Loading branch information
MauriceNino committed Jun 3, 2022
1 parent c3ab094 commit 0a14c1f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 29 deletions.
26 changes: 8 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To read more about configuration options, you can visit [the configuration secti
docker container run -it \
-p 80:3001 \
-v /etc/os-release:/etc/os-release:ro \
--mount type=bind,source=$(readlink -f /sys/class/net/$(ip addr show | awk '/inet.*brd/{print $NF; exit}')),destination=/mnt/eth0 \
-v /proc/1/ns/net:/mnt/host_ns_net:ro \
--privileged \
mauricenino/dashdot
```
Expand All @@ -69,19 +69,18 @@ docker container run -it \
<!-- -->

> Note: The `--mount` flag is needed to correctly determine the network info.
> If the source part does not correctly resolve for you, you can manually get the
> path by executing `readlink -f /sys/class/net/DEFAULT_INTERFACE`, where the default
> interface is mostly named "eth0". If you don't want to use this flag, you can instead
> use `--network host`, but then you will not be able to use custom docker networking
> any more.
> Note: The volume mount on `/proc/1/ns/net:/host_ns_net:ro` is needed to
> correctly determine the network info. If you are not able to use this mount,
> you will need to fall back to `--net host`, or you will only get the network
> stats of the container instead of the host.
<!-- -->

> Note: The volume mount on `/etc/os-release:/etc/os-release:ro` is for the
> dashboard to show the OS version of the host instead of the OS of the docker
> container (which is running on Alpine Linux). If you wish to show the docker
> container OS instead, just remove this line.
> container OS instead, just remove this line. If you are not able to use this
> mount, you can pass a custom OS with the `DASHDOT_OVERRIDE_OS` flag.
### Docker-Compose

Expand All @@ -104,16 +103,7 @@ services:
- '80:3001'
volumes:
- /etc/os-release:/etc/os-release:ro
- type: bind
source: ${DEFAULT_INTERFACE_PATH}
target: /mnt/eth0
```
After setting up your `docker-compose.yml` file, you have to run the following
command to start the dashboard:

```bash
DEFAULT_INTERFACE_PATH=$(readlink -f /sys/class/net/$(ip addr show | awk '/inet.*brd/{print $NF; exit}')) docker-compose up
- /proc/1/ns/net:/mnt/host_ns_net:ro
```
### Git
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/dynamic-info.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CpuLoad, NetworkLoad, RamLoad, StorageLoad } from '@dash/common';
import { exec as cexec } from 'child_process';
import * as fs from 'fs';
import { interval, mergeMap, Observable, ReplaySubject } from 'rxjs';
import * as si from 'systeminformation';
import { inspect, promisify } from 'util';
import { CONFIG } from './config';
import { NET_INTERFACE } from './setup-networking';
import { getStaticServerInfo } from './static-info';

const exec = promisify(cexec);
Expand Down Expand Up @@ -91,9 +91,10 @@ export const netowrkObs = createBufferedInterval(
CONFIG.network_shown_datapoints,
CONFIG.network_poll_interval,
async (): Promise<NetworkLoad> => {
if (fs.existsSync('/mnt/eth0/')) {
if (NET_INTERFACE !== 'unknown') {
const { stdout } = await exec(
`cat /mnt/eth0/statistics/rx_bytes;cat /mnt/eth0/statistics/tx_bytes;`
`cat /mnt/host_sys/class/net/${NET_INTERFACE}/statistics/rx_bytes;` +
`cat /mnt/host_sys/class/net/${NET_INTERFACE}/statistics/tx_bytes;`
);
const [rx, tx] = stdout.split('\n').map(Number);

Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { inspect } from 'util';
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';

const app = express();
Expand All @@ -32,9 +33,6 @@ if (environment.production) {
app.get('/system-info', async (_, res) => {
res.send(await getStaticServerInfo());
});
app.get('/ping', async (_, res) => {
res.send('pong');
});

// Send current system status
io.on('connection', socket => {
Expand Down Expand Up @@ -75,6 +73,8 @@ server.listen(CONFIG.port, async () => {
})
);

await setupNetworking();

console.log('Running speed-test (this may take a few minutes)...');
try {
console.log(
Expand Down
33 changes: 33 additions & 0 deletions apps/api/src/setup-networking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { exec as exaca } from 'child_process';
import * as fs from 'fs';
import { promisify } from 'util';

const exec = promisify(exaca);

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(
'mountpoint -q /mnt/host_sys || nsenter --net=/mnt/host_ns_net mount -t sysfs nodevice /mnt/host_sys'
);
} catch (e) {
console.warn(e);
}

try {
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}"`);
} catch (e) {
console.warn(e);
}
} else {
console.log(`Using default container network interface`);
}
};
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ services:
volumes:
- ./:/app
- /etc/os-release:/etc/os-release:ro
- type: bind
source: ${DEFAULT_INTERFACE_PATH}
target: /mnt/eth0
- /proc/1/ns/net:/mnt/host_ns_net:ro
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"start": "node .",
"build": "nx run-many --target=build --configuration=production --all=true --parallel=true",
"serve": "nx run-many --target=serve --all=true --parallel=true",
"start-dev": "docker-compose -p dashdot_dev -f ./docker-compose.yml up --remove-orphans",
"dev": "DEFAULT_INTERFACE_PATH=$(readlink -f /sys/class/net/$(ip addr show | awk '/inet.*brd/{print $NF; exit}')) yarn run start-dev",
"dev": "docker-compose -p dashdot_dev -f ./docker-compose.yml up --remove-orphans",
"test": "nx run-many --target=test --all=true --parallel=true",
"postinstall": "husky install",
"commit": "git-cz"
Expand Down

0 comments on commit 0a14c1f

Please sign in to comment.