mirror of
https://github.com/IgorKha/Grafana-Mikrotik.git
synced 2025-12-06 02:19:28 +00:00
added run.sh
This commit is contained in:
parent
13266b3263
commit
14dfc12bc7
6 changed files with 223 additions and 6 deletions
6
.env
6
.env
|
|
@ -1,2 +1,4 @@
|
|||
GF_ADMIN_USER=admin
|
||||
GF_ADMIN_PASSWORD=mikrotik
|
||||
GF_ADMIN_USER=admin
|
||||
GF_ADMIN_PASSWORD=mikrotik
|
||||
CURRENT_USER=root
|
||||
MIKROTIK_IP=192.168.88.1
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +0,0 @@
|
|||
prometheus/data/*
|
||||
|
|
@ -16,11 +16,13 @@ services:
|
|||
|
||||
prometheus:
|
||||
image: prom/prometheus
|
||||
user: root
|
||||
user: ${CURRENT_USER}
|
||||
container_name: mk_prometheus
|
||||
restart: always
|
||||
environment:
|
||||
- MIKROTIK_IP=${MIKROTIK_IP}
|
||||
volumes:
|
||||
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
- ./prometheus/:/etc/prometheus
|
||||
- ./prometheus/data/:/prometheus
|
||||
ports:
|
||||
- "9090:9090"
|
||||
|
|
|
|||
4
prometheus/data/.gitignore
vendored
Normal file
4
prometheus/data/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this file
|
||||
!.gitignore
|
||||
|
|
@ -40,7 +40,7 @@ scrape_configs:
|
|||
- job_name: Mikrotik
|
||||
static_configs:
|
||||
- targets:
|
||||
- 192.168.88.1 # SNMP device IP.
|
||||
- 192.168.88.1 # SNMP device IP.
|
||||
metrics_path: /snmp
|
||||
params:
|
||||
module: [mikrotik]
|
||||
|
|
|
|||
210
run.sh
Executable file
210
run.sh
Executable file
|
|
@ -0,0 +1,210 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# 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}
|
||||
|
||||
#? 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
|
||||
|
||||
BOLD=$(printf '\033[1m')
|
||||
|
||||
#? Docker
|
||||
DCUID=$(id -u)
|
||||
DCGID=$(id -g)
|
||||
|
||||
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() {
|
||||
printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&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
|
||||
exit 1
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
||||
clone_git() {
|
||||
|
||||
echo "${BLUE}Git cloning ${REPO}...${RESET}"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
router_ip() {
|
||||
if [ "$CONFIG" = yes ]; then
|
||||
if ask "Change target mikrotik IP address ?" Y; then
|
||||
read -p 'Enter target mikrotik IP address: ' IP
|
||||
if [ -d "./${REPO}" ]; then
|
||||
sed -i -e 's/192.168.88.1/'"${IP}"'/g' \
|
||||
${REPO}/prometheus/prometheus.yml
|
||||
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' ${REPO}/$ENV_FILE
|
||||
else
|
||||
sed -i -e 's/192.168.88.1/'"${IP}"'/g' \
|
||||
./prometheus/prometheus.yml
|
||||
sed -ri -e 's/^(MIKROTIK_IP=)(.*)$/\1'"$IP"'/g' $ENV_FILE
|
||||
fi
|
||||
return
|
||||
fi
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
||||
grafana_credentials() {
|
||||
if [ "$CONFIG" = yes ]; then
|
||||
echo "${YELLOW}Grafana${RESET}"
|
||||
if ask "Change default credentials Grafana ?" N; then
|
||||
read -p 'Enter grafana Username: ' GF_USER
|
||||
read -sp '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
|
||||
else
|
||||
echo "Default Grafana:
|
||||
User: ${YELLOW}admin${RESET}
|
||||
Password: ${YELLOW}mikrotik\n${RESET}"
|
||||
fi
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
||||
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
|
||||
else
|
||||
if [ -d "./${REPO}" ]; then
|
||||
cd ${REPO} && docker-compose up -d
|
||||
print_success
|
||||
else
|
||||
docker-compose up -d
|
||||
print_success
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo "============================================="
|
||||
echo "${YELLOW}Grafana http://localhost:3000${RESET}"
|
||||
echo "${BLUE}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
|
||||
case $1 in
|
||||
--help) HELP=yes;;
|
||||
--config) CONFIG=yes ;;
|
||||
stop) STOP=yes ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
helper
|
||||
router_ip
|
||||
grafana_credentials
|
||||
|
||||
# Change UID:GID prometheus container to current user
|
||||
sed -ri -e 's/^(CURRENT_USER=)(.*)$/\1'"$DCUID\:$DCGID"'/g' $ENV_FILE
|
||||
|
||||
docker
|
||||
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Loading…
Add table
Add a link
Reference in a new issue