为了满足定制化需求,特对物理主机的多路径软件multipath服务和路径进行了脚本采集,并提供自动化部署路径。
1、多路径采集脚本
#!/bin/bash
#result include valid_num\invalid_num\
source /etc/profile
##获取ip地址
bond1=`ip address show dev bond1 | grep 172.29 | awk -F"/" '{print $1}' | awk '{print $2}' | head -1 | tr -d " "`
echo "####" ${bond1}
bond0=`ip address show dev bond0 | grep 172.29 | awk -F"/" '{print $1}' | awk '{print $2}' | head -1 | tr -d " "`
[[ -z $bond0 ]] && ip=$bond1 || ip=$bond0
#multipath -F
echo "ip:"$ip
res_name="/tmp/multipath_$ip"
date_str=`date +%Y-%m-%d_%H:%M`
echo "$date_str" > $res_name
os_multipath_check (){
valid_num=`multipath -l | egrep "active .* running" | wc -l`
invalid_num=`multipath -l | egrep "failed .* running" | wc -l`
proc_num=`ps -ef | grep multipathd | grep -v grep | wc -l`
if [ "${proc_num}" -gt 0 ];then
echo "service_status=on" >> $res_name
else
echo "service_status=off" >> $res_name
exit
fi
if [ $valid_num != 0 ];then
echo "Check multipathd_service Pass"
echo "valid_path = $valid_num" >> $res_name
else
echo "valid_path = 0" >> $res_name
fi
if [ $invalid_num != 0 ];then
echo "invalid_path = $invalid_num" >> $res_name
else
echo "invalid_path = 0" >> $res_name
fi
}
###list all produce soft
##device-mapper-multipath
path_A="device-mapper-multipath"
##ZTE
path_B="multipath-ZTE"
which_path (){
rpm_path=`rpm -qa | grep path`
if [ $? -eq 0 ];then
if [[ $rpm_path == $path_B* ]];then
echo "multipath_soft is ZTE-multipath "
exit
elif [[ $rpm_path == $path_A* ]];then
echo "multipath_soft is device-mapper-multipath "
os_multipath_check
fi
fi
}
which_path
上面主要功能是,使用multipath -ll 去查看多路径条目数。
2、执行结果
上面脚本执行后会在/tmp目录下多出一个带ip地址的执行结果文件:
3、添加计划任务,每2分钟运行一次。
*/2 * * * * /bin/bash /tmp/multi.sh
4、将以上过程汇总成脚本,实现自动下载,部署脚本,添加计划任务
上面汇总成的脚本放到一个可以下载的地方,如172.29.129.171的http服务下
#!/bin/bash
mul_file="multi.sh"
mul_nm="/tmp/multi.sh"
echo "### $mul_nm"
multipath_num=`rpm -qa device-mapper-multipath* | wc -l `
echo "$multipath_num"
if [ "${multipath_num}" -gt 0 ];then
if [ -n "{$mul_nm}" ];then
curl -o $mul_nm http://172.29.129.171/icons/multipath/multi.sh-0330 && chmod +x $mul_nm && (echo "*/2 * * * * /bin/bash $mul_nm " ; crontab -l )| crontab
echo "get_crontab"
else
echo "no action"
fi
else
echo "no"
fi
5、在客户端或要采集的主机上执行命令完成自动化部署
curl -o /tmp/get_multipath.sh http://172.29.129.171/icons/multipath/get_multipath.sh &&
chmod +x /tmp/get_multipath.sh && cd /tmp/ &&
./get_multipath.sh &&
rm -rf /tmp/get_multipath.sh
6、验证:
在计划任务中新增一条任务:每2分钟执行一次;
在客户端多出一个multi.sh脚本;
在客户端有脚本的执行结果文件;