shell初始化安装脚本执行时,需从网络上安装一些rpm包,所有需要先检测网络的畅通性,
代码
#检测网络链接&&ftp上传数据 function networkAndFtp() { #超时时间 timeout=5 #目标网站 target=www.baidu.com #获取响应状态码 ret_code=`curl -I -s --connect-timeout $timeout $target -w %{http_code} | tail -n1` if [ "x$ret_code" = "x200" ]; then #网络畅通 else #网络不畅通 fi }
实际脚本:
#判断网络是否配置正确,如果网络不通,退出安装程序 net_status=`curl -I -s --connect-timeout 5 www.baidu.com -w %{http_code} |tail -n1` if [ $net_status -eq 200 ];then echo -e "\033[32m[ #########the network connecting is normal, installing now############ ]\033[0m" sleep 1 else echo -e "\033[31m\033[01m[ ##########the network connecting is unstable, please check the network firstly ,then start the install again . 网络连接有问题,安装即将退出,请检查网络后再次安装########## ]\033[0m" sleep 1 exit 1 fi
IDC机房到阿里云vpc网络ping网络连通性
配置zabbix客户端配置文件
vim /etc/zabbix/zabbix_agentd.conf
添加 Include=/etc/zabbix/zabbix_agentd.d/
cat net_status.sh
#!/bin/bash
#判断网络延时 idc机房到阿里云vpc网络
#设置环境变量
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
export PATH
while getopts "c:i:" opt; do
case $opt in
c)
count=$OPTARG
;;
i)
ips=$OPTARG
;;
\?)
;;
esac
done
tmp_fifofile="/tmp/ping.fifo"
mkfifo $tmp_fifofile
exec 6<>$tmp_fifofile
rm $tmp_fifofile
for ((i=0;i<20;i++));do
echo
done >&6
for ip in `echo $ips |awk -F'|' '{ for(i=1;i<=NF;i++) {print $i} }'`
do
read -u 6
{
rtt=`ping -c $count $ip |grep rtt |awk '{print $4}' |awk -F'/' '{print $2}'`
rtt=${rtt:-'0'}
echo $rtt
}&
echo >&6
done
wait
exec 6>&-
[root@gpu007 zabbix_agentd.d]# cat zhiyi.conf
UserParameter=net_status,/etc/zabbix/scripts/net_status.sh -c 4 -i 192.168.0.167
zabbix 客户端重新启动 zabbix-agent
[root@compute zabbix_agentd.d]# systemctl restart zabbix-agent
在 zabbix server 端添加 相应的item
添加相应的trigger
添加相应的graphs