สวัสดีครับต่อยอดจากเรื่อง Server Down รู้ได้ทันทีผ่าน SMS แถมฟรี ที่เขียนไว้วันก่อน วันนี้เอามาประยุกต์ให้ script เช็คสถานะของ hard disk ครับโดยให้ส่ง sms เตือนเมื่อมีการใช้งาน partition ใดๆ เกิน 90%
มาดูตัว code ของ script กันครับ
#!/bin/sh
# Simple SHELL script for Linux and UNIX system monitoring with
# df command
# -------------------------------------------------------------------------
# Copyright (c) 2009 mrGilL
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# Setup SMS User below
# See URL for more info:
# http://imrgill.com/2009/12/13/monitor-server-with-sms-alert-shell-script/
# -------------------------------------------------------------------------
# Hard type (hdx,sdx,scx)
HDD_TYPE="sd"
# Partition lists
PLIST=`df -h | grep $HDD_TYPE | awk '{ print $1 }' | sed -e 's/\/dev\///g'`
# Setting SMS User (Gmail)
GCUSER="user@gmail.com"
GCPASSWD="gmail-passwd"
for p in $PLIST
do
disk=$(df -h | grep $p | awk '{ print $5 }' | sed -e 's/\%//g')
pname=$(df -h | grep $p | awk '{ print $6 }')
if [ $disk -ge 90 ]; then
# disk less than 90%
wget --force-html --recursive --no-check-certificate -nd --delete-after "https://imrgill.com/google_sms/gsms.php?username=$GCUSER&password=$GCPASSWD&title=$(hostname) partition $pname < 90%"
fi
done
วิธีนำไปใช้
1. แก้ไข HDD_TYPE เป็นประเภท hard disk ที่คุณใช้ ดูได้จากคำสั่ง fdisk -l หรือ df ผลลัพธ์ที่ได้จะประมาณ /dev/sda ตัวอย่างผมได้ sda ให้ใส่เป็น HDD_TYPE=”sd”
[mrgill@Webserver ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 59515556 1260296 55183232 3% /
/dev/sda8 11710104 2895576 8210084 27% /usr
/dev/sda7 1019208 37992 928608 4% /tmp
/dev/sda6 109109796 9092520 94385292 9% /var
/dev/sda5 287660612 11604092 261208448 5% /home
/dev/sda1 101086 33812 62055 36% /boot
tmpfs 517284 0 517284 0% /dev/shm
*เหตุผลที่ไม่ใส่ sda ไปตรงๆ เลยก็เพราะ กรณีมี hard disk หลายลูกเช่น sda, sdb ทำให้ script ไม่สามารถตรวจสอบ hard disk ที่เป็น sdb ได้
2. แก้ไข GCUSER และ GCPASSWD เป็น user gmail ของเรา
3. กำหนดสิทธิ์ให้สามารถรันได้
4. นำ script ไปใส่ /etc/cron.daily/ เพื่อให้ script ทำงานทุกวัน
หรือใครจะนำไปใส่ใน crontab ก็ไม่ว่ากัน
[root@Webserver ~]$ crontab -e
# m h dom mon dow command
@daily /root/observman_disk.sh > /dev/null
หวังว่าบทความนี้คงเป็นประโยชน์ต่อผู้อ่าน