脚本一键部署prometheus node grafana
版本:prometheus-2.35.0.linux-amd64 node_exporter-1.3.1.linux-amd64 grafana-enterprise-8.5.2-1.x86_64
脚本描述:
脚本名:prometheus-node_exporter-grafana.sh
准备所需要的机器多台
在脚本相同目录下创建ip.txt文件:如图(除Prometheus节点外的ip地址)
193.168.0.118 123456
192.168.0.119 123456
ip空格加密码,需要部署对少台就输入多少台的ip和密码
在脚本相同目录下创建pwd.txt:如图(每个需用空格隔开)
创建脚本文件:
#!/bin/bash
echo "关闭防火墙,关闭SELinux,开启ens33"
systemctl stop firewalld && setenforce 0 && ifup ens33
echo " = = = = = = = = = = = = = = = = 免密交互 = = = = = = = = = = = = = = = = "
echo " = = = = = = = = = = = = = = = = expect正在安装 = = = = = = = = = = = = = = = = "
rm -rf /var/run/yum.pid && yum -y install expect
echo " = = = = = = = = = = = = = = = = expect已安装 = = = = = = = = = = = = = = = = "
if [ ! -f /root/.ssh/id_rsa ];then
ssh-keygen -t rsa -N '' -f /root/.ssh/id_rsa -q
echo "id_rsa已创建成功"
else
echo "id_rsa已存在"
fi
while read -r line
do
ip=`echo $line | cut -d " " -f 1`
passwd=`echo $line | cut -d " " -f 2`
expect << aaa
set timeout 20
spawn ssh-copy-id $ip
expect {
#exp_continue当没有出现(yes/no)?时跳转直接执行下一步
"(yes/no)?" {send "yes\r";exp_continue}
"password:" {send "$passwd\r"};
}
expect eof
aaa
if [ $? -eq 0 ];then
echo "$ip互信成功"
else
echo "$ip互信失败"
fi
done < ip.txt
Installationpath=`awk '{print $1}' pwd.txt`
Packagepath=`awk '{print $2}' pwd.txt`
prometheusname=`awk '{print $3}' pwd.txt`
nodename=`awk '{print $4}' pwd.txt`
grafananame=`awk '{print $5}' pwd.txt`
echoprometheus=`echo $Installationpath/prometheus*`
echonode=`echo $Installationpath/node*`
echogarfana=`echo $Installationpath/grafana*`
mkdir $Installationpath
tar zxvf $Packagepath/$nodename -C $Installationpath
mv $echonode $Installationpath/node_exporter
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
chown -R prometheus:prometheus $Installationpath/node_exporter
cat > /etc/systemd/system/node_exporter.service << aaa
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=$Installationpath/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
aaa
systemctl daemon-reload
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = 重载配置成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = 重载配置失败 = = = = = = = = = = = = = "
fi
systemctl enable node_exporter
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = node_exporter开机自启成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = node_exporter开机自启失败 = = = = = = = = = = = = = "
fi
systemctl start node_exporter
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = node_exporter启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = node_exporter启动失败 = = = = = = = = = = = = = "
fi
while read -r line
do
nip2=`echo $line | cut -d " " -f 1`
scp -rp $Packagepath/$nodename root@$nip2:/root/
ssh $nip2 << nnn
mkdir /app
tar zxvf /root/$nodename -C $Installationpath
mv $echonode $Installationpath/node_exporter
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
chown -R prometheus:prometheus $Installationpath/node_exporter/
cat > /etc/systemd/system/node_exporter.service << aaa
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=$Installationpath/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
aaa
systemctl daemon-reload
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = 重载配置成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = 重载配置失败 = = = = = = = = = = = = = "
fi
systemctl enable node_exporter
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = $nip2 node_exporter开机自启成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = $nip2 node_exporter开机自启失败 = = = = = = = = = = = = = "
fi
systemctl start node_exporter
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = $nip2 node_exporter启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = $nip2 node_exporter启动失败 = = = = = = = = = = = = = "
fi
nnn
done < ip.txt
yum -y localinstall $Packagepath/$grafananame
systemctl enable grafana-server.service
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = grafana开机自启成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = grafana开机自启失败 = = = = = = = = = = = = = "
fi
systemctl start grafana-server.service
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = grafana启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = grafana启动失败 = = = = = = = = = = = = = "
fi
tar zxvf $Packagepath/$prometheusname -C $Installationpath
mv $echoprometheus $Installationpath/prometheus
chown -R prometheus:prometheus $Installationpath/prometheus
mkdir -p /var/lib/prometheus
chown -R prometheus:prometheus /var/lib/prometheus
node=`awk '{print $1}' ip.txt |xargs |sed "s/ /:9100','/g"`
nodeip=`echo \'${node}:9100\'`
ipaddr=`ifconfig | awk 'NR==2{print $2}'`
cat >> $Installationpath/prometheus/prometheus.yml << aaa
- job_name: 'node_exporter'
static_configs:
- targets: ['$ipaddr:9100',$nodeip]
aaa
cat > /etc/systemd/system/prometheus.service << bbb
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
# Type设置为notify时,服务会不断重启
Type=simple
User=prometheus
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=$Installationpath/prometheus/prometheus --config.file=$Installationpath/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
bbb
systemctl daemon-reload
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = 重载配置成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = 重载配置失败 = = = = = = = = = = = = = "
fi
systemctl enable prometheus.service
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = prometheus开机自启成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = prometheus开机自启失败 = = = = = = = = = = = = = "
fi
systemctl start prometheus.service
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = prometheus启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = prometheus启动失败 = = = = = = = = = = = = = "
fi
执行脚本即可
到此完成