Ubuntu Server 20.04 : Logwatch & Disk usage check scripts

1. Introduction of disk usage check scripts

1.1 Script creation

# cd /opt/script/
# vi disk_capacity_check.sh
Contents of disk_capacity_check.sh

#!/bin/bash

#Specify email address for notification
MAIL=”<your mailaddress>

DVAL=`/bin/df / | /usr/bin/tail -1 | /bin/sed ‘s/^.* \([0-9]*\)%.*$/\1/’`

if [ $DVAL -gt 80 ]; then
echo “Disk usage alert: $DVAL %” | mail -s “Disk Space Alert in `hostname`” $MAIL
fi

# chmod 700 disk_capacity_check.sh

1.2 Run confirmation

①Check your current usage

# df -h

The following is displayed

Filesystem               Size       Used     Avail     Use%      Mounted on
udev                   1.9G           0     1.9G         0%         /dev
tmpfs                                                 391M     1.6M    390M        1%         /run
/dev/mapper/ubuntu–vg-ubuntu–lv    19G      9.2G     8.6G      52%         /
dev/loop0                                           56M        56M        0      100%         /snap/core18/2253
/dev/loop1                                          62M        62M        0      100%         /snap/core20/1270

②Create a dummy file to achieve at least 80% usage(In the example, the name dummyfile is 8G)

# dd if=/dev/zero of=dummyfile bs=1M count=8192

③Reaffirmation

# df -h

Confirmation that the rate is above 80%.

④Run a disk usage check script

# /opt/script/disk_capacity_check.sh

You will receive an email to your email address with the text “Disk usage alert : 98%”.

⑤Delete the “dummyfile” you have created

# rm dummyfile

⑥Periodic execution settings

# crontab -e
30 2 * * * /opt/script/disk_capacity_check.sh

2. Log analysis tool “Logwatch” installation

2.1 Install logwatch

# apt -y install logwatch

2.2 Editing the logwatch configuration file

①Copy the default configuration file

# cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/

②Change email address etc.

# vi /etc/logwatch/conf/logwatch.conf
Line 54
MailTo =<your mailaddress> # < = Send email to
Line 86
Detail = High # < = Log detail level(Low(0), Med(5), High(10)  or Integer from 1 to 10)

2.3 Create directory

There is no directory to use for caching, so create one.

# mkdir /var/cache/logwatch

2.4 動作確認

If you install logwatch, it will be registered as a cron by default, so you will receive daily report emails
If you want to check now, you can do the following

# /etc/cron.daily/00logwatch
タイトルとURLをコピーしました