平时写的各种linux shell脚本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.2 KiB

3 years ago
#!/bin/bash
##### name:一键安装node-exporter节点 #####
##### authro:xiaoz<xiaoz93@outlook.com> #####
##### update:2021/12/09 #####
#获取版本号
VERSION=$1
#instance名称
INSTANCE=$2
#用户名、密码
USERNAME_PASSWORD=$3
#安装目录
INSTALL_PATH="/opt/node_exporter"
#安装前准备
depend(){
3 years ago
echo "Pre-installation preparation is in progress..."
3 years ago
if [ -e "/usr/bin/yum" ]
then
3 years ago
yum -y install wget curl
3 years ago
else
#更新软件,否则可能make命令无法安装
apt-get -y update
3 years ago
apt-get install -y wget curl
3 years ago
fi
#创建目录
#mkdir -p ${INSTALL_PATH}
}
#下载
download(){
3 years ago
echo "Ready to download the installation package..."
3 years ago
wget -P /opt http://soft.xiaoz.org/linux/node_exporter-${VERSION}.linux-amd64.tar.gz
cd /opt && tar -xvf node_exporter-${VERSION}.linux-amd64.tar.gz
mv node_exporter-${VERSION}.linux-amd64 node_exporter
}
#一些额外的配置
setting(){
3 years ago
#设置密码访问
3 years ago
cat >> $INSTALL_PATH/config.yaml << EOF
basic_auth_users:
$USERNAME_PASSWORD
EOF
}
#放行端口
3 years ago
release_port(){
3 years ago
echo "Detecting firewall type..."
#检测防火墙类型
which firewall-cmd
if [ $? -eq 0 ]
then
3 years ago
firewall_status=$(firewall-cmd --state)
if[[ "firewall_status" == "running" ]]
then
firewall-cmd --zone=public --add-port=29100/tcp --permanent
firewall-cmd --reload
fi
3 years ago
fi
which ufw
if [ $? -eq 0 ]
then
ufw allow 29100/tcp
fi
3 years ago
}
#注册服务并启动
reg_systemd(){
echo "Registering service..."
echo "[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=${INSTALL_PATH}/node_exporter --web.listen-address=":29100" --web.config=${INSTALL_PATH}/config.yaml
[Install]
WantedBy=default.target" > /etc/systemd/system/node_exporter.service
#重载服务
systemctl daemon-reload
#启动服务
systemctl start node_exporter.service
3 years ago
#设置开机自启
systemctl enable node_exporter.service
3 years ago
}
3 years ago
#安装完成
install_success(){
3 years ago
myip=$(curl -s ip.sb)
3 years ago
echo "Installation is complete, please visit http://${myip}:29100"
}
3 years ago
#清理工作
clean_work() {
3 years ago
echo "Cleaning installation packages..."
3 years ago
rm -rf /opt/node_exporter-${VERSION}.linux-amd64.tar.gz
}
3 years ago
depend && download && setting && release_port && reg_systemd && clean_work && install_success