有一些linux基础,最经刚开始学shell,参考了阿良老师的一个监测服务器硬盘状态的脚本,自己进行了一些扩展,今天比较晚了,后边会把注释放上来,感觉脚本还很不完善,希望大家一起探讨一下,共同学习
#!/bin/sh
source /etc/profile
source /root/.bashrc
HOST_INFO=${PWD}/all_hosts.info
TMP_FILE=${PWD}/all_tmp.log
ALERT_FILE=${PWD}/all_alert.log
echo null > $ALERT_FILE & echo null > $TMP_FILE
for IP in $(awk ‘/^[^#]/{print $1}‘ $HOST_INFO)
do
echo "开始测试服务器:$IP"
ping -c3 $IP 2>&1 >/dev/null
if [ $? -eq 0 ]
then
echo "服务器:$IP 可以连通"
else
DATE=`date`
echo -e "注意!!!服务器:$IP 在时间:$DATE 未能连通\n" 2>&1 >>$ALERT_FILE
fi
USER=$(awk -v ip=$IP ‘ip==$1{print $2}‘ $HOST_INFO)
PORT=$(awk -v ip=$IP ‘ip==$1{print $3}‘ $HOST_INFO)
echo "-------开始检测测盘占用,详情见文件:all_tmp.log,和报警文件:all_alert.log---------"
echo ‘------------------------------------------------------------------------------------‘ >> $TMP_FILE
echo $IP >> $TMP_FILE
ssh -p $PORT [email protected]$IP ‘df -hP‘ >> $TMP_FILE
if [ $? -gt 0 ]
then
echo "请检查服务器:$IP ssh连接!" 2>&1 >>$ALERT_FILE
fi
USE_RATE_LIST=$( awk ‘BEGIN{OFS="="}/^\/dev/{print $1,int($5)}‘ $TMP_FILE)
for USE_RATE in $USE_RATE_LIST; do
PART_NAME=${USE_RATE%=*}
USE_RATE=${USE_RATE#*=}
if [ $USE_RATE -ge 80 ]
then
echo -e "警告:服务器:$IP 磁盘:$PART_NAME 使用率: $USE_RATE" 2>&1 >$ALERT_FILE
fi
done
done
附 :
阿良老师脚本(测试版,改动了文件路径等):
#!/bin/bash
HOST_INFO=/opt/tools/stat_host.info
TMP_FILE=/opt/tools/tmp_stat_disk.log
date > $TMP_FILE
echo ‘------------------------------------------------------------------------------------‘ >> $TMP_FILE
for IP in $(awk ‘/^[^#]/{print $1}‘ $HOST_INFO); do
USER=$(awk -v ip=$IP ‘ip==$1{print $2}‘ $HOST_INFO)
PORT=$(awk -v ip=$IP ‘ip==$1{print $3}‘ $HOST_INFO)
echo ‘------------------------------------------------------------------------------------‘ >> $TMP_FILE
echo $IP >> $TMP_FILE
ssh -p $PORT [email protected]$IP ‘df -h -P‘ >> $TMP_FILE
USE_RATE_LIST=$( awk ‘BEGIN{OFS="="}/^\/dev/{print $1,int($5)}‘ $TMP_FILE)
for USE_RATE in $USE_RATE_LIST; do
PART_NAME=${USE_RATE%=*}
USE_RATE=${USE_RATE#*=}
if [ $USE_RATE -ge 80 ]; then
echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
fi
done
done
more $TMP_FILE
原文地址:https://www.cnblogs.com/levi-w/p/9643486.html