虚拟化容器,大数据,DBA,中间件,监控。

zabbix3对tcp连接数及状态的监控

22 11月
作者:admin|分类:监控安全
zabbix3对tcp连接数及状态的监控优化


tcp的监控采用netstat命令,发现在服务器繁忙的时候效果不理想,这个命令占用大量的cpu有时候高达90%以上,可能会导致业务的不稳定,所以改用ss命令对脚本进行优化

对tcp连接数和状态的监控意义主要有以下几点:
1.可以观察服务器的压力分布(连接数大于5W的时候可能系统会有一定的压力,可以考虑加服务器)
2.如果服务器的连接数突然变得极小(比如100以下),可能是业务系统故障导致在线用户被踢出


在需要被监控的zabbix-agent端添加脚本编写
创建文件夹
mkdir -p /usr/local/zabbix-agent/scripts/
mkdir -p /etc/zabbix/zabbix_agentd.d/
vim /usr/local/zabbix-agent/scripts/tcp_status_ss.sh


#!/bin/bash 
#scripts for tcp status 
function SYNRECV { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'SYN-RECV' | awk '{print $2}'

function ESTAB { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'ESTAB' | awk '{print $2}'

function FINWAIT1 { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-1' | awk '{print $2}'

function FINWAIT2 { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-2' | awk '{print $2}'

function TIMEWAIT { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'TIME-WAIT' | awk '{print $2}'

function LASTACK { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LAST-ACK' | awk '{print $2}'

function LISTEN { 
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LISTEN' | awk '{print $2}'

$1

或者


 
  1. function SYNRECV {   
  2. /usr/sbin/ss -s | grep 'synrecv' | awk '{print $2}'  
  3. }   
  4. function ESTAB {   
  5. /usr/sbin/ss -s | grep 'estab' | awk '{print $2}'  
  6. }   
  7. function FINWAIT1 {   
  8. /usr/sbin/ss -o |  grep 'FIN-WAIT-1'|wc -l  
  9. }   
  10. function FINWAIT2 {   
  11. /usr/sbin/ss -ant| grep 'FIN-WAIT-2' | wc -l  
  12. }   
  13. function TIMEWAIT {   
  14. /usr/sbin/ss -ant | grep 'TIME-WAIT' | wc -l  
  15. }   
  16. function LASTACK {   
  17. /usr/sbin/ss -ant | grep 'LAST-ACK' | wc -l  
  18. }   
  19. function LISTEN {   
  20. /usr/sbin/ss -ant | grep 'LISTEN' | wc -l  
  21. }   
  22. $1  


赋予脚本执行权限
chmod +x /usr/local/zabbix-agent/scripts/tcp_status_ss.sh 


二、填写key值:当然大家在加入key值之后最好再服务器上面去执行看有没有返回值:


vim /etc/zabbix/zabbix_agentd.d/tcp_status_ss.conf


#monitor tcp 
UserParameter=tcp[*],/usr/local/zabbix-agent/scripts/tcp_status_ss.sh $1


重启agent
service zabbix-agent restart


zabbix-server服务端测试
zabbix_get -s 192.168.3.18 -p 10050 -k "tcp[LISTEN]"
zabbix_get -s 192.168.3.18 -p 20050 -k "tcp[LISTEN]"
22


zabbix web端配置:
登录Zabbix3.0 的web界面,一次选择 Configuration > Templates,在主界面的右上角有个 Import 按钮,用来导入模板


模板文件:

  1. zbx_tcp_status.xml  
  2.   
  3. <?xml version="1.0" encoding="UTF-8"?>  
  4. <zabbix_export>  
  5.     <version>3.0</version>  
  6.     <date>2016-10-20T10:07:50Z</date>  
  7.     <groups>  
  8.         <group>  
  9.             <name>Templates</name>  
  10.         </group>  
  11.     </groups>  
  12.     <templates>  
  13.         <template>  
  14.             <template>Templates tcp status ss</template>  
  15.             <name>Templates tcp status ss</name>  
  16.             <description/>  
  17.             <groups>  
  18.                 <group>  
  19.                     <name>Templates</name>  
  20.                 </group>  
  21.             </groups>  
  22.             <applications>  
  23.                 <application>  
  24.                     <name>tcp status</name>  
  25.                 </application>  
  26.             </applications>  
  27.             <items>  
  28.                 <item>  
  29.                     <name>tcp ESTAB</name>  
  30.                     <type>0</type>  
  31.                     <snmp_community/>  
  32.                     <multiplier>0</multiplier>  
  33.                     <snmp_oid/>  
  34.                     <key>tcp[ESTAB]</key>  
  35.                     <delay>30</delay>  
  36.                     <history>90</history>  
  37.                     <trends>365</trends>  
  38.                     <status>0</status>  
  39.                     <value_type>3</value_type>  
  40.                     <allowed_hosts/>  
  41.                     <units/>  
  42.                     <delta>0</delta>  
  43.                     <snmpv3_contextname/>  
  44.                     <snmpv3_securityname/>  
  45.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  46.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  47.                     <snmpv3_authpassphrase/>  
  48.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  49.                     <snmpv3_privpassphrase/>  
  50.                     <formula>1</formula>  
  51.                     <delay_flex/>  
  52.                     <params/>  
  53.                     <ipmi_sensor/>  
  54.                     <data_type>0</data_type>  
  55.                     <authtype>0</authtype>  
  56.                     <username/>  
  57.                     <password/>  
  58.                     <publickey/>  
  59.                     <privatekey/>  
  60.                     <port/>  
  61.                     <description/>  
  62.                     <inventory_link>0</inventory_link>  
  63.                     <applications>  
  64.                         <application>  
  65.                             <name>tcp status</name>  
  66.                         </application>  
  67.                     </applications>  
  68.                     <valuemap/>  
  69.                     <logtimefmt/>  
  70.                 </item>  
  71.                 <item>  
  72.                     <name>tcp fin_wait1</name>  
  73.                     <type>0</type>  
  74.                     <snmp_community/>  
  75.                     <multiplier>0</multiplier>  
  76.                     <snmp_oid/>  
  77.                     <key>tcp[FINWAIT1]</key>  
  78.                     <delay>30</delay>  
  79.                     <history>90</history>  
  80.                     <trends>365</trends>  
  81.                     <status>0</status>  
  82.                     <value_type>3</value_type>  
  83.                     <allowed_hosts/>  
  84.                     <units/>  
  85.                     <delta>0</delta>  
  86.                     <snmpv3_contextname/>  
  87.                     <snmpv3_securityname/>  
  88.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  89.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  90.                     <snmpv3_authpassphrase/>  
  91.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  92.                     <snmpv3_privpassphrase/>  
  93.                     <formula>1</formula>  
  94.                     <delay_flex/>  
  95.                     <params/>  
  96.                     <ipmi_sensor/>  
  97.                     <data_type>0</data_type>  
  98.                     <authtype>0</authtype>  
  99.                     <username/>  
  100.                     <password/>  
  101.                     <publickey/>  
  102.                     <privatekey/>  
  103.                     <port/>  
  104.                     <description/>  
  105.                     <inventory_link>0</inventory_link>  
  106.                     <applications>  
  107.                         <application>  
  108.                             <name>tcp status</name>  
  109.                         </application>  
  110.                     </applications>  
  111.                     <valuemap/>  
  112.                     <logtimefmt/>  
  113.                 </item>  
  114.                 <item>  
  115.                     <name>tcp fin_wait2</name>  
  116.                     <type>0</type>  
  117.                     <snmp_community/>  
  118.                     <multiplier>0</multiplier>  
  119.                     <snmp_oid/>  
  120.                     <key>tcp[FINWAIT2]</key>  
  121.                     <delay>30</delay>  
  122.                     <history>90</history>  
  123.                     <trends>365</trends>  
  124.                     <status>0</status>  
  125.                     <value_type>3</value_type>  
  126.                     <allowed_hosts/>  
  127.                     <units/>  
  128.                     <delta>0</delta>  
  129.                     <snmpv3_contextname/>  
  130.                     <snmpv3_securityname/>  
  131.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  132.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  133.                     <snmpv3_authpassphrase/>  
  134.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  135.                     <snmpv3_privpassphrase/>  
  136.                     <formula>1</formula>  
  137.                     <delay_flex/>  
  138.                     <params/>  
  139.                     <ipmi_sensor/>  
  140.                     <data_type>0</data_type>  
  141.                     <authtype>0</authtype>  
  142.                     <username/>  
  143.                     <password/>  
  144.                     <publickey/>  
  145.                     <privatekey/>  
  146.                     <port/>  
  147.                     <description/>  
  148.                     <inventory_link>0</inventory_link>  
  149.                     <applications>  
  150.                         <application>  
  151.                             <name>tcp status</name>  
  152.                         </application>  
  153.                     </applications>  
  154.                     <valuemap/>  
  155.                     <logtimefmt/>  
  156.                 </item>  
  157.                 <item>  
  158.                     <name>tcp last_ack</name>  
  159.                     <type>0</type>  
  160.                     <snmp_community/>  
  161.                     <multiplier>0</multiplier>  
  162.                     <snmp_oid/>  
  163.                     <key>tcp[LASTACK]</key>  
  164.                     <delay>30</delay>  
  165.                     <history>90</history>  
  166.                     <trends>365</trends>  
  167.                     <status>0</status>  
  168.                     <value_type>3</value_type>  
  169.                     <allowed_hosts/>  
  170.                     <units/>  
  171.                     <delta>0</delta>  
  172.                     <snmpv3_contextname/>  
  173.                     <snmpv3_securityname/>  
  174.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  175.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  176.                     <snmpv3_authpassphrase/>  
  177.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  178.                     <snmpv3_privpassphrase/>  
  179.                     <formula>1</formula>  
  180.                     <delay_flex/>  
  181.                     <params/>  
  182.                     <ipmi_sensor/>  
  183.                     <data_type>0</data_type>  
  184.                     <authtype>0</authtype>  
  185.                     <username/>  
  186.                     <password/>  
  187.                     <publickey/>  
  188.                     <privatekey/>  
  189.                     <port/>  
  190.                     <description/>  
  191.                     <inventory_link>0</inventory_link>  
  192.                     <applications>  
  193.                         <application>  
  194.                             <name>tcp status</name>  
  195.                         </application>  
  196.                     </applications>  
  197.                     <valuemap/>  
  198.                     <logtimefmt/>  
  199.                 </item>  
  200.                 <item>  
  201.                     <name>tcp listen</name>  
  202.                     <type>0</type>  
  203.                     <snmp_community/>  
  204.                     <multiplier>0</multiplier>  
  205.                     <snmp_oid/>  
  206.                     <key>tcp[LISTEN]</key>  
  207.                     <delay>30</delay>  
  208.                     <history>90</history>  
  209.                     <trends>365</trends>  
  210.                     <status>0</status>  
  211.                     <value_type>3</value_type>  
  212.                     <allowed_hosts/>  
  213.                     <units/>  
  214.                     <delta>0</delta>  
  215.                     <snmpv3_contextname/>  
  216.                     <snmpv3_securityname/>  
  217.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  218.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  219.                     <snmpv3_authpassphrase/>  
  220.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  221.                     <snmpv3_privpassphrase/>  
  222.                     <formula>1</formula>  
  223.                     <delay_flex/>  
  224.                     <params/>  
  225.                     <ipmi_sensor/>  
  226.                     <data_type>0</data_type>  
  227.                     <authtype>0</authtype>  
  228.                     <username/>  
  229.                     <password/>  
  230.                     <publickey/>  
  231.                     <privatekey/>  
  232.                     <port/>  
  233.                     <description/>  
  234.                     <inventory_link>0</inventory_link>  
  235.                     <applications>  
  236.                         <application>  
  237.                             <name>tcp status</name>  
  238.                         </application>  
  239.                     </applications>  
  240.                     <valuemap/>  
  241.                     <logtimefmt/>  
  242.                 </item>  
  243.                 <item>  
  244.                     <name>tcp syn_recv</name>  
  245.                     <type>0</type>  
  246.                     <snmp_community/>  
  247.                     <multiplier>0</multiplier>  
  248.                     <snmp_oid/>  
  249.                     <key>tcp[SYNRECV]</key>  
  250.                     <delay>30</delay>  
  251.                     <history>90</history>  
  252.                     <trends>365</trends>  
  253.                     <status>0</status>  
  254.                     <value_type>3</value_type>  
  255.                     <allowed_hosts/>  
  256.                     <units/>  
  257.                     <delta>0</delta>  
  258.                     <snmpv3_contextname/>  
  259.                     <snmpv3_securityname/>  
  260.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  261.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  262.                     <snmpv3_authpassphrase/>  
  263.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  264.                     <snmpv3_privpassphrase/>  
  265.                     <formula>1</formula>  
  266.                     <delay_flex/>  
  267.                     <params/>  
  268.                     <ipmi_sensor/>  
  269.                     <data_type>0</data_type>  
  270.                     <authtype>0</authtype>  
  271.                     <username/>  
  272.                     <password/>  
  273.                     <publickey/>  
  274.                     <privatekey/>  
  275.                     <port/>  
  276.                     <description/>  
  277.                     <inventory_link>0</inventory_link>  
  278.                     <applications>  
  279.                         <application>  
  280.                             <name>tcp status</name>  
  281.                         </application>  
  282.                     </applications>  
  283.                     <valuemap/>  
  284.                     <logtimefmt/>  
  285.                 </item>  
  286.                 <item>  
  287.                     <name>tcp timewait</name>  
  288.                     <type>0</type>  
  289.                     <snmp_community/>  
  290.                     <multiplier>0</multiplier>  
  291.                     <snmp_oid/>  
  292.                     <key>tcp[TIMEWAIT]</key>  
  293.                     <delay>30</delay>  
  294.                     <history>90</history>  
  295.                     <trends>365</trends>  
  296.                     <status>0</status>  
  297.                     <value_type>3</value_type>  
  298.                     <allowed_hosts/>  
  299.                     <units/>  
  300.                     <delta>0</delta>  
  301.                     <snmpv3_contextname/>  
  302.                     <snmpv3_securityname/>  
  303.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  304.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  305.                     <snmpv3_authpassphrase/>  
  306.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  307.                     <snmpv3_privpassphrase/>  
  308.                     <formula>1</formula>  
  309.                     <delay_flex/>  
  310.                     <params/>  
  311.                     <ipmi_sensor/>  
  312.                     <data_type>0</data_type>  
  313.                     <authtype>0</authtype>  
  314.                     <username/>  
  315.                     <password/>  
  316.                     <publickey/>  
  317.                     <privatekey/>  
  318.                     <port/>  
  319.                     <description/>  
  320.                     <inventory_link>0</inventory_link>  
  321.                     <applications>  
  322.                         <application>  
  323.                             <name>tcp status</name>  
  324.                         </application>  
  325.                     </applications>  
  326.                     <valuemap/>  
  327.                     <logtimefmt/>  
  328.                 </item>  
  329.             </items>  
  330.             <discovery_rules/>  
  331.             <macros/>  
  332.             <templates/>  
  333.             <screens/>  
  334.         </template>  
  335.     </templates>  
  336.     <graphs>  
  337.         <graph>  
  338.             <name>linux tcp status</name>  
  339.             <width>900</width>  
  340.             <height>200</height>  
  341.             <yaxismin>0.0000</yaxismin>  
  342.             <yaxismax>100.0000</yaxismax>  
  343.             <show_work_period>1</show_work_period>  
  344.             <show_triggers>1</show_triggers>  
  345.             <type>0</type>  
  346.             <show_legend>1</show_legend>  
  347.             <show_3d>0</show_3d>  
  348.             <percent_left>0.0000</percent_left>  
  349.             <percent_right>0.0000</percent_right>  
  350.             <ymin_type_1>0</ymin_type_1>  
  351.             <ymax_type_1>0</ymax_type_1>  
  352.             <ymin_item_1>0</ymin_item_1>  
  353.             <ymax_item_1>0</ymax_item_1>  
  354.             <graph_items>  
  355.                 <graph_item>  
  356.                     <sortorder>0</sortorder>  
  357.                     <drawtype>0</drawtype>  
  358.                     <color>1A7C11</color>  
  359.                     <yaxisside>0</yaxisside>  
  360.                     <calc_fnc>2</calc_fnc>  
  361.                     <type>0</type>  
  362.                     <item>  
  363.                         <host>Templates tcp status ss</host>  
  364.                         <key>tcp[ESTAB]</key>  
  365.                     </item>  
  366.                 </graph_item>  
  367.                 <graph_item>  
  368.                     <sortorder>1</sortorder>  
  369.                     <drawtype>0</drawtype>  
  370.                     <color>F63100</color>  
  371.                     <yaxisside>0</yaxisside>  
  372.                     <calc_fnc>2</calc_fnc>  
  373.                     <type>0</type>  
  374.                     <item>  
  375.                         <host>Templates tcp status ss</host>  
  376.                         <key>tcp[FINWAIT1]</key>  
  377.                     </item>  
  378.                 </graph_item>  
  379.                 <graph_item>  
  380.                     <sortorder>2</sortorder>  
  381.                     <drawtype>0</drawtype>  
  382.                     <color>2774A4</color>  
  383.                     <yaxisside>0</yaxisside>  
  384.                     <calc_fnc>2</calc_fnc>  
  385.                     <type>0</type>  
  386.                     <item>  
  387.                         <host>Templates tcp status ss</host>  
  388.                         <key>tcp[FINWAIT2]</key>  
  389.                     </item>  
  390.                 </graph_item>  
  391.                 <graph_item>  
  392.                     <sortorder>3</sortorder>  
  393.                     <drawtype>0</drawtype>  
  394.                     <color>A54F10</color>  
  395.                     <yaxisside>0</yaxisside>  
  396.                     <calc_fnc>2</calc_fnc>  
  397.                     <type>0</type>  
  398.                     <item>  
  399.                         <host>Templates tcp status ss</host>  
  400.                         <key>tcp[LASTACK]</key>  
  401.                     </item>  
  402.                 </graph_item>  
  403.                 <graph_item>  
  404.                     <sortorder>4</sortorder>  
  405.                     <drawtype>0</drawtype>  
  406.                     <color>FC6EA3</color>  
  407.                     <yaxisside>0</yaxisside>  
  408.                     <calc_fnc>2</calc_fnc>  
  409.                     <type>0</type>  
  410.                     <item>  
  411.                         <host>Templates tcp status ss</host>  
  412.                         <key>tcp[LISTEN]</key>  
  413.                     </item>  
  414.                 </graph_item>  
  415.                 <graph_item>  
  416.                     <sortorder>5</sortorder>  
  417.                     <drawtype>0</drawtype>  
  418.                     <color>6C59DC</color>  
  419.                     <yaxisside>0</yaxisside>  
  420.                     <calc_fnc>2</calc_fnc>  
  421.                     <type>0</type>  
  422.                     <item>  
  423.                         <host>Templates tcp status ss</host>  
  424.                         <key>tcp[SYNRECV]</key>  
  425.                     </item>  
  426.                 </graph_item>  
  427.                 <graph_item>  
  428.                     <sortorder>6</sortorder>  
  429.                     <drawtype>0</drawtype>  
  430.                     <color>AC8C14</color>  
  431.                     <yaxisside>0</yaxisside>  
  432.                     <calc_fnc>2</calc_fnc>  
  433.                     <type>0</type>  
  434.                     <item>  
  435.                         <host>Templates tcp status ss</host>  
  436.                         <key>tcp[TIMEWAIT]</key>  
  437.                     </item>  
  438.                 </graph_item>  
  439.             </graph_items>  
  440.         </graph>  
  441.     </graphs>  
  442. </zabbix_export> 
浏览2934 评论0
返回
目录
返回
首页
ora-0100 maximum opencursors exceeded差错打开游标过大的解决 zabbix监控TCP连接状态