dba 监控脚本

    技术2022-05-19  23

    dba 监控脚本 

     

    #! /usr/bin/ksh

    定义脚本中用到的局部变量.这些变量主要是方便程序的移植性.

    # dba列表如果是多个用户必须用双引号括起来并用空格分开.

    DBALIST="leishifei@gmail.com"; export DBALIST

    实际程序执行路径中间由于乱跑了几次所以需要这个路径回到执行文件夹

    WORKPATH=/home/oracle/DBAmonitor; export WORKPATH

    # alert数据源存储路径可以登录sqlplus后执行show parameter dest得到.

    ALERTPATH=/oracle/admin/dw/bdump; export ALERTPATH

    最终发送给客户的信息存储位置

    filename=monitor_result.log; export filename 

    可以从tnsnames中看到

    ORASID=dw; export ORASID

    #  报表头 

    #  首先监听处理的历史文件实际上这里还有其他的更好办法后期优化调整.

    #  对结果文件的删除是冗余的可以不处理

    rm -f lsnr.exist

    rm -f $filename 

    echo "********the monitor log ***********" >$filename

    echo "********create by zhanglei ********" >>$filename

    监控监听是否启动如果没启动 则发出警告信息.

    # test the listener

    echo "************monitor the listener*********">>$filename

    ps -ef | grep tnslsnr |grep -v grep >lsnr.exist

    if [ -s lsnr.exist ]

    then 

    echo "listener lsnrctl on `hostname` is runing...">>$filename 

    else

    echo  "Listener tnslsnr on `hostname` is down" $DBALIST

    fi

    监控alert_sid文件中是否有错误提示内容如果报表显示有错误则需要对应处理.

    # monitor the alert.log

    echo "" >>$filename

    echo "">>$filename

    echo "****** monitor the alert.log ********">>$filename

    cd $ALERTPATH

    if [ -f alert_$ORASID.log ]

    then

    mv alert_$ORASID.log alert_work.log

    touch alert_$ORASID.log

    cat alert_work.log>>alert_`date`.hist

    grep ORA-  alert_work.log>>$WORKPATH/$filename

    fi

    cd $WORKPATH

    监控数据库中的tablespace, latch, lock, cpu&memory等信息

    这里有个对应的sql文件,在文件monitor.sql

    echo "">>$filename

    echo "">>$filename

     

    #sqlplus /<<!

    #connect / as sysdba

    sqlplus -s /nolog<<!

    connect / as sysdba

    @monitor.sql

    exit

    !

    判断结果是否有内容如果有内容的话则发送给dba

    if [ `cat monitor.log.list|wc -l` -gt 0 ]

    then

    cat monitor.log.list >>$filename

    fi

    #monitor the cpu

    echo "******* monitor the system IO********">>$filename

    iostat -s>>$filename

    echo "********monitor the paging space *********">>$filename

    lsps -as>>$filename

    #echo "***********monitor the cpu used *********">>$filename

    #vmstat 1 3>>$filename

    mail -s "DBA Monitor----`date`" $DBALIST<$filename

     

    附录1 monitor.sql文件内容.

    Rem 

    Rem $Header: jaxmonitor.sql, v1.0 2009/07/27 

    Rem

    Rem Copyright(c) 2009 by jaxzhang

    Rem

    set feed off

    set linesize 100

    set pagesize 200

    Rem

    Rem Monitor the tablespace free space

    Rem

    spool /home/oracle/DBAmonitor

    set heading off

    select '

    *********   monitor the free space of tablespace *********' from dual;

    set heading on

    SELECT F.TABLESPACE_NAME,

           (T.TOTAL_SPACE - F.FREE_SPACE) "USED (MB)",

           F.FREE_SPACE "FREE (MB)",

           T.TOTAL_SPACE "TOTAL (MB)",

           (ROUND((F.FREE_SPACE / T.TOTAL_SPACE) * 100)) ||  '% ' PER_FREE

      FROM (SELECT TABLESPACE_NAME,

                   ROUND(SUM(BLOCKS *

                             (SELECT VALUE / 1024

                                FROM V$PARAMETER

                               WHERE NAME = 'db_block_size') / 1024)) FREE_SPACE

              FROM DBA_FREE_SPACE

             GROUP BY TABLESPACE_NAME) F,

           (SELECT TABLESPACE_NAME, ROUND(SUM(BYTES / 1048576)) TOTAL_SPACE

              FROM DBA_DATA_FILES

             GROUP BY TABLESPACE_NAME) T

     WHERE F.TABLESPACE_NAME = T.TABLESPACE_NAME

       AND (ROUND((F.FREE_SPACE / T.TOTAL_SPACE) * 100))<40;

    Rem

    Rem Monitor the error log in the etl procedure

    Rem

    set heading off

    select '

    *********  monitor the error log in the etl procedure *********' from dual;

    set heading on

    select 日志等级日志信息 from ctl.run_log t

    where t.日志时间 > trunc(sysdate) and t.日志等级 = '异常';

    Rem

    Rem Monitor the buffer cache 

    Rem

    set heading off

    select '

    *********  monitor the buffer cache get rate. percent 95 is musted  *********' from dual;

    set heading on

    select 1 - ((physical.value - direct.value - lobs.value) / logical.value) "Buffer Cache Hit Ratio"

      from v$sysstat physical,

           v$sysstat direct,

           v$sysstat lobs,

           v$sysstat logical

     where physical.name = 'physical reads'

       and direct.name = 'physical reads direct'

       and lobs.name = 'physical reads direct (lob)'

       and logical.name = 'session logical reads';

    Rem

    Rem Monitor the librarycache cache 

    Rem

    set heading off

    select '

    ********* monitor the librarycache reparsing is said the hard parsing rate *********' from dual;

    set heading on

        select namespace,

           sum(pins) pins,

           sum(pinhits) pinhits,

           sum(reloads) reloads,

           sum(invalidations) invalidations,

           100-(decode(sum(pins),0,1,sum(pinhits)/sum(pins))) *100 reparsing

     from v$librarycache ttt

     group by rollup(ttt.NAMESPACE )

     having sum(pins) > 0

     order by grouping(ttt.NAMESPACE), reparsing desc;

    Rem

    Rem Monitor the librarycache cache 

    Rem

    set heading off

    select '

    ********* monitor the wait thing by session *********' from dual;

    set heading on

     select event,

           sum(decode(wait_Time, 0, 0, 1)) "Prev",

           sum(decode(wait_Time, 0, 1, 0)) "Curr",

           count(*) "Tot"

      from v$session_wait

     group by event

     order by 4;

    spool off

     

    附录2, crontab设定程序运行时间

    cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。由于Cron Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:

     

    /sbin/service crond start //启动服务 

    /sbin/service crond stop //关闭服务 

    /sbin/service crond restart //重启服务 

    /sbin/service crond reload //重新载入配置 

     

    你也可以将这个服务在系统启动的时候自动启动: 

    /etc/rc.d/rc.local这个脚本的末尾加上: 

    /sbin/service crond start 

     

    现在Cron这个服务已经在进程里面了,我们就可以用这个服务了,Cron服务提供以下几种接口供大家使用: 

     

    1、直接用crontab命令编辑 

     

    cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明: 

     

    crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数 

    crontab -l //列出某个用户cron服务的详细内容 

    crontab -r //删除某个用户的cron服务 

    crontab -e //编辑某个用户的cron服务 

     

    这个格式的前一部分是对时间的设定,后面一部分是要执行的命令,如果要执行的命令太多,可以把这些命令写到一个脚本里面,然后在这里直接调用这个脚本就可以了,调用的时候记得写出命令的完整路径。时间的设定我们有一定的约定,前面五个*号代表五个数字,数字的取值范围和含义如下: 

     

    分钟 (0-59) 

    小時 (0-23) 

    日期 (1-31) 

    月份 (1-12) 

    星期 (0-6//0代表星期天 

     

    除了数字还有几个个特殊的符号就是"*""/""-"","*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。以下举几个例子说明问题: 

     

    每天早上6点 

     

    0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都emailroot的信箱了。 

     

    每两个小时 

     

    0 */2 * * * echo "Have a break now." >> /tmp/test.txt 

     

    晚上11点到早上8点之间每两个小时,早上八点 

     

    0 23-7/28 * * * echo "Have a good dream:)" >> /tmp/test.txt 

     

    每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 

     

    0 11 4 * 1-3 command line 

     

    11日早上4点 

     

    0 4 1 1 * command line 

     

    每次编辑完某个用户的cron设置后,cron自动在/var/spool/cron下生成一个与此用户同名的文件,此用户的cron信息都记录在这个文件中,这个文件是不可以直接编辑的,只可以用crontab -e 来编辑。cron启动后每过一份钟读一次这个文件,检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务。 

     

    2、编辑/etc/crontab 文件配置cron 

     

    cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用 cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是: 

     

    SHELL=/bin/bash 

     

    PATH=/sbin:/bin:/usr/sbin:/usr/bin 

     

    MAILTO=root      //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 

     

    HOME=/    //使用者运行的路径,这里是根目录 

     

    # run-parts 

     

    01 * * * * root run-parts /etc/cron.hourly //每小时执行/etc/cron.hourly内的脚本 

    02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本

    22 4 * * 0 root run-parts /etc/cron.weekly //每星期执行/etc/cron.weekly内的脚本 

    42 4 1 * * root run-parts /etc/cron.monthly //每月去执行/etc/cron.monthly内的脚本 

    大家注意"run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了。

     

    基本格式 :

    *  *  *  *  *  command

    分 时 日 月 周 命令

     

    1列表示分钟159 每分钟用*或者 */1表示

    2列表示小时1230表示0点)

    3列表示日期131

    4列表示月份112

    5列标识号星期060表示星期天)

    6列要运行的命令

     

    crontab文件的一些例子:

     

    30 21 * * * /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每晚的21:30重启lighttpd 

     

    45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每月11022日的4 : 45重启lighttpd 

     

    10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每周六、周日的1 : 10重启lighttpd 

     

    0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示在每天18 : 0023 : 00之间每隔30分钟重启lighttpd 

     

    0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart

    上面的例子表示每星期六的11 : 00 pm重启lighttpd 

     

    * */1 * * * /usr/local/etc/rc.d/lighttpd restart

    每一小时重启lighttpd 

     

    * 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart

    晚上11点到早上7点之间,每隔一小时重启lighttpd 

     

    0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart

    每月的4号与每周一到周三的11点重启lighttpd 

     

    0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart

    一月一号的4点重启lighttpd 

     

     

     

     


    最新回复(0)