業務用エアコン関連の技術情報、エラーコード、環境問題対策に関する別サイト「エアコンの安全な修理・適切なフロン回収」

CentOS7.9 : Logwatch , Disk usage check script

1. Logwatch

①Install

# yum install logwatch
②Edit configuration file
●Per line 45
Insert "#" at the beginning of the "MailTo = root" line and set the email address you want to receive notifications to the line below it.
#MailTo = root
MailTo = [Email address]
●Per line 7: Set the level of detail for log notifications
#Detail = Low
Detail = High

③Output Logwatch reports

# logwatch --output stdout
You will get the following message
################### Logwatch 7.6 (01/22/22) ####################
Processing Initiated: Fri May 13 09:41:46 2022
Date Range Processed: yesterday
( 2022-May-12 )
Period is day.
Detail Level of Output: 10
Type of Output/Format: stdout / text
Logfiles for Host: fedora
###########################################################

--------------------- Amavisd-new Begin ------------------------

----omission-----

-------------------- Disk Space Begin ------------------------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/fedora_fedora-root 15G 4.4G 11G 30% /
/dev/nvme0n1p1 1014M 245M 770M 25% /boot
---------------------- Disk Space End -------------------------

###################### Logwatch End #########################

④Test to see if the report arrives at the address you set.
Check if you receive a log report email like the one above.

# /etc/cron.daily/0logwatch

2.Introduce disk usage check script

2.1 Script Creation

# cd /var/www/system
# vi disk_capacity_check.sh
Contents of disk_capacity_check.sh
#!/bin/bash

#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

2.2 Execution Confirmation

①Check current utilization

# df -h

It appears as follows

Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                        898M     0  898M   0% /dev
tmpfs                           910M     0  910M   0% /dev/shm
tmpfs                           910M  9.7M  900M   2% /run
tmpfs                           910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos_lepard-root   17G  5.0G   13G  29% /
/dev/sda1                      1014M  185M  830M  19% /boot
tmpfs                           182M     0  182M   0% /run/user/1000
tmpfs                           182M     0  182M   0% /run/user/0

②Create a dummy file (in the example, it is named "dummyfile" and is about 10G) so that the utilization is 80% or more.

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

③check again

# df -h

Run it and make sure it is above 80%.

④Run disk space check script

# /var/www/system/disk_capacity_check.sh

You will receive an email to the email address you set up with the body of the message as "Disk usage alert : 87%".

⑤Delete the "dummyfile" you created.

# rm dummyfile

⑥Periodic Execution Setting

# crontab -e
30 2 * * * /var/www/system/disk_capacity_check.sh
Copied title and URL