监控硬盘空间脚本(请指正)
近日,公司要求对磁盘空间进行监控,编写了以下脚本。希望大家指正,并且帮助我完善此脚本。
目标:
监控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
近日,公司要求对磁盘空间进行监控,编写了以下脚本。希望大家指正,并且帮助我完善此脚本。
目标:
监控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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23757700/viewspace-696540/