zabbix-進階-2

zabbix進階

zabbix宏和用戶自定義監控(02)_

zabbix發現、自動註冊和web監控(03)_

zabbix分佈式監控(04)_

 

1. web宏和自定義監控

要求宏的名字只能使用大寫字母數字下劃線只能以大寫字母開頭//不能有小寫

zabbix有許多內置的宏

宏定義zabbix支持在全局模板或主機級別使用用戶自定義的宏,(user marco

引用用戶自定義宏要使用{$MACRO}調用宏,//系統定義的宏,不需要使用$

應用應用在item keysdescriptiontrigger名稱和表達式上主機接口IP/DNS及端口discovery機制SNMP協議的相關信息上

分類全局宏模板宏主機級別宏

宏替換次序主機級別-à主機上的一級模板{多個模板按其ID號排序}----à二級模板--。。。

全局宏

自定義宏的創建

全局宏Administration----general----Macros//自定義宏

主機或模板級別的宏編輯響應的主機或模板的屬性即可 

administrator—template-àmacros //模板級別的宏

//在模板中定義宏假如以後需要修改的話只需要修改宏即可不在需要修改值

測試

在自定義模板中configurationàtemplates---新建的模板名 macros {$NETINLIMIT} =>  8000

configurationàtemplates---新建的模板名triggers--expression{10.0.0.201:net.if.in[ieno16777736].last(,0)}>{$NETINLIMIT} //直接使用即可

創建宏的方法

1. configuration—template—模板名{Macros} //定義宏
//如果使用的是模板,則模板中的macro大於admin中的Macro

2. Administrator—General---右側選擇Macros---創建一個宏即可

 

 

用戶自定義參數Userparameter功能

//agent上執行腳本

free | awk '/^Mem:/{print $4}' //獲取空閒空間

語法UserParameter=<key>,<command> //多個key的話,使用;隔開

一般來說返回的數據量不能大於512k,不能返回太大的數據

vim  /etc/zabbix/agntd.conf 修改該配置文件即可,在agentd

UserParameter=memory.free,/usr/bin/free | awk '/^Mem:/{pring $4}'  //這樣就會有一個返回值,返回退出,然後重啓agent

server使用zabbix_get -s 10.0.0.201 -k "memory.free" //可以測試得到大小

//server上添加item對應keymemory.free

//最後在monitoring---latest data上可以看到效果

//但是獲取到的數據是以k計算的

修改item---use custom information---1024//,然後清空數據,會自動變成,再次查看lastest data會自動變成 M

清空數據

configuration—hsots---選中主機名---最後一個clear  history and trends //清空數據

cat /proc/meminfo | awk '/^MemFree/{print $2}' //也可以獲取內存還剩多少

UserParameter=key[*],command //其實可以接受多個參數

接受多個參數

vim /etc/zabbix/zabbix_agentd.conf

UserParameter=memory.usage[*],/bin/cat /proc/meminfo | awk  ‘/^$1:/{print $$2}’

//cat命令的執行結果就是函數的返回值

//命令自身也有變量的話,需要在$之前再加一個$, awk一定要使用單引號

zabbix_get -s 10.0.0.201 -k "memory.usage[MemTotal]" //在服務器端測試

這樣在/etc/proc/meminfo  的信息都能夠被採集到

//作業

創建多個item實現監控agentmemory的使用情況

注意在此之前刪除mem的相關item

監控nginx的信息:

例如:監控nginx的各種信息

 

1實驗parameter

UserParameter=Nginx.active[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^Active/ {print $NF}'

UserParameter=Nginx.reading[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Reading' | cut -d" " -f2

UserParameter=Nginx.writing[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Writing' | cut -d" " -f4

UserParameter=Nginx.waiting[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Waiting' | cut -d" " -f6

UserParameter=Nginx.accepted[*], /usr/bin/curl -s "http://$1:$2/status" |

awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$1}'

//開頭是數字

UserParameter=Nginx.handled[*], /usr/bin/curl -s "http://$1:$2/status" |

awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$2}'

UserParameter=Nginx.requests[*], /usr/bin/curl -s "http://$1:$2/status" |

awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$3}'

活動的個數 //$1,$2 服務器地址和端口

readingnginx讀到客戶端的Header信息數

writing返回給客戶端Heade信息數

waiting開啓keep-alive的情況下這個值active – ( reading + writing) ,意思是Nginx已經處理完正在等候下次請求指令的駐留連接數


 

實驗監控nginix

1. yum install nginx

vim /etc/nginx/conf.d/default.conf

啓用nginx status

server {      //server

。。。。。

stub_status on;

location  /status {   

access_log off;

       allow 127.0.0.1;

        allow 10.0.0.0/24;

        deny all;}

。。。。

}

service nginx start

curl http://127.0.0.1/status       //測試一下

2. agentvim /etc/zabbix/zabbix_agentd.d/nginx.conf //新建的配置文件

1實驗parameter貼上

UserParameter=Nginx.active[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^Active/ {print $NF}'

UserParameter=Nginx.reading[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Reading' | cut -d" " -f2

UserParameter=Nginx.writing[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Writing' | cut -d" " -f4

UserParameter=Nginx.waiting[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Waiting' | cut -d" " -f6

//前三個都需要減去前值後三個不需要減去前值delta

UserParameter=Nginx.accepted[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$1}'

UserParameter=Nginx.handled[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$2}'

UserParameter=Nginx.requests[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$3}'

server上測試

zabbix_get -s 10.0.0.201 -k "Nginx.accepted[10.0.0.201,80]"

//也就是說  s agent測試也爲自己

3. 創建item

name nginx-1

key Nginx.accepted[{HOST.IP},80]  //HOST.IP是一個宏

Nginx. handled [{10.0.0.201},80] //第二個item

Nginx.active[{10.0.0.201},80]

Store valuedelta

New Applicationnginx-status


4. 假如要使用腳本的話, 一次只能返回一個值,而且是單個,其次,

腳本放在

rpm –ql zabbix-agent  //server端, 默認路徑/usr/lib/zabbix/externalscripts

agent放在任何路徑下都可以


5. 添加自定義腳本,agent上,實現監控

要求

1. 必須有輸出值

2. zabbix用戶必須有執行權限或者777

3. 接受的參數$1--$9

6.實現

vim  zabbix_agentd.conf

UnsafeUserParameters=1 //自定義腳本的時候需要開啓

UserParameter=test.icmp[*],/etc/zabbix/script/2.sh $1

出現錯誤item

很有可能是因爲採集的數值不匹配//創建item時,採用的單位不對

pklossrestime   type of informationfloat

units %   ms 

2.sh腳本

#!/bin/bash

count=1

server=`grep -i "^server" /etc/zabbix/zabbix_agentd.conf | head -1 |cut -d= -f2`

# get the server ip

data=`ping -c $count  $server`

case $1 in

"pkloss")

echo $data | grep loss | head -1 | awk -F, '{print $3}'| cut -d% -f1 | cut -d' ' -f2

;;

"restime")

echo  $data | grep rtt |cut -d/ -f5

;;

*)

esac

 

3.腳本

#!/bin/bash

count=1

server=`grep -i "^server" /etc/zabbix/zabbix_agentd.conf | head -1 |cut -d= -f2`

# get the server ip

data=`ping -c $count  $server`

case $1 in

"pkloss")

echo $data | grep loss | head -1 | awk -F, '{print $3}'| cut -d% -f1 | cut -d' ' -f2

;;

"restime")

echo  $data | grep rtt |cut -d/ -f5

;;

"min")

echo $data |  awk -Frtt '{print $2}' | tail -1 | cut -d/ -f4 |cut -d' ' -f3

;;

"max")

echo $data | awk -Frtt '{print $2}' | tail -1 | cut -d/ -f6

;;

*)

esac

 

7. 附加基於UDP的測試,還有其他指標

cat a | awk -Frtt '{print $2}' | tail -1 | cut -d/ -f4 |cut -d' ' -f3min

cat a | awk -Frtt '{print $2}' | tail -1 | cut -d/ -f6max

 

 

 

 

 

任務

1.安裝hping3

寫一個腳本調用 udpnock實現,userparameter的功能

wget http://www.hping.org/hping3-20051105.tar.gz  //使用源碼安裝

1. yum install gcc  

2. yum install libpcap-devel  

3. yum install tcl-devel 

4. vim   bytesex.h

wKiom1gmmS_Tt5MuAAAJa1jN5Ho999.png

 ln -s /usr/include/pcap-bpf.h /usr/include/net/bpf.h   //爲其創建鏈接文件

./configure

make && make install

 

問題1

touch: 無法創建"/tmp/a.tmp": 權限不夠

方案使用變量保存起來

 

問題2udpnock 用法

/etc/zabbix/bin/udpnock  -c  10  -i  40  -P 5900 60.205.230.140 5130

-c 請求次數

-i :源端口,ip,目標端口

-P

/udpnock的使用方法:

agent上,實現,nock測試 //端口是否是指定的,本地的端口是自定義打開的,但是對端的端口是需要手動打開的,而且要接受參數

 

hping3的使用方法

hping

-c :發送多少個

-i 間隔默認是s可以使用msus

-n 不進行lookup

-q 只顯示最後的統計數據

-z ctrl+z增減發送出去包的TTL,按一次增加1,按兩次增加2,按住不放過是減少TTL

-0|-1|-2|-8| :RAW IP模式ICMPUDP掃描模式默認tcp

//all所有端口1-65535known/etc/service記錄的所有端口,

//多個不相連的端口1,4,443,445-224

//!kown 取反

-9 : --listen signature //監聽模式,監聽含有特定signature的包並且將含有該signature的包dump

-a :(--spoof hostname)僞裝自己的地址,會造成自己收不到返回的包,在進行idle掃描時候有用,// ip地址是成功的僞裝了,但是mac還是自己的

--rand-source : 使用隨機地址僞裝自己的地址

--rand-dest : 隨機選擇目標

-t :制定包的TTL ,具體要設定爲多大要看是做什麼,最大是255

 -r : 顯示id的增量,而不顯示id

 hping3 192.168.0.107 -m 8 -f  //-mmtu-f fragmentmtu默認是16kbyte,當發送包的大小大於16kbyte的時候,就進行fragment

 -G--rroute 顯示路由

-s:(--baseport)設定發送包的最開始的端口,如果要使用一個固定的端口的話,就要加上 -k--keep

hping –i  u1 10.0.0.1 //每一us發送一個 ,這是一種***行爲

hping3 -8 known -S 192.168.0.1  <---這裏-S 表示採用SYN掃描

hping3 -8 1-1024 -S 192.168.0.1 //指定掃描端口

hping3 -a www.baidu.com 192.168.0.107  //自己是收不到包的會返回給192.168.0.107

hping3 --rand-source 192.168.0.107  //在防火牆貨這路由器上可以看到效果

hping3  --rand-dest -I eth0 192.168.0.x   <--使用x代表0-255,要是在整個網絡中選擇目標可以換成4x;在這種模式下一定要制定interface

hping3 192.168.0.107 -m 8 -f 

hping -G www.baidu.com 

hping3 -s 2 192.168.0.107

 

iperf的安裝和使用

wget https://iperf.fr/download/fedora/iperf3-3.1.3-1.fc24.x86_64.rpm

yum localinstall


在服務端運行iperf,輸入命令iperf –s –p 12345 –i 1 以在本機端口12345上啓用iperf

客戶端:iperf –c server-ip –p server-port –i 1 –t 10 –w 20K

-c客戶端模式後面是server-ip

-pserver的監聽端口

-i帶寬報告的時間間隔s

-t設定測試的時長默認爲s

-w設置tcp窗口大小,一般可以不用設置,默認即可

//以上爲單線程TCP

iperf –c 10.0.0.200  -p 12345 –i 1 –t 3 –P 2 //啓動兩個線程 ,客戶單

iperf –s  -p 12345 –m –i 1 //server


UDP測試

iperf –s -u –p 12345 –i 1 以在本機端口12345上啓用iperf,並運行於udp模式

iperf -c server-ip -p server-port -i 1 -t 10 –b

-c:以什麼身份運行

-b:設置udp的發送帶寬單位bit/s

顯示的內容

Jitter抖動  Lost/Total:丟包率Datagrams爲包數量


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