一、编写安装脚本
[root@jindada ~]$ vim Ins-Node-Exporter.sh
##########################################################################
# Author: jin.hui
# QQ: 974089352
# File Name: Ins-Node-Exporter.sh
# Copyright (C): 2021 All rights reserved
# Created Time: 2021年07月14日 星期三 10时30分09秒
#########################################################################
#!/usr/bin/bash
. /etc/init.d/functions
software_storage_path=/data/software
node_exporter_url=https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
node_exporter_tar=node_exporter-1.1.2.linux-amd64.tar.gz
node_exporter_ins_dir=/opt
node_exporter_version=node_exporter-1.1.2.linux-amd64
node_exporter_link=node_exporter
# 判断软件目录是否存在
function software_storage_dir(){
if [ -d $software_storage_path ];then
echo "Software storage path is exist;"
else
echo "Software storage path is not exist,creating $software_storage_path now"
mkdir $software_storage_path
fi
}
# 判断是否下载node_exporter
function download_node_exporter(){
if [ -f "$software_storage_path/$node_exporter_tar" ];then
echo "File $software_storage_path/$node_exporter_tar is exist,deleting this file"
rm -rf $software_storage_path/$node_exporter_tar
cd $software_storage_path
wget $node_exporter_url &>/dev/null
else
echo "File $software_storage_path/$node_exporter_tar is not exist,starting download file"
cd $software_storage_path
wget $node_exporter_url &>/dev/null
fi
}
# 判断是否存在旧的node_exporter应用程序
function ins_node_exporter(){
ls $node_exporter_ins_dir |grep $node_exporter_version &>/dev/null
if [ $? -eq 0 ];then
echo "Directory $node_exporter_ins_dir/$node_exporter_version is exist,deleting this directory..."
rm -rf $node_exporter_ins_dir/$node_exporter_version
tar xf $software_storage_path/$node_exporter_tar -C $node_exporter_ins_dir
else
tar xf $software_storage_path/$node_exporter_tar -C $node_exporter_ins_dir
fi
}
# 判断是否存在旧的node_exporter软连接
function node_exporter_link(){
if [ -L "$node_exporter_ins_dir"/"$node_exporter_link" ];then
echo "Link $node_exporter_ins_dir/$node_exporter_link is exist"
else
echo "Link $node_exporter_ins_dir/$node_exporter_link is not exist,creating link $node_exporter_link"
ln -s $node_exporter_ins_dir/$node_exporter_version $node_exporter_ins_dir/$node_exporter_link
fi
}
# 判断是否存在node_exporter的system启动
function system_node_exporter(){
ls /etc/systemd/system/ |grep node_exporter.service &>/dev/null
if [ $? -eq 0 ];then
echo "File node_exporter.service is exist"
else
echo "File node_exporter.service doesn't' exist,creating file node_exporter.service..."
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/opt/node_exporter/node_exporter
[Install]
WantedBy=default.target
EOF
fi
systemctl daemon-reload
systemctl enable --now node_exporter
}
# 判断node_exporter服务是否启动
function node_exporter_start(){
ps -ef |grep node_e[xporter] &>/dev/null
if [ $? -eq 0 ];then
action "node_exporter is started..." /bin/true
else
systemctl restart node_exporter
action "node_exporter is starting..." /bin/true
fi
}
# 调用函数
software_storage_dir
download_node_exporter
ins_node_exporter
node_exporter_link
system_node_exporter
node_exporter_start
二、添加执行权限
[root@jindada ~]$ chmod +x Ins-Node-Exporter.sh
三、执行脚本
[root@jindada ~]$ sh Ins-Node-Exporter.sh