linux:linux
在linux上手動得到tcp鏈接數的命令爲:bash
netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' #ESTABLISHED socket已經創建鏈接 #CLOSED socket沒有被使用,無鏈接 #CLOSING 服務器端和客戶端都同時關閉鏈接 #CLOSE_WAIT 等待關閉鏈接 #TIME_WAIT 表示收到了對方的FIN報文,併發送出了ACK報文,等待2MSL後就可回到CLOSED狀態 #LAST_ACK 遠端關閉,當前socket被動關閉後發送FIN報文,等待對方ACK報文 #LISTEN 監聽狀態 #SYN_RECV 接收到SYN報文 #SYN_SENT 已經發送SYN報文
在zabbix_agent端編輯配置文件:服務器
vi /etc/zabbix/zabbix_agentd.conf Include=/etc/zabbix/zabbix_agentd.d/
#編寫一個獲取tcp狀態的腳本: mkdir /etc/zabbix/zabbix_agentd.d/script #此處注意,必定不要將腳本直接放到/etc/zabbix/zabbix_agentd.d/目錄下,不然zabbix_agent會把它當作本身的配置文件執行,形成重啓不成功 vi /etc/zabbix/zabbix_agentd.d/script/tcp_status.sh #!/bin/bash #this script is get tcp status tmp_file=/tmp/tcp_status.txt /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' > $tmp_file case $1 in closed) output=$(awk '/CLOSED/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; listen) output=$(awk '/LISTEN/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; synrecv) output=$(awk '/SYN_RECV/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; synsent) output=$(awk '/SYN_SENT/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; established) output=$(awk '/ESTABLISHED/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; timewait) output=$(awk '/TIME_WAIT/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; closing) output=$(awk '/CLOSING/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; closewait) output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; lastack) output=$(awk '/LAST_ACK/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; finwait1) output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; finwait2) output=$(awk '/FIN_WAIT2/{print $2}' $tmp_file) if [ "$output" == "" ];then echo 0 else echo $output fi ;; *) echo -e "\e[033mUsage: sh $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m" esac
#賦予腳本可執行權限 chmod +x /etc/zabbix/zabbix_agentd.d/script/tcp_status.sh
編寫zabbix_agent上的自定義key:併發
vi /etc/zabbix/zabbix_agentd.conf UserParameter=tcp.status[*],/etc/zabbix/zabbix_agentd.d/script/tcp_status.sh $1
重啓zabbix_agent:
socket
/etc/init.d/zabbix_agent restart
在服務端測試輸入參數返回的值:tcp
zabbix_get -s 118.190.114.140 -p 10050 -k tcp.status[listen]
最後在zabbix上建立監控項和觸發器,附件中上傳了一個附件,裏面是tcp_status模板,直接在配置——模板——導入便可。ide