監控硬碟空間指令碼

yhj20041128001發表於2011-05-28
監控硬碟空間指令碼(請指正)

近日,公司要求對磁碟空間進行監控,編寫了以下指令碼。希望大家指正,並且幫助我完善此指令碼。

目標:
監控20臺Linux伺服器的硬碟空間,如果超過一定限度,發報警信給系統管理員.

思路:
指令碼分為兩大部分
1. client
功能:
收集資料: chkfs-client.sh (將每臺機器的資料定時蒐集到指定路徑)
上傳資料: autoftp.sh (定時上傳到monitor server)

2.server
功能:
分析資料:chkfs-server.sh (分析各臺主機傳送來的資料)
傳送報警信:send_msg (超過threshold時,自動發信)

chkfs-client.sh

#!/bin/bash
host=`uname -n`
m=`date +%m`
d=`date +%d`
log=/tmp/monitor/$host.df.${m}${d}.txt

rm -f /tmp/monitor/*.df.*.txt

df -k |grep -i -v "Use" |awk {'print $5$6'} > $log


autoftp.sh

#!/bin/bash
#Purpose: upload files to the monitor server
#Author: Kevin Zhang (kevincszhang at gmail dot com)
#Licence: GPL
#History:
#/11/10/2007: 1st edition

user=upload
passwd=upload

ftp -n << !
open ip_address_of_monitor_server
user $user $passwd
binary
prompt off
lcd /tmp/monitor
cd /tmp/monitor/chkdu
put *.du.*
cd /tmp/monitor/chkfs
put *.df.*
close
bye
!

chkfs-server.sh

#!/bin/bash
#Purpose: Periodically check the file system of server          #
#Author: Kevin Zhang ( kevincszhang at gmail dot com)                   #
#Licence: GPL                                                           #
#History:                                                               #
#/11/10/2007: 1st edition                                               #
#########################################################################

bin=/root/sysadmin/server/chkfs
log=/tmp/monitor/chkfs
file=${log}/grcntj0
usedfs=${log}/usedfs.txt
date=`date +%m-%d-%H`

#### checking whether old file exists. if so, remove it.

(
if [ -f ${log}/chkfs.alarm.${date}.txt ]; then
   rm -f ${log}/chkfs.alarm.${date}.txt
fi

#### starting to check file systems

cc=1
while [ $cc -le 10 ]
do

    if [ -f ${file}${cc}.df.${date}.txt ]; then
      cat ${file}${cc}.df.${date}.txt > ${usedfs}
      l=`cat ${bin}/filesystem.txt |wc -l`
      c=1
       while [ $c -le $l ]
       do
         fs=`awk 'NR == i {print $1}' i=$c ${bin}/filesystem.txt`
         b_limit=`awk 'NR == i {print $2}' i=$c ${bin}/filesystem.txt`
         b_used=`cat ${usedfs} |grep "${fs}$" |awk 'BEGIN {FS = "%"} {print $1}'`
         host=`echo "${file}${cc}" | cut -c 20-`

           if [ ${b_used} -ge ${b_limit} ]; then
           echo -e "\n\nWARNING - block usage of filesystem $fs on host $host is now ${b_used}%. It has exceeded the ${b_limit}% threshold. \n\nATTENTION IT team:\nPls check it immediately.\nThank You.\n\n\n" >> ${log}/chkfs.alarm.${date}.txt
           fi
        c=` expr $c + 1 `
       done
    fi
cc=` expr $cc + 1`
done

rm -f ${log}/*.df.*
rm -f ${usedfs}

#### sending alarm message to the corresponding sysadmin

TO="123@163.com, [email]234@163.com[/email]"
SUBJECT="Filesystem Alarm - IT team, pls take action!"
MESSAGE="${log}/chkfs.alarm.${date}.txt"
CC="567@163.com"
BCC=""
if [ -s ${log}/chkfs.alarm.${date}.txt ]; then
   echo "send alarm information to system administrator."
   ${bin}/send_msg "$TO" "$SUBJECT" "$MESSAGE" "$CC" "$BCC"
fi

) > ${log}/chk_filesystem.${date}.log 2>&1
     
                                   
send_msg

#SENDMAIL="/usr/lib/sendmail -oi -t"
#SENDMAIL="/usr/bin/mailx"
#hostname="$6"
#FROM="$hostname"
SUBJECT="$2"
TO="$1"
MESSAGE="$3"
CC="$4"
BCC="$5"
#
/bin/cat ${MESSAGE} | /usr/bin/mailx "$TO" -s "$SUBJECT" -c "$CC" -b "$BCC"


filesystem.txt 設定threshold
/ 80
/home 80
/usr 80
/var 80

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23757700/viewspace-696540/,如需轉載,請註明出處,否則將追究法律責任。

相關文章