码迷,mamicode.com
首页 > 系统相关 > 详细

linux主机内存告警shell脚本

时间:2019-10-09 10:50:45      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:$2   script   ESS   --   shel   ppi   inux   bin   man   

#!/bin/sh

ramusage=$(free | awk '/Mem/{printf("RAM Usage: %.2f\n"), $3/$2*100}'| awk '{print $3}')

if [ "$ramusage" > 20 ]; then
  SUBJECT="ATTENTION: Memory Utilization is High on $(hostname) at $(date)"
  MESSAGE="/tmp/Mail.out"
  TO="2daygeek@gmail.com"
  echo "Memory Current Usage is: $ramusage%" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "Top Memory Consuming Process Using top command" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "$(top -b -o +%MEM | head -n 20)" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "Top Memory Consuming Process Using ps command" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "$(ps -eo pid,ppid,%mem,%Memory,cmd --sort=-%mem | head)" >> $MESSAGE
  mail -s "$SUBJECT" "$TO" < $MESSAGE
  rm /tmp/Mail.out
fi

# crontab -e
# */5 * * * * /bin/bash /opt/scripts/memory-alert.sh

# /usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' |  awk '{print $3}' | awk '{ if($1 > 20) print $0;}' | mail -s "High Memory Alert" 1103324414@qq.com

linux主机内存告警shell脚本

标签:$2   script   ESS   --   shel   ppi   inux   bin   man   

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/11640322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!