forked from marcmoiagese/checkcontainers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkcontainers.sh
85 lines (76 loc) · 2.85 KB
/
checkcontainers.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# version b0.4
#config
XARXA='/etc/sysconfig/network-scripts/'
TOTAL=$(ls /etc/sysconfig/network-scripts/ifcfg-eth0:* | wc -l)
MAXDOCKER=20
# Adding cardvisor to total count
MAXDOCKER=$((MAXDOCKER+1))
RUTA=$(dirname "$0");
BASEDEDADES="$RUTA/contenidors";
function checkrequisites {
if [ -f "$BASEDEDADES" ]; then
echo "file found";
else
echo "File contenidors not found, ";
exit 1;
fi
}
function NetejaInterficies {
for x in $(ls ${XARXA}ifcfg-eth0:*);
do
if docker ps | awk '{print $12}' |grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep $(cat $x | grep IPADDR |grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"); then
echo $x "exist, so i ignore"
else
echo $x "no docker found with this interface i proceed to clean it";
rm -f $x
fi
done
}
function ComproboContainers {
if [ $(docker ps --format '{{.Names}}' | wc -l) -gt ${MAXDOCKER} ]; then
echo "this node have more containers than ${MAXDOCKER}"
for x in $(docker ps --format '{{.Names}}');
do
if grep $x $BASEDEDADES; then
echo "$x allowed"
else
echo "$x Not found, stoping"
docker stop $x
fi
done
else
echo "this node can allocate more containers"
echo " --- checking running containers"
for x in $(docker ps --format '{{.Names}}');
do
echo "checking if $x is into database"
if grep $x $BASEDEDADES; then
echo "$x found into database"
else
echo "$x Not found, i proceed to add"
echo $x >> $BASEDEDADES
fi
done
echo "--- Reviewing and cleaning database"
for x in $(cat $BASEDEDADES);
do
if docker ps --format '{{.Names}}' | grep $x; then
echo "keep $x on database"
else
echo "$x not present cleaning"
sed -i "/${x}/d" $BASEDEDADES
fi
done
fi
}
#Runing script
echo "checking requirements"
checkrequisites
echo "Starting checks $(date +"%Y%m%d-%H%M%S")"
#Container tasks
echo "checking if this host have more containers that allowed"
ComproboContainers
#Runing clean tasks
echo "Starting cleaning interface config"
NetejaInterficies