本文介绍了8个常用的监控数据shell脚本。首先回顾了一些dba常用的unix命令,以及解释了如何通过unix cron来定时执行dba脚本。网上
本文介绍了8个常用的监控数据shell脚本。首先回顾了一些dba常用的unix命令,以及解释了如何通过unix cron来定时执行dba脚本。网上也有好多类似的文章,但基本上都不能正常运行,花点时间重新整理了下,以后就能直接使用了。
一.同时文章还介绍了8个重要的脚本来监控oracle数据库:
1.检查实例的可用性
2.检查监听器的可用性
3.检查alert日志文件中的错误信息
4.在存放log文件的地方满以前清空旧的log文件
5.分析table和index以获得更好的性能
6.检查表空间的使用情况
7.找出无效的对象
8.监控用户和事务
二.dba需要的unix基本知识
基本的unix命令,以下是一些常用的unix命令:
ps--显示进程
grep--搜索文件中的某种文本模式
mailx--读取或者发送mail
cat--连接文件或者显示它们
cut--选择显示的列
awk--模式匹配语言
df--显示剩余的磁盘空间
以下是dba如何使用这些命令的一些例子:
1. 显示服务器上的可用实例:
$ ps -ef| grep smon
oracle 22086 1 0 02:32:24 ? 0:04 ora_smon_pprd10
oracle 5215 28972 0 08:10:19 pts/4 0:00 grep smon
2. 显示服务器上的可用监听器:
$ ps -ef grep listener grep -v grep
(grep命令应该加上-i参数,即grep -i listener,该参数的作用是忽略大小写,因为有些时候listener是大写的,,这时就会看不到结果)
$ ps -ef|grep -i listener
oracle 9655 1 0 mar 12 ? 0:01 /data/app/oracle/9.2.0/bin/tnslsnr listener -inherit
oracle 22610 1 0 02:45:02 ? 0:02 /data/app/oracle/10.2.0/bin/tnslsnr listener -inherit
oracle 5268 28972 0 08:13:02 pts/4 0:00 grep -i listener
3. 查看oracle存档目录的文件系统使用情况
$ df -k | grep /data
/dev/md/dsk/d50 104977675 88610542 15317357 86% /data
4. 统计alter.log文件中的行数:
$ cat alert_pprd10.log | wc -l
13124
$ more alert_pprd10.log | wc -l
13124
5. 列出alert.log文件中的全部oracle错误信息:
$ grep ora-* alert.log
ora-00600: internal error code, arguments: [kcrrrfswda.1], [], [], [], [], []
ora-00600: internal error code, arguments: [1881], [25860496], [25857716], []
6. crontab基本
一个crontab文件中包含有六个字段:
分钟 0-59
小时 0-23
月中的第几天 1-31
月份 1 - 12
星期几 0 - 6, with 0 = sunday
7. unix命令或者shell脚本
要编辑一个crontab文件,输入: crontab -e
要查看一个crontab文件,输入: crontab -l
0 4 * * 5 /dba/admin/analyze_table.ksh
30 3 * * 3,6 /dba/admin/hotbackup.ksh /dev/null 2>&1
在上面的例子中,第一行显示了一个分析表的脚本在每个星期5的4:00am运行。第二行显示了一个执行热备份的脚本在每个周三和周六的3:00a.m.运行。
三.监控数据库的常用shell脚本
以下提供的8个shell脚本覆盖了dba每日监控工作的90%,你可能还需要修改unix的环境变量。
1. 检查oracle实例的可用性
oratab文件中列出了服务器上的所有数据库
$ cat /var/opt/oracle/oratab
#
# this file is used by oracle utilities. it is created by root.sh
# and updated by the database configuration assistant when creating
# a database.
# a colon, ':', is used as the field terminator. a new line terminates
# the entry. lines beginning with a pound sign, '#', are comments.
#
# entries are of the form:
# $oracle_sid:$oracle_home::
#
# the first and second fields are the system identifier and home
# directory of the database respectively. the third filed indicates
# to the dbstart utility that the database should , y, or should not,
# n, be brought up at system boot time.
#
# multiple entries with the same $oracle_sid are not allowed.
#
#
# *:/data/app/oracle/9.2.0:n
trng:/data/app/oracle/9.2.0:y
*:/data/app/oracle/9.2.0:n
pprd:/data/app/oracle/10.2.0:y
pprd10:/data/app/oracle/10.2.0:n
以下的脚本检查oratab文件中列出的所有数据库,并且找出该数据库的状态(启动还是关闭)
###################################################################
## ckinstance.ksh ##
###################################################################
oratab=/var/opt/oracle/oratab
echo `date`
echo oracle database(s) status `hostname` :\n
db=`egrep -i :y|:n $oratab | cut -d: -f1 | grep -v \# | grep -v \*`
pslist=`ps -ef | grep pmon`
for i in $db ; do
echo $pslist | grep ora_pmon_$i > /dev/null 2>$1
if (( $? )); then
echo oracle instance - $i: down
else
echo oracle instance - $i: up
fi
done
使用以下的命令来确认该脚本是可以执行的:
$ chmod 744 ckinstance.ksh
$ ls -l ckinstance.ksh
-rwxr--r-- 1 oracle dba 657 mar 5 22:59 ckinstance.ksh
以下是实例可用性的报表:
$ sh ckinstance.ksh
wed may 13 12:51:20 pdt 2009
oracle database(s) status gambels :
oracle instance - pprd: up
oracle instance - pprd10: up
2. 检查oracle监听器的可用性
以下有一个类似的脚本检查oracle监听器。假如监听器停了,该脚本将会重新启动监听器:
#####################################################################
## cklsnr.sh ##
#####################################################################
#!/bin/ksh
tns_admin=/var/opt/oracle; export tns_admin
oracle_sid= pprd10; export oracle_sid
oraenv_ask=no; export oraenv_ask
path=$path:/bin:/usr/local/bin; export path
. oraenv
dbalist=www.linuxidc.com,;export dbalist
cd /var/opt/oracle
rm -f lsnr.exist
ps -ef | grep pprd10 | grep -v grep > lsnr.exist
if [ -s lsnr.exist ]
then
echo
else
echo alert | mailx -s listener 'pprd10' on `hostname` is down $dbalist
lsnrctl start pprd10
fi
3. 检查alert日志(ora-xxxxx)
####################################################################
## ckalertlog.sh ##
####################################################################
#!/bin/ksh
editor=vi; export editor
oracle_sid=pprd10; export oracle_sid
oracle_base=/data/app/oracle; export oracle_base
oracle_home=$oracle_base/10.2.0; export oracle_home
ld_library_path=$oracle_home/lib; export ld_library_path
tns_admin=/var/opt/oracle;export tns_admin
nls_lang=american; export nls_lang
nls_date_format='mon dd yyyy hh24:mi:ss'; export nls_date_format
oratab=/var/opt/oracle/oratab;export oratab
path=$path:$oracle_home:$oracle_home/bin:/usr/ccs/bin:/bin:/usr/bin:/usr/sbin:/sbin:/usr/openwin/bin:/opt/bin:.; export path
dbalist=www.linuxidc.com,;export dbalist
cd $oracle_base/admin/pprd10/bdump
if [ -f alert_pprd10.log ]
then
mv alert_pprd10.log alert_work.log
touch alert_pprd10.log
cat alert_work.log >> alert_pprd10.hist
grep ora- alert_work.log > alert.err
fi
if [ `cat alert.err | wc -l` -gt 0 ]
then
mailx -s pprd10 oracle alert errors $dbalist fi
rm -f alert.err
rm -f alert_work.log