ubuntu 16.04 + zabbix 3.4 + postgresql UserParameter

os: ubuntu 16.04
zabbix: 3.4node

ip 規劃
192.168.56.101 node1 pgsql 9.6 master
192.168.56.102 node2 pgsql 9.6 slave
192.168.56.103 node3 zabbix proxy
192.168.56.104 node4 zabbix serverweb

本篇blog介紹在 node一、node2 節點上使用 UserParameter 監控 postgresql的具體過程。sql

建立 postgresql.conf

# egrep ^[A-Z] /etc/zabbix/zabbix_agentd.conf |grep -i include
Include=/etc/zabbix/zabbix_agentd.d/*.conf

# vi /etc/zabbix/zabbix_agentd.d/manual_postgresql96.conf

#postgresql version
UserParameter=manual_postgresql96.version,su - postgres -c "psql --version|head -n1"

#postgres isalive 
UserParameter=manual_postgresql96.isalive,su - postgres -c "psql -U zabbix postgres -Atqc 'select 1'"

#postgresql connection count
UserParameter=manual_postgresql96.server_processes,su - postgres -c "psql -U zabbix postgres -Atqc 'select sum(numbackends) from pg_stat_database'"

#postgresql commited transactions
UserParameter=manual_postgresql96.tx_commited,su - postgres -c "psql -U zabbix postgres -Atqc 'select sum(xact_commit) from pg_stat_database'"

#postgresql rolled back transactions
UserParameter=manual_postgresql96.tx_rolledback,su - postgres -c "psql -U zabbix postgres -Atqc 'select sum(xact_rollback) from pg_stat_database'"

測試一下shell

# zabbix_agentd -t manual_postgresql96.version
# zabbix_agentd -t manual_postgresql96.isalive
# zabbix_agentd -t manual_postgresql96.server_processes
# zabbix_agentd -t manual_postgresql96.tx_commited
# zabbix_agentd -t manual_postgresql96.tx_rolledback

建立模版文件

就是手動建立一個符合規範的模版文件 Template_Manual_PostgreSQL96.xml
內容較長,附後。express

導入監控模版

zabbix web 頁面操做ubuntu

Configuration -> Templates -> Importruby

能夠看到模版 Template_Manual_PostgreSQL96.xml app

主機配置模板

zabbix web 頁面操做tcp

Configuration -> Hostside

點擊某臺機器,進去選擇 Templates,
點擊Select,選中 Template DB Manual PostgreSQL96 ,點擊頁下面的 Select
點擊 Add
點擊 Update

稍等一會,就能看到採集到postgresql的數據了。

一些注意事項

須要注意的是模版文件的 value_type 須要和 manual_postgresql96.conf 對應之類型一致,不然會報錯。
value_type integer
0 - 浮點數
1 - 字符
2 - 日誌
3 - 數字(無正負)
4 - 文本

下面這個錯誤描述的是模版定義的是 「Numeric (unsigned)」 類型,可是實際執行返回的值是 「string」

1640:20180906:090855.099 item "node1:manual_postgresql96.isalive" became not supported: Value "su: must be run from a terminal" of type "string" is not suitable for value type "Numeric (unsigned)"
  1640:20180906:090855.099 item "node1:manual_postgresql96.server_processes" became not supported: Value "su: must be run from a terminal" of type "string" is not suitable for value type "Numeric (unsigned)"
  1640:20180906:090855.099 item "node1:manual_postgresql96.tx_commited" became not supported: Value "su: must be run from a terminal" of type "string" is not suitable for value type "Numeric (unsigned)"
  1640:20180906:090855.100 item "node1:manual_postgresql96.tx_rolledback" became not supported: Value "su: must be run from a terminal" of type "string" is not suitable for value type "Numeric (unsigned)"

這是是須要修改 AllowRoot=1

# vi /etc/zabbix/zabbix_agentd.conf
AllowRoot=1

# /etc/init.d/zabbix-agent restart

而後在 zabbix server 的日誌能夠看到以下信息。

# tail -f /var/log/zabbix/zabbix_server.log 

  1639:20180906:095615.954 item "node1:manual_postgresql96.isalive" became supported
  1637:20180906:095625.966 item "node1:manual_postgresql96.server_processes" became supported
  1637:20180906:095625.967 item "node1:manual_postgresql96.tx_commited" became supported
  1637:20180906:095625.967 item "node1:manual_postgresql96.tx_rolledback" became supported

本次是經過 UserParameter 實現了監控 postgresql 的幾個指標,應該是做爲一個實現的樣例。

實際中都是經過shell腳本集成到zabbix裏來實現監控補充的。

參考:
https://www.zabbix.com/documentation/3.4/zh/manual/config/items/userparameters
https://www.zabbix.com/documentation/3.4/zh/manual/config/items/userparameters/extending_agent

https://www.zabbix.com/documentation/3.4/zh/manual/xml_export_import/templates?s[]=value&s[]=type

Template_Manual_PostgreSQL96.xml 的具體內容

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.4</version>
    <date>2018-09-05T15:51:31Z</date>
    <groups>
        <group>
            <name>Templates/Databases</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template DB Manual PostgreSQL96</template>
            <name>Template DB Manual PostgreSQL96</name>
            <description/>
            <groups>
                <group>
                    <name>Templates/Databases</name>
                </group>
            </groups>
            <applications/>
            <items>
                <item>
                    <name>Manual PostgreSQL Status</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>net.tcp.listen[5432]</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications/>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Manual PostgreSQL Version</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>manual_postgresql96.version</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>0</trends>
                    <status>0</status>
                    <value_type>0</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications/>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Manual PostgreSQL IsAlive</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>manual_postgresql96.isalive</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications/>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Manual PostgreSQL Server Processes</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>manual_postgresql96.server_processes</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications/>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Manual PostgreSQL Tx Commited</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>manual_postgresql96.tx_commited</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications/>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Manual PostgreSQL Tx Rollback</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>manual_postgresql96.tx_rolledback</key>
                    <delay>30s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications/>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
            </items>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <triggers>
        <trigger>
            <expression>{Template DB Manual PostgreSQL96:net.tcp.listen[5432].last()}=0</expression>
            <recovery_mode>0</recovery_mode>
            <recovery_expression/>
            <name>PostgreSQL is down</name>
            <correlation_mode>0</correlation_mode>
            <correlation_tag/>
            <url/>
            <status>0</status>
            <priority>3</priority>
            <description/>
            <type>0</type>
            <manual_close>0</manual_close>
            <dependencies/>
            <tags/>
        </trigger>
    </triggers>
    <graphs>
        <graph>
            <name>Manual PostgreSQL Status</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template DB Manual PostgreSQL96</host>
                        <key>net.tcp.listen[5432]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>Manual PostgreSQL Server Processes</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template DB Manual PostgreSQL96</host>
                        <key>manual_postgresql96.server_processes</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>Manual PostgreSQL Tx</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template DB Manual PostgreSQL96</host>
                        <key>manual_postgresql96.tx_commited</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>F63100</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template DB Manual PostgreSQL96</host>
                        <key>manual_postgresql96.tx_rolledback</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
    </graphs>
</zabbix_export>
相關文章
相關標籤/搜索