1、脚本如下所示,需按指定格式提供主机列表文件及安装sshpass包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
[root@imzcy ~]# cat imzcy/index.sh #!/bin/bash #author:zcy #date:2018-09-07 #from:https://www.imzcy.cn f="/root/imzcy/host.txt" l="/root/imzcy/logs" MyTime=`date "+%Y%m%d_%H%M"` x=`cat /root/imzcy/host.txt |wc -l` disk (){ guazai=`cat ${l}/${MyTime}-disk-${zip}.log |awk 'NR>1{if($2!=""){print $NF}}'` n=`cat ${l}/${MyTime}-disk-${zip}.log |awk 'NR>1{if($2!=""){print $NF}}' |wc -l` echo ${zip}: for i in ${canshu} ; do x=1 for z in ${guazai} ; do if [ "$i" != "$z" ] ; then if [ $x -lt $n ] ; then x=$(($x+1)) else echo "$i 不存在,请检查" break fi else cat ${l}/${MyTime}-disk-${zip}.log |awk '{if($2!=""){print $0}}' |grep "$i"$ |grep ^" " >/dev/null if [ $? -eq 0 ] ; then echo $i $(cat ${l}/${MyTime}-disk-${zip}.log |awk '{if($2!=""){print $0}}' |grep "$i"$ |awk '{print "总共: "$1,"可用: "$3,"使用率: "$4}') else echo $i $(cat ${l}/${MyTime}-disk-${zip}.log |awk '{if($2!=""){print $0}}' |grep "$i"$ |awk '{print "总共: "$2,"可用: "$4,"使用率: "$5}') fi break fi done done echo } [ ! -d $l ] && mkdir -p $l for o in `seq 1 $x`; do eval $(cat $f |head -n $o |tail -n 1 |awk -F'|' '{printf("zip=%s; zuser=%s; zpwd=%s; zport=%s; canshu=%s",$1,$2,$3,$4,$5)}') /usr/local/bin/sshpass -p $zpwd ssh -o StrictHostKeyChecking=no -p $zport ${zuser}@${zip} "df -h" >${l}/${MyTime}-disk-${zip}.log disk y="" done [root@imzcy ~]# |
2、主机列表文件如下所示,需以|分割提供服务器IP地址、用户名、密码、ssh端口、空格分开的多个需要监控的分区挂载点:
1 2 3 4 |
[root@imzcy ~]# cat imzcy/host.txt 192.168.2.4|zcy|123_pass|2222|"/ /u01/app/oracle/oradata" 192.168.2.5|zcy|123456|22|"/ /data" [root@imzcy ~]# |
3、执行效果如下:
1 2 3 4 5 6 7 8 9 10 |
[root@imzcy ~]# sh imzcy/index.sh 192.168.2.4: / 总共: 36G 可用: 1.5G 使用率: 96% /u01/app/oracle/oradata 总共: 394G 可用: 12G 使用率: 97% 192.168.2.5: / 总共: 509G 可用: 210G 使用率: 57% /data 总共: 509G 可用: 3.0G 使用率: 100% [root@imzcy ~]# |