1. Select directory to create script file:
2. Give script executable permissions
3.vim test.sh Edit Script, paste the following:
#!/bin/bash
#author: JGD
#Date: 2017-09-15
#自动检测home目录空间, mail notification and cleanup log over 82%
#----Start----
#获取目录空间大小
sizeall=$ (df-h | grep Home | awk ' {print $} ')
#获取目录空间已使用大小
sizeused=$ (df-h | grep Home | awk ' {print $} ')
#获取目录空间可用大小
sizeavail=$ (df-h | grep Home | awk ' {print $} ')
#获取目录已使用百分比
usedpercent=$ (df-h | grep Home | awk ' {print $} ')
#清理项目及tomcat日志脚本路径
cleanproject=/home/zyj13424063620/myshell/auto-del-tp-log-2-day.sh
# Log Output
Monitorlog=/home/zyj13424063620/myshell/logs/monitor/homespacemonitor.log
#服务器号
servernum=04
#阈值
warnnum=82
Monitor ()
{
echo [INFO] starts monitoring tomcat ... [$ (date + '%F%h:%m:%s ')] "
echo [INFO] Total size of the directory space $sizeall
echo [INFO] directory space has used size $sizeused
echo "[INFO] directory space available size $sizeavail"
echo "[INFO] directory space has been used $usedpercent"
echo "[INFO] set threshold $warnnum%"
If [${usedpercent%%%}-gt $warnNum];then
echo "[info] starts cleaning up the project and tomcat log ..."
echo "[INFO] only holds three days ..."
echo "[info] starts cleaning ..."
$cleanProject
echo [INFO] Cleanup Complete ... "
aftercleanusedpercent=$ (df-h | grep Home | awk ' {print $} ')
The echo [INFO] has been cleaned up with a space size of $aftercleanusedpercent
If [${aftercleanusedpercent%%%}-gt $warnNum];then
The disk space is still greater than $warnnum% after the Echo [warning] cleanup
echo "[INFO] ready to send mail to engineer to process ..."
Echo-e "engineer, hello: \ nthe resource pool $servernum Server home directory space is insufficient. Specific information: Set thresholds for $warnnum%, automatic processing before the directory space has been used $usedpercent, automatic processing has been used $aftercleanus
Edpercent. Automatic processing does not achieve space-releasing effects. Please deal with it in time ... \ mm Security Center business side Alert script automatically sent \ Sincerely, salute "| Mail-s "Warming:resourcepool-num$servernum-server" xxx@139.com
echo [INFO] has sent a message to the engineer to process the
Else
Echo-e "engineer, hello: \ n resource pool $servernum Server home directory space is not enough, has been automatically cleaned up." Specific information: Set a threshold of $warnnum%, automatic processing before the directory space has been used $usedpercent, automatically processed after the $
Aftercleanusedpercent. Automatic processing drops below $warnnum%. Please pay attention to observe ... \ mm Security Center business side Alert script automatically sent \ Sincerely, salute "| Mail-s "Warming:resourcepool-num$servernum-se
RVer "18319340236@139.com,13560043430@139.com
echo "[INFO] automatically cleans up space has been used size $aftercleanusedpercent"
Fi
Else
echo "[INFO] directory space does not exceed threshold"
Fi
echo "------------------------------"
}
Monitor >> $MonitorLog
4. Save exit, add timer, command crontab-e
I'm here configured to execute the script once every 30 minutes
5. This is done, for testing, can be configured to perform once per minute, and then to see the log output.