Linux disk usage reaches the threshold alarm email reminder

Time:2021-3-6

If a large number of logs are written or a large file occupies the disk, the application will crash. For this problem, we can use a simple shell to realize the function of e-mail reminder when the disk utilization rate is close to the threshold.

Environmental Science

  • Ubuntu 16.04

mail serve

Install heirloom mailx

sudo apt-get install heirloom-mailx

Configure SMTP

vi /etc/s- nail.rc  (or nail.rc )

According to the corresponding mailbox service configuration,passwordAuthorization code is generally used

// s-nail.rc set from="[email protected]" set smtp="smtp.163.com" set smtp-auth-user="[email protected]" set smtp-auth-password="xxx" set smtp-auth=login

Test whether the transmission is successful

Echo date "email content" | heirloom mailx - vs "title" [email protected]

Shell script

Create scriptdisk-space-notice.sh, if/dev/sda1If the disk partition utilization rate exceeds 85%, an email reminder will be sent.

// disk-space- notice.sh  dev_ sda1=`df -h | sed -n '4p' | awk '{print $5}' | cut -f 1 -d '%'` if         ((dev_ Sda1 > 85); then echo "VM server dev"_ SHA1 disk usage exceeds threshold "| heirloom mailx - vs" > server alert notification“ [email protected]  fi

Simply explain the code

This command is mainly used

df -h | sed -n '4p' | awk '{print $5}' | cut -f 1 -d '%'

df -hView disk occupancy

File system capacity used up% mount point udev 1.9g 0 1.9g 0% / dev TMPFS 393m 41m 352m 11% / run / dev / sda1 29g 23g 4.6g 84% / TMPFS 2.0g 10m 2.0g 1% / dev / SHM TMPFS 5.0m 4.0k 5.0m 1% / run / lock TMPFS 2.0g 0 2.0g 0% / sys / Fs / CGroup

In the end, we’re going to get it/dev/sda1Value of used rate of84

  • sed -n '4p'output/dev/sda1Bank
  • awk '{print $5}'Get used rate84%
  • cut -f 1 -d '%'Cut off%

Timed task

Add the shell that detects the disk utilization rate to the timing task and execute it once in a specified period of time

// crontab -e  */10 * * * * /bin/bash /home/ghost/disk-space-notice.sh

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]