分布式监控系统Zabbix-3.0.3-完整安装记录(3)-监控nginx,php,memcache,Low-level discovery磁盘IO

2018-01-22 16:39:14 浏览数 (1)

前段时间在公司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 $itemb" | 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 $itemb" | 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 $itemb" | 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 $itemb" | 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监控界面里的时候重复了!

image.pngimage.png

也就是将复制过来的模块里的端口改一下!!!!

可以使用替换命

:%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 'ntt{'     printf ""{#DEV_NAME}":"${DEV_NAME}","     printf ""{#FS_NAME}":"${FS_NAME}","     printf ""{#SEC_SIZE}":"${SEC_SIZE}"}"     ((num--))     [ "$num" == 0 ] && break     printf ","     done < "$tmpfile"     printf 'nt]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的监控效果图如下:

0 人点赞