IT技术博客大学习 共学习 共进步

tbstat:实时监控数据库统计状态的小工具

NinGoo.net 2010-01-13 14:07:17 浏览 2,901 次

用perl写了一个简单的工具,用于实时查看数据库的统计状态信息,展现信息主要来源于Oracle数据字典中v$systat和v$system_event。写这个工具的初衷,是因为目前我们对于数据库的监控,更多的是分钟级别抽样的数据来绘制的图形,粒度相对还比较粗,有一些比较深的问题,需要更加细粒度的数据。而如果把监控的粒度做到秒级,则收集的数据量就会非常大,因此需要一个平衡,平时采用分钟级别的抽样数据已经足够用于预警,而秒级的则用于某个具体问题的分析。

当前tbstat功能还比较简单,类似于iostat/vmstat等os工具,tbstat可以通过指定抽样间隔和抽样次数,来循环抓取Oracle的统计状态信息。tbstat支持三个参数 -i 表示间隔时间 -c 表示循环次数 -n 表示需要查看的统计信息的名字(使用前后%的like来查询)

  • tbstat -i 2 -c 10 表示间隔时间2s,循环次数10次,展示经过人工筛选的36项统计信息
  • tbstat -i 2 -c 10 -n parse 表示间隔时间2s,循环次数10次,展示所有名字包含parse的统计信息
  • tbstat -i 2 -c 10 -n all 表示间隔时间2s,循环次数10次,展示所有不为零的统计信息

也可以使用简化的参数输入方法,第一位表示间隔时间,第二位表示循环次数,第三位表示统计信息名。直接敲入tbstat,则默认的参数为间隔时间10s,次数无限,经过挑选过滤的一些常用的v$sysstat中的统计信息。如果name参数传入的值是event,则展示v$system_event中的等待事件的信息。

$tbstat 1 0
--------------------------------------------------------------------------
-- tbstat v0.3.3 --- a tool for oracle system statistics and event.
-- Powered by NinGoo.net
--------------------------------------------------------------------------                      

               CPU used by this session:     40                       CR blocks created:       5
        DBWR checkpoint buffers written:    569                  DBWR undo block writes:      64
 bytes received via SQL*Net from client: 314297        bytes sent via SQL*Net to client: 2761660
  cleanouts only - consistent read gets:      4                         consistent gets:   48855
                       db block changes:   2122                           db block gets:    3714
                       enqueue requests:    900                           enqueue waits:       7
                          execute count:   3145                   free buffer requested:    1402
         index crx upgrade (positioned):      3            index fast full scans (full):       0
                 leaf node 90-10 splits:      0                        leaf node splits:       0
                      logons cumulative:      1                  parse count (failures):       0
                     parse count (hard):      0                          physical reads:    1546
          physical reads cache prefetch:      0                         physical writes:     603
                              redo size: 618436                         redo synch time:      16
                      redo synch writes:    181                         redo write time:      15
                            redo writes:    174   rollbacks only - consistent read gets:       0
                           sorts (disk):      0                          sorts (memory):     259
              table scans (long tables):      0              table scans (short tables):       9
                  transaction rollbacks:      0                            user commits:     182
$tbstat 1 0 event
-------------------------------------------------------------------------------
-- tbstat v0.3.3 --- a tool for oracle system statistics and event.
-- Powered by NinGoo.net
-------------------------------------------------------------------------------              

                   Event Name:   waits   time                       Event Name: waits   time
--------------------------------------------------------------------------------------------
      LGWR wait for redo copy:       1   0.01    SQL*Net more data from client:   151  19.95
  SQL*Net more data to client:    1218   0.01                buffer busy waits:     2   0.01
  control file parallel write:       1   0.51     control file sequential read:     1   0.26
                cursor: pin S:       0   0.00          cursor: pin S wait on X:     0   0.00
        db file parallel read:       0   0.00           db file parallel write:     0   0.00
       db file scattered read:       0   0.00          db file sequential read:  2040   3.43
             direct path read:     269   0.71            direct path read temp:     0   0.00
            direct path write:      23   0.26           direct path write temp:     0   0.00
         enq: CF - contention:       0   0.00             enq: HW - contention:     7   9.00
         enq: SQ - contention:       0   0.00     enq: TX - allocate ITL entry:     0   0.00
   enq: TX - index contention:       0   0.00    enq: TX - row lock contention:     0   0.00
                   latch free:       0   0.00      latch: cache buffers chains:     0   0.00
         latch: library cache:       0   0.00              latch: redo writing:     0   0.00
    latch: session allocation:       0   0.00               library cache lock:     0   0.00
             log buffer space:       0   0.00          log file parallel write:   145   0.60
     log file sequential read:     145   0.53       log file switch completion:     0   0.00
                log file sync:     147   0.78                os thread startup:     0   0.00
        read by other session:       0   0.00                   row cache lock:     0   0.00
       undo segment extension:       0   0.00

如果输入的name是精确匹配到只有一条统计信息的,会在后面打印出间隔时间内排名前10的sid的值。利用此功能,可以很方便的抓到造成某些统计信息异常的会话和SQL语句,会话和SQL信息是通过关联v$session来获取的。因此需要注意,如果统计信息对应的事件持续时间很短,从v$session里抓取到的sql可能并不是造成统计信息升高的罪魁祸首,但是sid一般来说还是准确的,因为应用采用的大多是连接池来连接数据库的,因此还是可以更具sid和machine信息来看看造成异常的是哪个具体的应用。

例如,全表扫描一般会导致physical reads cache prefetch等待事件,因此可以通过查看该事件对应的top sid来获得具体的语句,当然,不是所有的physical reads cache prefetch都是全表扫描导致的,因此对于获得的结果,还需要DBA根据具体情况做进一步分析:

$tbstat 1 0 'physical reads cache prefetch'
-------------------------------------------------------------------------------
-- tbstat v0.3.3 --- a tool for oracle system statistics and event.
-- Powered by NinGoo.net
-------------------------------------------------------------------------------
 physical reads cache prefetch:         526                             

              sid        value     %              machine         sql_id
       ----------  ----------- -----  ------------------- --------------
             2928          302  69.7               test11  79db58a3dg921
             4902           67  15.5               test71  79db58a3dg921
             4821           64  14.8               test33  3afdq50xt03ch
             4544            0   0.0               test54  3afdq50xt03ch
             1801            0   0.0               test06  79db58a3dg921
             2830            0   0.0               test12  79db58a3dg921
              898            0   0.0               test09  4n7675hwwcndc
             1031            0   0.0               test16  79db58a3dg921
              463            0   0.0               test04  3afdq50xt03ch
             1364            0   0.0               test08 cq749u66x06uj
             1408            0   0.0               test27  39rbqj3ck76w3
              722            0   0.0               test37  26hdkf07336uf

当然,tbstat只是一个用于抽取统计状态的小工具而已,如果要用于故障诊断,则还是要求DBA对于v$systat和v$system_event中各种统计和事件非常的熟悉。tbstat使用了DBD::Oracle以sysdba身份来连接数据库,因此需要为Perl安装DBI和DBD::Oracle模块,并且在数据库服务器本机上执行。如果你对于这个工具有兴趣,可以在这里下载源代码,使用过程中,如果有什么建议和需求,欢迎告诉我。

建议继续学习

  1. Mysql监控指南 (阅读 21,102)
  2. 批量添加主机到cacti+nagios的监控报警系统中 (阅读 14,683)
  3. 我常用的主机监控shell脚本 (阅读 13,104)
  4. 7 天打造前端性能监控系统 (阅读 11,063)
  5. 如何监控HP服务器硬件状态 (阅读 10,505)
  6. Cacti 添加 Nginx 监控 (阅读 10,363)
  7. Linux下三种常用的流量监控软件对比 (阅读 9,983)
  8. Cacti 添加 Memcached 监控 (阅读 9,162)
  9. Cacti 添加 Apache 监控 (阅读 8,984)
  10. 你应该知道的16个Linux服务器监控命令 (阅读 8,408)