mirror of
https://github.com/IgorKha/Grafana-Mikrotik.git
synced 2025-12-06 10:29:28 +00:00
parent
101c1342ee
commit
8148fedc19
1 changed files with 78 additions and 99 deletions
175
run.sh
175
run.sh
|
|
@ -1,42 +1,38 @@
|
|||
#!/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:
|
||||
# sh run.sh --config
|
||||
#
|
||||
#
|
||||
#
|
||||
############################################################################
|
||||
|
||||
set -e
|
||||
|
||||
REPO=Grafana-Mikrotik
|
||||
ENV_FILE=${ENV_FILE:-.env}
|
||||
IP=$(grep -R 'MIKROTIK_IP' ./.env | cut -d= -f2)
|
||||
|
||||
#? Colors
|
||||
RED=$(printf '\033[31m')
|
||||
GREEN=$(printf '\033[32m')
|
||||
YELLOW=$(printf '\033[33m')
|
||||
BLUE=$(printf '\033[34m')
|
||||
BOLD=$(printf '\033[1m')
|
||||
RESET=$(printf '\033[m') #? No Color
|
||||
RED='\033[31m'
|
||||
GREEN='\033[32m'
|
||||
YELLOW='\033[33m'
|
||||
BLUE='\033[34m'
|
||||
BOLD='\033[1m'
|
||||
RESET='\033[m' #? No Color
|
||||
|
||||
BOLD=$(printf '\033[1m')
|
||||
|
||||
#? Docker
|
||||
DCUID=$(id -u)
|
||||
DCGID=$(id -g)
|
||||
BOLD='\033[1m'
|
||||
|
||||
ask() {
|
||||
local prompt default reply
|
||||
|
||||
if [[ ${2:-} = 'Y' ]]; then
|
||||
if [[ ${2} = 'Y' ]]; then
|
||||
prompt='Y/n'
|
||||
default='Y'
|
||||
elif [[ ${2:-} = 'N' ]]; then
|
||||
elif [[ ${2} = 'N' ]]; then
|
||||
prompt='y/N'
|
||||
default='N'
|
||||
else
|
||||
|
|
@ -47,88 +43,77 @@ ask() {
|
|||
while true; do
|
||||
|
||||
#? 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 -r reply </dev/tty
|
||||
|
||||
#? Default?
|
||||
if [[ -z $reply ]]; then
|
||||
reply=$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 ;;
|
||||
case "${reply}" in
|
||||
Y* | y*) return 0 ;;
|
||||
N* | n*) return 1 ;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
command_exists() {
|
||||
command -v "$@" >/dev/null 2>&1
|
||||
command -v "$@" || {
|
||||
fmt_error "$* is not installed. Please install $* first."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
fmt_error() {
|
||||
printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2
|
||||
echo -e "\n${BOLD}${RED}Error: $*${RESET}\n" >&2
|
||||
}
|
||||
|
||||
helper() {
|
||||
if [ "$HELP" = yes ]; then
|
||||
cat <<EOF
|
||||
|
||||
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
|
||||
help() {
|
||||
if [ "${HELP}" = yes ]; then
|
||||
sed -n -e 's/^# //' -e '3,12p;' "$0"
|
||||
exit 1
|
||||
return
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
||||
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 || {
|
||||
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
|
||||
echo
|
||||
}
|
||||
|
||||
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${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"
|
||||
${REPO}/prometheus/prometheus.yml
|
||||
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' "${ENV_FILE}"
|
||||
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/prometheus.yml
|
||||
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"${IP}"'/g' "${ENV_FILE}"
|
||||
echo -e "\n${GREEN}... Prometheus target IP changed to ${IP}"
|
||||
fi
|
||||
return
|
||||
return
|
||||
fi
|
||||
echo -e "\n${BLUE}...Skipped step"
|
||||
return
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
@ -153,17 +138,17 @@ router_ip() {
|
|||
# }
|
||||
|
||||
grafana_credentials() {
|
||||
if [ "$CONFIG" = yes ]; then
|
||||
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_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_USER=)(.*)$/\1'"${GF_USER}"'/g' "${ENV_FILE}"
|
||||
sed -ri -e 's/^(GF_ADMIN_PASSWORD=)(.*)$/\1'"${GF_PASSWD}"'/g' "${ENV_FILE}"
|
||||
else
|
||||
echo "Default Grafana:
|
||||
echo -e "Default Grafana:
|
||||
User: ${YELLOW}admin${RESET}
|
||||
Password: ${YELLOW}mikrotik${RESET}"
|
||||
fi
|
||||
|
|
@ -172,68 +157,62 @@ grafana_credentials() {
|
|||
}
|
||||
|
||||
docker() {
|
||||
if ! command_exists docker-compose; then
|
||||
echo "${YELLOW}docker-compose is not installed.${RESET} Please install docker-compose first."
|
||||
echo "https://docs.docker.com/compose/install/"
|
||||
exit 1
|
||||
else
|
||||
if [ "$STOP" = yes ]; then
|
||||
if [ -d "./${REPO}" ]; then
|
||||
cd ${REPO} && docker-compose down
|
||||
else
|
||||
docker-compose down
|
||||
fi
|
||||
if [[ "${STOP}" = yes ]]; then
|
||||
if [ -d "./${REPO}" ]; then
|
||||
cd ${REPO} && docker-compose down
|
||||
else
|
||||
if [ -d "./${REPO}" ]; then
|
||||
cd ${REPO} && docker-compose up -d
|
||||
print_success
|
||||
else
|
||||
docker-compose up -d
|
||||
print_success
|
||||
fi
|
||||
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 "${GREEN}Grafana http://localhost:3000"
|
||||
echo "Prometheus http://localhost:9090/targets${RESET}"
|
||||
echo -e "${GREEN}Grafana http://localhost:3000"
|
||||
echo -e "Prometheus http://localhost:9090/targets${RESET}"
|
||||
}
|
||||
|
||||
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
|
||||
while [ $# -gt 0 ]; do
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--help) HELP=yes;;
|
||||
--help) HELP=yes ;;
|
||||
--config) CONFIG=yes ;;
|
||||
stop) STOP=yes ;;
|
||||
esac
|
||||
shift
|
||||
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
|
||||
# snmp_on
|
||||
grafana_credentials
|
||||
|
||||
# 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
|
||||
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue