Skip to content

Commit

Permalink
Add Docker Support closes #3
Browse files Browse the repository at this point in the history
Co-authored-by: Morteza Ghasempour <mso.ghasempour@digikala.com>
  • Loading branch information
mrtzgh and Morteza Ghasempour committed Dec 17, 2021
1 parent 15143b0 commit dd8385a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -84,6 +84,18 @@ for i in range(100):
print(row)
```

## Run in Docker container
#### On the host:
```
nohup bash fakeproc.sh &
```
after you run above script in the host you should mount the `/host/proc/net/dev` to the container. for example:

```
docker run -it -v "/tmp/dev/:/host/proc/net/dev:ro" <YOURIMAGE> bash
```


## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)
9 changes: 9 additions & 0 deletions fakeproc.sh
@@ -0,0 +1,9 @@
#!/bin/bash
# replicate the host dev file to another place in order to read it from docker container
DEVFILE=/proc/net/dev

while [ true ]; do
echo "$(<$DEVFILE)" > /tmp/dev
sleep 0.5
done

10 changes: 9 additions & 1 deletion iometrics/network.py
Expand Up @@ -10,6 +10,7 @@
"""
import re
import time
import os.path
from dataclasses import dataclass
from typing import Dict
from typing import List
Expand Down Expand Up @@ -83,7 +84,14 @@ def get_network_bytes() -> Dict[str, NetworkStats]:
interface_filter_out = re.compile(r"^(lo|tun.+|face.+|bond.+|.+\.\d+)$")

# Note: all counters at /proc/* are starting with zero when the kernel starts.
with open("/proc/net/dev") as file:

src_path: str = ""
if os.path.exists("/host/proc/net/dev"):
src_path = "/host/proc/net/dev"
else:
src_path = "/proc/net/dev"

with open(src_path) as file:
content: str = file.read()

lines: List[str] = content.splitlines()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
psutil

0 comments on commit dd8385a

Please sign in to comment.