add multi-port support

This commit is contained in:
vmorganp
2021-10-13 22:46:05 -07:00
parent 524b40dd44
commit 99dedcfccf
3 changed files with 193 additions and 140 deletions

View File

@@ -18,11 +18,32 @@ $ cd Lazytainer
$ docker-compose up
```
### Or put in your docker compose
```
lazytainer:
container_name: lazytainer
build: .
environment:
- PORT=81,82 # comma separated list of ports...or just the one
- LABEL=lazytainer # value of com.lazytainer.marker for other containers that lazytainer checks
# - TIMEOUT=30 # OPTIONAL number of seconds to let container idle
# - RXHISTLENGTH=10 # OPTIONAL number of seconds to keep rx history, uptime is calculated as first item and last item from this and must have a gap of at least $MINPACKETTHRESH
# - MINPACKETTHRESH=10 # OPTIONAL number of packets that must be recieved to keepalive/start container
ports:
- 81:81
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
whoami1:
container_name: whoami1
image: containous/whoami
command: --port 81 # make this run on the port passed through on lazytainer
network_mode: service:lazytainer
depends_on:
- lazytainer # wait for lazytainer to start before starting
labels:
- "com.lazytainer.marker=lazytainer" # required label to make it work
```
## TODO
- support multiple ports
- test on common services
- docker security probably? this really shouldn't be getting exposed except to forward traffic so idk probably firewall all non listed ports?
- improve logging
- inevitable bugfixes
- ???
- profit
- improve logging - verbosity flags

View File

@@ -4,22 +4,33 @@ services:
container_name: lazytainer
build: .
environment:
- PORT=81 # TODO make this work with more than one port
- PORT=81,82 # comma separated list of ports...or just the one
- LABEL=lazytainer # value of com.lazytainer.marker for other containers that lazytainer checks
- TIMEOUT=30 # number of seconds to let container idle
# - TIMEOUT=30 # OPTIONAL number of seconds to let container idle
# - RXHISTLENGTH=10 # OPTIONAL number of seconds to keep rx history, uptime is calculated as first item and last item from this and must have a gap of at least $MINPACKETTHRESH
# - MINPACKETTHRESH=10 # OPTIONAL number of packets that must be recieved to keepalive/start container
ports:
- 81:81
- 82:82
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
whoami2:
container_name: whoami2
whoami1:
container_name: whoami1
image: containous/whoami
command: --port 81
network_mode: service:lazytainer
depends_on:
- lazytainer
# ports:
# - 80:80
labels:
- "com.lazytainer.marker=lazytainer"
whoami2:
container_name: whoami2
image: containous/whoami
command: --port 82
network_mode: service:lazytainer
depends_on:
- lazytainer
labels:
- "com.lazytainer.marker=lazytainer"

View File

@@ -33,6 +33,27 @@ func main() {
min_packet_threshhold = 10
}
// ports to check for active connections
ports := os.Getenv("PORT")
check(err)
if ports == "" {
panic("you must set a port for this to work")
}
ports_arr := strings.Split(string(strings.TrimSpace(string(ports))), ",")
portsarg := ""
for i, port := range ports_arr {
if i == 0 {
portsarg += "'"
}
if i +1 < len(ports_arr){
portsarg += string(port)+"\\|"
}else{
portsarg += string(port)+"'"
}
}
// how many seconds to wait in between polls
poll_rate, err := strconv.Atoi(os.Getenv("POLLRATE"))
if check_env_fetch_recoverable(err) {
@@ -57,7 +78,7 @@ func main() {
// if the container is running, see if it needs to be stopped
if container_state_on {
// get active clients
out, err := exec.Command("/bin/sh", "-c", "netstat -n | grep ESTABLISHED | awk '{ print $4 }' | rev | cut -d: -f1| rev | grep -w \"$PORT\" | wc -l").Output() // todo make this handle multiple ports?
out, err := exec.Command("/bin/sh", "-c", "netstat -n | grep ESTABLISHED | awk '{ print $4 }' | rev | cut -d: -f1| rev | grep "+portsarg+" | wc -l").Output() // todo make this handle multiple ports?
check(err)
active_clients, err := strconv.Atoi(strings.TrimSpace(string(out)))
check(err)