#!/bin/bash { # this ensures the entire script is downloaded # #auto replace parameter start WEB_TOKEN=${webToken} DATACENTER_ID=${datacenterID} TYPE=${type} REMOTE_IP=${remoteIP} REMOTE_PORT=${remotePort} SSL_ENABLED=${sslEnabled} if [ ! -z "$REMOTE_PORT" ];then REMOTE_IP=$REMOTE_IP:$REMOTE_PORT fi LOCALHOST=${localhost} #auto replace parameter end ROOT_PATH=/opt/nezha AGENT_TOKEN=$(cut -d '-' -f 1 /proc/sys/kernel/random/uuid) INSTALL_PATH=$ROOT_PATH/nz-agent WORK_PATH=/tmp/.nezha/nz-agent rm -rf $WORK_PATH&&mkdir -p $WORK_PATH function osInfo() { if [ -e /etc/centos-release ];then echo 'centos' elif [ -e /etc/debian_version ]; then echo 'debian' elif [ -e /etc/lsb_release ]; then echo 'ubuntu' else echo 'centos'; fi } CURRENT_OS=$(osInfo) function log() { case $1 in "red") echo -e "\e[1;31m$2\e[0m" ;; "yellow") echo -e "\e[1;33m$2\e[0m" ;; "green") echo -e "\e[1;32m$2\e[0m" ;; *) echo $2 ;; esac } function isInstall() { case $CURRENT_OS in 'centos') if [ $(rpm -aq |grep nz-agent|wc -l) -lt 1 ];then echo 0 else echo 1; fi ;; esac } function getPackageName() { case $CURRENT_OS in 'centos') echo "nz-agent.rpm" ;; esac } PACKAGE_NAME=$(getPackageName $CURRENT_OS) function downloadPackaget() { cd $WORK_PATH; log yellow "current os is $CURRENT_OS ,download package from http://$REMOTE_IP/agent/download?os=$CURRENT_OS" if command -v wget >/dev/null 2>&1;then if $SSL_ENABLED ;then wget -O $PACKAGE_NAME --no-check-certificate --header="Authorization:"$WEB_TOKEN "https://$REMOTE_IP/agent/download?os="$CURRENT_OS else wget -O $PACKAGE_NAME --header="Authorization:"$WEB_TOKEN "http://$REMOTE_IP/agent/download?os="$CURRENT_OS fi return 0 elif command -v curl >/dev/null 2>&1;then if $SSL_ENABLED ;then curl -o $PACKAGE_NAME -k -H "Authorization:"$WEB_TOKEN "https://$REMOTE_IP/agent/download?os="$CURRENT_OS else curl -o $PACKAGE_NAME -H "Authorization:"$WEB_TOKEN "http://$REMOTE_IP/agent/download?os="$CURRENT_OS fi return 0 else log red "The wget/curl command is required" exit 1 fi } function installPackage() { case $CURRENT_OS in 'centos') log yellow 'install package right now...' rpm -ivh $WORK_PATH/$PACKAGE_NAME && mkdir -p $INSTALL_PATH/config; echo -n $AGENT_TOKEN > $INSTALL_PATH/config/token.auth ;; esac AGENT_NAME=$(hostname) if [ $AGENT_NAME == 'localhost.localdomain' ];then AGENT_NAME=$(cut -d '-' -f 1 /proc/sys/kernel/random/uuid) fi if [ 0 -eq $? ];then cat > $WORK_PATH/param.tmp <