Skip to content

Latest commit

 

History

History
63 lines (51 loc) · 1.71 KB

11磁盘监控脚本.md

File metadata and controls

63 lines (51 loc) · 1.71 KB

磁盘监控脚本

系统版本:CentOS7

脚本/usr/bin/disk-monitor.sh内容如下:

#!/bin/bash
LOCAL_HOST=192.168.1.3
RECEIVE_LIST="[email protected]"
CC_LIST="[email protected],[email protected],[email protected]"

MOUNT_NODE_COUNT=`df -h|wc -l`

echo "挂载节点数为:${MOUNT_NODE_COUNT}"

while [[ ${MOUNT_NODE_COUNT} -ne 1 ]];
do
  FILE_SYSTEM=`df -h |sed -n "$MOUNT_NODE_COUNT p"|awk '{print $1}'`
  MOUNT_NODE=`df -h |sed -n "$MOUNT_NODE_COUNT p"|awk '{print $6}'`
  PART_FREE_SPACE=`df -h |sed -n "$MOUNT_NODE_COUNT p"|awk '{print $4}'`
  UTILIZATION_RATE=`df -h |sed -n "$MOUNT_NODE_COUNT p"|awk '{print $5}'`
  UTILIZATION_RATE_VALUE=`echo ${UTILIZATION_RATE}|sed 's/.$//'`
  # echo "文件系统:`echo ${FILE_SYSTEM}`,挂在节点:`echo "$MOUNT_NODE"`,分区磁盘使用率为:`echo ${UTILIZATION_RATE}`, 剩余磁盘空间:`echo ${PART_FREE_SPACE}`"
  if [[  ${UTILIZATION_RATE_VALUE} -gt 95 ]]; then
     MAIL_CONTENT="[当前地址]:${LOCAL_HOST}
[文件系统]:`echo ${FILE_SYSTEM}`
[挂在节点]:`echo "$MOUNT_NODE"`
[分区磁盘使用率]:`echo ${UTILIZATION_RATE}`

已达告警阈值,请及时清理!!!"
     echo ${MAIL_CONTENT}
     echo "${MAIL_CONTENT}" | mail -s "磁盘剩余空间告警" -c ${CC_LIST} ${RECEIVE_LIST} &> /dev/null
  fi
  let MOUNT_NODE_COUNT--
done

安装mailx

yum install -y mailx

配置mailx,/etc/mail.rc追加以下内容

set [email protected]
set smtp=smtp.xxx.com:587
set smtp-auth-user=aaa
set smtp-auth-password=******
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

配置定时任务

cat >> /etc/crontab <<EOF
0 */1 * * * root /usr/bin/disk-monitor.sh
EOF