Shell Script แจ้งเตือน SMS เมื่อ Hard Disk ใกล้เต็มแบบฟรีๆ

mrgill   | December 13, 2009   |   2,133 Views

สวัสดีครับต่อยอดจากเรื่อง 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. กำหนดสิทธิ์ให้สามารถรันได้

[root@Webserver ~]$ chmod +x observman_disk.sh

4. นำ script ไปใส่ /etc/cron.daily/ เพื่อให้ script ทำงานทุกวัน

[root@Webserver ~]$ cp observman_disk.sh /etc/cron.daily/

หรือใครจะนำไปใส่ใน crontab ก็ไม่ว่ากัน

[root@Webserver ~]$ crontab -e
# m h dom mon dow command @daily /root/observman_disk.sh > /dev/null

หวังว่าบทความนี้คงเป็นประโยชน์ต่อผู้อ่าน

Category : Linux,Programming,Security,Server

Tags: free, google calendar, hard disk, Linux, monitoring, shell script, sms

Related Posts

Server Down รู้ได้ทันทีผ่าน SMS แถมฟรีด้วย Script ต่อ True Wi-Fi อัตโนมัติ(Truewifi Auto Login V2.04)

4 Comments Leave a Comment

  • 1. aj  |  December 15, 2009 at 11:26 AM

    ผมลองโหลด API ลงมาไว้ที่host ผมแต่มันส่งไม่ได้ครับ ขึ้น Error

    WARNING: Certificate verification error for 203.146.32.xx: unable to get local issuer certificate
    WARNING: certificate common name `www.snakeoil.dom’ doesn’t match requested host name `203.146.32.xx’.

    ผมต้อง ลงอะไรที่ host อีกไหมครับ

  • 2. mrgill  |  December 15, 2009 at 4:49 PM

    เรียก https://yourdomain.com/google_sms/gsms.php จาก browser ได้หรือเปล่าครับ
    ถ้าเรียกแล้วเกิด error แสดงว่ามีเจอไฟล์ gsms.php อยู่บน server แล้ว แต่เพราะมันไม่ได้ใส่ input ให้จึงมี error ดังกล่าว
    ให้ลองเรียก gsms.php ตามตัว parameter เหมือนใน script ดูครับ
    ตัวอย่าง

    https://imrgill.com/google_sms/gsms.php?username=user@gmail.com&password=gmail-passwd&title=Server%20192.168.1.2%20Down

    แล้วลองไปดูใน google calendar ว่ามี event เข้ามาหรือเปล่า

    จากนั้นลองมาเรียกจา script ถ้า script รันผ่านมันจะมี output ประมาณนี้ครับ

    root@mrgill-laptop:/var/www# ./observman.sh --2009-12-15 16:38:14-- https://imrgill.com/google_sms/gsms.php?username=user@gmail.com&password=gmail-passwd&title=Server%20192.168.1.2%20Down Resolving imrgill.com... 61.19.246.xxx Connecting to imrgill.com|61.19.246.xxx|:443... connected. WARNING: cannot verify imrgill.com's certificate, issued by `/C=TH/ST=Bangkok/L=Newbury/O=Webhosting/OU=SSL/CN=61.19.246.xxx/emailAddress=ssl@localhost': Self-signed certificate encountered. WARNING: certificate common name `61.19.246.xxx' doesn't match requested host name `imrgill.com'. HTTP request sent, awaiting response... 200 OK Length: 5 [text/html] Saving to: `gsms.php?username=user@gmail.com&password=gmail-passwd&title=Server 192.168.1.2 Down' 100%[===================================================================================================================>] 5 --.-K/s in 0s 2009-12-15 16:38:16 (124 KB/s) - `gsms.php?username=user@gmail.com&password=gmail-passwd&title=Server 192.168.1.2 Down' saved [5/5] Removing gsms.php?username=user@gmail.com&password=gmail-passwd&title=Server 192.168.1.2 Down. FINISHED --2009-12-15 16:38:16-- Downloaded: 1 files, 5 in 0s (124 KB/s)
  • 3. aj  |  December 23, 2009 at 11:28 AM

    ได้แล้วครับ ขอบคุณครับ

    พอดีกำหนดเวลา ไม่ถูกครับ

  • 4. OpenDNS&hellip  |  May 4, 2010 at 10:38 AM

    OpenDNS…

    Another night raid on February 12, soon after the new directive had been issued, showed clearly that it had not changed anything. The raid, obviously carried out without informing local officials, not only blundered into a family celebration and killed…

Leave a Comment

(required)

(required), (Hidden)

 

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

TrackBack URL  |  RSS feed for comments on this post.


Chat

Recent Entries

Random Posts

Social Network

Tag Cloud

Calendar

December 2009
S M T W T F S
« Nov   Jan »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Twitter: imrgill

RSS LinuxToday

RSS HowToForge

RSS UbuntuClub

RSS Blognone

RSS PowerWindowsBlog

Tag

Categories

Archives

Blogroll

Meta

Get Adobe Flash playerPlugin by wpburn.com wordpress themes