前段時間在公司IDC服務器上部署了zabbix3.0.3監控系統,除了自帶的內存/帶寬/CPU負載等系統資源監控模板以及mysql監控模板外,接下來對諸如nginx、php、memcache、磁盤IO的監控項配置在此作下記錄,但願能幫助到有用到的朋友們~php
nginx的監控模板:zbx_nginx_templates.xml
php的監控模板:zbx_php-fpm_templates.xml
memcache的監控模板:zbx_memcached_templates.xml (這是11211端口監控模板,其餘端口照此模板更換端口便可)
Low-level discovery監控磁盤IO的監控模板:Template Linux DiskIO.xmlmysql
以上四個監控項的模板的下載地址:http://pan.baidu.com/s/1geAMZfl (提取密碼:zpyx)nginx
--------------------------------------------------------------------------------------------------
1、添加nginx的監控
1)首先在客戶機的nginx配置文件裏添加:
[root@115r ~]# vim www.world.huanqiu.conf
.......
location /nginx_status {
stub_status on;
access_log off;
}
........web
添加後,重啓nginx服務:
[root@115r ~]#/usr/local/nginx/sbin/nginx -s reloadsql
2)zabbix監控nginx的自定義鍵值json
在客戶機的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 0vim
3)配置nginx-status的key文件
《前提是/usr/local/zabbix/etc/zabbix_agentd.conf裏已經定義了Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/的路徑了》bash
[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 app
訪問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的兩個監控項的效果圖:
--------------------------------------------------------------------------------------------------
2、添加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監控項的效果圖:
--------------------------------------------------------------------------------------------------
3、添加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監控模板依次導入進去,添加到相應機器的監控配置中。
***************************************************************************************************************
--------------------------------------------------------------------------------------------------
4、添加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的監控效果圖以下: