Zabbix監控案例

使用192.168.1.102實驗

目的:調用自己編寫的shell腳本,監控數據

案例一、監控nginx服務的運行狀態

1.1、環境準備

  1. 運行zabbix_agentd服務 並啓用的自定義監控項功能
[root@host102 ~]# yum -y install gcc pcre-devel
[root@host102 ~]# tar -zxf zabbix-3.4.4.tar.gz 
[root@host102 ~]# cd zabbix-3.4.4/
[root@host102 zabbix-3.4.4]# ./configure  --enable-agent
[root@host102 zabbix-3.4.4]# make install 
[root@host102 ~]# vim /usr/local/etc/zabbix_agentd.conf
93 Server=127.0.0.1,192.168.2.5
134 ServerActive=192.168.2.5:10051
265 Include=/usr/local/etc/zabbix_agentd.conf.d/ *.conf
280 UnsafeUserParameters=1

[root@host102 ~]# useradd zabbix
[root@host102 ~]# zabbix_agentd 
[root@host102 ~]# netstat -utnlp | grep 10050
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      9130/zabbix_agentd  
  1. 安裝源碼nginx軟件並加載狀態模塊
[root@host102 ~]# rpm -q gcc pcre-devel zlib-devel
gcc-4.8.5-28.el7.x86_64
pcre-devel-8.32-17.el7.x86_64
package zlib-devel is not installed
[root@host102 ~]# yum -y install zlib-devel
[root@host102 ~]# tar -zxf nginx-1.12.2.tar.gz 
[root@host102 ~]# cd nginx-1.12.2/

[root@host102 nginx-1.12.2]# ./configure --with-http_stub_status_module

[root@host102 ~]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@host102 ~]# 
[root@host102 ~]# vim /usr/local/nginx/conf/nginx.conf
	server {
        	location /status {
            stub_status on; 
       		} 

[root@host102 ~]# /usr/local/nginx/sbin/nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@host102 ~]# systemctl  stop httpd
[root@host102 ~]# systemctl  disable httpd
[root@host102 ~]# /usr/local/nginx/sbin/nginx
[root@host102 ~]# netstat  -utnlp  | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11731/nginx: master 

[root@host102 ~]# curl http://localhost/status
Active connections: 1 	//實時連接數
server accepts handled requests
 1 1 1 		//歷史累計連接數量、處理連接數量、處理請求數量
Reading: 0 Writing: 1 Waiting: 0	//讀、寫、等待

1.2、編寫監控腳本

  1. 編寫
[root@host102 ~]# vim /usr/local/bin/nginx_status.sh
#!/bin/bash	
case $1 in
"shishi") 	#獲取實時連接數
	curl -s  http://localhost/status | awk 'NR==1{print $3}' ;;

 "lishi")	#歷史累計連接數量
	curl -s  http://localhost/status | awk 'NR==3{print $1}' ;;

"Waiting")	#等待數量
 	curl -s  http://localhost/status | awk 'NR==4{print $6}' ;;
esac
  1. 測試
[root@host102 ~]# /usr/local/bin/nginx_status.sh shishi
[root@host102 ~]# /usr/local/bin/nginx_status.sh lishi
[root@host102 ~]# /usr/local/bin/nginx_status.sh Waiting

1.3、定義監控服務使用命令

  1. 把shell腳本定義爲zabbix_agentd服務可以使用命令
[root@host102 ~]# vim /usr/local/etc/zabbix_agentd.conf.d/x.conf
UserParameter=nginx_status[*],/usr/local/bin/nginx_status.sh $1
  1. 重啓服務
[root@host102 ~]# killall -9 zabbix_agentd
[root@host102 ~]# killall -9 zabbix_agentd
zabbix_agentd: no process found
[root@host102 ~]# zabbix_agentd
[root@host102 ~]# netstat  -utnlp | grep 10050
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      12003/zabbix_agentd 
  1. 測試命令
[root@host102 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k nginx_status[lishi]
32
[root@host102 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k nginx_status[shishi]
1
[root@host102 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k nginx_status[Waiting]
0

1.4、web頁面配置

  1. 創建新模板 ATMP2
    在這裏插入圖片描述在這裏插入圖片描述

  2. 創建新應用集 status
    在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

  3. 創建監控項 名 監控命令
    sum_link nginx_status[lishi]
    now_link nginx_status[shishi]
    waiting_link nginx_status[Waiting]
    在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

  4. 監控200主機,調用新創建的模板 ATMP2
    在這裏插入圖片描述在這裏插入圖片描述

  5. 查看監控數據
    在這裏插入圖片描述

案例二、監控服務器的TCP連接狀態

2.1、概述

客戶端與服務器建立連接時的連接方式: TCP 和 UDP
TCP標記位:SYN ACK FIN
TCP連接狀態

  • 連接時三次握手
    在這裏插入圖片描述
  • 斷開時四次握手
    在這裏插入圖片描述

2.2、編寫監控腳本

[root@host102 ~]# vim /usr/local/bin/net_status.sh
#!/bin/bash
case "$1" in
"estab")
	ss -antp | awk  'BEGIN{z=0}/^ESTAB/{z++}END{print z}';;
"timewait")
	ss -antp | awk  'BEGIN{y=0}/^TIME-WAIT/{y++}END{print y}';;
"closewait")
	ss -antp | awk  'BEGIN{x=0}/^CLOSE_WAIT/{x++}END{print x}';;
esac

[root@host102 ~]# chmod +x  /usr/local/bin/net_status.sh

3.3、定義監控服務使用命令

  1. 定義命令
[root@host102 ~]# vim /usr/local/etc/zabbix_agentd.conf.d/x.conf
UserParameter=net_status[*],/usr/local/bin/net_status.sh $1
  1. 重啓服務
[root@host102 ~]# killall -9 zabbix_agentd
[root@host102 ~]# killall -9 zabbix_agentd
zabbix_agentd: no process found
[root@host102 ~]# zabbix_agentd 
[root@host102 ~]# netstat  -utnlp  | grep 10050
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      14368/zabbix_agentd 
  1. 測試命令
[root@host102 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k net_status[closewait]
0
[root@host102 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k net_status[timewait]
44
[root@host102 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k net_status[estab]
3

3.4、web頁面配置

  1. 在ATMP2模板裏添加新應用集 TCP_STATUS
    在這裏插入圖片描述

  2. 創建新的監控項
    名 命令
    close_wait net_status[closewait]
    time_wait net_status[timewait]
    ESTAB net_status[estab]

在這裏插入圖片描述

  1. 查看監控數據

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章