#!/bin/bash
# Filename:  vmagent_install.sh
# Revision:   v1.1
# Date:        2024/9/4
# By:           Psn.liu
# Update:    2024/11/12
# Up_by:      Psn.liu
# Official Site:  https://www.psnsys.com
# Description:  二进制安装node_exporter并通过systemd管理服务
#安装调用方式：curl -o - 'https://download.xlsys.cn/tools/prometheus/node/node_exporter-1.8.2.linux-amd64.tar.gz' | bash -s install
#卸载调用方式：curl -o - 'https://download.xlsys.cn/tools/prometheus/node/node_exporter-1.8.2.linux-amd64.tar.gz' | bash -s uninstall

# 文件路径及变量
exporter_name="https://download.xlsys.cn/tools/Shell/node_exporter.sh"
exporter_url="https://download.xlsys.cn/tools/prometheus/node/node_exporter-1.8.2.linux-amd64.tar.gz"
exporter_tar="node_exporter-1.8.2.linux-amd64.tar.gz"
exporter_dir="node_exporter-1.8.2.linux-amd64"
install_dir="/opt/node_exporter"
service_file="/usr/lib/systemd/system/node.service"

# 函数：检查 node_exporter 进程是否存在
check_node_exporter_process() {
    if pgrep -x "node_exporter" > /dev/null; then
        echo -e "\e[31mnode_exporter 进程已存在，无需重复安装。\e[0m"
        exit 1
    fi
}

# 函数：安装 node_exporter
install_node_exporter() {
    check_node_exporter_process

    # 下载 node_exporter
    echo -e "\e[34m==================================================================\e[0m"
    echo -e "\e[34m下载 node_exporter 可执行文件...\e[0m"
    curl -O "$exporter_url" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 可执行文件下载失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34mnode_exporter 可执行文件下载成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 解压 node_exporter
    echo -e "\e[34m解压 node_exporter 可执行文件...\e[0m"
    tar -xzf "$exporter_tar" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 可执行文件解压失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34mnode_exporter 可执行文件解压成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 移动 node_exporter 到安装目录
    echo -e "\e[34m移动 node_exporter 到安装目录 ${install_dir}...\e[0m"
    mv "$exporter_dir" "$install_dir" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 移动到安装目录失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34mnode_exporter 移动到安装目录成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 更改文件权限和所有者
    echo -e "\e[34m更改文件权限和所有者...\e[0m"
    chown root:root -R "$install_dir" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31m文件权限和所有者更改失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34m文件权限和所有者更改成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 配置 systemd 管理
    echo -e "\e[34m配置 systemd 管理服务...\e[0m"
    cat << EOF > "$service_file"
[Unit]
Description=node-server
After=network.target

[Service]
Type=simple
ExecStart=/opt/node_exporter/node_exporter
ExecStop=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

    if [ $? -ne 0 ]; then
        echo -e "\e[31msystemd 配置文件创建失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34msystemd 配置文件创建成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 重新加载 systemd 配置
    echo -e "\e[34m重新加载 systemd 配置...\e[0m"
    systemctl daemon-reload &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31msystemd 配置重新加载失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34msystemd 配置重新加载成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 启动并启用 node_exporter 服务
    echo -e "\e[34m启动并启用 node_exporter 服务...\e[0m"
    systemctl start node &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 服务启动失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34mnode_exporter 服务启动成功。\e[0m"

    systemctl enable node &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 服务开机自启设置失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34mnode_exporter 服务开机自启设置成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 检查服务状态
    echo -e "\e[34m检查 node_exporter 服务状态...\e[0m"
    systemctl status node &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 服务状态异常。\e[0m"
        exit 1
    fi
    echo -e "\e[34mnode_exporter 服务状态正常。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 测试访问节点导出器
    echo -e "\e[34m测试访问节点导出器...\e[0m"
    curl http://127.0.0.1:9100 &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31m节点导出器访问失败。\e[0m"
        exit 1
    fi
    echo -e "\e[34m节点导出器访问成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"

    # 输出安装完成信息
    echo -e "\e[33m-------------------------服务已安装完毕--------------------------\e[0m"
    echo -e "\e[33mnode_exporter 服务已安装并启动成功。\e[0m"
    echo ""

    echo -e "\e[34m==================================================================\e[0m"
}

# 函数：卸载 node_exporter
uninstall_node_exporter() {
    echo -e "\e[34m==================================================================\e[0m"
    echo -e "\e[34m开始卸载 node_exporter 服务...\e[0m"

    # 停止并禁用服务
    echo -e "\e[34m停止并禁用 node_exporter 服务...\e[0m"
    systemctl stop node &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 服务停止失败。\e[0m"
    else
        echo -e "\e[34mnode_exporter 服务停止成功。\e[0m"
    fi

    systemctl disable node &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mnode_exporter 服务开机自启取消失败。\e[0m"
    else
        echo -e "\e[34mnode_exporter 服务开机自启取消成功。\e[0m"
    fi

    # 删除 systemd 配置文件
    echo -e "\e[34m删除 systemd 配置文件...\e[0m"
    rm -f "$service_file" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31msystemd 配置文件删除失败。\e[0m"
    else
        echo -e "\e[34msystemd 配置文件删除成功。\e[0m"
    fi

    # 删除安装目录
    echo -e "\e[34m删除安装目录 ${install_dir}...\e[0m"
    rm -rf "$install_dir" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31m安装目录删除失败。\e[0m"
    else
        echo -e "\e[34m安装目录删除成功。\e[0m"
    fi

    # 删除下载的 tar 文件
    echo -e "\e[34m删除下载的 tar 文件...\e[0m"
    rm -f "$exporter_tar" &> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "\e[31mtar 文件删除失败。\e[0m"
    else
        echo -e "\e[34mtar 文件删除成功。\e[0m"
    fi

    echo -e "\e[34m==================================================================\e[0m"
    echo -e "\e[33m-------------------------服务已卸载完毕--------------------------\e[0m"
    echo -e "\e[33mnode_exporter 服务已卸载成功。\e[0m"
    echo -e "\e[34m==================================================================\e[0m"
}

# 主程序入口
if [ -z "$1" ]; then
    echo -e "\e[31m无效的选项，请输入 'install' 或 'uninstall'。\e[0m"
    echo -e "\e[34m如果需要远程调用，请执行以下命令：\e[0m"
    echo -e "\e[34m  - 安装服务：curl -o - '${exporter_name}' | bash -s install \e[0m"
    echo -e "\e[34m  - 卸载服务：curl -o - '${exporter_name}' | bash -s uninstall \e[0m"

else
    case $1 in
        install)
            install_node_exporter
            ;;
        uninstall)
            uninstall_node_exporter
            ;;
        *)
            echo -e "\e[31m无效的选项，请输入 'install' 或 'uninstall'。\e[0m"
            ;;
    esac
fi
