zabbix server web界面,需要导入 tcp 监控模板
操作步骤:
Configuration --> Templates --> Import ,选择 本地的 zbx_tcp_status.xml
Configuration --> Templates --> Import ,选择 本地的 zbx_tcp_status.xml
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>3.0</version> <date>2016-10-20T10:07:50Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Templates tcp status ss</template> <name>Templates tcp status ss</name> <description/> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>tcp status</name> </application> </applications> <items> <item> <name>tcp ESTAB</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[ESTAB]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>tcp fin_wait1</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[FINWAIT1]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>tcp fin_wait2</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[FINWAIT2]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>tcp last_ack</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[LASTACK]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>tcp listen</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[LISTEN]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>tcp syn_recv</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[SYNRECV]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>tcp timewait</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>tcp[TIMEWAIT]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>tcp status</name> </application> </applications> <valuemap/> <logtimefmt/> </item> </items> <discovery_rules/> <macros/> <templates/> <screens/> </template> </templates> <graphs> <graph> <name>linux tcp 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>Templates tcp status ss</host> <key>tcp[ESTAB]</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>Templates tcp status ss</host> <key>tcp[FINWAIT1]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>2774A4</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Templates tcp status ss</host> <key>tcp[FINWAIT2]</key> </item> </graph_item> <graph_item> <sortorder>3</sortorder> <drawtype>0</drawtype> <color>A54F10</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Templates tcp status ss</host> <key>tcp[LASTACK]</key> </item> </graph_item> <graph_item> <sortorder>4</sortorder> <drawtype>0</drawtype> <color>FC6EA3</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Templates tcp status ss</host> <key>tcp[LISTEN]</key> </item> </graph_item> <graph_item> <sortorder>5</sortorder> <drawtype>0</drawtype> <color>6C59DC</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Templates tcp status ss</host> <key>tcp[SYNRECV]</key> </item> </graph_item> <graph_item> <sortorder>6</sortorder> <drawtype>0</drawtype> <color>AC8C14</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Templates tcp status ss</host> <key>tcp[TIMEWAIT]</key> </item> </graph_item> </graph_items> </graph> </graphs> </zabbix_export>
实现方式: 通过ansible-playbook完成,一下是代码
代码目录结构;
. ├── add_tcp_readme ├── dba_hosts ├── do_add_tcp.yml ├── roles │ └── add_tcp_monitor │ ├── files │ │ ├── tcp_status_ss.conf │ │ └── tcp_status_ss.sh │ └── tasks │ └── main.yml └── zbx_tcp_status.xml 4 directories, 7 files
dba_hosts:
[all] 0.0.0.0
do_add_tcp.yml
--- - name: "add tcp monitor to remote server ..." hosts: "all" remote_user: devops become: yes roles: - add_tcp_monitor
tcp_status_ss.conf
#monitor tcp UserParameter=tcp[*],/etc/zabbix/tcp_status_ss.sh $1
tcp_status_ss.sh
#!/bin/bash #scripts for tcp status function SYNRECV { /usr/sbin/ss -s | grep 'synrecv' | awk '{print $2}' } function ESTAB { /usr/sbin/ss -s | grep 'estab' | awk '{print $2}' } function FINWAIT1 { /usr/sbin/ss -o | grep 'FIN-WAIT-1'|wc -l } function FINWAIT2 { /usr/sbin/ss -ant| grep 'FIN-WAIT-2' | wc -l } function TIMEWAIT { /usr/sbin/ss -ant | grep 'TIME-WAIT' | wc -l } function LASTACK { /usr/sbin/ss -ant | grep 'LAST-ACK' | wc -l } function LISTEN { /usr/sbin/ss -ant | grep 'LISTEN' | wc -l } $1
main.yml
--- - name: "copy tcp conf to remote ..." copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" owner: "zabbix" group: "zabbix" with_items: - {'src':'tcp_status_ss.conf','dest':'/etc/zabbix/zabbix_agentd.d/','mode':'0644'} - {'src':'tcp_status_ss.sh','dest':'/etc/zabbix/','mode':'0775'} - name: "update zabbix agent conf ..." lineinfile: dest: "/etc/zabbix/zabbix_agentd.conf" state: present regexp: "^EnableRemoteCommands=1" line: "EnableRemoteCommands=1" - name: "restart zabbix agent ..." shell: "service zabbix-agent restart"
执行命令:
ansible-playbook -i dba_hosts do_add_tcp.yml