-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathrun.sh
executable file
·224 lines (189 loc) · 5.73 KB
/
run.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
############################################################################
# You can also pass some arguments to script to set some these options:
#
# --config: change the user and password to grafana and specify the mikrotik IP address
# --stop: stop docker containers
#
# For example:
# bash run.sh --config
#
############################################################################
set -e
REPO=Grafana-Mikrotik
ENV_FILE=${ENV_FILE:-.env}
ENV_FILE_GF=${ENV_FILE_GF:-.grafana}
ENV_FILE_PROMETHEUS=${ENV_FILE_PROMETHEU:-.prometheus}
#? Colors
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
BOLD='\033[1m'
RESET='\033[m' #? No Color
BOLD='\033[1m'
ask() {
local prompt default reply
if [[ ${2} = 'Y' ]]; then
prompt='Y/n'
default='Y'
elif [[ ${2} = 'N' ]]; then
prompt='y/N'
default='N'
else
prompt='y/n'
default=''
fi
while true; do
#? Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [${prompt}] "
#? Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read -r reply </dev/tty
#? Default?
if [[ -z ${reply} ]]; then
reply=${default}
fi
#? Check if the reply is valid
case "${reply}" in
Y* | y*) return 0 ;;
N* | n*) return 1 ;;
esac
done
}
command_exists() {
command -v "$@" >/dev/null 2>&1 || {
fmt_error "$* is not installed. Please install $* first."
exit 1
}
}
fmt_error() {
echo -e "\n${BOLD}${RED}Error: $*${RESET}\n" >&2
}
help() {
if [ "${HELP}" = yes ]; then
sed -n -e 's/^# //' -e '3,12p;' "$0"
exit 1
return
fi
}
clone_git() {
echo -e "${BLUE}Git cloning ${REPO}...${RESET}"
git clone --depth=1 https://github.com/IgorKha/${REPO}.git ||
{
fmt_error "git clone of ${REPO} repo failed"
exit 1
}
echo
}
router_ip() {
if [[ "${CONFIG}" = yes ]]; then
IP=$(grep -R 'MIKROTIK_IP' "${ENV_FILE_PROMETHEUS}" 2>&1 | cut -d= -f2)
echo -e "\n${BLUE}===================================="
echo -e "\n${BOLD}Prometheus${RESET}\n"
if ask "Change target mikrotik IP address ? (current ${IP})" Y; then
read -rp 'Enter target mikrotik IP address: ' IP
if [ -d "./${REPO}" ]; then
sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \
${REPO}/prometheus/prometheus.yml
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' "${ENV_FILE_PROMETHEUS}"
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
else
sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \
./prometheus/prometheus.yml
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"${IP}"'/g' "${ENV_FILE_PROMETHEUS}"
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
fi
return
fi
echo -e "\n${BLUE}...Skipped step"
return
fi
}
# snmp_on() {
# if [ "$CONFIG" = yes ]; then
# echo -e "\n${BLUE}===================================="
# echo -e "${BOLD}Mikrotik SNMP ACTIVATION${RESET}"
# if ask "Activate snmp mikrotik using ssh?" N; then
# read -rp 'Enter login: ' MK_LOGIN
# # read -rsp 'Enter password: ' MK_PASSWD
# COMM="
# snmp
# "
# ssh ${MK_LOGIN}@$IP "${COMM}"
# else
# echo "skipped"
# fi
# return
# fi
# }
grafana_credentials() {
if [[ "${CONFIG}" = yes ]]; then
echo -e "\n${YELLOW}===================================="
echo -e "\n${BOLD}Grafana${RESET}\n"
if ask "Change default credentials Grafana ?" N; then
read -rp 'Enter grafana Username: ' GF_USER
read -rsp 'Enter grafana Password: ' GF_PASSWD
sed -ri -e 's/^(GF_SECURITY_ADMIN_USER=)(.*)$/\1'"${GF_USER}"'/g' "${ENV_FILE_GF}"
sed -ri -e 's/^(GF_SECURITY_ADMIN_PASSWORD=)(.*)$/\1'"${GF_PASSWD}"'/g' "${ENV_FILE_GF}"
else
echo -e "Default Grafana:
User: ${YELLOW}admin${RESET}
Password: ${YELLOW}mikrotik${RESET}"
fi
return
fi
}
docker-cmd() {
if [[ "${STOP}" = yes ]]; then
if [ -d "./${REPO}" ]; then
cd ${REPO} && docker compose down
else
docker compose down
fi
else
if [[ -d "./${REPO}" ]]; then
cd ${REPO} && docker compose up -d
print_success
else
docker compose up -d
print_success
fi
fi
}
print_success() {
echo "============================================="
echo -e "${GREEN}Grafana http://localhost:3000"
echo -e "Prometheus http://localhost:9090/targets${RESET}"
}
main() {
#? Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--help) HELP=yes ;;
--config) CONFIG=yes ;;
--stop) STOP=yes ;;
esac
shift
done
help
command_exists git
command_exists docker
#? init
if [[ -d "./${REPO}" ]]; then
ENV_FILE=${REPO}/.env
ENV_FILE_GF=${REPO}/.grafana
ENV_FILE_PROMETHEUS=${REPO}/.prometheus
elif [[ ! -e ${ENV_FILE} ]]; then
clone_git
ENV_FILE=${REPO}/.env
ENV_FILE_GF=${REPO}/.grafana
ENV_FILE_PROMETHEUS=${REPO}/.prometheus
fi
router_ip
# snmp_on
grafana_credentials
# Change UID:GID prometheus container to current user
sed -ri -e 's/^(CURRENT_USER=)(.*)$/\1'"$(id -u)\:$(id -g)"'/g' "${ENV_FILE}"
docker-cmd
}
main "$@"