脚本 : DiskCheck
用途 : 检查磁盘空间使用情况
适用 : 文件 服务器
功能 : 在config中设定允许的最大百分比,每天增长最大百分比,;如果有超出设定范围的,则会发mail通知相关人员。
前提 : 配置邮件服务器地址
====================================
DiskCheck.csh
代码:
#!/bin/csh
# ------------------------------------
# Check Disk Space and Send Mail to User
#
# ------------------------------------

# ----Declare variables--------------
set progpath = $0
set sourcedir = $progpath:h
set a = `grep "server:" $sourcedir/DiskCheck.config`
set servername = $a[2]
set a = `grep "maxused:" $sourcedir/DiskCheck.config`
set maxused = $a[2]
set b = `grep "maxincrease:" $sourcedir/DiskCheck.config`
set maxincrs = $b[2]
set mails = `grep "mailto:" $sourcedir/DiskCheck.config`
set b = `grep "mailday:" $sourcedir/DiskCheck.config`
set mailday = $b[2]
set UsedFlag = "F"
set IncrsFlag = "F"

#-----Check OS and write data to new log file-------
set mOS = `uname`
if ( $mOS == "HP-UX" ) then
bdf > $sourcedir/DiskCheck.log.new
else
df -k > $sourcedir/DiskCheck.log.new
endif

#---Check if Current day have to send mail------
set a = `date`
if ( $a[1] == $mailday ) then
foreach mailto($mails)
#----check if $mailto is a validate mail address----
set flag = `echo $mailto|awk -F@ '{print NF-1}'`
if !( $flag == "0" ) then
mailx -s $servername" Disk Usage Ratio Check (weekly)" $mailto < $sourw
endif
end
endif

#--set allused = `awk '{print $5}' $sourcedir/DiskCheck.log.new`
set allused = `awk -F"%" '{print $1}' $sourcedir/DiskCheck.log.new | awk '{prin`
#-----Check maxused disk space and send mail------
set index = 2
while ( $index <= $#allused )
#----check max value----
if ( $allused[$index] >= $maxused ) then
set UsedFlag = "T"
endif
@ index = $index + 1
end

if ( $UsedFlag == "T" ) then
foreach mailto($mails)
#----check if $mailto is a validate mail address----
set flag = `echo $mailto|awk -F@ '{print NF-1}'`
if !( $flag == "0" ) then
mailx -s $servername" Disk Usage Ratio Alarm Mail" $mailto < $sourcedirw
endif
end
endif

#------Check Increasing speed and send mail-----
#-----Check old log exist-----------------------
if ( -e $sourcedir/DiskCheck.log ) then
set oldused = `awk -F"%" '{print $1}' $sourcedir/DiskCheck.log | awk '{print`
set index = 2
while ( $index <= $#oldused )
#-----Check increasing speed-----
@ incrs = $allused[$index] - $oldused[$index]
if ( $incrs >= $maxincrs ) then
set IncrsFlag = "T"
endif
@ index = $index + 1
end

if ( $IncrsFlag == "T" ) then
foreach mailto($mails)
#----check if $mailto is a validate mail address----
set flag = `echo $mailto|awk -F@ '{print NF-1}'`
if !( $flag == "0" ) then
mailx -s $servername" Disk Usage Ratio Increasing Alarm Mail" $mailw
endif
end
endif
endif
#------replace old log with the new one-----
\mv $sourcedir/DiskCheck.log.new $sourcedir/DiskCheck.log