技术
成就梦想!

服务器多网卡多Bond连通性检测脚本

问题背景

      如今数据中心存储数据主要使用Ceph分布式存储进行构建,而构建底层基础架构服务器会有多张网络(管理网、业务网、存储网),如果部署的服务器数量较少的话可以手动进行检查,如果服务器数量比较多的话手动不仅效率低而且错误率较高。

解决办法

     我们可以通过Ansible批量执行检查脚本来检测网卡的连通性,进而排查VLAN透传、Bond绑定、Bond聚合、网关地址这些与期望是否一致,提升工作效率。脚本通过获取bondVLAN子接口的网关地址进行Ping测试,具体shell脚本如下,大家可以参考思路进行修改,解决实际问题。

IPV4连通性检测:

#!/bin/bash
#检测各网关连通性

bond0_net_01=`ls /etc/sysconfig/network-scripts/ifcfg-bond0.* | awk '{print $0}' |sed -n "1p"`
bond1_net_01=`ls /etc/sysconfig/network-scripts/ifcfg-bond1.* | awk '{print $0}' |sed -n "1p"`
bond1_net_02=`ls /etc/sysconfig/network-scripts/ifcfg-bond1.* | awk '{print $0}' |sed -n "2p"`
bond2_net_01=`ls /etc/sysconfig/network-scripts/ifcfg-bond2.* | awk '{print $0}' |sed -n "1p"`

#bond地址获取
[ -e "$bond0_net_01" ] && bond0_ip=`cat $bond0_net_01  |grep IPADDR |tr -d '\r'|cut -d "=" -f 2`
[ -e "$bond0_net_01" ] && bond0_ip_gat=`cat $bond0_net_01  |grep GATEWAY |tr -d '\r' |cut -d "=" -f 2`

[ -e "$bond1_net_01" ] && bond1_ip_01=`cat $bond1_net_01  |grep IPADDR |tr -d '\r'|cut -d "=" -f 2`
[ -e "$bond1_net_01" ] && bond1_ip_gat_01=`cat $bond1_net_01  |grep GATEWAY|tr -d '\r' |cut -d "=" -f 2`

[ -e "$bond1_net_02" ] && bond1_ip_02=`cat $bond1_net_02  |grep IPADDR |tr -d '\r'|cut -d "=" -f 2`
[ -e "$bond1_net_02" ] && bond1_ip_gat_02=`cat "$bond1_net_02"  |grep GATEWAY |tr -d '\r' |cut -d "=" -f 2`

[ -e "$bond2_net_01" ] && bond2_ip=`cat $bond2_net_01  |grep IPADDR |tr -d '\r'|cut -d "=" -f 2`
[ -e "$bond2_net_01" ] && bond2_ip_gat=`cat $bond2_net_01  |grep GATEWAY|tr -d '\r' |cut -d "=" -f 2`

#地址测试
[ -e "$bond0_net_01" ] && ping=`ping -c 3 -w 3 -i 0.01 ${bond0_ip_gat} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond0_ip_gat_status=Fail || bond0_ip_gat_status=OK
[ -e "$bond1_net_01" ] && ping=`ping -c 3 -w 3 -i 0.01 ${bond1_ip_gat_01} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond1_ip_gat_01_status=Fail || bond1_ip_gat_01_status=OK
[ -e "$bond1_net_02" ] && ping=`ping -c 3 -w 3 -i 0.01 ${bond1_ip_gat_02} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond1_ip_gat_02_status=Fail || bond1_ip_gat_02_status=OK
[ -e "$bond2_net_01" ] && ping=`ping -c 3 -w 3 -i 0.01 ${bond2_ip_gat} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond2_ip_gat_status=Fail || bond2_ip_gat_status=OK

if [ -e "$bond1_net_02" ];then
	echo "服务器管理网IP:$bond0_ip ------管理网IP:$bond0_ip 网关:$bond0_ip_gat 状态:$bond0_ip_gat_status"
	echo "服务器管理网IP:$bond0_ip ------业务网01_IP:$bond1_ip_01 业务网01_网关:$bond1_ip_gat_01 状态:$bond1_ip_gat_01_status"
	echo "服务器管理网IP:$bond0_ip ------业务网02_IP:$bond1_ip_02 业务网02_网关:$bond1_ip_gat_02 状态:$bond1_ip_gat_02_status"
	echo "服务器管理网IP:$bond0_ip ------存储网IP:$bond2_ip 存储网网关:$bond2_ip_gat 状态:$bond2_ip_gat_status"
else
        echo "服务器管理网IP:$bond0_ip ------管理网IP:$bond0_ip 网关:$bond0_ip_gat 状态:$bond0_ip_gat_status"
        echo "服务器管理网IP:$bond0_ip ------业务网01_IP:$bond1_ip_01 业务网01_网关:$bond1_ip_gat_01 状态:$bond1_ip_gat_01_status"
        echo "服务器管理网IP:$bond0_ip ------存储网IP:$bond2_ip 存储网网关:$bond2_ip_gat 状态:$bond2_ip_gat_status"
fi

IPV6连通性检测:

#!/bin/bash
#检测各IPV6网关连通性

bond0_net_01=`ls /etc/sysconfig/network-scripts/ifcfg-bond0.* | awk '{print $0}' |sed -n "1p"`
bond1_net_01=`ls /etc/sysconfig/network-scripts/ifcfg-bond1.* | awk '{print $0}' |sed -n "1p"`
bond1_net_02=`ls /etc/sysconfig/network-scripts/ifcfg-bond1.* | awk '{print $0}' |sed -n "2p"`
bond2_net_01=`ls /etc/sysconfig/network-scripts/ifcfg-bond2.* | awk '{print $0}' |sed -n "1p"`

#bond管理地址获取
[ -e "$bond0_net_01" ] && bond0_ip=`cat $bond0_net_01  |grep IPADDR |tr -d '\r'|cut -d "=" -f 2`
[ -e "$bond0_net_01" ] && bond0_ip_gat=`cat $bond0_net_01  |grep GATEWAY |tr -d '\r' |cut -d "=" -f 2`

#bond_IPV6_地址获取
[ -e "$bond0_net_01" ] && bond0_v6_ip=`cat $bond0_net_01  |grep IPV6ADDR |tr -d '\r'|cut -d "=" -f 2 |cut -d "/" -f 1`
[ -e "$bond0_net_01" ] && bond0_v6_ip_gat=`cat $bond0_net_01  |grep IPV6_DEFAULTGW |tr -d '\r' |cut -d "=" -f 2 |cut -d "/" -f 1`

[ -e "$bond1_net_01" ] && bond1_v6_ip_01=`cat $bond1_net_01  |grep IPV6ADDR |tr -d '\r'|cut -d "=" -f 2 |cut -d "/" -f 1`
[ -e "$bond1_net_01" ] && bond1_v6_ip_gat_01=`cat $bond1_net_01  |grep IPV6_DEFAULTGW|tr -d '\r' |cut -d "=" -f 2`

[ -e "$bond1_net_02" ] && bond1_v6_ip_02=`cat $bond1_net_02  |grep IPV6ADDR |tr -d '\r'|cut -d "=" -f 2 |cut -d "/" -f 1`
[ -e "$bond1_net_02" ] && bond1_v6_ip_gat_02=`cat "$bond1_net_02"  |grep IPV6_DEFAULTGW |tr -d '\r' |cut -d "=" -f 2`

[ -e "$bond2_net_01" ] && bond2_v6_ip=`cat $bond2_net_01  |grep IPV6ADDR |tr -d '\r'|cut -d "=" -f 2 |cut -d "/" -f 1`
[ -e "$bond2_net_01" ] && bond2_v6_ip_gat=`cat $bond2_net_01  |grep IPV6_DEFAULTGW|tr -d '\r' |cut -d "=" -f 2`

#地址测试
[ -e "$bond0_v6_net_01" ] && ping=`ping6 -c 3 -w 3 -i 0.01 ${bond0_v6_ip_gat} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond0_v6_ip_gat_status=Fail || bond0_v6_ip_gat_status=OK
[ -e "$bond1_v6_net_01" ] && ping=`ping6 -c 3 -w 3 -i 0.01 ${bond1_v6_ip_gat_01} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond1_v6_ip_gat_01_status=Fail || bond1_v6_ip_gat_01_status=OK
[ -e "$bond1_v6_net_02" ] && ping=`ping6 -c 3 -w 3 -i 0.01 ${bond1_v6_ip_gat_02} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond1_v6_ip_gat_02_status=Fail || bond1_v6_ip_gat_02_status=OK
[ -e "$bond2_v6_net_01" ] && ping=`ping6 -c 3 -w 3 -i 0.01 ${bond2_v6_ip_gat} |grep ttl |wc -l` && [ $ping -eq 0 ] && bond2_v6_ip_gat_status=Fail || bond2_v6_ip_gat_status=OK

if [ -e "$bond1_net_02" ];then
	echo "服务器管理网IP:$bond0_ip ------管理网IP:$bond0_v6_ip 网关:$bond0_v6_ip_gat 状态:$bond0_v6_ip_gat_status"
	echo "服务器管理网IP:$bond0_ip ------业务网01_IP:$bond1_v6_ip_01 业务网01_网关:$bond1_v6_ip_gat_01 状态:$bond1_v6_ip_gat_01_status"
	echo "服务器管理网IP:$bond0_ip ------业务网02_IP:$bond1_v6_ip_02 业务网02_网关:$bond1_v6_ip_gat_02 状态:$bond1_v6_ip_gat_02_status"
	echo "服务器管理网IP:$bond0_ip ------存储网IP:$bond2_v6_ip 存储网网关:$bond2_v6_ip_gat 状态:$bond2_v6_ip_gat_status"
else
    echo "服务器管理网IP:$bond0_ip ------管理网IP:$bond0_v6_ip 网关:$bond0_v6_ip_gat 状态:$bond0_v6_ip_gat_status"
    echo "服务器管理网IP:$bond0_ip ------业务网01_IP:$bond1_v6_ip_01 业务网01_网关:$bond1_v6_ip_gat_01 状态:$bond1_v6_ip_gat_01_status"
    echo "服务器管理网IP:$bond0_ip ------存储网IP:$bond2_v6_ip 存储网网关:$bond2_v6_ip_gat 状态:$bond2_v6_ip_gat_status"
fi

检查截图

ansible批量检查
赞(4)
未经允许不得转载:未来往事 » 服务器多网卡多Bond连通性检测脚本

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址