linux系统整体监控脚本

来源:互联网 发布:上海工资计算软件 编辑:程序博客网 时间:2024/05/18 02:36

author:skate
time :2009/03/10


 

 os平台:centos 4.7


作为DBA要时刻注意系统的整体状况,我们不能人为的一直监控系统
这就需要DBA要写一些日常的监控脚本,帮助DBA分析问题,我们最近

要上个新的系统,我于是写了linux系统的整体监控脚本

 

 

monilog_timeswitch.sh :日志轮询脚本,保留最近5个监控日志,定期删除最陈旧的监控日志
moni_system.sh        :linux 系统整体监控脚本

 

功能:监控系统的资源使用信息,及负载信息

使用方法:把这两个脚本做成定时任务


例如:
[root@ticket-A sh]# crontab -l
*/10 * * * * sh /home/oracle/sh/moni_system.sh >> moni_system.log
00 * * * * sh /home/oracle/sh/monilog_timeswitch.sh >> monilog_timeswitch.sh.log

 

 

脚本内容如下:

 

 

[root@ticket-A sh]# more moni_system.sh
#!/bin/bash

########################################################################
#This scripts is checking your host system: the cpu ,i/o,mem,network,
#processes stat.
#  vision 1.0 Bate
#  Write by skate
#If you have some advise about it ,you can mail :810312zxg@163.com
########################################################################

cd /home/oracle/sh

#make the local language is chinese
#export LANG=zh_CN


removedate=`date +%d`
curdate=`date +%H`
logdir=/home/oracle/sh/log
logcpuuse=$logdir/cpuuse.log
logcpuload=$logdir/cpuload.log
logioload=$logdir/ioload.log
lognetworkload=$logdir/networkload.log
logsysprocess=$logdir/sysprocess.log
logmemuse=$logdir/memuse.log
lognetsession=$logdir/netsession.log


################################
#统计cpu的使用率信息
################################

#判断文件是否存在
if [ -f $logcpuuse ]
then
#echo "logfile is exist !"
if [ "$curdate" == "23" ]
then
date +"%D %r" >>$logcpuuse
sar -u 2 1 |grep all |head -1 >> $logcpuuse
else
sar -u 2 1 |grep all |head -1 >> $logcpuuse
fi
else
#touch $logfile

date > $logcpuuse
sar -u 2 2 | grep CPU >> $logcpuuse

fi

###############################
#统计cpu的负载信息
###############################

if [ -f $logcpuload ]
then
#echo "logfile is exist !"

uptime  >> $logcpuload
else
#touch $logfile
date > $logcpuload
uptime >> $logcpuload
fi


##############################
#磁盘io的负载的信息
##############################

if [ -f $logioload ]
then
date +"%D %r" >> $logioload
iostat -xd 1 1 | sed -n '2,$p' >> $logioload

else
date +"%D %r" > $logioload
iostat -xd 1 1 | sed -n '2,$p' >> $logioload
fi


#################################
#统计网络负载的信息
################################

if [ -f $lognetworkload ]
then
date +"%D %r" >> $lognetworkload
sar -n DEV 1 3| grep eth0 >> $lognetworkload

else
date +"%D %r" > $lognetworkload
sar -n DEV 1 3|head -3 |tail -1 >> $lognetworkload
sar -n DEV 1 3| grep eth0 >> $lognetworkload
fi

###################################
#统计系统进程总数信息
###################################
if [ -f $logsysprocess ]
then
date +"%D %r" >> $logsysprocess
sysnum=`ps aux | wc -l`
oranum=` ps -ef |awk '/ora/&&/dbticket/' |wc -l`
echo "processes of system is: $sysnum" >> $logsysprocess
echo "processes of oracle is: $oranum" >> $logsysprocess
else

date +"%D %r" > $logsysprocess
sysnum=`ps aux | wc -l`
oranum=` ps -ef |awk '/ora/&&/dbticket/' |wc -l`
echo "processes of system is: $sysnum" >> $logsysprocess
echo "processes of oracle is: $oranum" >> $logsysprocess


fi

##############################
#统计内存的信息
##############################
if [ -f $logmemuse ]
then
date +"%D %r" >> $logmemuse
free |grep -1 Mem |head -n 2|tail -1 >> $logmemuse
free | grep Swap >> $logmemuse
else
date +"%D %r" > $logmemuse
free |grep -1 Mem |head -n 2 >> $logmemuse
free | grep Swap >> $logmemuse
fi


###############################
#查看网络连接数,tcp协议
###############################
if [ -f $lognetsession ]
then
date +"%D %r" >> $lognetsession
netstat -an | grep -E "^(tcp)" | cut -c 74- | sort | uniq -c | sort -n >> $lognetsession
else
date +"%D %r" >> $lognetsession
netstat -an | grep -E "^(tcp)" | cut -c 74- | sort | uniq -c | sort -n >> $lognetsession
fi


##############################
#定期清除统计信息
#############################

if [ "$removedate" == "01" ]
then

tail -1000 $logcpuuse > $logcpuuse_bak
tail -1000 $logcpuload > $logcpuload_bak
tail -1000 $logioload > $logioload_bak
tail -1000 $lognetworkload > $lognetworkload_bak
tail -1000 $logsysprocess > $logsysprocess_bak
tail -1000 $logmemuse >  $logmemuse_bak
tail -1000 $lognetsession > $lognetsession_bak

rm -rf $logcpuuse $logcpuload $logioload $lognetworkload $logsysprocess $logmemuse $lognetsession

fi

exit 0

 

 

 

 

[root@ticket-A sh]# vi monilog_timeswitch.sh

#!/bin/bash

###################################################
#time    : 2009/03/10
#author  :skate
#desc    :auto switch log ,keep lasted five logfile
#         the lasted logfile of order is: 04,03,02,01
#####################################################

cd /home/oracle/sh/log

DIR=/home/oracle/sh/log
for file in `ls $DIR |grep -v "[0-9]"`
do
  echo $file
  size=`ls -l $file |awk '{print $5}'`
  echo $size
  if [ $size -gt 100 ]
  then
    if [ -f "$file"_01 ]
    then
       if [ -f "$file"_02 ]
       then
          if [ -f "$file"_03 ]
          then
            if [ -f "$file"_04 ]
            then
              mv -f "$file"_02  "$file"_01
              mv -f "$file"_03  "$file"_02
              mv -f "$file"_04  "$file"_03
              renamefile="$file"_04
            else
            renamefile="$file"_04
            fi
          else
            renamefile="$file"_03
          fi
       else
         renamefile="$file"_02
       fi
    else
      renamefile="$file"_01
    fi
    mv $file $renamefile
  fi

done

 

 

这个脚本只是收集了统计信息,还没有做到去分析统计信息,当达到预计的阀值就报警给相关负责人

 

---续---

 

 

原创粉丝点击