Scroll Top

Bash Scripts Notifikasi Low Memori Otomatis Via Email Di Linux

Kali ini ayies akan share mengenai Scripts yang bisa kamu gunakan untuk mengirimkan email notifikasi jika memori PC/Server Linux kamu dalam keadaan low/rendah. Scripts ini berguna sekali jika kamu ingin mendapat notifikasi sesegera mungkin mengenai kondisi peforma server kamu terutama memori!Skrip ini dibuat oleh : Aaron Kili Kisinga

Buat file scripts berikut. Dan taruh dalam folder yang kamu inginkan.

sudo nano /etc/scripts/alertmemory.sh

#!/bin/bash 
#######################################################################################
#Script Name    :alertmemory.sh
#Description    :send alert mail when server memory is running low
#Args           :       
#Author         :Aaron Kili Kisinga
#Email          :aaronkilik@gmail.com
#License       : GNU GPL-3	
#######################################################################################
## Deklarasi Variables
##Subyek/Judul Email
subject="Server Memory Status Alert"
##Kirim sebagai
from="server.monitor@example.com"
##Kirim Notif kepada
to="admin1@example.com"
#Kirim CC kepada
also_to="admin2@example.com"
## get total free memory size in megabytes(MB) 
free=$(free -mt | grep Total | awk '{print $4}')
## check if free memory is less or equals to  100MB
if [[ "$free" -le 100  ]]; then
## get top processes consuming system memory and save to temporary file 
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/tmp/top_proccesses_consuming_memory.txt
file=/tmp/top_proccesses_consuming_memory.txt
## send email if system memory is running low
echo -e "Warning, server memory is running low!\n\nFree memory: $free MB" | mailx -a "$file" -s "$subject" -r "$from" -c "$to" "$also_to"
fi
exit 0

Buka terminal dan jalankan perintah berikut untuk memberi akses executable dan set lewat cron untuk running perjam.

# chmod +x /etc/scripts/alertmemory.sh
# ln -s -t /etc/cron.hourly/alertmemory.sh /etc/scripts/alertmemory.sh

Related Posts