-
Notifications
You must be signed in to change notification settings - Fork 16
/
mtr-monitor.sh
executable file
·84 lines (73 loc) · 2.31 KB
/
mtr-monitor.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
#!/bin/bash
# number of pings sent
CYCLES=30
# inverval between MTR tests in seconds
INTERVAL=40
MTR_HOSTS=("8.8.8.8" "reddit.com" "facebook.com")
# set to no to not download & start docker image with influx
INFLUXDB_DOCKER="yes"
# if you need to change it, change also in influx-cli.sh
INFLUXDB_DOCKER_CONTAINER_NAME="mtr-influxdb"
INFLUXDB_HOST="localhost"
INFLUXDB_PORT=51113
# docker image parameter only
INFLUXDB_ADMIN_PORT=51112
# docker image version
INFLUXDB_VERSION=1.7-alpine
# set to "no" to not download & start grafana docker image
GRAFANA_DOCKER="yes"
GRAFANA_DOCKER_CONTAINER_NAME="mtr-grafana"
# password for admin user
GRAFANA_ADMIN_PASSWORD="grafana"
GRAFANA_PORT=51111
TIMEZONE="Europe/Warsaw"
# END OF CONFIG
WORKDIR=`dirname $0`
function monitor_mtr() {
for MTR_HOST in "${MTR_HOSTS[@]}"; do
( mtr --report --json --report-cycles $CYCLES $MTR_HOST | $WORKDIR/save_data.py --host $INFLUXDB_HOST --port $INFLUXDB_PORT ) &
done
}
which mtr &>/dev/null
if [ $? -eq 1 ]; then
echo "mtr is not available on this system - it is required for this script to work"
exit 1
fi
if [[ $INFLUXDB_DOCKER == "yes" ]] ; then
if [ ! "$(sudo docker ps -a | grep $INFLUXDB_DOCKER_CONTAINER_NAME)" ]; then
sudo docker run -d \
--restart=unless-stopped \
--name="$INFLUXDB_DOCKER_CONTAINER_NAME" \
-p $INFLUXDB_ADMIN_PORT:8083 \
-p $INFLUXDB_PORT:8086 \
-v /etc/localtime:/etc/localtime:ro \
-v $WORKDIR/influxdb:/var/lib/influxdb \
influxdb:$INFLUXDB_VERSION
else
sudo docker start $INFLUXDB_DOCKER_CONTAINER_NAME
fi
echo "starting influxdb docker container"
fi
if [[ $GRAFANA_DOCKER == "yes" ]]; then
if [ ! "$(sudo docker ps -a | grep $GRAFANA_DOCKER_CONTAINER_NAME)" ]; then
sudo docker run -d \
--restart=unless-stopped \
--name="$GRAFANA_DOCKER_CONTAINER_NAME" \
-p $GRAFANA_PORT:3000 \
-v /etc/localtime:/etc/localtime:ro \
-v $WORKDIR/grafana.ini:/etc/grafana/grafana.ini:ro \
-v $WORKDIR/grafana:/var/lib/grafana/ \
-e "GF_SECURITY_ADMIN_PASSWORD=$GRAFANA_ADMIN_PASSWORD" \
-e "TZ=$TIMEZONE" \
grafana/grafana
else
sudo docker start $GRAFANA_DOCKER_CONTAINER_NAME
fi
echo "starting grafana docker container"
fi
# wait for influx to initialize
sleep 5
while true; do
monitor_mtr
sleep $INTERVAL
done