Sunday, October 27, 2024
Bash Script Centos

Membuat Alert Disk Space dengan Bash Script

bash

“Membuat Alert Disk Space dengan Bash Script”

Pengantar

Penggunaan disk adalah laporan yang dihasilkan oleh sistem Linux tentang berbagai disk yang tersedia atau dibuat pada memori sekunder. Disk ini juga dikenal sebagai partisi, mereka memiliki sistem file yang terisolasi. Fasilitas ini memberi kita pengukuran berbagai label atau fitur seperti Ruang yang digunakan, Ruang kosong, Sistem file disk, dll. Untuk melihat semua label ini, Linux memiliki beberapa perintah internal yang membantu kita untuk memvisualisasikannya, tetapi itu adalah perintah terminal, dan kita perlu membuat skrip shell untuk pengguna menggunakan perintah tersebut.

Persiapan

yum update -y
yum install mailx -y
mailx -V

Test email

 echo "This is test email" | mail -s "Test Email" games@gmail.com

Cara 1 -> set threshold at 60%

# vi /opt/script/disk-usage-alert.sh

#!/bin/sh
dusage=$(df -Ph | grep -vE '^tmpfs|cdrom' | sed s/%//g | awk '{ if($5 > 60) print $0;}')
fscount=$(echo "$dusage" | wc -l)
if [ $fscount -ge 2 ]; then
echo "$dusage" | mail -s "Disk Space Alert On $(hostname) at $(date)" example@gmail.com
else
echo "Disk usage is in under threshold"
  fi

Ganti Permission

chmod +x /opt/script/disk-usage-alert.sh

Jalankan command

sh  /opt/script/disk-usage-alert.sh

Output

Filesystem                            Size  Used Avail Use Mounted on
/dev/mapper/vg_2g-lv_root              10G  6.7G  3.4G  67 /
/dev/mapper/vg_2g-lv_home             5.0G  4.3G  784M  85 /home

Crontab 10 menit

# crontab -e
*/10 * * * * /bin/bash /opt/script/disk-usage-alert.sh

Cara 2

# vi /opt/script/disk-usage-alert-1.sh

#!/bin/sh
df -Ph | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5,$1 }' | while read output;
do
  echo $output
  used=$(echo $output | awk '{print $1}' | sed s/%//g)
  partition=$(echo $output | awk '{print $2}')
  if [ $used -ge 60 ]; then
  echo "The partition \"$partition\" on $(hostname) has used $used% at $(date)" | mail -s "Disk Space Alert: $used% Used On $(hostname)" example@gmail.com
else
echo "Disk space usage is in under threshold"
  fi
done

Output

The partition "/dev/mapper/vg_2g-lv_home" on 2g.CentOS7 has used 85% at Wed Feb 24 06:16:14 IST 2021

The partition "/dev/mapper/vg_2g-lv_root" on 2g.CentOS7 has used 67% at Wed Feb 24 06:16:14 IST 2021

Cara 3 -> Spesific Path

# vi /opt/script/disk-usage-alert-2.sh

#!/bin/bash
used=$(df -Ph | grep '/dev/mapper/vg_2g-lv_dbs' | awk {'print $5'})
max=80%
if [ ${used%?} -ge ${max%?} ]; then
echo "The Mount Point "/DB" on $(hostname) has used $used at $(date)" | mail -s "Disk space alert on $(hostname): $used used" example@gmail.com
fi

Output

The partition /dev/mapper/vg_2g-lv_dbs on 2g.CentOS6 has used 82% at Wed Feb 24 06:16:14 IST 2021

Cara 4

#!/bin/sh

path1=$(du -sh /var)
path2=$(du -sh /home)
main(){
  echo "Disk Used Pada Server $(hostname) :"
  echo ""
  echo "$path1"
  echo "$path2"
}
main |mail -s "Disk Usage Server $(hostname)- $(date)" sample@gmail.com

Output

Disk Used Pada Server games.com :

760M    /var
12K     /home

Cara 5

#!/bin/sh

path1=$(du -ch --max-depth=1 /var | sort -h)
main(){
  echo "Disk Used Pada Server $(hostname) :"
  echo ""
  echo "$path1"
}
main |mail -s "Disk Usage Server $(hostname)- $(date)" games@gmail.com

Output

Cara 6

#!/bin/bash
# Set Variables
Today="at $(date '+%H:%M:%S - %d-%b-%y')"
CurrentDate=`date +"%d_%b_%Y"`
IPADDRESS=`hostname -I | awk '{print $1}'`
​
# HTML Table Report
(
  echo '<HTML><HEAD><TITLE>Folder Size</TITLE></HEAD>'
  echo '<BODY>'
  echo '<H3>Folder Size Report Server - '$(uname -n) ip ${IPADDRESS}'</H3>'
  echo '<P>Report Generated '${Today}'</P>'
  echo '<TABLE BORDER=3 CELLSPACING=2 CELLPADDING=0>'
  echo '<TR BGCOLOR=\"#BBFFFF\"> <TH>Size</TH> <TH>Path</TH><//TR>'
​
# Folder Size
ArryCount=0
du -h --max-depth=1 /var |sort -rh|while read Size Path
 do
     echo '<TR><TD>'$Size'</TD><TD>'$Path'</TD>'
     ArryCount=$ArryCount+1
 done
) | tee `hostname`_folder_size-$CurrentDate.html
​
# Sending E-Mail Notification
(
  echo To: yuby@gmail.com
  echo From: admin
  echo "Content-Type: text/html;"
  echo Subject: Folder Size Report for server `hostname` ip $IPADDRESS
  echo
  cat  `hostname`_folder_size-$CurrentDate.html
) | sendmail -t
echo -e "Report Generation is Completed... \n\a"

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Membuat Alert Disk Space dengan Bash Script . Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

(Visited 232 times, 1 visits today)

Similar Posts