PostgreSQL的监控三(zabbix)

前端之家收集整理的这篇文章主要介绍了PostgreSQL的监控三(zabbix)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

3 共通的监控工具

前面介绍的Postgresql监控工具都偏向于性能分析,没有告警功能。而且它们只是针对Postgresql的监视,有时需要监控整个业务相关的系统,这时候就要考虑通用的监控工具了。Linux下比较适合监控数据库的常用的工具有Nagios和Zabbix。Zabbix更容易使用,现在看上去也更被多数人看好,所以本文只介绍Zabbix监控Postgresql方法

3.1 Zabbix简介



Zabbix是一个all in one高度集成的企业级监控解决方案。由一个中心的Zabbix Server和若干可能安装有Zabbix Agent被监控设备构成,主要特性可概括为以下几点
数据采集
支持agent和agent less(SNMP,IPMI,HTTP,FTP...)
支持基于JMX对java应用的监视
可灵活定制agent
数据存储
数据库为Postgresql,MysqL,Oracle,sqlite或DB2
可配置历史和趋势数据的保存时间
内建旧清理程序防止数据膨胀
报警
可定制报警阈值
灵活设置报警方式,邮件,SMS,脚本
支持报警升级
报警消息可使用宏变量定制
可视化
可定制的数据图形
仪表盘
地图
所有配置都通过GUI编辑
大规模部署
支持模板
自动发现主机和监控项目
通过Zabbix Proxy实现分布式部署
其他
Zabbix API
认证和访问控制
IT资产收集
zabbix要想监视Postgresql这种应用型的对象,一般使用zabbix agent。zabbix agent有2种工作方式。
方式1:被动代理
由Zabbix Server(或Proxy)主动查询数据(如cpu负载),作为响应Zabbix Agent返回查询结果。这也是最简单最常用的方式。
方式2:主动代理
Zabbix Agent先从Zabbix Server获取需要主动报告的监控项目一览,然后定期发送新值到Zabbix Server。主动代理可以用于处理时间比较长的监控项,比如log 。
也可以使用Zabbix Trapper
方式3:Trapper
由Zabbix Agent主动报告数据。被监控端可调用zabbix_send命令或直接利用Socket发送数据到Zabbix Server。通过Trapper可以只在状态变更时进行报告。
另外还有把Postgresql状态通过SNMP代理发布的方案(http://pgsnmpd.projects.pgfoundry.org/),估计用的不多,本文不涉及。
详细参考:
https://www.zabbix.com/documentation/2.4/manual/concepts/agent
https://www.zabbix.com/documentation/2.4/manual/appendix/items/activepassive
https://www.zabbix.com/documentation/2.4/manual/config/items/itemtypes/trapper
Zabbix没有内置对Postgresql的监控项,所以如果要监控Postgresql需要做一些监控项的配置或定制,下面会介绍几种方法
3.2 方法1:直接扩充UserParameter 自己修改zabbix_agentd.conf的配置文件,在Zabbix agent上增加Postgresql相关的监控项,使用psqlsql的方式获取Postgresql性能数据。

zabbix_agentd.conf
  1. #Get the Postgresql version
  2. UserParameter=psql.version,psql--version|head-n1
  3. #Get the total numberofServer Processes that are active
  4. UserParameter.server_processes-t-c"select sum(numbackends) from pg_stat_database"
  5. #Get the total numberofcommited transactions
  6. UserParameter.tx_commited"select sum(xact_commit) from pg_stat_database"
  7. #Get the total numberofrolled back transactions
  8. UserParameter.tx_rolledback"select sum(xact_rollback) from pg_stat_database"

参照
https://www.zabbix.com/wiki/howto/monitor/db/postgresql
3.3方法2:扩充UserParameter调用Postgresql监视脚本

方法1类似,但方法1中定义的UserParameter太多,为方便起见,可以把监控Postgresqlsql命令做成一个单独的脚本。

比如参照下面的例子
https://www.zabbix.com/forum/showthread.PHP?t=8009
zabbix_agentd.conf:
UserParameter=postgresql[*]/opt/zabbix/bin/zapost $1 $2

zapost:
#
  • #Name:zapost
  • #
  • # Checks Postgresql activity.
  • #
  • # Author:bashman
  • #
  • # Version:1.0
  • #

  • zapostver="1.0"
  • rval=0
  • sql""

  • case $1in

  • #'summary')
  • # sql"select a.datname,pg_size_pretty(pg_database_size(a.datid)) as size,cast(blks_hit/(blks_read+blks_hit+0.000001)*100.0 as numeric(5,2)) as cache,cast(xact_commit/(xact_rollback+xact_commit+0.000001)*100.0 as numeric(5,2)) as success from pg_stat_database a order by a.datname"
  • #;;

  • #'size')
  • #comprobar aqui los parametros
  • # shift
  • # sql"select pg_database_size('$1') as size"
  • #'version''select version()'
  • #;

  • 'totalsize')
  • sql"select sum(pg_database_size(datid)) as total_size from pg_stat_database"
  • 'db_cache')
  • # comprueba los parametros
  • if!-z $2;then
  • shift
  • sql"select cast(blks_hit/(blks_read+blks_hit+0.000001)*100.0 as numeric(5,2)) as cache from pg_stat_database where datname = '$1'"
  • fi
  • 'db_success'"select cast(xact_commit/(xact_rollback+xact_commit+0.000001)*100.0 as numeric(5,2)) as success from pg_stat_database where datname = '$1'"
  • fi
  • 'server_processes'"select sum(numbackends) from pg_stat_database"
  • 'tx_commited'"select sum(xact_commit) from pg_stat_database"
  • 'tx_rolledback'"select sum(xact_rollback) from pg_stat_database"
  • 'db_size'"select pg_database_size('$1')"#assize"
  • fi
  • ;;

  • 'db_connections')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select numbackends from pg_stat_database where datname'$1'"
  • fi
  • ;;

  • 'db_returned')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select tup_returned from pg_stat_database where datname"
  • fi
  • ;;

  • 'db_fetched')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select tup_fetched from pg_stat_database where datname"
  • fi
  • ;;

  • 'db_inserted')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select tup_inserted from pg_stat_database where datname"
  • fi
  • ;;

  • 'db_updated')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select tup_updated from pg_stat_database where datname"
  • fi
  • ;;

  • 'db_deleted')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select tup_deleted from pg_stat_database where datname"
  • fi
  • ;;

  • 'db_commited')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select xact_commit from pg_stat_database where datname"
  • fi
  • ;;

  • 'db_rolled')
  • # comprueba los parametros
  • if [ ! -z $2 ]; then
  • shift
  • sql="select xact_rollback from pg_stat_database where datname"
  • fi
  • ;;

  • 'version')
  • sql="version"
  • ;;

  • 'zapostver')
  • echo "$zapostver"
  • exit $rval
  • ;;

  • *)
  • echo "zapost version:$zapostver"
  • echo "usage:"
  • echo " $0 totalsize-Check the total databasessize."
  • echo " $0 db_cache<dbname>-Check the database cache hit ratio(percentage)"
  • echo " $0 db_success-Check the database success rate"
  • echo " $0 server_processes-Check the total numberofServer Processes that are active"
  • echo " $0 tx_commitedofcommited transactions"
  • echo " $0 tx_rolledbackofrolled back transactions"
  • echo " $0 db_size-Check theofa Database(inbytes"
  • echo " $0 db_connections-Check the numberofactive connectionsfora specified database"
  • echo " $0 db_returnedoftuples returned"
  • echo " $0 db_fetchedoftuples fetched"
  • echo " $0 db_insertedoftuples inserted"
  • echo " $0 db_updatedoftuples updated"
  • echo " $0 db_deletedoftuples deleted"
  • echo " $0 db_commitedofcommited back transactions"
  • echo " $0 db_rolled"
  • echo " $0 version-The Postgresql version"
  • echo " $0 zapostver-Versionofthisscript"
  • exit $rval
  • ;;
  • esac

  • if [ "$sql" != "" ]; then
  • if [ "" == "version" ]; then
  • psql --version|head -n1
  • rval=$?
  • else
  • psql -t -c ""
  • rval=$?
  • fi
  • fi

  • if [ "$rval" -ne 0 ]; then
  • echo "ZBX_NOTSUPPORTED

  • 3.4方法3:使用Postbix或DBforBIX插件 和前面提到的2种方法相比,Postbix插件功能更全面,它包含了Postgresql相关的监控项和图形的Zabbix模板。Postbix以一个的后台java deamon运行,这个deamon通过jdbc查询远端被监控数据库的状态然后以trap agent的方式发送到Zabbix Server。
    Postbix来自http://www.smartmarmot.com/,除了Postbix该公司还有Orabbix,MysqLBix,这些工具的内部架构和使用方法基本相同,唯一的区别就是支持的被监控数据库不同。所以smartmarmot又推出了整合这几种数据库监控能力的DBforBIX。DBforBIX的内部结构和使用方法和Postbix基本相同,下面介绍一下DBforBIX的简单的使用例子。
    1)下载dbforbix
    http://www.smartmarmot.com/product/dbforbix/dbforbix-download/
    2)在Zabbix Server上安装dbforbix
    [root@zabbix ~#mkdir /opt/dbforbix
  • #cd /opt/dbforbix
  • [root@zabbix dbforbix#unzip /root/dbforbix-0.6.1.zip
  • #cp /opt/dbforbix/init.d/dbforbix /etc/init.d/dbforbix
  • #chmod+x /etc/init+x /opt/dbforbix/run.sh
  • #chkconfig dbforbix on

  • 3)导入dbforix的模板到Zabbix服务器
    点击Zabbix GUI画面的"Configuration->Templates->Import"把下面的模板文件导入Zabbix服务器。
    /opt/dbforbix/template/template_postgresql.xml


    4)在被监控Postgresql实例上创建DBforBIX使用的账号并赋予权限

    1. CREATE USER zabbix WITH PASSWORD'passw0rd';
    2. GRANT SELECT ON pg_stat_activity to zabbix;
    3. GRANT SELECT ON pg_stat_activity to zabbix;
    4. GRANT SELECT ON pg_database to zabbix;
    5. GRANT SELECT ON pg_authid to zabbix;
    6. GRANT SELECT ON pg_stat_bgwriter to zabbix;
    7. GRANT SELECT ON pg_locks to zabbix;
    8. GRANT SELECT ON pg_stat_database to zabbix
    5)修改config.props
    通过拷贝config.props.sample生成config.props,然后修改config.props设置Zabbix Server的IP和端口号,设置被监控Postgresql数据库的访问账号

    #cp/opt/dbforbix/conf/config.props.sample /opt/dbforbix/conf/config.props
  • #vi /opt/dbforbix/conf/config.props
  • ZabbixServerList=ZabbixServer
  • ZabbixServer.Address=IP_ADDRESS_OF_ZABBIX_SERVER
  • ZabbixServer.Port=PORT_OF_ZABBIX_SERVER
  • ..
  • DBforBIX.PidFile=/logs/dbforbix.pid
  • .
  • DatabaseList=PGsqlDB2
  • PGsqlDB2.Url=jdbc:postgresql://host:port/database
  • PGsqlDB2.User=zabbix
  • PGsqlDB2.Password=passw0rd

  • 注意:/opt/dbforbix/init.d/dbforbix有个Bug,本来应该从config.props中读取pid文件名的,结果写死了是dbforbix.pid,config.props.sample中的默认值又是orabix.pid。

    6)修改pgsqlquery.props
    通过拷贝pgsqlquery.props.sample生成pgsqlquery.props。pgsqlquery.props中定义了监控项目及对应的查询sql,可以编辑QueryList对监控项做筛选。

    /opt/dbforbix/conf/pgsqlquery.sample.props

    看一下 pgsqlquery.props.sample包含的内容
    pgsqlquery.props.sample:
    QueryList=activeconntupinsertedtupdeletedxactrollbackaccessexclusivelockrowsharelockshareupdateexclusivelockcheckpoints_timedbuffers_checkpointmaxwritten_cleanbuffers_alloc

  • #statisticofdatabase
  • activeconn.Query=select sum(numbackends)from pg_stat_database
  • tupreturned(tup_returned)from pg_stat_database
  • tupfetched(tup_fetched)from pg_stat_database
  • tupinserted(tup_inserted)from pg_stat_database
  • tupupdated(tup_updated)from pg_stat_database
  • tupdeleted(tup_deleted)from pg_stat_database
  • xactcommit=SELECT sum(xact_commit)FROM pg_stat_database
  • xactrollback(xact_rollback)FROM pg_stat_database

  • #locks
  • exclusivelock=SELECT count)FROM pg_locks where mode'ExclusiveLock'
  • accessexclusivelock'AccessExclusiveLock'
  • accesssharelock'AccessShareLock'
  • rowsharelock'RowShareLock'
  • rowexclusivelock'RowExclusiveLock'
  • shareupdateexclusivelock'ShareUpdateExclusiveLock'
  • sharerowexclusivelock'ShareRowExclusiveLock'

  • checkpoints_timed=select checkpoints_timed from pg_stat_bgwriter
  • checkpoints_req=select checkpoints_req from pg_stat_bgwriter
  • buffers_checkpoint=select buffers_checkpoint from pg_stat_bgwriter
  • buffers_clean=select buffers_clean from pg_stat_bgwriter
  • maxwritten_clean=select maxwritten_clean from pg_stat_bgwriter
  • buffers_backend=select buffers_backend from pg_stat_bgwriter
  • buffers_alloc=select buffers_alloc from pg_stat_bgwriter

  • 7)启动dbforbix deamon

    #/etc/init.d/dbforbix start

    8) 在Zabbix Server上创建Host
    点击Zabbix GUI画面的"Configuration->Hosts->Create Host"为被监控数据库创建一个专门的Host。"Host name"设置为“PGsqlDB2”( 必须和config.props的DatabaseList中的名称一致,这里是“PGsqlDB2”)。并且把Host“PGsqlDB2”链接到前面导入的模板"Template_Postgesql"。

    9) 检查数据是否已被收集
    点击Zabbix GUI画面的"Monitoring->Last data"检查数据是否已被收集。
    参考
    http://www.smartmarmot.com/wiki/index.PHP/DBforBIX
    3.5方法4:使用pg_monz模板 pg_monz是一套可以监控Postgresql的zabbix模板,通过定制的agent U serParameter监控Postgresql数据库 ,并且利用Zabbix的发现机制可以自动发现和监视数据库和表。
    pg_monz由下面几个文件组成
    文件 说明
    pg_monz_template.xml
    模版定义文件
    userparameter_pgsql.conf
    提供Postgresql监控项目的用户参数定义
    find_dbname.sh
    PG数据库自动发现脚本
    find_dbname_table.sh
    PG数据表的自动发现脚本


    要了解pg_monz支持哪些监控项目,看一下 userparameter_pgsql.conf 就可以了
    userparameter_pgsql.conf:

    点击(此处)折叠或打开

    1. # Postgresql user parameter

    2. #
    3. # Server specific examples
    4. #
    5. # Get the total numberofcommited transactions
    6. UserParameter-h $1-p $2-U $3-d $4"select sum(xact_commit) from pg_stat_database"
    7. # Get the total numberofrolled back transactions
    8. UserParameter"select sum(xact_rollback) from pg_stat_database"
    9. # Max Connections
    10. UserParameter.server_maxcon"show max_connections"
    11. # Postgresql is running
    12. UserParameter.running"select 1">/dev/null 2&1echo$?

    13. # Added by SRA OSS
    14. # Get numberofcheckpoint count(by checkpoint_timeout)
    15. UserParameter.checkpoints_timed"select checkpoints_timed from pg_stat_bgwriter"
    16. # Get number(by checkpoint_segments.checkpoints_req"select checkpoints_req from pg_stat_bgwriter"
    17. # Get the total numberofconnections
    18. UserParameter.server_connections"select count(*) from pg_stat_activity;"
    19. # Get the total numberofactive(on processing sql)connections
    20. UserParameter.active_connections"select count(state) from pg_stat_activity where state = 'active'"
    21. # Get the total numberofidle connections
    22. UserParameter.idle_connections"select count(state) from pg_stat_activity where state = 'idle'"
    23. # Get the total numberofidleintransaction connections
    24. UserParameter.idle_tx_connections"select count(state) from pg_stat_activity where state = 'idle in transaction'"
    25. # Get the total numberlock-waiting connections
    26. UserParameter.locks_waiting"select count(*) from pg_stat_activity where waiting = 't'"

    27. # Get buffer information
    28. UserParameter.buffers_checkpoint"select buffers_checkpoint from pg_stat_bgwriter"
    29. UserParameter.buffers_clean"select buffers_clean from pg_stat_bgwriter"
    30. UserParameter.maxwritten_clean"select maxwritten_clean from pg_stat_bgwriter"
    31. UserParameter.buffers_backend"select buffers_backend from pg_stat_bgwriter"
    32. UserParameter.buffers_backend_fsync"select buffers_backend_fsync from pg_stat_bgwriter"
    33. UserParameter.buffers_alloc"select buffers_alloc from pg_stat_bgwriter"

    34. # Get numberofslow queries
    35. UserParameter.slow_queries"select count(*) from pg_stat_activity where state = 'active' and now() - query_start > '$5 sec'::interval"
    36. UserParameter.slow_select_queries"select count(*) from pg_stat_activity where state = 'active' and now() - query_start > '$5 sec'::interval and query ilike 'select%'"
    37. UserParameter.slow_dml_queries"select count(*) from pg_stat_activity where state = 'active' and now() - query_start > '$5 sec'::interval and query ~* '^(insert|update|delete)'"

    38. #
    39. # Database specific examples
    40. #
    41. # Get the.db_size"select pg_database_size('$5')"
    42. # Get numberfora specified database
    43. UserParameter.db_connections"select numbackends from pg_stat_database where datname = '$5'"
    44. # Get number.db_returned"select tup_returned from pg_stat_database where datname = '$5'"
    45. # Get number.db_fetched"select tup_fetched from pg_stat_database where datname = '$5'"
    46. # Get number.db_inserted"select tup_inserted from pg_stat_database where datname = '$5'"
    47. # Get number.db_updated"select tup_updated from pg_stat_database where datname = '$5'"
    48. # Get number.db_deleted"select tup_deleted from pg_stat_database where datname = '$5'"
    49. # Get numberofcommited/rolled back transactions.db_tx_commited"select xact_commit from pg_stat_database where datname = '$5'"
    50. UserParameter.db_tx_rolledback"select xact_rollback from pg_stat_database where datname = '$5'"

    51. # Cache Hit Ratio
    52. UserParameter.cachehit_ratio"SELECT round(blks_hit*100/(blks_hit+blks_read),2) AS cache_hit_ratio FROM pg_stat_database WHERE datname = '$5' and blks_read > 0 union all select 0.00 AS cache_hit_ratio order by cache_hit_ratio desc limit 1"

    53. # Added by SRA OSS
    54. # Get numberoftemp files
    55. UserParameter.db_temp_files"select temp_files from pg_stat_database where datname = '$5'"
    56. # Get temp file.db_temp_bytes"select temp_bytes from pg_stat_database where datname = '$5'"
    57. # Get percentageofdead tuplesofall tables.db_dead_tup_ratio"select round(sum(n_dead_tup)*100/sum(n_live_tup+n_dead_tup),2) as dead_tup_ratio from pg_stat_all_tables where n_live_tup > 0"
    58. # Get numberofdeadlocks(9.2orlater.db_deadlocks"select deadlocks from pg_stat_database where datname = '$5'"


    59. #
    60. # Table specific examples
    61. #
    62. # Get table cache hit ratioofa specific table
    63. UserParameter.table_cachehit_ratio"select round(heap_blks_hit*100/(heap_blks_hit+heap_blks_read),2) as cache_hit_ratio from pg_statio_user_tables where schemaname = '$5' and relname = '$6' and heap_blks_read > 0 union all select 0.00 as cache_hit_ratio order by cache_hit_ratio desc limit 1"
    64. # Get numberofsequencial scan.table_seq_scan"select seq_scan from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    65. # Get numberofindex scan.table_idx_scan"select coalesce(idx_scan,0) from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    66. # Get numberofvacuum count.table_vacuum_count"select vacuum_count from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    67. # Get numberofanalyze count.table_analyze_count"select analyze_count from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    68. # Get numberofautovacuum count.table_autovacuum_count"select autovacuum_count from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    69. # Get numberofautoanalyze count.table_autoanalyze_count"select autoanalyze_count from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"

    70. # Get numberoftuples.table_n_tup_ins"select n_tup_ins from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    71. UserParameter.table_n_tup_upd"select n_tup_upd from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    72. UserParameter.table_n_tup_del"select n_tup_del from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    73. UserParameter.table_seq_tup_read"select seq_tup_read from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    74. UserParameter.table_idx_tup_fetch"select coalesce(idx_tup_fetch,0) from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    75. UserParameter.table_n_tup_hot_upd"select n_tup_hot_upd from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    76. UserParameter.table_n_live_tup"select n_live_tup from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"
    77. UserParameter.table_n_dead_tup"select n_dead_tup from pg_stat_user_tables where schemaname = '$5' and relname = '$6'"


    78. #
    79. # Discovery Rule
    80. #
    81. # Database Discovery
    82. UserParameter=db.list.discovery$5/find_dbname.sh $1 $2 $3 $4
    83. UserParameter=db_table$5/find_dbname_table.sh $1 $2 $3 $4

    使用例:
    1)下载pg_monz
    https://github.com/pg-monz/pg_monz/releases
    2)安装pg_monz
    #tar xfz pg_monz-1.0.tar.gz
  • #cd pg_monz.0/pg_monz
  • [root@zabbix pg_monz#cp find_dbname.sh find_dbname_table.sh /usr/local/bin/
  • #cp userparameter_pgsql.conf /etc/zabbix/zabbix_agentd.d/
  • +x /usr//bin/find_dbname/bin/find_dbname_table.d/zabbix-agent restart

  • 3)导入模板到Zabbix服务器
    点击Zabbix GUI画面的"Configuration->Templates->Import"把模板文件pg_monz_template.xml导入Zabbix服务器。

    4)设置模板中的宏
    点击Zabbix GUI画面的"Configuration->Templates",再点击其中的"Postgresql Check"模板,然后点击"Marcos" Tab设置必要宏参数(尤其是连接相关的参数)。
    5)在Zabbix Server上创建Host
    点击Zabbix GUI画面的"Configuration->Hosts->Create Host"为被监控数据库所在主机创建一个Host,如果该主机的Host已存在也可使用已有Host。这个Host要设置Zabbix Agent,并且把该Host链接到前面导入的模板"Postgesql Check"。
    6) 检查数据是否已被收集
    http://pg-monz.github.io/pg_monz/index-en.html
    3.6 小结

    以上的方法1和方法2都需要自己再进行定制,而DBforBIXpg_monz已经比较成熟了。onzDBforBIX相比更简单,可监控的Postgresql项目也更多,还可以自动发现库和表;DBforBIX的优势则在于支持监控多种常用的数据库以及可以使用jdbc连接池。综合而言如果不需要监控多种数据库个人倾向于pg_monz

    转载:http://blog.chinaunix.net/uid-20726500-id-4513716.html

    原文链接:https://www.f2er.com/postgresql/194861.html

    猜你在找的Postgre SQL相关文章