2021-10-11 21:06:56 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
############################################################################
|
2021-10-10 04:20:17 +03:00
|
|
|
# You can also pass some arguments to script to set some these options:
|
2021-10-15 18:44:25 +03:00
|
|
|
#
|
2021-10-10 04:20:17 +03:00
|
|
|
# --config: change the user and password to grafana and specify the mikrotik IP address
|
|
|
|
|
# stop: stop docker containers
|
2021-10-15 18:44:25 +03:00
|
|
|
#
|
2021-10-10 04:20:17 +03:00
|
|
|
# For example:
|
|
|
|
|
# sh run.sh --config
|
2021-10-15 18:44:25 +03:00
|
|
|
#
|
|
|
|
|
############################################################################
|
2021-10-10 04:20:17 +03:00
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
REPO=Grafana-Mikrotik
|
|
|
|
|
ENV_FILE=${ENV_FILE:-.env}
|
|
|
|
|
|
|
|
|
|
#? Colors
|
2021-10-15 18:44:25 +03:00
|
|
|
RED='\033[31m'
|
|
|
|
|
GREEN='\033[32m'
|
|
|
|
|
YELLOW='\033[33m'
|
|
|
|
|
BLUE='\033[34m'
|
|
|
|
|
BOLD='\033[1m'
|
|
|
|
|
RESET='\033[m' #? No Color
|
2021-10-10 04:20:17 +03:00
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
BOLD='\033[1m'
|
2021-10-10 04:20:17 +03:00
|
|
|
|
|
|
|
|
ask() {
|
|
|
|
|
local prompt default reply
|
|
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
if [[ ${2} = 'Y' ]]; then
|
2021-10-10 04:20:17 +03:00
|
|
|
prompt='Y/n'
|
|
|
|
|
default='Y'
|
2021-10-15 18:44:25 +03:00
|
|
|
elif [[ ${2} = 'N' ]]; then
|
2021-10-10 04:20:17 +03:00
|
|
|
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)
|
2021-10-15 18:44:25 +03:00
|
|
|
echo -n "$1 [${prompt}] "
|
2021-10-10 04:20:17 +03:00
|
|
|
|
|
|
|
|
#? Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
|
|
|
|
|
read -r reply </dev/tty
|
|
|
|
|
|
|
|
|
|
#? Default?
|
2021-10-15 18:44:25 +03:00
|
|
|
if [[ -z ${reply} ]]; then
|
|
|
|
|
reply=${default}
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
#? Check if the reply is valid
|
2021-10-15 18:44:25 +03:00
|
|
|
case "${reply}" in
|
|
|
|
|
Y* | y*) return 0 ;;
|
|
|
|
|
N* | n*) return 1 ;;
|
2021-10-10 04:20:17 +03:00
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command_exists() {
|
2021-10-15 18:44:25 +03:00
|
|
|
command -v "$@" || {
|
|
|
|
|
fmt_error "$* is not installed. Please install $* first."
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
2021-10-10 04:20:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt_error() {
|
2021-10-15 18:44:25 +03:00
|
|
|
echo -e "\n${BOLD}${RED}Error: $*${RESET}\n" >&2
|
2021-10-10 04:20:17 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
help() {
|
|
|
|
|
if [ "${HELP}" = yes ]; then
|
|
|
|
|
sed -n -e 's/^# //' -e '3,12p;' "$0"
|
2021-10-10 04:20:17 +03:00
|
|
|
exit 1
|
2021-10-15 18:44:25 +03:00
|
|
|
return
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clone_git() {
|
|
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
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
|
|
|
|
|
}
|
2021-10-10 04:20:17 +03:00
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
echo
|
2021-10-10 04:20:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router_ip() {
|
2021-10-15 18:44:25 +03:00
|
|
|
if [[ "${CONFIG}" = yes ]]; then
|
|
|
|
|
IP=$(grep -R 'MIKROTIK_IP' "${ENV_FILE}" 2>&1 | cut -d= -f2)
|
2021-10-13 15:47:04 +03:00
|
|
|
echo -e "\n${BLUE}===================================="
|
|
|
|
|
echo -e "\n${BOLD}Prometheus${RESET}\n"
|
|
|
|
|
if ask "Change target mikrotik IP address ? (current ${IP})" Y; then
|
2021-10-11 21:06:56 +03:00
|
|
|
read -rp 'Enter target mikrotik IP address: ' IP
|
2021-10-10 04:20:17 +03:00
|
|
|
if [ -d "./${REPO}" ]; then
|
2021-10-13 15:47:04 +03:00
|
|
|
sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \
|
2021-10-15 18:44:25 +03:00
|
|
|
${REPO}/prometheus/prometheus.yml
|
|
|
|
|
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' "${ENV_FILE}"
|
2021-10-13 15:47:04 +03:00
|
|
|
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
|
2021-10-10 04:20:17 +03:00
|
|
|
else
|
2021-10-13 15:47:04 +03:00
|
|
|
sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \
|
2021-10-15 18:44:25 +03:00
|
|
|
./prometheus/prometheus.yml
|
|
|
|
|
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"${IP}"'/g' "${ENV_FILE}"
|
2021-10-13 15:47:04 +03:00
|
|
|
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
2021-10-15 18:44:25 +03:00
|
|
|
return
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
2021-10-13 15:47:04 +03:00
|
|
|
echo -e "\n${BLUE}...Skipped step"
|
2021-10-15 18:44:25 +03:00
|
|
|
return
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 15:47:04 +03:00
|
|
|
# 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
|
|
|
|
|
# }
|
|
|
|
|
|
2021-10-10 04:20:17 +03:00
|
|
|
grafana_credentials() {
|
2021-10-15 18:44:25 +03:00
|
|
|
if [[ "${CONFIG}" = yes ]]; then
|
2021-10-13 15:47:04 +03:00
|
|
|
echo -e "\n${YELLOW}===================================="
|
|
|
|
|
echo -e "\n${BOLD}Grafana${RESET}\n"
|
2021-10-10 04:20:17 +03:00
|
|
|
if ask "Change default credentials Grafana ?" N; then
|
2021-10-11 21:06:56 +03:00
|
|
|
read -rp 'Enter grafana Username: ' GF_USER
|
|
|
|
|
read -rsp 'Enter grafana Password: ' GF_PASSWD
|
2021-10-10 04:20:17 +03:00
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
sed -ri -e 's/^(GF_ADMIN_USER=)(.*)$/\1'"${GF_USER}"'/g' "${ENV_FILE}"
|
|
|
|
|
sed -ri -e 's/^(GF_ADMIN_PASSWORD=)(.*)$/\1'"${GF_PASSWD}"'/g' "${ENV_FILE}"
|
2021-10-10 04:20:17 +03:00
|
|
|
else
|
2021-10-15 18:44:25 +03:00
|
|
|
echo -e "Default Grafana:
|
2021-10-10 04:20:17 +03:00
|
|
|
User: ${YELLOW}admin${RESET}
|
2021-10-10 12:30:54 +03:00
|
|
|
Password: ${YELLOW}mikrotik${RESET}"
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
docker() {
|
2021-10-15 18:44:25 +03:00
|
|
|
if [[ "${STOP}" = yes ]]; then
|
|
|
|
|
if [ -d "./${REPO}" ]; then
|
|
|
|
|
cd ${REPO} && docker-compose down
|
|
|
|
|
else
|
|
|
|
|
docker-compose down
|
|
|
|
|
fi
|
2021-10-10 04:20:17 +03:00
|
|
|
else
|
2021-10-15 18:44:25 +03:00
|
|
|
if [[ -d "./${REPO}" ]]; then
|
|
|
|
|
cd ${REPO} && docker-compose up -d
|
|
|
|
|
print_success
|
2021-10-10 04:20:17 +03:00
|
|
|
else
|
2021-10-15 18:44:25 +03:00
|
|
|
docker-compose up -d
|
|
|
|
|
print_success
|
2021-10-10 04:20:17 +03:00
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_success() {
|
|
|
|
|
echo "============================================="
|
2021-10-15 18:44:25 +03:00
|
|
|
echo -e "${GREEN}Grafana http://localhost:3000"
|
|
|
|
|
echo -e "Prometheus http://localhost:9090/targets${RESET}"
|
2021-10-10 04:20:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main() {
|
|
|
|
|
|
|
|
|
|
#? Parse arguments
|
2021-10-15 18:44:25 +03:00
|
|
|
while [[ $# -gt 0 ]]; do
|
2021-10-10 04:20:17 +03:00
|
|
|
case $1 in
|
2021-10-15 18:44:25 +03:00
|
|
|
--help) HELP=yes ;;
|
2021-10-10 04:20:17 +03:00
|
|
|
--config) CONFIG=yes ;;
|
|
|
|
|
stop) STOP=yes ;;
|
|
|
|
|
esac
|
|
|
|
|
shift
|
|
|
|
|
done
|
|
|
|
|
|
2021-10-15 18:44:25 +03:00
|
|
|
help
|
|
|
|
|
|
|
|
|
|
command_exists git
|
|
|
|
|
command_exists docker-compose
|
|
|
|
|
|
|
|
|
|
#? init
|
|
|
|
|
if [[ -d "./${REPO}" ]]; then
|
|
|
|
|
ENV_FILE=${REPO}/.env
|
|
|
|
|
elif [[ ! -e ${ENV_FILE} ]]; then
|
|
|
|
|
clone_git
|
|
|
|
|
ENV_FILE=${REPO}/.env ;
|
|
|
|
|
fi
|
|
|
|
|
|
2021-10-10 04:20:17 +03:00
|
|
|
router_ip
|
2021-10-13 15:47:04 +03:00
|
|
|
# snmp_on
|
2021-10-10 04:20:17 +03:00
|
|
|
grafana_credentials
|
|
|
|
|
|
|
|
|
|
# Change UID:GID prometheus container to current user
|
2021-10-15 18:44:25 +03:00
|
|
|
sed -ri -e 's/^(CURRENT_USER=)(.*)$/\1'"$(id -u)\:$(id -g)"'/g' "${ENV_FILE}"
|
2021-10-10 04:20:17 +03:00
|
|
|
|
|
|
|
|
docker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main "$@"
|