This commit is contained in:
IgorKha 2021-10-15 18:44:25 +03:00
parent 101c1342ee
commit 8148fedc19
No known key found for this signature in database
GPG key ID: F283C5AB2D59E902

175
run.sh
View file

@ -1,42 +1,38 @@
#!/bin/bash #!/bin/bash
# ############################################################################
# You can also pass some arguments to script to set some these options: # 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 # --config: change the user and password to grafana and specify the mikrotik IP address
# stop: stop docker containers # stop: stop docker containers
#
# For example: # For example:
# sh run.sh --config # sh run.sh --config
# #
# ############################################################################
#
set -e set -e
REPO=Grafana-Mikrotik REPO=Grafana-Mikrotik
ENV_FILE=${ENV_FILE:-.env} ENV_FILE=${ENV_FILE:-.env}
IP=$(grep -R 'MIKROTIK_IP' ./.env | cut -d= -f2)
#? Colors #? Colors
RED=$(printf '\033[31m') RED='\033[31m'
GREEN=$(printf '\033[32m') GREEN='\033[32m'
YELLOW=$(printf '\033[33m') YELLOW='\033[33m'
BLUE=$(printf '\033[34m') BLUE='\033[34m'
BOLD=$(printf '\033[1m') BOLD='\033[1m'
RESET=$(printf '\033[m') #? No Color RESET='\033[m' #? No Color
BOLD=$(printf '\033[1m') BOLD='\033[1m'
#? Docker
DCUID=$(id -u)
DCGID=$(id -g)
ask() { ask() {
local prompt default reply local prompt default reply
if [[ ${2:-} = 'Y' ]]; then if [[ ${2} = 'Y' ]]; then
prompt='Y/n' prompt='Y/n'
default='Y' default='Y'
elif [[ ${2:-} = 'N' ]]; then elif [[ ${2} = 'N' ]]; then
prompt='y/N' prompt='y/N'
default='N' default='N'
else else
@ -47,88 +43,77 @@ ask() {
while true; do while true; do
#? Ask the question (not using "read -p" as it uses stderr not stdout) #? Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] " echo -n "$1 [${prompt}] "
#? Read the answer (use /dev/tty in case stdin is redirected from somewhere else) #? Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read -r reply </dev/tty read -r reply </dev/tty
#? Default? #? Default?
if [[ -z $reply ]]; then if [[ -z ${reply} ]]; then
reply=$default reply=${default}
fi fi
#? Check if the reply is valid #? Check if the reply is valid
case "$reply" in case "${reply}" in
Y*|y*) return 0 ;; Y* | y*) return 0 ;;
N*|n*) return 1 ;; N* | n*) return 1 ;;
esac esac
done done
} }
command_exists() { command_exists() {
command -v "$@" >/dev/null 2>&1 command -v "$@" || {
fmt_error "$* is not installed. Please install $* first."
exit 1
}
} }
fmt_error() { fmt_error() {
printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2 echo -e "\n${BOLD}${RED}Error: $*${RESET}\n" >&2
} }
helper() { help() {
if [ "$HELP" = yes ]; then if [ "${HELP}" = yes ]; then
cat <<EOF sed -n -e 's/^# //' -e '3,12p;' "$0"
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:
sh run.sh --config
EOF
exit 1 exit 1
return return
fi fi
} }
clone_git() { clone_git() {
echo "${BLUE}Git cloning ${REPO}...${RESET}" 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
}
command_exists git || { echo
fmt_error "git is not installed"
exit 1
}
git clone --depth=1 https://github.com/IgorKha/$REPO.git \
|| {
fmt_error "git clone of ${REPO} repo failed"
exit 1
}
echo
} }
router_ip() { router_ip() {
if [ "$CONFIG" = yes ]; then if [[ "${CONFIG}" = yes ]]; then
IP=$(grep -R 'MIKROTIK_IP' "${ENV_FILE}" 2>&1 | cut -d= -f2)
echo -e "\n${BLUE}====================================" echo -e "\n${BLUE}===================================="
echo -e "\n${BOLD}Prometheus${RESET}\n" echo -e "\n${BOLD}Prometheus${RESET}\n"
if ask "Change target mikrotik IP address ? (current ${IP})" Y; then if ask "Change target mikrotik IP address ? (current ${IP})" Y; then
read -rp 'Enter target mikrotik IP address: ' IP read -rp 'Enter target mikrotik IP address: ' IP
if [ -d "./${REPO}" ]; then if [ -d "./${REPO}" ]; then
sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \ sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \
${REPO}/prometheus/prometheus.yml ${REPO}/prometheus/prometheus.yml
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' "$ENV_FILE" sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' "${ENV_FILE}"
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}" echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
else else
sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \ sed -ri -e '/mikrotik_ip/s/(- ).*( #.*)/\1'"${IP}"'\2/g' \
./prometheus/prometheus.yml ./prometheus/prometheus.yml
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' "$ENV_FILE" sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"${IP}"'/g' "${ENV_FILE}"
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}" echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
fi fi
return return
fi fi
echo -e "\n${BLUE}...Skipped step" echo -e "\n${BLUE}...Skipped step"
return return
fi fi
} }
@ -153,17 +138,17 @@ router_ip() {
# } # }
grafana_credentials() { grafana_credentials() {
if [ "$CONFIG" = yes ]; then if [[ "${CONFIG}" = yes ]]; then
echo -e "\n${YELLOW}====================================" echo -e "\n${YELLOW}===================================="
echo -e "\n${BOLD}Grafana${RESET}\n" echo -e "\n${BOLD}Grafana${RESET}\n"
if ask "Change default credentials Grafana ?" N; then if ask "Change default credentials Grafana ?" N; then
read -rp 'Enter grafana Username: ' GF_USER read -rp 'Enter grafana Username: ' GF_USER
read -rsp 'Enter grafana Password: ' GF_PASSWD read -rsp 'Enter grafana Password: ' GF_PASSWD
sed -ri -e 's/^(GF_ADMIN_USER=)(.*)$/\1'"$GF_USER"'/g' "$ENV_FILE" 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" sed -ri -e 's/^(GF_ADMIN_PASSWORD=)(.*)$/\1'"${GF_PASSWD}"'/g' "${ENV_FILE}"
else else
echo "Default Grafana: echo -e "Default Grafana:
User: ${YELLOW}admin${RESET} User: ${YELLOW}admin${RESET}
Password: ${YELLOW}mikrotik${RESET}" Password: ${YELLOW}mikrotik${RESET}"
fi fi
@ -172,68 +157,62 @@ grafana_credentials() {
} }
docker() { docker() {
if ! command_exists docker-compose; then if [[ "${STOP}" = yes ]]; then
echo "${YELLOW}docker-compose is not installed.${RESET} Please install docker-compose first." if [ -d "./${REPO}" ]; then
echo "https://docs.docker.com/compose/install/" cd ${REPO} && docker-compose down
exit 1
else
if [ "$STOP" = yes ]; then
if [ -d "./${REPO}" ]; then
cd ${REPO} && docker-compose down
else
docker-compose down
fi
else else
if [ -d "./${REPO}" ]; then docker-compose down
cd ${REPO} && docker-compose up -d fi
print_success else
else if [[ -d "./${REPO}" ]]; then
docker-compose up -d cd ${REPO} && docker-compose up -d
print_success print_success
fi else
docker-compose up -d
print_success
fi fi
fi fi
} }
print_success() { print_success() {
echo "=============================================" echo "============================================="
echo "${GREEN}Grafana http://localhost:3000" echo -e "${GREEN}Grafana http://localhost:3000"
echo "Prometheus http://localhost:9090/targets${RESET}" echo -e "Prometheus http://localhost:9090/targets${RESET}"
} }
main() { main() {
#? init
if [ -d "./${REPO}" ]; then
ENV_FILE=${REPO}/.env
else
if [[ -e $ENV_FILE ]]; then
:
else
clone_git
ENV_FILE=${REPO}/.env
fi
fi
#? Parse arguments #? Parse arguments
while [ $# -gt 0 ]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
--help) HELP=yes;; --help) HELP=yes ;;
--config) CONFIG=yes ;; --config) CONFIG=yes ;;
stop) STOP=yes ;; stop) STOP=yes ;;
esac esac
shift shift
done done
helper 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
router_ip router_ip
# snmp_on # snmp_on
grafana_credentials grafana_credentials
# Change UID:GID prometheus container to current user # Change UID:GID prometheus container to current user
sed -ri -e 's/^(CURRENT_USER=)(.*)$/\1'"$DCUID\:$DCGID"'/g' "$ENV_FILE" sed -ri -e 's/^(CURRENT_USER=)(.*)$/\1'"$(id -u)\:$(id -g)"'/g' "${ENV_FILE}"
docker docker
} }
main "$@" main "$@"