Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1502884
  • 博文数量: 297
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 3082
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 11:36
文章分类

全部博文(297)

文章存档

2011年(1)

2009年(45)

2008年(67)

2007年(184)

我的朋友

分类: LINUX

2007-12-05 12:31:28

思路:
  1. 定义机器列表文件hostlists
  2. 使用saru.sh自动rlogin到各机器上使用sar -u 60 180(间隔和次数选项可自定义)
  3. 把各机器的统计结果各自存放
  4. 从各机器的统计结果中取出最后的平均值放入简明日志中
注:以下脚本在HP-UX上通过,hp-ux的bash为/sbin/sh,sun-solaris为/sbin/bash.

# cat ./saru.sh

#!/sbin/sh
#define logfile&hostlists
hostlists="/etc/chk/hosts.sar-u.lst"
if [ ! -d /etc/chk/log/`date +%Y%m%d` ];then
        mkdir -p /etc/chk/log/`date +%Y%m%d`
fi
logfile=/etc/chk/log/`date +%Y%m%d`/sar-u.log                 
cat /dev/null >$logfile   

#monitor the check-logfile in-real-time
#tail -f $logfile &

#define shell-script's option
if [ "$1" = "" ]
then
        intv=60
else
        intv=$1
fi
if [ "$2" = "" ]
then
        counts=180
else
        counts=$2
fi
if [ "$3" = "" ]
then
        sleeptimes=60
else
        sleeptimes=$3
fi

#main shell-script begin
for i in `cat $hostlists | grep -v \#`
do
#clear old-checking-log of today
        if [ -f $logfile.$i ]
        then
                cat /dev/null >$logfile.$i
        else
                touch $logfile.$i
        fi
#begin to check item
        echo "check $i"
        (sleep 10;echo "sar -u $intv $counts &";sleep $(((intv+intv/10+1)*(counts+10)));) | rlogin $i | egrep "^$i|Average|:|idle" >>$logfile.$i &
done

#monitor if check-log-file has been finished
while :;do
        working="done"
        for i in `cat $hostlists | grep -v \#`
        do
                if [ ! -f $logfile.$i ];then
                        working="nofile"
                elif [ `ls -l $logfile.$i | awk '{print $5}'` -eq 0 ];then
                        working="zero"
                fi
        done
        if [ "$working" = "done" ];then
                break
        fi
        sleep $sleeptimes
done

#get brief-result from detail check-log-file for per-host
sleep 5
for i in `cat $hostlists | grep -v \#`
do
        cat $logfile.$i | egrep "sar|Average|idle" >>$logfile
        echo >>$logfile
done

#exit and kill the tail-f process
echo ""
echo "the host in $hostlists has not been checked:"
echo `cat $hostlists | grep \#`
echo "all the sar-d brief-result has been saved to $logfile."
echo "for detail result,please watch $logfile.hostname file."
#kill `ps -f -u root | grep "tail -f $logfile" | awk '{print $2}'` >/dev/null

exit 0

用法:
saru.sh [intervals] [counts] [sleeptimes]
        note:
          default value:  interval=30,counts=60,sleeptimes=60
          sleeptimes:the interval-times,
          to check if detail-check-logfile has been finished.
          you must be sure that the machine in the hostlist-file
          can be logined by use "rlogin" command.   
        example:
        ./saru.sh 30 60 &
        ./saru.sh 5 10 10 &
        nohup ./saru.sh &




 原文地址 http://yuhuohu.cublog.cn
阅读(1261) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~