From c67776fe457d280da006872b7e7ea778e21d0634 Mon Sep 17 00:00:00 2001 From: fscarmen <62703343+fscarmen@users.noreply.github.com> Date: Sun, 13 Nov 2022 03:53:01 +0000 Subject: [PATCH] Agent supports Alpine OS. --- script/install.sh | 80 ++++++++++++++++++++++++++------------------ script/install_en.sh | 78 +++++++++++++++++++++++++----------------- 2 files changed, 95 insertions(+), 63 deletions(-) diff --git a/script/install.sh b/script/install.sh index f8c1b62..41be950 100755 --- a/script/install.sh +++ b/script/install.sh @@ -1,7 +1,7 @@ #!/bin/bash #======================================================== -# System Required: CentOS 7+ / Debian 8+ / Ubuntu 16+ / +# System Required: CentOS 7+ / Debian 8+ / Ubuntu 16+ / Alpine 3+ / # Arch 未测试 # Description: 哪吒监控安装脚本 # Github: https://github.com/naiba/nezha @@ -20,13 +20,10 @@ plain='\033[0m' export PATH=$PATH:/usr/local/bin os_arch="" +[ -e /etc/os-release ] && cat /etc/os-release | grep -i "PRETTY_NAME" | grep -qi "alpine" && os_alpine='1' pre_check() { - command -v systemctl >/dev/null 2>&1 - if [[ $? != 0 ]]; then - echo "不支持此系统:未找到 systemctl 命令" - exit 1 - fi + [ "$os_alpine" != 1 ] && ! command -v systemctl >/dev/null 2>&1 && echo "不支持此系统:未找到 systemctl 命令" && exit 1 # check root [[ $EUID -ne 0 ]] && echo -e "${red}错误: ${plain} 必须使用root用户运行此脚本!\n" && exit 1 @@ -133,7 +130,8 @@ install_soft() { (command -v yum >/dev/null 2>&1 && yum makecache && yum install $* selinux-policy -y) || (command -v apt >/dev/null 2>&1 && apt update && apt install $* selinux-utils -y) || (command -v pacman >/dev/null 2>&1 && pacman -Syu $*) || - (command -v apt-get >/dev/null 2>&1 && apt-get update && apt-get install $* selinux-utils -y) + (command -v apt-get >/dev/null 2>&1 && apt-get update && apt-get install $* selinux-utils -y) || + (command -v apk >/dev/null 2>&1 && apk update && apk add $* -f) } install_dashboard() { @@ -186,12 +184,14 @@ install_dashboard() { selinux(){ #判断当前的状态 - getenforce | grep '[Ee]nfor' - if [ $? -eq 0 ];then - echo -e "SELinux是开启状态,正在关闭!" - setenforce 0 &>/dev/null - find_key="SELINUX=" - sed -ri "/^$find_key/c${find_key}disabled" /etc/selinux/config + if [ "$os_alpine" != 1 ];then + getenforce | grep '[Ee]nfor' + if [ $? -eq 0 ];then + echo -e "SELinux是开启状态,正在关闭!" + setenforce 0 &>/dev/null + find_key="SELINUX=" + sed -ri "/^$find_key/c${find_key}disabled" /etc/selinux/config + fi fi } @@ -247,10 +247,12 @@ install_agent() { modify_agent_config() { echo -e "> 修改Agent配置" - wget -t 2 -T 10 -O $NZ_AGENT_SERVICE https://${GITHUB_RAW_URL}/script/nezha-agent.service >/dev/null 2>&1 - if [[ $? != 0 ]]; then - echo -e "${red}文件下载失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}" - return 0 + if [ "$os_alpine" != 1 ];then + wget -t 2 -T 10 -O $NZ_AGENT_SERVICE https://${GITHUB_RAW_URL}/script/nezha-agent.service >/dev/null 2>&1 + if [[ $? != 0 ]]; then + echo -e "${red}文件下载失败,请检查本机能否连接 ${GITHUB_RAW_URL}${plain}" + return 0 + fi fi if [ $# -lt 3 ]; then @@ -272,21 +274,30 @@ modify_agent_config() { nz_client_secret=$3 fi - sed -i "s/nz_grpc_host/${nz_grpc_host}/" ${NZ_AGENT_SERVICE} - sed -i "s/nz_grpc_port/${nz_grpc_port}/" ${NZ_AGENT_SERVICE} - sed -i "s/nz_client_secret/${nz_client_secret}/" ${NZ_AGENT_SERVICE} + if [ "$os_alpine" != 1 ];then + sed -i "s/nz_grpc_host/${nz_grpc_host}/" ${NZ_AGENT_SERVICE} + sed -i "s/nz_grpc_port/${nz_grpc_port}/" ${NZ_AGENT_SERVICE} + sed -i "s/nz_client_secret/${nz_client_secret}/" ${NZ_AGENT_SERVICE} - shift 3 - if [ $# -gt 0 ]; then - args=" $*" - sed -i "/ExecStart/ s/$/${args}/" ${NZ_AGENT_SERVICE} + shift 3 + if [ $# -gt 0 ]; then + args=" $*" + sed -i "/ExecStart/ s/$/${args}/" ${NZ_AGENT_SERVICE} + fi + else + echo "@reboot nohup ${NZ_AGENT_PATH}/nezha-agent -s ${nz_grpc_host}:${nz_grpc_port} -p ${nz_client_secret} >/dev/null 2>&1 &" >> /etc/crontabs/root + crond fi echo -e "Agent配置 ${green}修改成功,请稍等重启生效${plain}" - systemctl daemon-reload - systemctl enable nezha-agent - systemctl restart nezha-agent + if [ "$os_alpine" != 1 ];then + systemctl daemon-reload + systemctl enable nezha-agent + systemctl restart nezha-agent + else + nohup ${NZ_AGENT_PATH}/nezha-agent -s ${nz_grpc_host}:${nz_grpc_port} -p ${nz_client_secret} >/dev/null 2>&1 & + fi if [[ $# == 0 ]]; then before_show_menu @@ -477,10 +488,15 @@ show_agent_log() { uninstall_agent() { echo -e "> 卸载Agent" - systemctl disable nezha-agent.service - systemctl stop nezha-agent.service - rm -rf $NZ_AGENT_SERVICE - systemctl daemon-reload + if [ "$os_alpine" != 1 ];then + systemctl disable nezha-agent.service + systemctl stop nezha-agent.service + rm -rf $NZ_AGENT_SERVICE + systemctl daemon-reload + else + sed -i "/nezha-agent/d" /etc/crontabs/root + pkill nezha + fi rm -rf $NZ_AGENT_PATH clean_all @@ -652,4 +668,4 @@ if [[ $# > 0 ]]; then esac else show_menu -fi +fi \ No newline at end of file diff --git a/script/install_en.sh b/script/install_en.sh index b730636..dcf79af 100755 --- a/script/install_en.sh +++ b/script/install_en.sh @@ -1,7 +1,7 @@ #!/bin/bash #======================================================== -# System Required: CentOS 7+ / Debian 8+ / Ubuntu 16+ / +# System Required: CentOS 7+ / Debian 8+ / Ubuntu 16+ / Alpine 3+ / # Arch not tested # Description: Nezha Monitoring Install Script # Github: https://github.com/naiba/nezha @@ -20,13 +20,10 @@ plain='\033[0m' export PATH=$PATH:/usr/local/bin os_arch="" +[ -e /etc/os-release ] && cat /etc/os-release | grep -i "PRETTY_NAME" | grep -qi "alpine" && os_alpine='1' pre_check() { - command -v systemctl >/dev/null 2>&1 - if [[ $? != 0 ]]; then - echo "This system is not supported: systemctl not found" - exit 1 - fi + [ "$os_alpine" != 1 ] && ! command -v systemctl >/dev/null 2>&1 && echo "This system is not supported: systemctl not found" && exit 1 # check root [[ $EUID -ne 0 ]] && echo -e "${red}ERROR: ${plain} This script must be run with the root user!\n" && exit 1 @@ -103,7 +100,8 @@ install_soft() { (command -v yum >/dev/null 2>&1 && yum makecache && yum install $* selinux-policy -y) || (command -v apt >/dev/null 2>&1 && apt update && apt install $* selinux-utils -y) || (command -v pacman >/dev/null 2>&1 && pacman -Syu $*) || - (command -v apt-get >/dev/null 2>&1 && apt-get update && apt-get install $* selinux-utils -y) + (command -v apt-get >/dev/null 2>&1 && apt-get update && apt-get install $* selinux-utils -y) || + (command -v apk >/dev/null 2>&1 && apk update && apk add $* -f) } install_dashboard() { @@ -155,12 +153,14 @@ install_dashboard() { selinux(){ #Check SELinux - getenforce | grep '[Ee]nfor' - if [ $? -eq 0 ];then - echo -e "SELinux running,closing now!" - setenforce 0 &>/dev/null - find_key="SELINUX=" - sed -ri "/^$find_key/c${find_key}disabled" /etc/selinux/config + if [ "$os_alpine" != 1 ];then + getenforce | grep '[Ee]nfor' + if [ $? -eq 0 ];then + echo -e "SELinux running,closing now!" + setenforce 0 &>/dev/null + find_key="SELINUX=" + sed -ri "/^$find_key/c${find_key}disabled" /etc/selinux/config + fi fi } @@ -216,10 +216,12 @@ install_agent() { modify_agent_config() { echo -e "> Modify Agent Configuration" - wget -t 2 -T 10 -O $NZ_AGENT_SERVICE https://${GITHUB_RAW_URL}/script/nezha-agent.service >/dev/null 2>&1 - if [[ $? != 0 ]]; then - echo -e "${red}Fail to download service, please check if the network can link ${GITHUB_RAW_URL}${plain}" - return 0 + if [ "$os_alpine" != 1 ];then + wget -t 2 -T 10 -O $NZ_AGENT_SERVICE https://${GITHUB_RAW_URL}/script/nezha-agent.service >/dev/null 2>&1 + if [[ $? != 0 ]]; then + echo -e "${red}Fail to download service, please check if the network can link ${GITHUB_RAW_URL}${plain}" + return 0 + fi fi if [ $# -lt 3 ]; then @@ -241,21 +243,30 @@ modify_agent_config() { nz_client_secret=$3 fi - sed -i "s/nz_grpc_host/${nz_grpc_host}/" ${NZ_AGENT_SERVICE} - sed -i "s/nz_grpc_port/${nz_grpc_port}/" ${NZ_AGENT_SERVICE} - sed -i "s/nz_client_secret/${nz_client_secret}/" ${NZ_AGENT_SERVICE} + if [ "$os_alpine" != 1 ];then + sed -i "s/nz_grpc_host/${nz_grpc_host}/" ${NZ_AGENT_SERVICE} + sed -i "s/nz_grpc_port/${nz_grpc_port}/" ${NZ_AGENT_SERVICE} + sed -i "s/nz_client_secret/${nz_client_secret}/" ${NZ_AGENT_SERVICE} - shift 3 - if [ $# -gt 0 ]; then - args=" $*" - sed -i "/ExecStart/ s/$/${args}/" ${NZ_AGENT_SERVICE} + shift 3 + if [ $# -gt 0 ]; then + args=" $*" + sed -i "/ExecStart/ s/$/${args}/" ${NZ_AGENT_SERVICE} + fi + else + echo "@reboot nohup ${NZ_AGENT_PATH}/nezha-agent -s ${nz_grpc_host}:${nz_grpc_port} -p ${nz_client_secret} >/dev/null 2>&1 &" >> /etc/crontabs/root + crond fi echo -e "Agent configuration ${green} modified successfully, please wait for agent self-restart to take effect${plain}" - systemctl daemon-reload - systemctl enable nezha-agent - systemctl restart nezha-agent + if [ "$os_alpine" != 1 ];then + systemctl daemon-reload + systemctl enable nezha-agent + systemctl restart nezha-agent + else + nohup ${NZ_AGENT_PATH}/nezha-agent -s ${nz_grpc_host}:${nz_grpc_port} -p ${nz_client_secret} >/dev/null 2>&1 & + fi if [[ $# == 0 ]]; then before_show_menu @@ -445,10 +456,15 @@ show_agent_log() { uninstall_agent() { echo -e "> Uninstall Agent" - systemctl disable nezha-agent.service - systemctl stop nezha-agent.service - rm -rf $NZ_AGENT_SERVICE - systemctl daemon-reload + if [ "$os_alpine" != 1 ];then + systemctl disable nezha-agent.service + systemctl stop nezha-agent.service + rm -rf $NZ_AGENT_SERVICE + systemctl daemon-reload + else + sed -i "/nezha-agent/d" /etc/crontabs/root + pkill nezha + fi rm -rf $NZ_AGENT_PATH clean_all