Loading

shell脚本批量监控主机磁盘信息

  • 写一个配置文件保存被监控主机SSH连接信息,格式:IP User Port
[root@Test ~]# cat host
10.10.10.10 root 22
10.10.10.11 root 22
  • 配置防火墙规则
iptables -A INPUT  -m state --state RELATED,ESTABLISHED -j ACCEPT
  • 创建ssh免密通道
[root@Test ~]# ssh-keygen -t rsa -f ~/.ssh/id_dsa -P ""
[root@Test ~]# ssh-copy-id root@10.10.10.10
[root@Test ~]# ssh-copy-id root@10.10.10.11
  • 脚本如下
#!/bin/bash
disk_file=/tmp/disk
host_info=host
function info() {
    echo -e "\033[32m [INFO] \033[0m$@ "
}
function warn() {
    echo -e "\033[31m [WARN] \033[0m$@"
}
for IP in `cat $host_info |awk '/^[^#]/{print $1}'`; do
    user=`awk -v ip=$IP  '$1==ip{print $2}' $host_info`
    port=`awk -v ip=$IP  '$1==ip{print $3}'  $host_info`
    ssh -p $port $user@$IP  df -h  > $disk_file
    USE_RATE_LIST=`cat $disk_file |awk 'BEGIN{OFS="="}/^\/dev/{print $1,int($5)}'`
	log_info "------------------------------------------------------"
    for USE_RATE in $USE_RATE_LIST; do
        PART_NAME=${USE_RATE%=*}
        USE_RATE=${USE_RATE#*=}
	
		if [ $USE_RATE -ge 10 ]; then
			warn " $IP $PART_NAME  $USE_RATE%!"
		else
			info " $IP $PART_NAME  $USE_RATE%!"
		fi
    done
done
  • 测试效果
[root@Test ~]# sh a.sh 
 [INFO] ------------------------------------------------------ 
 [WARN]  10.10.10.10 /dev/mapper/centos-root  10%! 
 [INFO]  10.10.10.10 /dev/sda1  1%! 
 [INFO]  10.10.10.10 /dev/mapper/centos-var  4%! 
 [WARN]  10.10.10.10 /dev/mapper/centos-home  36%! 
 [WARN]  10.10.10.10 /dev/mapper/centos-opt  15%! 
 [INFO] ------------------------------------------------------ 
 [WARN]  10.10.10.11 /dev/mapper/centos-root  18%! 
 [WARN]  10.10.10.11 /dev/sda1  15%! 

这是颜色内容

这是颜色内容

posted @ 2019-07-12 19:11  Outsrkem  阅读(631)  评论(0编辑  收藏  举报