脚本的思路:
1.环境设定:
在Oracle用户的$HOME目录下,创建cpu_alarm,pid,output三个目录。
1.1 考虑到有些主机上可能有多个sid的情况,因此,以Oracle用户登陆主机,先通过以下命令:
$ps -ef | grep ora_smon
获取当前主机上所安装的oracle。并将sid名称放在ora.txt文件中。
2.脚本内容 cpu_monitor.ksh:
#!/usr/bin/ksh
#-----------------------------------------------------
#Enviroment setting
#-----------------------------------------------------
export ORACLE_HOME=/u01/app/oracle/PRoduct/9.2.0
export PATH=$ORACLE_HOME/bin:$PATH
export HOME=/u79/home/oracle
#-----------------------------------------------------
for j in `cat $HOME/cpu_alarm/ora.txt`
##在ora.txt中循环获取sid名字
do
##判断cpu的百分比是否>1,如果>1将其取出
for i in `ps aux |grep -E "^oracle|' oracle'"| grep $j
| awk '{if ($3 ~/[0-9]/ && $3 > 1) print $2}'`
do
##找到cpu>1的进程后,过30秒看进程cpu是否仍然>1
if [ `ps aux|grep $i| awk '{printf "%d", $3}'` -gt "1" ]
then
sleep 30 #sleep 30 =》 30 seconds
if [ `ps aux |grep $i | awk '{printf "%d", $3}'` -gt "0.5" ]
then
echo $i > $HOME/cpu_alarm/pid/$j ##若30s后仍然>1,将进程号写
入$HOME/cpu_alarm/pid目录下以sid命名的文件中。
fi
fi
done
done
cd $HOME/cpu_alarm/pid
##$HOME/cpu_alarm/pid目录下,列出所有sid名,并将这些文件名中的
sid号循环代入根据pid号获得sql的sql语句中,并将所产生的sql语句
产生到 for k in `ls -1` do echo "export ORACLE_SID=${k}" echo "set markup html on spool on" >> $HOME/cpu_alarm/output/$k.sql TIME=`date +%Y%m%d%H%M` echo "spool $HOME/cpu_alarm/output/${k}_${TIME}.html" >> $HOME/cpu_alarm/output/$k.sql for t in `cat $k` do echo "SELECT to_char(a.logon_time,'yyyy-mm-dd hh24:mi:ss') as "LONGON_TIME",a.username,a.machine,a.program,a.sid,a.serial#,a.sta tus,c.piece,c.sql_text FROM v$session a,v$process b, v$sqltext c WHERE b.spid=$t AND b.addr=a.paddr AND a.sql_address=c.address(+) ORDER BY a.sid,c.piece;" >> $HOME/cpu_alarm/output/$k.sql echo "set markup html off spool off" >> $HOME/cpu_alarm/output/$k.sql export ORACLE_SID=${k} echo "exit" >> $HOME/cpu_alarm/output/$k.sql sqlplus "/ as sysdba" @$HOME/cpu_alarm/output/${k}.sql rm $HOME/cpu_alarm/output/*.sql uuencode $HOME/cpu_alarm/output/${k}_$ {TIME}.html $HOME/cpu_alarm/output/${k}_$ {TIME}.html | mail -s "${k}_cpu_alarm" abc@abc.com ##注意:由于spool出来的结果是html文件,因此,要用uuencode file1 file1 |mail -s ...的格式去发附件。
新闻热点
疑难解答