前段時間在公司IDC伺服器上部署了zabbix3.0.3監控系統,除了自帶的記憶體/頻寬/CPU負載等系統資源監控模板以及mysql監控模板外,接下來對諸如nginx、php、memcache、磁碟IO的監控項配置在此做下記錄,希望能幫助到有用到的朋友們~
nginx的監控模板:zbx_nginx_templates.xml
php的監控模板:zbx_php-fpm_templates.xml
memcache的監控模板:zbx_memcached_templates.xml (這是11211埠監控模板,其他埠照此模板更換埠即可)
Low-level discovery監控磁碟IO的監控模板:Template Linux DiskIO.xml
以上四個監控項的模板的下載地址:http://pan.baidu.com/s/1geAMZfl (提取密碼:zpyx)
--------------------------------------------------------------------------------------------------
一、新增nginx的監控
1)首先在客戶機的nginx配置檔案裡新增:
[root@115r ~]# vim www.world.huanqiu.conf
.......
location /nginx_status {
stub_status on;
access_log off;
}
........
新增後,重啟nginx服務:
[root@115r ~]#/usr/local/nginx/sbin/nginx -s reload
2)zabbix監控nginx的自定義鍵值
在客戶機的zabbix的monitor_scripts目錄下新增nginx-status.sh檔案,進行如下配置(這個配置可以適用於其他機器的nginx監控)
[root@115r ~]# cd /usr/local/zabbix/monitor_scripts/
[root@115r monitor_scripts]#cat nginx-status.sh
#!/bin/bash
##################################
# Zabbix monitoring script
#
# nginx:
# - anything available via nginx stub-status module
#
##################################
# Contact:
# vincent.viallet@gmail.com
##################################
# ChangeLog:
# 20100922 VV initial creation
##################################
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
URL="http://127.0.0.1/nginx_status"
WGET_BIN="/usr/bin/wget"
#
# Error handling:
# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
# - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.9900"
ERROR_NO_ACCESS="-0.9901"
ERROR_WRONG_PARAM="-0.9902"
ERROR_DATA="-0.9903" # either can not connect / bad host / bad port
# save the nginx stats in a variable for future parsing
NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
# error during retrieve
if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
echo $ERROR_DATA
exit 1
fi
#
# Extract data from nginx stats
#
case $ZBX_REQ_DATA in
active_connections) echo "$NGINX_STATS" | head -1 | cut -f3 -d' ';;
accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';;
handled_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';;
handled_requests) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';;
reading) echo "$NGINX_STATS" | tail -1 | cut -f2 -d' ';;
writing) echo "$NGINX_STATS" | tail -1 | cut -f4 -d' ';;
waiting) echo "$NGINX_STATS" | tail -1 | cut -f6 -d' ';;
*) echo $ERROR_WRONG_PARAM; exit 1;;
esac
exit 0
3)配置nginx-status的key檔案
《前提是/usr/local/zabbix/etc/zabbix_agentd.conf裡已經定義了Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/的路徑了》
[root@115r ~]# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@115r zabbix_agentd.conf.d]# cat nginx-status.conf
UserParameter=nginx[*],/usr/local/zabbix/monitor_scripts/nginx-status.sh "$1"
重啟zabbix
[root@fangfull_web2 ~]#pkill -9 zabbix_agent
[root@fangfull_web2 ~]#/usr/local/zabbix/sbin/zabbix_agent
訪問web的status進行檢視試試
接著在zabbix server端進行測試
如下結果,說明zabbxi的server端能成功接收到agent客戶端的資料(121.201.114.116是上面客戶機的ip)
[root@Zabbix-server ~]# /usr/local/zabbix/bin/zabbix_get -s 121.201.114.116 -p 10050 -k "nginx"
-0.9902
4)最後,登入zabbix介面,新增nginx的監控模板zbx_nginx_templates.xml
將nginx監控模板下載到本地,在zabbix裡面匯入模板:
接著在相應監控主機的監控配置中連線這個模板
這樣,nginx的zabbix監控配置就完成了。下面是nginx的兩個監控項的效果圖:
--------------------------------------------------------------------------------------------------
二、新增php的監控
1)配置php-fpm狀態頁。客戶機配置如下:
[root@115r ~]# cd /usr/local/php/etc/
[root@115r ~]# vim php-fpm.conf 新增下面的內容
.........
.........
pm.status_path = /status
然後重啟php-fpm服務
[root@115r ~]# pkill -9 php-fpm
[root@115r ~]# /usr/local/php/sbin/php-fpm
2)配置nginx,新增如下內容
[root@115r ~]# vim www.world.huanqiu.conf
.........
.........
location ~ ^/(status|ping)$
{
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
新增後,重啟nginx服務:
[root@115r ~]# /usr/local/nginx/sbin/nginx -s reload
測試,出現如下結果即可:
[root@115r ~]# curl http://127.0.0.1/status
pool: www
process manager: dynamic
start time: 20/Jul/2016:19:01:37 +0800
start since: 3889550
accepted conn: 59309528
listen queue: 0
max listen queue: 1
listen queue len: 128
idle processes: 99
active processes: 1
total processes: 100
max active processes: 36
max children reached: 0
slow requests: 0
3)zabbix監控php的自定義鍵值
在客戶機的zabbix的monitor_scripts下新增php-fpm-status.sh監控指令碼(適用於其他機器的php監控)
[root@115r ~]# cd /usr/local/zabbix/monitor_scripts/
[root@115r monitor_scripts]# cat php-fpm-status.sh
#!/bin/bash
##################################
# Zabbix monitoring script
#
# php-fpm:
# - anything available via FPM status page
#
##################################
# Contact:
# vincent.viallet@gmail.com
##################################
# ChangeLog:
# 20100922 VV initial creation
##################################
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
# FPM defaults
URL="http://127.0.0.1/status"
WGET_BIN="/usr/bin/wget"
#
# Error handling:
# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
# - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.9900"
ERROR_NO_ACCESS="-0.9901"
ERROR_WRONG_PARAM="-0.9902"
ERROR_DATA="-0.9903" # either can not connect / bad host / bad port
# save the FPM stats in a variable for future parsing
FPM_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
# error during retrieve
if [ $? -ne 0 -o -z "$FPM_STATS" ]; then
echo $ERROR_DATA
exit 1
fi
#
# Extract data from FPM stats
#
RESULT=$(echo "$FPM_STATS" | sed -n -r "s/^$ZBX_REQ_DATA: +([0-9]+)/\1/p")
if [ $? -ne 0 -o -z "$RESULT" ]; then
echo $ERROR_WRONG_PARAM
exit 1
fi
echo $RESULT
exit 0
接著,配置php-fpm-status.sh的key檔案
《前提是/usr/local/zabbix/etc/zabbix_agentd.conf裡已經定義了Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/的路徑了》
[root@115r ~]# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@115r zabbix_agentd.conf.d]# cat php-fpm-status.conf
UserParameter=php-fpm[*],/usr/local/zabbix/monitor_scripts/php-fpm-status.sh "$1"
保證以上檔案的屬性(zabbix.zabbix)
然後在zabbix server端測試是否能正常接收agent客戶端的資料(121.201.114.116是上面客戶機的ip)
如下結果說明能正常監控~
[root@Zabbix-server ~]# /usr/local/zabbix/bin/zabbix_get -s 121.201.114.116 -p 10050 -k "php-fpm"
-0.9902
4)最後,登入zabbix介面,新增nginx的監控模板zbx_php-fpm_templates.xml
將php監控模板下載到本地,在zabbix裡面匯入模板,然後在相對應的主機監控配置裡面將這個php-fpm的監控模板連線上(如上面新增nginx模板步驟一樣,在此省略~)
這樣,php的zabbix監控配置就完成了。
下面是php監控項的效果圖:
--------------------------------------------------------------------------------------------------
三、新增memcached的監控 (下面是11211埠的memcache監控,如果是其他埠,照此操作流程更換埠即可!)
1)zabbix監控memcache的自定義鍵值
在/usr/local/zabbix/monitor_scripts目錄下新增memcached監控指令碼:
[root@115r ~]# cd /usr/local/zabbix/monitor_scripts/
[root@115r monitor_scripts]# cat memcached-11211status.sh
#!/bin/bash
item=$1
ip=127.0.0.1
port=11211
(echo "stats";sleep 0.5) | telnet $ip $port 2>/dev/null | grep "STAT $item\b" | awk '{print $3}'
2)新增memcached-11211status.sh的key檔案
《前提是/usr/local/zabbix/etc/zabbix_agentd.conf裡已經定義了Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/的路徑了》
[root@115r ~]# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@115r zabbix_agentd.conf.d]# cat memcache-11211status.conf
UserParameter=memcached.stat[*],/usr/local/zabbix/monitor_scripts/memcached-11211status.sh "$1"
3)登入zabbix介面,新增memcache監控模板zbx_memcached_templates.xml
將memcache監控模板下載到本地,在zabbix裡面匯入模板,然後在相對應的主機監控配置裡面將這個memcache的監控模板連線上(如上面新增nginx模板步驟一樣,在此省略~)
memcache的監控效果圖如下:
如果要換成監控其他埠的,就將模組和配置中的11211埠更新為其他埠,按照上面的步驟操作就ok~
注意,上面的檔案許可權都是zabbix.zabbix
***************************************************************************************************************
這裡順便記錄下:zabbix監控memcache多埠的操作
(1)修改指令碼指令碼
下面依次是11211,11212,11213埠的配置
[root@xw4 monitor_scripts]# pwd
/usr/local/zabbix/monitor_scripts
[root@xw4 monitor_scripts]# cat memcached-11211status.sh
#!/bin/bash
item=$1
ip=127.0.0.1
port=11211
(echo "stats";sleep 0.5) | telnet $ip $port 2>/dev/null | grep "STAT $item\b" | awk '{print $3}'
[root@xw4 monitor_scripts]# cat memcached-11212status.sh
#!/bin/bash
item=$1
ip=127.0.0.1
port=11212
(echo "stats";sleep 0.5) | telnet $ip $port 2>/dev/null | grep "STAT $item\b" | awk '{print $3}'
[root@xw4 monitor_scripts]# cat memcached-11213status.sh
#!/bin/bash
item=$1
ip=127.0.0.1
port=11213
(echo "stats";sleep 0.5) | telnet $ip $port 2>/dev/null | grep "STAT $item\b" | awk '{print $3}'
(2)修改key檔案的配置
下面依次是11211,11212,11213埠的配置
[root@xw4 zabbix_agentd.conf.d]# pwd
/usr/local/zabbix/etc/zabbix_agentd.conf.d
[root@xw4 zabbix_agentd.conf.d]# cat memcache-11211status.conf
UserParameter=memcached.stat[*],/usr/local/zabbix/monitor_scripts/memcached-11211status.sh "$1"
[root@xw4 zabbix_agentd.conf.d]# cat memcache-11212status.conf
UserParameter=memcached11212.stat[*],/usr/local/zabbix/monitor_scripts/memcached-11212status.sh "$1"
[root@xw4 zabbix_agentd.conf.d]# cat memcache-11213status.conf
UserParameter=memcached11213.stat[*],/usr/local/zabbix/monitor_scripts/memcached-11213status.sh "$1"
一定要注意上面三個key檔案中的UserParameter名字不能重複,這個是key名稱!
另外:
在11211,11212,11213三個埠的監控模板裡,要做如下兩個修改:
a)修改key的名稱,即將複製過來的模組裡的key名稱改一下
memcached.stat修改成上面key檔案中定義的名稱!!!!這個很重要!!!
如下:
b)修改模組的名稱,即將複製過來的模組裡的名稱改一下,以防匯入到zabbix監控介面裡的時候重複了!
也就是將複製過來的模組裡的埠改一下!!!!
可以使用替換命
:%s/11211/11212/g
:%s/11211/11213/g
最後,登入zabbi介面,將上面的多個埠的memcache監控模板依次匯入進去,新增到相應機器的監控配置中。
***************************************************************************************************************
--------------------------------------------------------------------------------------------------
四、新增Low-level discovery監控磁碟IO的監控
zabbix自帶的“Template OS Linux”模板支援監控已掛載的磁碟空間利用率,是利用LLD(Low-level discovery)實現的,卻沒有對磁碟IO監控。
下面就本人線上利用zabbix LLD功能監控磁碟IO的操作做一記錄:
1)在zabbix客戶端建立查詢磁碟分割槽的指令碼 mount_disk_discovery.sh
[root@115r ~]# cd /usr/local/zabbix/monitor_scripts/
[root@115r monitor_scripts]# vim mount_disk_discovery.sh
#!/bin/bash
#Function: low-level discovery mounted disk
#Script_name: mount_disk_discovery.sh
mount_disk_discovery()
{
local regexp="\b(btrfs|ext2|ext3|ext4|jfs|reiser|xfs|ffs|ufs|jfs|jfs2|vxfs|hfs|ntfs|fat32|zfs)\b"
local tmpfile="/tmp/mounts.tmp"
:> "$tmpfile"
# 過濾所有已掛載的檔案系統
egrep "$regexp" /proc/mounts > "$tmpfile"
local num=$(cat "$tmpfile" | wc -l)
printf '{\n'
printf '\t"data":[ '
while read line;do
# 磁碟分割槽名稱
DEV_NAME=$(echo $line | awk '{print $1}')
# 檔案系統名稱,即磁碟分割槽的掛載點
FS_NAME=$(echo $line | awk '{print $2}')
# blockdev命令獲取扇區大小,用於計算磁碟讀寫速率
SEC_SIZE=$(sudo /sbin/blockdev --getss $DEV_NAME 2>/dev/null)
printf '\n\t\t{'
printf "\"{#DEV_NAME}\":\"${DEV_NAME}\","
printf "\"{#FS_NAME}\":\"${FS_NAME}\","
printf "\"{#SEC_SIZE}\":\"${SEC_SIZE}\"}"
((num--))
[ "$num" == 0 ] && break
printf ","
done < "$tmpfile"
printf '\n\t]\n'
printf '}\n'
}
case "$1" in
mount_disk_discovery)
"$1"
;;
*)
echo "Bad Parameter."
echo "Usage: $0 mount_disk_discovery"
exit 1
;;
esac
指令碼首先過濾 /proc/mounts 中的磁碟型別,然後輸出包含磁碟名稱、檔案系統名稱和扇區大小的Json資料。
2)設定相關檔案許可權:
[root@115r monitor_scripts]# touch /tmp/mounts.tmp
[root@115r monitor_scripts]# chown zabbix:zabbix /tmp/mounts.tmp
[root@115r monitor_scripts]# chown zabbix.zabbix /usr/local/zabbix/monitor_scripts
[root@115r monitor_scripts]# chown -R zabbix:zabbix /usr/local/zabbix/monitor_scripts
[root@115r monitor_scripts]# chmod 755 /usr/local/zabbix/monitor_scripts/mount_disk_discovery.sh
測試一下:
[root@115r monitor_scripts]# /usr/local/zabbix/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery
{
"data":[
{"{#DEV_NAME}":"/dev/sda3","{#FS_NAME}":"/","{#SEC_SIZE}":"512"},
{"{#DEV_NAME}":"/dev/sda1","{#FS_NAME}":"/boot","{#SEC_SIZE}":"512"}
]
}
上面的資料表示,磁碟分割槽/dev/sda3掛載到了根目錄,扇區大小為512B;/dev/sda1掛載到了/boot目錄,扇區大小為512B。
3)配置mount_disk_discovery.sh的key檔案
《前提是/usr/local/zabbix/etc/zabbix_agentd.conf裡已經定義了Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/的路徑了》
[root@115r ~]# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@115r zabbix_agentd.conf.d]# cat disk_lld.conf
UserParameter=mount_disk_discovery,/bin/bash /usr/local/zabbix/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery
表示:建立使用者自定義引數 mount_disk_discovery ,當zabbix server向agent請求這個item key時,agent會呼叫/usr/local/zabbix/monitor_scripts/mount_disk_discovery.sh指令碼,返回json資料。
4)由於zabbix agent的啟動使用者是zabbix,所以要授權zabbix使用者無密碼執行/sbin/blockdev命令。使用visudo命令,在/usr/local/sudoers裡註釋掉Defaults requiretty:
[root@115r ~]# visudo
#Defaults requiretty
並在最後面加上:
zabbix ALL=(ALL) NOPASSWD: /sbin/blockdev
這樣在指令碼里執行類似“sudo /sbin/blockdev --getss /dev/sda1”這樣的指令就沒問題了。
5)最後重啟zabbix agent
[root@115r ~]# pkill -9 zabbix_agent
[root@115r ~]# /usr/local/zabbix/sbin/zabbix_agentd
6)接著在zabbix server端進行測試
我們從zabbix server上遠端get一下mount_disk_discovery
如下結果,說明zabbxi的server端能成功接收到agent客戶端的資料(121.201.114.116是上面客戶機的ip)
# /usr/local/zabbix/bin/zabbix_get -s 121.201.114.116 -k 'mount_disk_discovery'
{
"data":[
{"{#DEV_NAME}":"/dev/mapper/VolGroup-lv_root","{#FS_NAME}":"/","{#SEC_SIZE}":"512"},
{"{#DEV_NAME}":"/dev/sda1","{#FS_NAME}":"/boot","{#SEC_SIZE}":"512"},
{"{#DEV_NAME}":"/dev/mapper/VolGroup-lv_home","{#FS_NAME}":"/home","{#SEC_SIZE}":"512"}
]
}
7)最後,登入zabbix介面,新增磁碟IO監控模板Template Linux DiskIO.xml
將Low-level discovery監控磁碟IO監控模板下載到本地,在zabbix裡面匯入模板,然後在相對應的主機監控配置裡面將這個監控模板連線上(如上面新增nginx模板步驟一樣,在此省略~)
Low-level discovery監控磁碟IO的監控效果圖如下: