This script comes from a blog study with learning A Ming:
The most basic of company monitoring is to monitor the use of disks, otherwise it will lead to business accidents.
General monitoring requirements are as follows: every minute to scan the status of the disk.
When disk space usage or inode usage is higher than 90%, an alarm is required.
And all subdirectories of partitions with statistical utilization rate of more than 90% are arranged in order of size, and the names of the first three directories are sent to the mailbox.
First untreated, 30 minutes later in one.
#!/bin/bash #Purpose: Monitor disk usage. #Author: Caron maktini #Date: 18 October 2018 #Version: v0.1 #Exist script name in variable l-name l_name=`echo $0 | awk -F '/' 'print $NF'` #Define the recipient's mailbox mail_user=admin@admin.com #Define a space utilization function for checking disks chk_sp() { df -m | sed '1d' | awk -F '% | +' '$5>90 {print $7,$5}'>/tmp/chk_sp.log n=`wc -l /tmp/chk_sp.log | awk 'print $1'` if [ $n -gt 0 ] then tag=1 for d in `awk '{print $1}' /tmp/chk_sp.log` do find $d -type d | sed '1d' | xargs du -sm | sort -nr | head -3 done >/tmp/most_sp.txt fi } #Define check inode usage function chk_in() { df -i | sed `1d` | awk -F '% | +' '$5>90 {print $7,$5}'>/tmp/chk_in.log n=`wc -l /tmp/chk_in.log | awk '{print $1}'` if [ $n -gt 0 ] then tag=2 fi } #Define alarm function m_mail(){ log=$1 t_s=`date +%s` t_s2=`data -d "1 hours ago" +%s` if [ ! -f /tmp/$log ] then #Create a $log file touch /tmp/$log #Added a permission, only allowed additional content, not allowed to change or delete chattr +a /tmp/$log #The first alarm can be written directly to the timestamp one hour ago echo $t_s2 >> /tmp/$log fi #whether#Whether the log file has just been created requires looking at the timestamp of the last line t_s2=`tail -l /tmp/$log | awk '{print $1}'` # Take out the timestamp of the last line and the last alarm and write it to the current timestamp immediately echo $t_s >>/tmp/$log #Take the difference of two timestamps v=$[ $t_s-$t_s2 ] #If the difference exceeds 100, send the mail immediately. if [ $v -gt 1800 ] then #Send an email, where $2 is the second function of the mail function, and here is a file python mail.py $mail_user "Disk usage over 90%" #Define the temporary file of the technicians and write it to 0 echo "0" > /tmp/$log.count else #If the technicians temporary file does not exist, you need to create and write 0 if [ ! -f /tmp/$log.count } then echo "0" > /tmp/$log.count fi nu=`cat /tmp/$log.count` #For every alarm in 30 minutes, add 1 to the calculator nu2=$[ $nu+1 ] echo $nu2>/tmp/$log.count #When the number of alarms exceeds 30, it is necessary to send oil again. if [ $nu2 -gt 30 ] then python mail.py $mail_user "Disk utilization rate 90%It lasted 30 minutes." "`cat $2`" 2>/dev/null #After the second alarm, start the calculator from zero again echo "0" > /tmp/$log.count fi fi } #If the number of processes is greater than 0, the last script has not been executed. if [ $p_n -gt 0 ] then exit fi chk_sp chk_in if [ $tag == 1 ] then m_mail chk_sp /tmp/most_sp.txt elif [ $tag == 2 ] then m_mail chk_in /tmp/chk_in.log fi