编写脚本检测多台机器磁盘使用情况
-
将需要检测的机器的ip统一touch到一个文件里面,此处我们命名为 linux_Server_List.config
-
然后写shell就OK了
shell如下
#!/bin/bash
#文件名:DiskCheck.sh
#用途:批量检查磁盘空间
cd /DiskCheck/
#删除之前已经存在的文件
rm -rf df_all_grep.txt
#对原先的结果进行备份
mv df_result.txt df_result_bak.txt
rm -rf df_result1.txt
#for循环对文件内的ip进行遍历
for h incat linux_Server_List.config
do
#连接机器执行df -h 后写进 df_temp.txt
echo "$h :ssh -o ConnectTimeout=3 -o ConnectionAttempts=5 -o PasswordAuthentication=no -o StrictHostKeyChecking=no $h "hostname && df -h"
" >df_temp.txt
echo "head -n 1 df_temp.txt
:cat df_temp.txt | grep -w '/$'
" >> df_all_grep.txt
echo "head -n 1 df_temp.txt
:cat df_temp.txt | grep '/data'
" >> df_all_grep.txt
echo "head -n 1 df_temp.txt
:cat df_temp.txt | grep '/yhdx'
" >> df_all_grep.txt
echo "head -n 1 df_temp.txt
:cat df_temp.txt | grep '/log'
" >> df_all_grep.txt
echo "head -n 1 df_temp.txt
:cat df_temp.txt | grep '/opt'
" >> df_all_grep.txt
done
rm -rf df_temp.txt
cat df_all_grep.txt | awk ‘{print $1"|"$3"|"$9"|"$10}’ >>df_result1.txt
sed -i ‘s/123.57.140.241/10.172.197.195/g’ df_result1.txt
cat df_result1.txt | sort -t’|’ -k 3 -r -n | head -n 50 >>df_result.txt
cat df_result1.txt | sort -t’|’ -k 3 -r -n | head -n 300 >1.txt
cat 1.txt | grep nicetuan-api-base >2.txt
head df_result.txt
3、可以写进crontab 定时执行