fix installer bugs & improve (#341)

* fix installer bugs && improve

* Update install_en.sh
This commit is contained in:
UUBulb 2024-03-30 11:08:29 +08:00 committed by GitHub
parent 140f9809a0
commit 0373d03fb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 382 additions and 910 deletions

View File

@ -14,7 +14,7 @@ NZ_AGENT_SERVICE="/etc/systemd/system/nezha-agent.service"
NZ_AGENT_SERVICERC="/etc/init.d/nezha-agent" NZ_AGENT_SERVICERC="/etc/init.d/nezha-agent"
NZ_DASHBOARD_SERVICE="/etc/systemd/system/nezha-dashboard.service" NZ_DASHBOARD_SERVICE="/etc/systemd/system/nezha-dashboard.service"
NZ_DASHBOARD_SERVICERC="/etc/init.d/nezha-dashboard" NZ_DASHBOARD_SERVICERC="/etc/init.d/nezha-dashboard"
NZ_VERSION="v0.15.9" NZ_VERSION="v0.16.0"
red='\033[0;31m' red='\033[0;31m'
green='\033[0;32m' green='\033[0;32m'
@ -100,22 +100,64 @@ pre_check() {
Docker_IMG="registry.cn-shanghai.aliyuncs.com\/naibahq\/nezha-dashboard" Docker_IMG="registry.cn-shanghai.aliyuncs.com\/naibahq\/nezha-dashboard"
fi fi
fi fi
} }
confirm() { installation_check() {
if [[ $# > 1 ]]; then if docker compose version >/dev/null 2>&1; then
echo && read -e -p "$1 [默认$2]: " temp DOCKER_COMPOSE_COMMAND="docker compose"
if [[ x"${temp}" == x"" ]]; then if $DOCKER_COMPOSE_COMMAND ls | grep -qw "$NZ_DASHBOARD_PATH/docker-compose.yaml" >/dev/null 2>&1; then
temp=$2 NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "存在带有 nezha-dashboard 仓库的 Docker 镜像:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
FRESH_INSTALL=0
return
else
echo "未找到带有 nezha-dashboard 仓库的 Docker 镜像。"
fi
fi
elif command -v docker-compose >/dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker-compose"
if $DOCKER_COMPOSE_COMMAND -f "$NZ_DASHBOARD_PATH/docker-compose.yaml" config >/dev/null 2>&1; then
NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "存在带有 nezha-dashboard 仓库的 Docker 镜像:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
FRESH_INSTALL=0
return
else
echo "未找到带有 nezha-dashboard 仓库的 Docker 镜像。"
fi
fi fi
else
read -e -p "$1 [y/n]: " temp
fi fi
if [[ x"${temp}" == x"y" || x"${temp}" == x"Y" ]]; then
return 0 if [[ -f $NZ_DASHBOARD_PATH/app ]]; then
else IS_DOCKER_NEZHA=0
return 1 FRESH_INSTALL=0
fi
}
select_version() {
if [[ -z $IS_DOCKER_NEZHA ]]; then
echo -e "${yellow}请自行选择您的安装方式如果你是安装Agent输入哪个都是一样的\n1. Docker\n2. 独立安装${plain}"
while true; do
read -e -r -p "请输入选择 [1-2]" option
case "${option}" in
1)
IS_DOCKER_NEZHA=1
break
;;
2)
IS_DOCKER_NEZHA=0
break
;;
*)
echo "${red}请输入正确的数字 [1-2]${plain}"
;;
esac
done
fi fi
} }
@ -186,7 +228,7 @@ install_dashboard() {
echo -e "> 安装面板" echo -e "> 安装面板"
# 哪吒监控文件夹 # 哪吒监控文件夹
if [ ! -d "${NZ_DASHBOARD_PATH}/docker-compose.yaml" ]; then if [[ ! $FRESH_INSTALL == 0 ]]; then
mkdir -p $NZ_DASHBOARD_PATH mkdir -p $NZ_DASHBOARD_PATH
else else
echo "您可能已经安装过面板端,重复安装会覆盖数据,请注意备份。" echo "您可能已经安装过面板端,重复安装会覆盖数据,请注意备份。"
@ -208,17 +250,10 @@ install_dashboard() {
chmod 777 -R $NZ_DASHBOARD_PATH chmod 777 -R $NZ_DASHBOARD_PATH
command -v docker >/dev/null 2>&1 if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? != 0 ]]; then install_dashboard_docker
echo -e "正在安装 Docker" elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
bash <(curl -sL https://${Get_Docker_URL}) ${Get_Docker_Argu} >/dev/null 2>&1 install_dashboard_standalone
if [[ $? != 0 ]]; then
echo -e "${red}下载脚本失败,请检查本机能否连接 ${Get_Docker_URL}${plain}"
return 0
fi
systemctl enable docker.service
systemctl start docker.service
echo -e "${green}Docker${plain} 安装成功"
fi fi
modify_dashboard_config 0 modify_dashboard_config 0
@ -228,43 +263,34 @@ install_dashboard() {
fi fi
} }
install_dashboard_standalone() { install_dashboard_docker() {
install_base if [[ ! $FRESH_INSTALL == 0 ]]; then
command -v docker >/dev/null 2>&1
echo -e "> 安装面板" if [[ $? != 0 ]]; then
echo -e "正在安装 Docker"
# 哪吒监控文件夹 if [ "$os_alpine" != 1 ]; then
if [ ! -d "${NZ_DASHBOARD_PATH}/app" ]; then bash <(curl -sL https://${Get_Docker_URL}) ${Get_Docker_Argu} >/dev/null 2>&1
mkdir -p $NZ_DASHBOARD_PATH if [[ $? != 0 ]]; then
else echo -e "${red}下载脚本失败,请检查本机能否连接 ${Get_Docker_URL}${plain}"
echo "您可能已经安装过面板端,重复安装会覆盖数据,请注意备份。" return 0
read -e -r -p "是否退出安装? [Y/n] " input fi
case $input in systemctl enable docker.service
[yY][eE][sS] | [yY]) systemctl start docker.service
echo "退出安装" else
exit 0 apk add docker docker-compose >/dev/null 2>&1
;; rc-update add docker
[nN][oO] | [nN]) rc-service docker start
echo "继续安装" fi
;; echo -e "${green}Docker${plain} 安装成功"
*) installation_check
echo "退出安装" fi
exit 0
;;
esac
fi fi
}
install_dashboard_standalone() {
if [ ! -d "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" ] || [ ! -d "${NZ_DASHBOARD_PATH}/resource/static/custom" ]; then if [ ! -d "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" ] || [ ! -d "${NZ_DASHBOARD_PATH}/resource/static/custom" ]; then
mkdir -p "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" "${NZ_DASHBOARD_PATH}/resource/static/custom" >/dev/null 2>&1 mkdir -p "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" "${NZ_DASHBOARD_PATH}/resource/static/custom" >/dev/null 2>&1
fi fi
chmod 777 -R $NZ_DASHBOARD_PATH
modify_dashboard_config_standalone 0
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
selinux() { selinux() {
@ -404,134 +430,85 @@ modify_agent_config() {
modify_dashboard_config() { modify_dashboard_config() {
echo -e "> 修改面板配置" echo -e "> 修改面板配置"
echo -e "正在下载 Docker 脚本" if [[ $IS_DOCKER_NEZHA == 1 ]]; then
wget -t 2 -T 10 -O /tmp/nezha-docker-compose.yaml https://${GITHUB_RAW_URL}/script/docker-compose.yaml >/dev/null 2>&1 echo -e "正在下载 Docker 脚本"
if [[ $? != 0 ]]; then wget -t 2 -T 10 -O /tmp/nezha-docker-compose.yaml https://${GITHUB_RAW_URL}/script/docker-compose.yaml >/dev/null 2>&1
echo -e "${red}下载脚本失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}"
return 0
fi
wget -t 2 -T 10 -O /tmp/nezha-config.yaml https://${GITHUB_RAW_URL}/script/config.yaml >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "${red}下载脚本失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}"
return 0
fi
echo "关于 GitHub Oauth2 应用:在 https://github.com/settings/developers 创建无需审核Callback 填 http(s)://域名或IP/oauth2/callback" &&
echo "关于 Gitee Oauth2 应用:在 https://gitee.com/oauth/applications 创建无需审核Callback 填 http(s)://域名或IP/oauth2/callback" &&
read -ep "请输入 OAuth2 提供商(github/gitlab/jihulab/gitee默认 github): " nz_oauth2_type &&
read -ep "请输入 Oauth2 应用的 Client ID: " nz_github_oauth_client_id &&
read -ep "请输入 Oauth2 应用的 Client Secret: " nz_github_oauth_client_secret &&
read -ep "请输入 GitHub/Gitee 登录名作为管理员,多个以逗号隔开: " nz_admin_logins &&
read -ep "请输入站点标题: " nz_site_title &&
read -ep "请输入站点访问端口: (默认 8008)" nz_site_port &&
read -ep "请输入用于 Agent 接入的 RPC 端口: (默认 5555)" nz_grpc_port
if [[ -z "${nz_admin_logins}" || -z "${nz_github_oauth_client_id}" || -z "${nz_github_oauth_client_secret}" || -z "${nz_site_title}" ]]; then
echo -e "${red}所有选项都不能为空${plain}"
before_show_menu
return 1
fi
if [[ -z "${nz_site_port}" ]]; then
nz_site_port=8008
fi
if [[ -z "${nz_grpc_port}" ]]; then
nz_grpc_port=5555
fi
if [[ -z "${nz_oauth2_type}" ]]; then
nz_oauth2_type=github
fi
sed -i "s/nz_oauth2_type/${nz_oauth2_type}/" /tmp/nezha-config.yaml
sed -i "s/nz_admin_logins/${nz_admin_logins}/" /tmp/nezha-config.yaml
sed -i "s/nz_grpc_port/${nz_grpc_port}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_id/${nz_github_oauth_client_id}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_secret/${nz_github_oauth_client_secret}/" /tmp/nezha-config.yaml
sed -i "s/nz_language/zh-CN/" /tmp/nezha-config.yaml
sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml
sed -i "s/nz_site_port/${nz_site_port}/" /tmp/nezha-docker-compose.yaml
sed -i "s/nz_grpc_port/${nz_grpc_port}/g" /tmp/nezha-docker-compose.yaml
sed -i "s/nz_image_url/${Docker_IMG}/" /tmp/nezha-docker-compose.yaml
mkdir -p $NZ_DASHBOARD_PATH/data
mv -f /tmp/nezha-config.yaml ${NZ_DASHBOARD_PATH}/data/config.yaml
mv -f /tmp/nezha-docker-compose.yaml ${NZ_DASHBOARD_PATH}/docker-compose.yaml
echo -e "面板配置 ${green}修改成功,请稍等重启生效${plain}"
restart_and_update
if [[ $# == 0 ]]; then
before_show_menu
fi
}
modify_dashboard_config_standalone() {
echo -e "> 修改面板配置"
echo -e "正在下载配置模板"
wget -t 2 -T 10 -O /tmp/nezha-config.yaml https://${GITHUB_RAW_URL}/script/config.yaml >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "${red}下载文件失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}"
return 0
fi
echo "关于 GitHub Oauth2 应用:在 https://github.com/settings/developers 创建无需审核Callback 填 http(s)://域名或IP/oauth2/callback" &&
echo "关于 Gitee Oauth2 应用:在 https://gitee.com/oauth/applications 创建无需审核Callback 填 http(s)://域名或IP/oauth2/callback" &&
read -ep "请输入 OAuth2 提供商(github/gitlab/jihulab/gitee默认 github): " nz_oauth2_type &&
read -ep "请输入 Oauth2 应用的 Client ID: " nz_github_oauth_client_id &&
read -ep "请输入 Oauth2 应用的 Client Secret: " nz_github_oauth_client_secret &&
read -ep "请输入 GitHub/Gitee 登录名作为管理员,多个以逗号隔开: " nz_admin_logins &&
read -ep "请输入站点标题: " nz_site_title &&
read -ep "请输入站点访问端口: (默认 8008)" nz_site_port &&
read -ep "请输入用于 Agent 接入的 RPC 端口: (默认 5555)" nz_grpc_port
if [[ -z "${nz_admin_logins}" || -z "${nz_github_oauth_client_id}" || -z "${nz_github_oauth_client_secret}" || -z "${nz_site_title}" ]]; then
echo -e "${red}所有选项都不能为空${plain}"
before_show_menu
return 1
fi
if [[ -z "${nz_site_port}" ]]; then
nz_site_port=8008
fi
if [[ -z "${nz_grpc_port}" ]]; then
nz_grpc_port=5555
fi
if [[ -z "${nz_oauth2_type}" ]]; then
nz_oauth2_type=github
fi
sed -i "s/nz_oauth2_type/${nz_oauth2_type}/" /tmp/nezha-config.yaml
sed -i "s/nz_admin_logins/${nz_admin_logins}/" /tmp/nezha-config.yaml
sed -i "s/nz_grpc_port/${nz_grpc_port}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_id/${nz_github_oauth_client_id}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_secret/${nz_github_oauth_client_secret}/" /tmp/nezha-config.yaml
sed -i "s/nz_language/zh-CN/" /tmp/nezha-config.yaml
sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml
sed -i "s/80/${nz_site_port}/" /tmp/nezha-config.yaml
mkdir -p $NZ_DASHBOARD_PATH/data
mv -f /tmp/nezha-config.yaml ${NZ_DASHBOARD_PATH}/data/config.yaml
echo -e "正在下载服务文件"
if [ "$os_alpine" != 1 ]; then
wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICE https://${GITHUB_RAW_URL}/script/nezha-dashboard.service >/dev/null 2>&1
else
wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICERC https://${GITHUB_RAW_URL}/script/nezha-dashboard >/dev/null 2>&1
chmod +x $NZ_DASHBOARD_SERVICERC
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo -e "${red}文件下载失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}" echo -e "${red}下载脚本失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}"
return 0 return 0
fi fi
fi fi
wget -t 2 -T 10 -O /tmp/nezha-config.yaml https://${GITHUB_RAW_URL}/script/config.yaml >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "${red}下载脚本失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}"
return 0
fi
echo "关于 GitHub Oauth2 应用:在 https://github.com/settings/developers 创建无需审核Callback 填 http(s)://域名或IP/oauth2/callback" &&
echo "关于 Gitee Oauth2 应用:在 https://gitee.com/oauth/applications 创建无需审核Callback 填 http(s)://域名或IP/oauth2/callback" &&
read -ep "请输入 OAuth2 提供商(github/gitlab/jihulab/gitee默认 github): " nz_oauth2_type &&
read -ep "请输入 Oauth2 应用的 Client ID: " nz_github_oauth_client_id &&
read -ep "请输入 Oauth2 应用的 Client Secret: " nz_github_oauth_client_secret &&
read -ep "请输入 GitHub/Gitee 登录名作为管理员,多个以逗号隔开: " nz_admin_logins &&
read -ep "请输入站点标题: " nz_site_title &&
read -ep "请输入站点访问端口: (默认 8008)" nz_site_port &&
read -ep "请输入用于 Agent 接入的 RPC 端口: (默认 5555)" nz_grpc_port
if [[ -z "${nz_admin_logins}" || -z "${nz_github_oauth_client_id}" || -z "${nz_github_oauth_client_secret}" || -z "${nz_site_title}" ]]; then
echo -e "${red}所有选项都不能为空${plain}"
before_show_menu
return 1
fi
if [[ -z "${nz_site_port}" ]]; then
nz_site_port=8008
fi
if [[ -z "${nz_grpc_port}" ]]; then
nz_grpc_port=5555
fi
if [[ -z "${nz_oauth2_type}" ]]; then
nz_oauth2_type=github
fi
sed -i "s/nz_oauth2_type/${nz_oauth2_type}/" /tmp/nezha-config.yaml
sed -i "s/nz_admin_logins/${nz_admin_logins}/" /tmp/nezha-config.yaml
sed -i "s/nz_grpc_port/${nz_grpc_port}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_id/${nz_github_oauth_client_id}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_secret/${nz_github_oauth_client_secret}/" /tmp/nezha-config.yaml
sed -i "s/nz_language/zh-CN/" /tmp/nezha-config.yaml
sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml
if [[ $IS_DOCKER_NEZHA == 1 ]]; then
sed -i "s/nz_site_port/${nz_site_port}/" /tmp/nezha-docker-compose.yaml
sed -i "s/nz_grpc_port/${nz_grpc_port}/g" /tmp/nezha-docker-compose.yaml
sed -i "s/nz_image_url/${Docker_IMG}/" /tmp/nezha-docker-compose.yaml
elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
sed -i "s/80/${nz_site_port}/" /tmp/nezha-config.yaml
fi
mkdir -p $NZ_DASHBOARD_PATH/data
mv -f /tmp/nezha-config.yaml ${NZ_DASHBOARD_PATH}/data/config.yaml
if [[ $IS_DOCKER_NEZHA == 1 ]]; then
mv -f /tmp/nezha-docker-compose.yaml ${NZ_DASHBOARD_PATH}/docker-compose.yaml
fi
if [[ $IS_DOCKER_NEZHA == 0 ]]; then
echo -e "正在下载服务文件"
if [ "$os_alpine" != 1 ]; then
wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICE https://${GITHUB_RAW_URL}/script/nezha-dashboard.service >/dev/null 2>&1
else
wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICERC https://${GITHUB_RAW_URL}/script/nezha-dashboard >/dev/null 2>&1
chmod +x $NZ_DASHBOARD_SERVICERC
if [[ $? != 0 ]]; then
echo -e "${red}文件下载失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}"
return 0
fi
fi
fi
echo -e "面板配置 ${green}修改成功,请稍等重启生效${plain}" echo -e "面板配置 ${green}修改成功,请稍等重启生效${plain}"
restart_and_update_standalone restart_and_update
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
before_show_menu before_show_menu
@ -543,15 +520,10 @@ restart_and_update() {
cd $NZ_DASHBOARD_PATH cd $NZ_DASHBOARD_PATH
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then restart_and_update_docker
docker compose pull elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
docker compose down restart_and_update_standalone
docker compose up -d
else
docker-compose pull
docker-compose down
docker-compose up -d
fi fi
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@ -566,11 +538,13 @@ restart_and_update() {
fi fi
} }
restart_and_update_docker() {
$DOCKER_COMPOSE_COMMAND pull
$DOCKER_COMPOSE_COMMAND down
$DOCKER_COMPOSE_COMMAND up -d
}
restart_and_update_standalone() { restart_and_update_standalone() {
echo -e "> 重启并更新面板"
cd $NZ_DASHBOARD_PATH
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
systemctl stop nezha-dashboard systemctl stop nezha-dashboard
else else
@ -587,27 +561,15 @@ restart_and_update_standalone() {
rc-update add nezha-dashboard rc-update add nezha-dashboard
rc-service nezha-dashboard restart rc-service nezha-dashboard restart
fi fi
if [[ $? == 0 ]]; then
echo -e "${green}哪吒监控 重启成功${plain}"
echo -e "默认管理面板地址:${yellow}域名:站点访问端口${plain}"
else
echo -e "${red}重启失败,可能是因为启动时间超过了两秒,请稍后查看日志信息${plain}"
fi
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
start_dashboard() { start_dashboard() {
echo -e "> 启动面板" echo -e "> 启动面板"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then start_dashboard_docker
cd $NZ_DASHBOARD_PATH && docker compose up -d elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else start_dashboard_standalone
cd $NZ_DASHBOARD_PATH && docker-compose up -d
fi fi
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@ -621,34 +583,25 @@ start_dashboard() {
fi fi
} }
start_dashboard_standalone() { start_dashboard_docker() {
echo -e "> 启动面板" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND up -d
}
start_dashboard_standalone() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
systemctl start nezha-dashboard systemctl start nezha-dashboard
else else
rc-service nezha-dashboard start rc-service nezha-dashboard start
fi fi
if [[ $? == 0 ]]; then
echo -e "${green}哪吒监控 启动成功${plain}"
else
echo -e "${red}启动失败,请稍后查看日志信息${plain}"
fi
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
stop_dashboard() { stop_dashboard() {
echo -e "> 停止面板" echo -e "> 停止面板"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then stop_dashboard_docker
cd $NZ_DASHBOARD_PATH && docker compose down elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else stop_dashboard_standalone
cd $NZ_DASHBOARD_PATH && docker-compose down
fi fi
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@ -662,34 +615,25 @@ stop_dashboard() {
fi fi
} }
stop_dashboard_standalone() { stop_dashboard_docker() {
echo -e "> 停止面板" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND down
}
stop_dashboard_standalone() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
systemctl stop nezha-dashboard systemctl stop nezha-dashboard
else else
rc-service nezha-dashboard stop rc-service nezha-dashboard stop
fi fi
if [[ $? == 0 ]]; then
echo -e "${green}哪吒监控 停止成功${plain}"
else
echo -e "${red}停止失败,请稍后查看日志信息${plain}"
fi
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
show_dashboard_log() { show_dashboard_log() {
echo -e "> 获取面板日志" echo -e "> 获取面板日志"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then show_dashboard_log_docker
cd $NZ_DASHBOARD_PATH && docker compose logs -f elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else show_dashboard_log_standalone
cd $NZ_DASHBOARD_PATH && docker-compose logs -f
fi fi
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
@ -697,33 +641,27 @@ show_dashboard_log() {
fi fi
} }
show_dashboard_log_standalone() { show_dashboard_log_docker() {
echo -e "> 获取面板日志" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND logs -f
}
show_dashboard_log_standalone() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
journalctl -xf -u nezha-dashboard.service journalctl -xf -u nezha-dashboard.service
else else
echo "很抱歉OpenRC 无此功能" tail -n 10 /var/log/nezha-dashboard.err
fi
if [[ $# == 0 ]]; then
before_show_menu
fi fi
} }
uninstall_dashboard() { uninstall_dashboard() {
echo -e "> 卸载管理面板" echo -e "> 卸载管理面板"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then uninstall_dashboard_docker
cd $NZ_DASHBOARD_PATH && docker compose down elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else uninstall_dashboard_standalone
cd $NZ_DASHBOARD_PATH && docker-compose down
fi fi
rm -rf $NZ_DASHBOARD_PATH
docker rmi -f ghcr.io/naiba/nezha-dashboard >/dev/null 2>&1
docker rmi -f registry.cn-shanghai.aliyuncs.com/naibahq/nezha-dashboard >/dev/null 2>&1
clean_all clean_all
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
@ -731,9 +669,14 @@ uninstall_dashboard() {
fi fi
} }
uninstall_dashboard_standalone() { uninstall_dashboard_docker() {
echo -e "> 卸载管理面板" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND down
rm -rf $NZ_DASHBOARD_PATH
docker rmi -f ghcr.io/naiba/nezha-dashboard >/dev/null 2>&1
docker rmi -f registry.cn-shanghai.aliyuncs.com/naibahq/nezha-dashboard >/dev/null 2>&1
}
uninstall_dashboard_standalone() {
rm -rf $NZ_DASHBOARD_PATH rm -rf $NZ_DASHBOARD_PATH
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
@ -747,12 +690,6 @@ uninstall_dashboard_standalone() {
else else
rm $NZ_DASHBOARD_SERVICERC rm $NZ_DASHBOARD_SERVICERC
fi fi
clean_all
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
show_agent_log() { show_agent_log() {
@ -761,7 +698,7 @@ show_agent_log() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
journalctl -xf -u nezha-agent.service journalctl -xf -u nezha-agent.service
else else
echo "很抱歉OpenRC 无此功能" tail -n 10 /var/log/nezha-agent.err
fi fi
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
@ -811,59 +748,6 @@ clean_all() {
fi fi
} }
select_version() {
DOCKER_COMPOSE_COMMAND=""
if docker compose version >/dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker compose"
if $DOCKER_COMPOSE_COMMAND ls | grep -qw "$NZ_DASHBOARD_PATH/docker-compose.yaml" >/dev/null 2>&1; then
NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "存在带有 nezha-dashboard 仓库的 Docker 镜像:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
return
else
echo "未找到带有 nezha-dashboard 仓库的 Docker 镜像。"
fi
fi
elif command -v docker-compose >/dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker-compose"
if $DOCKER_COMPOSE_COMMAND -f "$NZ_DASHBOARD_PATH/docker-compose.yaml" config >/dev/null 2>&1; then
NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "存在带有 nezha-dashboard 仓库的 Docker 镜像:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
return
else
echo "未找到带有 nezha-dashboard 仓库的 Docker 镜像。"
fi
fi
fi
if [[ -f $NZ_DASHBOARD_PATH/app ]]; then
IS_DOCKER_NEZHA=0
else
echo -e "${yellow}请自行选择您的安装方式如果你是安装Agent输入哪个都是一样的\n1. Docker\n2. 独立安装${plain}"
while true; do
read -e -r -p "请输入数字 [1-2]" option
case "${option}" in
1)
IS_DOCKER_NEZHA=1
break
;;
2)
IS_DOCKER_NEZHA=0
break
;;
*)
echo "输入有误,请重新输入"
;;
esac
done
fi
}
show_usage() { show_usage() {
echo "哪吒监控 管理脚本使用方法: " echo "哪吒监控 管理脚本使用方法: "
echo "--------------------------------------------------------" echo "--------------------------------------------------------"
@ -908,8 +792,7 @@ show_menu() {
${green}0.${plain} 退出脚本 ${green}0.${plain} 退出脚本
" "
echo && read -ep "请输入选择 [0-13]: " num echo && read -ep "请输入选择 [0-13]: " num
if [[ $IS_DOCKER_NEZHA == 1 ]]; then case "${num}" in
case "${num}" in
0) 0)
exit 0 exit 0
;; ;;
@ -955,65 +838,14 @@ show_menu() {
*) *)
echo -e "${red}请输入正确的数字 [0-13]${plain}" echo -e "${red}请输入正确的数字 [0-13]${plain}"
;; ;;
esac esac
elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
case "${num}" in
0)
exit 0
;;
1)
install_dashboard_standalone
;;
2)
modify_dashboard_config_standalone
;;
3)
start_dashboard_standalone
;;
4)
stop_dashboard_standalone
;;
5)
restart_and_update_standalone
;;
6)
show_dashboard_log_standalone
;;
7)
uninstall_dashboard_standalone
;;
8)
install_agent
;;
9)
modify_agent_config
;;
10)
show_agent_log
;;
11)
uninstall_agent
;;
12)
restart_agent
;;
13)
update_script
;;
*)
echo -e "${red}请输入正确的数字 [0-13]${plain}"
;;
esac
else
select_version
fi
} }
pre_check pre_check
installation_check
if [[ $# > 0 ]]; then if [[ $# > 0 ]]; then
if [[ $IS_DOCKER_NEZHA == 1 ]]; then case $1 in
case $1 in
"install_dashboard") "install_dashboard")
install_dashboard 0 install_dashboard 0
;; ;;
@ -1059,84 +891,8 @@ if [[ $# > 0 ]]; then
update_script 0 update_script 0
;; ;;
*) show_usage ;; *) show_usage ;;
esac esac
elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
case $1 in
"install_dashboard")
install_dashboard_standalone 0
;;
"modify_dashboard_config")
modify_dashboard_config_standalone 0
;;
"start_dashboard")
start_dashboard_standalone 0
;;
"stop_dashboard")
stop_dashboard_standalone 0
;;
"restart_and_update")
restart_and_update_standalone 0
;;
"show_dashboard_log")
show_dashboard_log_standalone 0
;;
"uninstall_dashboard")
uninstall_dashboard_standalone 0
;;
"install_agent")
shift
if [ $# -ge 3 ]; then
install_agent "$@"
else
install_agent 0
fi
;;
"modify_agent_config")
modify_agent_config 0
;;
"show_agent_log")
show_agent_log 0
;;
"uninstall_agent")
uninstall_agent 0
;;
"restart_agent")
restart_agent 0
;;
"update_script")
update_script 0
;;
*) show_usage ;;
esac
else
case $1 in
"install_agent")
shift
if [ $# -ge 3 ]; then
install_agent "$@"
else
install_agent 0
fi
;;
"modify_agent_config")
modify_agent_config 0
;;
"show_agent_log")
show_agent_log 0
;;
"uninstall_agent")
uninstall_agent 0
;;
"restart_agent")
restart_agent 0
;;
"update_script")
update_script 0
;;
*) show_usage ;;
esac
fi
else else
select_version select_version
show_menu show_menu
fi fi

View File

@ -14,7 +14,7 @@ NZ_AGENT_SERVICE="/etc/systemd/system/nezha-agent.service"
NZ_AGENT_SERVICERC="/etc/init.d/nezha-agent" NZ_AGENT_SERVICERC="/etc/init.d/nezha-agent"
NZ_DASHBOARD_SERVICE="/etc/systemd/system/nezha-dashboard.service" NZ_DASHBOARD_SERVICE="/etc/systemd/system/nezha-dashboard.service"
NZ_DASHBOARD_SERVICERC="/etc/init.d/nezha-dashboard" NZ_DASHBOARD_SERVICERC="/etc/init.d/nezha-dashboard"
NZ_VERSION="v0.15.9" NZ_VERSION="v0.16.0"
red='\033[0;31m' red='\033[0;31m'
green='\033[0;32m' green='\033[0;32m'
@ -101,19 +101,60 @@ pre_check() {
fi fi
} }
confirm() { installation_check() {
if [[ $# > 1 ]]; then if docker compose version >/dev/null 2>&1; then
echo && read -e -p "$1 [默认$2]: " temp DOCKER_COMPOSE_COMMAND="docker compose"
if [[ x"${temp}" == x"" ]]; then if $DOCKER_COMPOSE_COMMAND ls | grep -qw "$NZ_DASHBOARD_PATH/docker-compose.yaml" >/dev/null 2>&1; then
temp=$2 NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "Docker image with nezha-dashboard repository exists:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
return
else
echo "No Docker images with the nezha-dashboard repository were found."
fi
fi
elif command -v docker-compose >/dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker-compose"
if $DOCKER_COMPOSE_COMMAND -f "$NZ_DASHBOARD_PATH/docker-compose.yaml" config >/dev/null 2>&1; then
NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "Docker image with nezha-dashboard repository exists:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
return
else
echo "No Docker images with the nezha-dashboard repository were found."
fi
fi fi
else
read -e -p "$1 [y/n]: " temp
fi fi
if [[ x"${temp}" == x"y" || x"${temp}" == x"Y" ]]; then
return 0 if [[ -f $NZ_DASHBOARD_PATH/app ]]; then
else IS_DOCKER_NEZHA=0
return 1 FRESH_INSTALL=0
fi
}
select_version() {
if [[ -z $IS_DOCKER_NEZHA ]]; then
echo -e "${yellow}Select your installation method(Input anything is ok if you are installing agent):\n1. Docker\n2. Standalone${plain}"
while true; do
read -e -r -p "Please enter [1-2]: " option
case "${option}" in
1)
IS_DOCKER_NEZHA=1
break
;;
2)
IS_DOCKER_NEZHA=0
break
;;
*)
echo "${red}Please enter the correct number [1-2]${plain}"
;;
esac
done
fi fi
} }
@ -184,7 +225,7 @@ install_dashboard() {
echo -e "> Install Panel" echo -e "> Install Panel"
# Nezha Monitoring Folder # Nezha Monitoring Folder
if [ ! -d "${NZ_DASHBOARD_PATH}/docker-compose.yaml" ]; then if [[ ! $FRESH_INSTALL == 0 ]]; then
mkdir -p $NZ_DASHBOARD_PATH mkdir -p $NZ_DASHBOARD_PATH
else else
echo "You may have already installed the dashboard, repeated installation will overwrite the data, please pay attention to backup." echo "You may have already installed the dashboard, repeated installation will overwrite the data, please pay attention to backup."
@ -205,17 +246,10 @@ install_dashboard() {
fi fi
chmod 777 -R $NZ_DASHBOARD_PATH chmod 777 -R $NZ_DASHBOARD_PATH
command -v docker >/dev/null 2>&1 if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? != 0 ]]; then install_dashboard_docker
echo -e "Installing Docker" elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
bash <(curl -sL https://${Get_Docker_URL}) ${Get_Docker_Argu} >/dev/null 2>&1 install_dashboard_standalone
if [[ $? != 0 ]]; then
echo -e "${red}Script failed to get, please check if the network can link ${Get_Docker_URL}${plain}"
return 0
fi
systemctl enable docker.service
systemctl start docker.service
echo -e "${green}Docker${plain} installed successfully"
fi fi
modify_dashboard_config 0 modify_dashboard_config 0
@ -225,43 +259,34 @@ install_dashboard() {
fi fi
} }
install_dashboard_standalone() { install_dashboard_docker() {
install_base if [[ ! $FRESH_INSTALL == 0 ]]; then
command -v docker >/dev/null 2>&1
echo -e "> Install Panel" if [[ $? != 0 ]]; then
echo -e "Installing Docker"
# Nezha Monitoring Folder if [ "$os_alpine" != 1 ]; then
if [ ! -d "${NZ_DASHBOARD_PATH}/app" ]; then bash <(curl -sL https://${Get_Docker_URL}) ${Get_Docker_Argu} >/dev/null 2>&1
mkdir -p $NZ_DASHBOARD_PATH if [[ $? != 0 ]]; then
else echo -e "${red}Script failed to get, please check if the network can link ${Get_Docker_URL}${plain}"
echo "You may have already installed the dashboard, repeated installation will overwrite the data, please pay attention to backup." return 0
read -e -r -p "Exit the installation? [Y/n] " input fi
case $input in systemctl enable docker.service
[yY][eE][sS] | [yY]) systemctl start docker.service
echo "Exit the installation." else
exit 0 apk add docker docker-compose >/dev/null 2>&1
;; rc-update add docker
[nN][oO] | [nN]) rc-service docker start
echo "Continue." fi
;; echo -e "${green}Docker${plain} installed successfully"
*) installation_check
echo "Exit the installation." fi
exit 0
;;
esac
fi fi
}
install_dashboard_standalone() {
if [ ! -d "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" ] || [ ! -d "${NZ_DASHBOARD_PATH}/resource/static/custom" ]; then if [ ! -d "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" ] || [ ! -d "${NZ_DASHBOARD_PATH}/resource/static/custom" ]; then
mkdir -p "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" "${NZ_DASHBOARD_PATH}/resource/static/custom" >/dev/null 2>&1 mkdir -p "${NZ_DASHBOARD_PATH}/resource/template/theme-custom" "${NZ_DASHBOARD_PATH}/resource/static/custom" >/dev/null 2>&1
fi fi
chmod 777 -R $NZ_DASHBOARD_PATH
modify_dashboard_config_standalone 0
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
selinux() { selinux() {
@ -401,11 +426,13 @@ modify_agent_config() {
modify_dashboard_config() { modify_dashboard_config() {
echo -e "> Modify Panel Configuration" echo -e "> Modify Panel Configuration"
echo -e "Download Docker Script" if [[ $IS_DOCKER_NEZHA == 1 ]]; then
wget -t 2 -T 10 -O /tmp/nezha-docker-compose.yaml https://${GITHUB_RAW_URL}/script/docker-compose.yaml >/dev/null 2>&1 echo -e "Download Docker Script"
if [[ $? != 0 ]]; then wget -t 2 -T 10 -O /tmp/nezha-docker-compose.yaml https://${GITHUB_RAW_URL}/script/docker-compose.yaml >/dev/null 2>&1
echo -e "${red}Script failed to get, please check if the network can link ${GITHUB_RAW_URL}${plain}" if [[ $? != 0 ]]; then
return 0 echo -e "${red}Script failed to get, please check if the network can link ${GITHUB_RAW_URL}${plain}"
return 0
fi
fi fi
wget -t 2 -T 10 -O /tmp/nezha-config.yaml https://${GITHUB_RAW_URL}/script/config.yaml >/dev/null 2>&1 wget -t 2 -T 10 -O /tmp/nezha-config.yaml https://${GITHUB_RAW_URL}/script/config.yaml >/dev/null 2>&1
@ -447,88 +474,37 @@ modify_dashboard_config() {
sed -i "s/nz_github_oauth_client_secret/${nz_github_oauth_client_secret}/" /tmp/nezha-config.yaml sed -i "s/nz_github_oauth_client_secret/${nz_github_oauth_client_secret}/" /tmp/nezha-config.yaml
sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml
sed -i "s/nz_language/en-US/" /tmp/nezha-config.yaml sed -i "s/nz_language/en-US/" /tmp/nezha-config.yaml
sed -i "s/nz_site_port/${nz_site_port}/" /tmp/nezha-docker-compose.yaml if [[ $IS_DOCKER_NEZHA == 1 ]]; then
sed -i "s/nz_grpc_port/${nz_grpc_port}/g" /tmp/nezha-docker-compose.yaml sed -i "s/nz_site_port/${nz_site_port}/" /tmp/nezha-docker-compose.yaml
sed -i "s/nz_image_url/${Docker_IMG}/" /tmp/nezha-docker-compose.yaml sed -i "s/nz_grpc_port/${nz_grpc_port}/g" /tmp/nezha-docker-compose.yaml
sed -i "s/nz_image_url/${Docker_IMG}/" /tmp/nezha-docker-compose.yaml
elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
sed -i "s/80/${nz_site_port}/" /tmp/nezha-config.yaml
fi
mkdir -p $NZ_DASHBOARD_PATH/data mkdir -p $NZ_DASHBOARD_PATH/data
mv -f /tmp/nezha-config.yaml ${NZ_DASHBOARD_PATH}/data/config.yaml mv -f /tmp/nezha-config.yaml ${NZ_DASHBOARD_PATH}/data/config.yaml
mv -f /tmp/nezha-docker-compose.yaml ${NZ_DASHBOARD_PATH}/docker-compose.yaml if [[ $IS_DOCKER_NEZHA == 1 ]]; then
mv -f /tmp/nezha-docker-compose.yaml ${NZ_DASHBOARD_PATH}/docker-compose.yaml
echo -e "Dashboard configuration ${green} modified successfully, please wait for Dashboard self-restart to take effect${plain}"
restart_and_update
if [[ $# == 0 ]]; then
before_show_menu
fi
}
modify_dashboard_config_standalone() {
echo -e "> Modify Panel Configuration"
echo -e "Download configuration template"
wget -t 2 -T 10 -O /tmp/nezha-config.yaml https://${GITHUB_RAW_URL}/script/config.yaml >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "${red}File failed to get, please check if the network can link ${GITHUB_RAW_URL}${plain}"
return 0
fi fi
echo "About the GitHub Oauth2 application: create it at https://github.com/settings/developers, no review required, and fill in the http(s)://domain_or_IP/oauth2/callback" && if [[ $IS_DOCKER_NEZHA == 0 ]]; then
echo "(Not recommended) About the Gitee Oauth2 application: create it at https://gitee.com/oauth/applications, no auditing required, and fill in the http(s)://domain_or_IP/oauth2/callback" && echo -e "Downloading service file"
read -ep "Please enter the OAuth2 provider (github/gitlab/jihulab/gitee, default github): " nz_oauth2_type && if [ "$os_alpine" != 1 ]; then
read -ep "Please enter the Client ID of the Oauth2 application: " nz_github_oauth_client_id && wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICE https://${GITHUB_RAW_URL}/script/nezha-dashboard.service >/dev/null 2>&1
read -ep "Please enter the Client Secret of the Oauth2 application: " nz_github_oauth_client_secret && else
read -ep "Please enter your GitHub/Gitee login name as the administrator, separated by commas: " nz_admin_logins && wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICERC https://${GITHUB_RAW_URL}/script/nezha-dashboard >/dev/null 2>&1
read -ep "Please enter the site title: " nz_site_title && chmod +x $NZ_DASHBOARD_SERVICERC
read -ep "Please enter the site access port: (default 8008)" nz_site_port && if [[ $? != 0 ]]; then
read -ep "Please enter the RPC port to be used for Agent access: (default 5555)" nz_grpc_port echo -e "${red}File failed to get, please check if the network can link ${GITHUB_RAW_URL}${plain}"
return 0
if [[ -z "${nz_admin_logins}" || -z "${nz_github_oauth_client_id}" || -z "${nz_github_oauth_client_secret}" || -z "${nz_site_title}" ]]; then fi
echo -e "${red}All options cannot be empty${plain}"
before_show_menu
return 1
fi
if [[ -z "${nz_site_port}" ]]; then
nz_site_port=8008
fi
if [[ -z "${nz_grpc_port}" ]]; then
nz_grpc_port=5555
fi
if [[ -z "${nz_oauth2_type}" ]]; then
nz_oauth2_type=github
fi
sed -i "s/nz_oauth2_type/${nz_oauth2_type}/" /tmp/nezha-config.yaml
sed -i "s/nz_admin_logins/${nz_admin_logins}/" /tmp/nezha-config.yaml
sed -i "s/nz_grpc_port/${nz_grpc_port}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_id/${nz_github_oauth_client_id}/" /tmp/nezha-config.yaml
sed -i "s/nz_github_oauth_client_secret/${nz_github_oauth_client_secret}/" /tmp/nezha-config.yaml
sed -i "s/nz_language/zh-CN/" /tmp/nezha-config.yaml
sed -i "s/nz_site_title/${nz_site_title}/" /tmp/nezha-config.yaml
sed -i "s/80/${nz_site_port}/" /tmp/nezha-config.yaml
mkdir -p $NZ_DASHBOARD_PATH/data
mv -f /tmp/nezha-config.yaml ${NZ_DASHBOARD_PATH}/data/config.yaml
echo -e "Downloading service file"
if [ "$os_alpine" != 1 ]; then
wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICE https://${GITHUB_RAW_URL}/script/nezha-dashboard.service >/dev/null 2>&1
else
wget -t 2 -T 10 -O $NZ_DASHBOARD_SERVICERC https://${GITHUB_RAW_URL}/script/nezha-dashboard >/dev/null 2>&1
chmod +x $NZ_DASHBOARD_SERVICERC
if [[ $? != 0 ]]; then
echo -e "${red}File failed to get, please check if the network can link ${GITHUB_RAW_URL}${plain}"
return 0
fi fi
fi fi
echo -e "Dashboard configuration ${green} modified successfully, please wait for Dashboard self-restart to take effect${plain}" echo -e "Dashboard configuration ${green} modified successfully, please wait for Dashboard self-restart to take effect${plain}"
restart_and_update_standalone restart_and_update
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
before_show_menu before_show_menu
@ -540,15 +516,10 @@ restart_and_update() {
cd $NZ_DASHBOARD_PATH cd $NZ_DASHBOARD_PATH
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then restart_and_update_docker
docker compose pull elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
docker compose down restart_and_update_standalone
docker compose up -d
else
docker-compose pull
docker-compose down
docker-compose up -d
fi fi
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@ -563,11 +534,13 @@ restart_and_update() {
fi fi
} }
restart_and_update_docker() {
$DOCKER_COMPOSE_COMMAND pull
$DOCKER_COMPOSE_COMMAND down
$DOCKER_COMPOSE_COMMAND up -d
}
restart_and_update_standalone() { restart_and_update_standalone() {
echo -e "> Restart and Update the Panel"
cd $NZ_DASHBOARD_PATH
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
systemctl stop nezha-dashboard systemctl stop nezha-dashboard
else else
@ -584,27 +557,15 @@ restart_and_update_standalone() {
rc-update add nezha-dashboard rc-update add nezha-dashboard
rc-service nezha-dashboard restart rc-service nezha-dashboard restart
fi fi
if [[ $? == 0 ]]; then
echo -e "${green}Nezha Monitoring Restart Successful${plain}"
echo -e "Default panel address: ${yellow}domain:Site_access_port${plain}"
else
echo -e "${red}The restart failed, probably because the boot time exceeded two seconds, please check the log information later${plain}"
fi
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
start_dashboard() { start_dashboard() {
echo -e "> Start Panel" echo -e "> Start Panel"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then start_dashboard_docker
cd $NZ_DASHBOARD_PATH && docker compose up -d elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else start_dashboard_standalone
cd $NZ_DASHBOARD_PATH && docker-compose up -d
fi fi
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@ -618,34 +579,25 @@ start_dashboard() {
fi fi
} }
start_dashboard_standalone() { start_dashboard_docker() {
echo -e "> Start Panel" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND up -d
}
start_dashboard_standalone() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
systemctl start nezha-dashboard systemctl start nezha-dashboard
else else
rc-service nezha-dashboard start rc-service nezha-dashboard start
fi fi
if [[ $? == 0 ]]; then
echo -e "${green}Nezha Monitoring Start Successful${plain}"
else
echo -e "${red}Failed to start, please check the log message later${plain}"
fi
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
stop_dashboard() { stop_dashboard() {
echo -e "> Stop Panel" echo -e "> Stop Panel"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then stop_dashboard_docker
cd $NZ_DASHBOARD_PATH && docker compose down elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else stop_dashboard_standalone
cd $NZ_DASHBOARD_PATH && docker-compose down
fi fi
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
@ -659,34 +611,25 @@ stop_dashboard() {
fi fi
} }
stop_dashboard_standalone() { stop_dashboard_docker() {
echo -e "> Stop Panel" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND down
}
stop_dashboard_standalone() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
systemctl stop nezha-dashboard systemctl stop nezha-dashboard
else else
rc-service nezha-dashboard stop rc-service nezha-dashboard stop
fi fi
if [[ $? == 0 ]]; then
echo -e "${green}Nezha Monitoring Stop Successful${plain}"
else
echo -e "${red}Failed to stop, please check the log message later${plain}"
fi
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
show_dashboard_log() { show_dashboard_log() {
echo -e "> View Panel Log" echo -e "> View Panel Log"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then show_dashboard_log_docker
cd $NZ_DASHBOARD_PATH && docker compose logs -f elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else show_dashboard_log_standalone
cd $NZ_DASHBOARD_PATH && docker-compose logs -f
fi fi
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
@ -694,33 +637,27 @@ show_dashboard_log() {
fi fi
} }
show_dashboard_log_standalone() { show_dashboard_log_docker() {
echo -e "> View Panel Log" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND logs -f
}
show_dashboard_log_standalone() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
journalctl -xf -u nezha-dashboard.service journalctl -xf -u nezha-dashboard.service
else else
echo "Sorry, OpenRC do not have this feature yet." tail -n 10 /var/log/nezha-dashboard.err
fi
if [[ $# == 0 ]]; then
before_show_menu
fi fi
} }
uninstall_dashboard() { uninstall_dashboard() {
echo -e "> Uninstall Panel" echo -e "> Uninstall Panel"
docker compose version if [[ $IS_DOCKER_NEZHA == 1 ]]; then
if [[ $? == 0 ]]; then uninstall_dashboard_docker
cd $NZ_DASHBOARD_PATH && docker compose down elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
else uninstall_dashboard_standalone
cd $NZ_DASHBOARD_PATH && docker-compose down
fi fi
rm -rf $NZ_DASHBOARD_PATH
docker rmi -f ghcr.io/naiba/nezha-dashboard >/dev/null 2>&1
docker rmi -f registry.cn-shanghai.aliyuncs.com/naibahq/nezha-dashboard >/dev/null 2>&1
clean_all clean_all
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
@ -728,9 +665,14 @@ uninstall_dashboard() {
fi fi
} }
uninstall_dashboard_standalone() { uninstall_dashboard_docker() {
echo -e "> Uninstall Panel" cd $NZ_DASHBOARD_PATH && $DOCKER_COMPOSE_COMMAND down
rm -rf $NZ_DASHBOARD_PATH
docker rmi -f ghcr.io/naiba/nezha-dashboard >/dev/null 2>&1
docker rmi -f registry.cn-shanghai.aliyuncs.com/naibahq/nezha-dashboard >/dev/null 2>&1
}
uninstall_dashboard_standalone() {
rm -rf $NZ_DASHBOARD_PATH rm -rf $NZ_DASHBOARD_PATH
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
@ -744,12 +686,6 @@ uninstall_dashboard_standalone() {
else else
rm $NZ_DASHBOARD_SERVICERC rm $NZ_DASHBOARD_SERVICERC
fi fi
clean_all
if [[ $# == 0 ]]; then
before_show_menu
fi
} }
show_agent_log() { show_agent_log() {
@ -758,7 +694,7 @@ show_agent_log() {
if [ "$os_alpine" != 1 ]; then if [ "$os_alpine" != 1 ]; then
journalctl -xf -u nezha-agent.service journalctl -xf -u nezha-agent.service
else else
echo "Sorry, OpenRC do not have this feature yet." tail -n 10 /var/log/nezha-agent.err
fi fi
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
@ -808,59 +744,6 @@ clean_all() {
fi fi
} }
select_version() {
DOCKER_COMPOSE_COMMAND=""
if docker compose version >/dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker compose"
if $DOCKER_COMPOSE_COMMAND ls | grep -qw "$NZ_DASHBOARD_PATH/docker-compose.yaml" >/dev/null 2>&1; then
NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "Docker image with nezha-dashboard repository exists:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
return
else
echo "No Docker images with the nezha-dashboard repository were found."
fi
fi
elif command -v docker-compose >/dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker-compose"
if $DOCKER_COMPOSE_COMMAND -f "$NZ_DASHBOARD_PATH/docker-compose.yaml" config >/dev/null 2>&1; then
NEZHA_IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "nezha-dashboard")
if [ -n "$NEZHA_IMAGES" ]; then
echo "Docker image with nezha-dashboard repository exists:"
echo "$NEZHA_IMAGES"
IS_DOCKER_NEZHA=1
return
else
echo "No Docker images with the nezha-dashboard repository were found."
fi
fi
fi
if [[ -f $NZ_DASHBOARD_PATH/app ]]; then
IS_DOCKER_NEZHA=0
else
echo -e "${yellow}Select your installation method(Input anything is ok if you are installing agent):\n1. Docker\n2. Standalone${plain}"
while true; do
read -e -r -p "Input a number [1-2]: " option
case "${option}" in
1)
IS_DOCKER_NEZHA=1
break
;;
2)
IS_DOCKER_NEZHA=0
break
;;
*)
echo "Wrong input, try again"
;;
esac
done
fi
}
show_usage() { show_usage() {
echo "Nezha Monitor Management Script Usage: " echo "Nezha Monitor Management Script Usage: "
echo "--------------------------------------------------------" echo "--------------------------------------------------------"
@ -905,8 +788,7 @@ show_menu() {
${green}0.${plain} Exit Script ${green}0.${plain} Exit Script
" "
echo && read -ep "Please enter [0-13]: " num echo && read -ep "Please enter [0-13]: " num
if [[ $IS_DOCKER_NEZHA == 1 ]]; then case "${num}" in
case "${num}" in
0) 0)
exit 0 exit 0
;; ;;
@ -952,63 +834,14 @@ show_menu() {
*) *)
echo -e "${red}Please enter the correct number [0-13]${plain}" echo -e "${red}Please enter the correct number [0-13]${plain}"
;; ;;
esac esac
elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
case "${num}" in
0)
exit 0
;;
1)
install_dashboard_standalone
;;
2)
modify_dashboard_config_standalone
;;
3)
start_dashboard_standalone
;;
4)
stop_dashboard_standalone
;;
5)
restart_and_update_standalone
;;
6)
show_dashboard_log_standalone
;;
7)
uninstall_dashboard_standalone
;;
8)
install_agent
;;
9)
modify_agent_config
;;
10)
show_agent_log
;;
11)
uninstall_agent
;;
12)
restart_agent
;;
13)
update_script
;;
*)
echo -e "${red}Please enter the correct number [0-13]${plain}"
;;
esac
fi
} }
pre_check pre_check
installation_check
if [[ $# > 0 ]]; then if [[ $# > 0 ]]; then
if [[ $IS_DOCKER_NEZHA == 1 ]]; then case $1 in
case $1 in
"install_dashboard") "install_dashboard")
install_dashboard 0 install_dashboard 0
;; ;;
@ -1054,84 +887,8 @@ if [[ $# > 0 ]]; then
update_script 0 update_script 0
;; ;;
*) show_usage ;; *) show_usage ;;
esac esac
elif [[ $IS_DOCKER_NEZHA == 0 ]]; then
case $1 in
"install_dashboard")
install_dashboard_standalone 0
;;
"modify_dashboard_config")
modify_dashboard_config_standalone 0
;;
"start_dashboard")
start_dashboard_standalone 0
;;
"stop_dashboard")
stop_dashboard_standalone 0
;;
"restart_and_update")
restart_and_update_standalone 0
;;
"show_dashboard_log")
show_dashboard_log_standalone 0
;;
"uninstall_dashboard")
uninstall_dashboard_standalone 0
;;
"install_agent")
shift
if [ $# -ge 3 ]; then
install_agent "$@"
else
install_agent 0
fi
;;
"modify_agent_config")
modify_agent_config 0
;;
"show_agent_log")
show_agent_log 0
;;
"uninstall_agent")
uninstall_agent 0
;;
"restart_agent")
restart_agent 0
;;
"update_script")
update_script 0
;;
*) show_usage ;;
esac
else
case $1 in
"install_agent")
shift
if [ $# -ge 3 ]; then
install_agent "$@"
else
install_agent 0
fi
;;
"modify_agent_config")
modify_agent_config 0
;;
"show_agent_log")
show_agent_log 0
;;
"uninstall_agent")
uninstall_agent 0
;;
"restart_agent")
restart_agent 0
;;
"update_script")
update_script 0
;;
*) show_usage ;;
esac
fi
else else
select_version select_version
show_menu show_menu
fi fi

View File

@ -1,62 +1,19 @@
#!/sbin/openrc-run #!/sbin/openrc-run
supervisor=supervise-daemon supervisor=supervise-daemon
name=nezha-agent name=nezha-agent
supervise_daemon_args="--stdout /var/log/${name}.log --stderr /var/log/${name}.err" output_log=/var/log/${name}.log
error_log=/var/log/${name}.err
SERVER="nz_grpc_host:nz_grpc_port" #Dashboard 地址 ip:port SERVER="nz_grpc_host:nz_grpc_port" #Dashboard 地址 ip:port
SECRET="nz_client_secret" #SECRET SECRET="nz_client_secret" #SECRET
NZ_BASE_PATH="/opt/nezha"
NZ_AGENT_PATH="${NZ_BASE_PATH}/agent"
pidfile="/run/${RC_SVCNAME}.pid" pidfile="/run/${RC_SVCNAME}.pid"
command="/opt/nezha/agent/nezha-agent" command="/opt/nezha/agent/nezha-agent"
command_args="-s ${SERVER} -p ${SECRET}" command_args="-s ${SERVER} -p ${SECRET}"
command_background=true command_background=true
depend() { depend() {
need net need net
} }
checkconfig() {
GITHUB_URL="github.com"
if [ ! -f "${NZ_AGENT_PATH}/nezha-agent" ]; then
if [[ $(uname -m | grep 'x86_64') != "" ]]; then
os_arch="amd64"
elif [[ $(uname -m | grep 'i386\|i686') != "" ]]; then
os_arch="386"
elif [[ $(uname -m | grep 'aarch64\|armv8b\|armv8l') != "" ]]; then
os_arch="arm64"
elif [[ $(uname -m | grep 'arm') != "" ]]; then
os_arch="arm"
elif [[ $(uname -m | grep 's390x') != "" ]]; then
os_arch="s390x"
elif [[ $(uname -m | grep 'riscv64') != "" ]]; then
os_arch="riscv64"
fi
local version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/agent/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://fastly.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gcore.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
echo -e "获取版本号失败,请检查本机能否链接 https://api.github.com/repos/nezhahq/agent/releases/latest"
return 0
else
echo -e "当前最新版本为: ${version}"
fi
wget -t 2 -T 10 -O nezha-agent_linux_${os_arch}.zip https://${GITHUB_URL}/nezhahq/agent/releases/download/${version}/nezha-agent_linux_${os_arch}.zip >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "Release 下载失败,请检查本机能否连接 ${GITHUB_URL}${plain}"
return 0
fi
mkdir -p $NZ_AGENT_PATH
chmod 755 -R $NZ_AGENT_PATH
unzip -qo nezha-agent_linux_${os_arch}.zip && mv nezha-agent $NZ_AGENT_PATH && rm -rf nezha-agent_linux_${os_arch}.zip README.md
fi
if [ ! -x "${NZ_AGENT_PATH}/nezha-agent" ]; then
chmod +x ${NZ_AGENT_PATH}/nezha-agent
fi
}
start_pre() { start_pre() {
if [ "${RC_CMD}" != "restart" ]; then checkpath -f -m 0644 -o root:root "/var/log/${name}.log"
checkconfig || return $?
fi
} }

12
script/nezha-dashboard Normal file → Executable file
View File

@ -1,4 +1,8 @@
#!/sbin/openrc-run #!/sbin/openrc-run
supervisor=supervise-daemon
name=nezha-dashboard
output_log=/var/log/${name}.log
error_log=/var/log/${name}.err
pidfile="/run/${RC_SVCNAME}.pid" pidfile="/run/${RC_SVCNAME}.pid"
command="/opt/nezha/dashboard/app" command="/opt/nezha/dashboard/app"
command_args="" command_args=""
@ -6,11 +10,9 @@ command_background=true
directory="/opt/nezha/dashboard" directory="/opt/nezha/dashboard"
depend() { depend() {
need net need net
} }
reload() { start_pre() {
ebegin "Reloading ${RC_SVCNAME}" checkpath -f -m 0644 -o root:root "/var/log/${name}.log"
start-stop-daemon --signal HUP --pidfile "${pidfile}"
eend $?
} }