根据搜集的资料安装测试并在安装测试过程中整理的文档,有些位置可能缺少相应的描述,但流程基本完整,部分参考文档在相应位置有标记。如有不足希望不吝赐教。


目录

一、zabbix-agent监控nginx

1.配置nginx_status

2.配置zabbix_agent

3.编辑文件getNginxInfo.py

4.编辑文件ngx_status.sh

5.启动agent

6.在zabbix-sercer添加刚刚配置的agent

二、zabbix-agent监控apache

三、zabbix-agent监控mysql

1、编写脚本check_mysql.sh

2.修改zabbix_agent.conf

3.重启zabbix_agent与配置server

四、zabbix-agent监控php-fpm

1.配置fpm

2.配置nginx

3.打开status页面

4.编写php-fpm status数值提取脚本

5.修改zabbix_agentd配置文件

6.在zabbix server上配置模板(自定义)

五、其他

1.查看启动状态


一、zabbix-agent监控nginx

使用zabbix监控nginx,实际上是通过nginx自带status模块来获取数据的,所以需要配置ngx_status。

启用 nginx status 模块,需要编译时带上参数 --with-http_sub_module(实际上在编译时带上 --with-http_stub_status_module 这个参数也是可以显示 nginx status的)。

如果没有安装nginx则在安装时注意参数配置,安装教程请自行搜索。

如果已经安装,则使用如下命令查看是否包含所需参数(目录执行是在nginx安装目录下,需要根据实际情况修改路径):

sbin/nginx -V

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

此处已经安装相关模块,如果没有请自行搜索nginx添加木块的资料。

 

1.配置nginx_status

修改nginx配置文件,添加nginx_status配置:

vim /usr/local/nginx/conf/nginx.conf

【说明】allow表示允许访问的地址,deny表示不允许访问的地址。其中:172.17.0.0/16表示允许172.17.0.0-172.17.255.255主机访问。

配置完成后在浏览器访问http://ip:port/nginx_status,如下所示:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

Nginx status 监控状态含义(图片来源网络):

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

2.配置zabbix_agent

由于监控 nginx 状态的 key 在 zabbix agent 中并没有预先定义的key,这时候我们可以通过编写 zabbix 的用户参数的方法来监控我们要求的项目 item。形象一点说 zabbix 代理端配置文件中的 User parameters就相当于通过脚本获取要监控的值,然后把相关的脚本或者命令写入到配置文件中的 User parameter 中然后 zabbix server 读取配置文件中的返回值通过处理前端的方式返回给用户。

配置/etc/zabbix/zabbix_agent.conf 语法(参考,图片来源网络):

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

vim /etc/zabbix/zabbix_agentd.conf

添加如下内容:

# NGINX - 参数固定
UserParameter=nginx.Accepted-Connections,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a accepted
UserParameter=nginx.Active-Connections,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a active
UserParameter=nginx.Handled-Connections,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a handled
UserParameter=nginx.Reading-Connections,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a reading
UserParameter=nginx.Total-Requests,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a requests
UserParameter=nginx.Waiting-Connections,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a waiting
UserParameter=nginx.Writting-Connections,/etc/zabbix/scripts/getNginxInfo.py -h 127.0.0.1 -p 8888 -a writing
# NGINX  - 变量形式
UserParameter=nginx.status[*],/etc/zabbix/scripts/ngx_status.sh $1

此处监控本机nginx,具体ip、端口要根据实际修改。截图如下:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

3.编辑文件getNginxInfo.py

vim /etc/zabbix/scripts/getNginxInfo.py

内容如下:

#!/bin/env python
#
# Options:
#
# -a active
# -a accepted
# -a handled
# -a requests
# -a reading
# -a writing
# -a waiting
#

import urllib2, base64, sys, getopt
import re

##

def Usage ():
        print "Usage: getNginxInfo.py  -h 127.0.0.1 -p 8888 -a [active|accepted|handled|request|reading|writing|waiting]"
        sys.exit(2)

##

def main ():

        # Default values
        host = "localhost"
        port = "8888"
        getInfo = "None"

        if len(sys.argv) < 2:
                Usage()

        try:
                opts, args = getopt.getopt(sys.argv[1:], "h:p:a:")
        except getopt.GetoptError:
                Usage()

        # Assign parameters as variables
        for opt, arg in opts :
                if opt == "-h" :
                        host = arg
                if opt == "-p" :
                        port = arg
                if opt == "-a" :
                        getInfo = arg


        url="http://" + host + ":" + port + "/nginx_status/"
        request = urllib2.Request(url)
        result = urllib2.urlopen(request)

        buffer = re.findall(r'\d{1,8}', result.read())

## Format:
## Active connections: 196
## server accepts handled requests
## 272900 272900 328835
## Reading: 0 Writing: 6 Waiting: 190

        if ( getInfo == "active"):
                print buffer[0]
        elif ( getInfo == "accepted"):
                print buffer[1]
        elif ( getInfo == "handled"):
                print buffer[2]
        elif ( getInfo == "requests"):
                print buffer[3]
        elif ( getInfo == "reading"):
                print buffer[4]
        elif ( getInfo == "writing"):
                print buffer[5]
        elif ( getInfo == "waiting"):
                print buffer[6]
        else:
                print "unknown"
                sys.exit(1)

if __name__ == "__main__":
    main()

 

4.编辑文件ngx_status.sh

vim /etc/zabbix/scripts/ngx_status.sh

内容如下:

#!/bin/bash

HOST="127.0.0.1"
PORT="8888"

#检查 nginx 进程是否存在
function ping {
    /sbin/pidof nginx|wc -l
}

#检查 nginx 性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | grep 'Active '| awk '{print $NF}'
}

function reading {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | grep 'Writing' | awk '{print $4}'
}

function waiting {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | grep 'Waiting' | awk '{print $6}'
}

function accepts {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | awk NR==3| awk '{print $1}'
}

function handled {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | awk NR==3| awk '{print $2}'
}

function requests {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null | awk NR==3|awk '{print $3}'
}

# 执行 funct
$1

 

5.启动agent

切换到文件目录:

cd /etc/zabbix/scripts/

先对刚刚两个文件赋权:

chmod +x getNginxInfo.py
chmod +x ngx_status.sh

使用文件测试是否成功如下:

./getNginxInfo.py  -h 127.0.0.1 -p 8888 -a handled

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

重启agent:

systemctl stop zabbix-agent
systemctl start zabbix-agent

 

6.在zabbix-sercer添加刚刚配置的agent

打开server前端页面,在配置-模板中选择导入

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

选择文件中选择nginx的模板(.xml文件,nginx、php-fpm、apache模板下载位置:zabbix监控nginx、php-fpm、apache监控模版文件,积分不足请留言邮箱),点击导入即可在模板列表找到。模板在附件中,或者从网络搜索资源下载。

导入完成后选择配置-主机,如果没有刚刚配置的主机则添加主机,因为本次是在本机测试,因此已经有主机,直接选择相应主机,选择模板,添加-更新即可。

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

添加之后稍等就可以在监测中看到数据了。

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

二、zabbix-agent监控apache

Apache 监控原理跟 Nginx 相同,通过 Apache 的 status 模块获取数据。

本部分在配置时未及时做记录,之后再补充。如有需要请先自行百度。

 

三、zabbix-agent监控mysql

1、编写脚本check_mysql.sh

为方便管理,mysql脚本也在目录/etc/zabbix/scripts下。

vim /etc/zabbix/scripts/check_mysql.sh

内容如下:

# -------------------------------------------------------------------------------
# FileName:    check_mysql.sh
# Revision:    1.0
# Date:        2018/09/04
# Author:      www
# Email:       caorui@biconjs.com
# Website:     www.biconjs.com
# Description:
# Notes:       ~
# -------------------------------------------------------------------------------
# 用户名
#MYSQL_USER='zabbix'

# 密码
#MYSQL_PWD='123456'

# 主机地址/IP
#MYSQL_HOST='127.0.0.1'

# 端口
#MYSQL_PORT='3306'

# 数据连接
#MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"

# 因为mysql在命令行用密码会有警告,因此在my.cnf文件中配置了mysql用户名密码等,
# 连接时不需要用户名密码host以及端口即可链接
# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin"

# 参数是否正确
if [ $# -ne "1" ];then
    echo "arg error!"
fi

# 获取数据
case $1 in
    Uptime)
        result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
        echo $result
        ;;
    Com_update)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
        echo $result
        ;;
    Slow_queries)
        result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
        echo $result
        ;;
    Com_select)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
        echo $result
                ;;
    Com_rollback)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
                echo $result
                ;;
    Questions)
        result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
                echo $result
                ;;
    Com_insert)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
                echo $result
                ;;
    Com_delete)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
                echo $result
                ;;
    Com_commit)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
                echo $result
                ;;
    Bytes_sent)
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
                echo $result
                ;;
    Bytes_received)
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
                echo $result
                ;;
    Com_begin)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
                echo $result
                ;;

        *)
        echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
        ;;
esac

 

2.修改zabbix_agent.conf

vim /etc/zabbix/zabbix_agentd.conf

添加如下内容:

# MYSQL
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/etc/zabbix/scripts/check_mysql.sh $1
# 当mysql down了的时候,zabbix监控不到。
# 因为默认的MySQL is down 的触发器的触发条件是mysql.ping.last(0)
# mysqladmin执行报错,错误信息返回不了数字0,所以zabbix触发不了
# 因此用telnet mysql端口的方式来判断
#UserParameter=mysql.ping,mysqladmin ping | grep -c alive #密码等参数在my.cnf中,此处省略
UserParameter=mysql.ping,sudo netstat -ntpl |grep 3306|grep mysql|wc |awk '{print $1}'

因为zabbix3.4自带mysql配置,这三个参数配置,因此如果要使用自定义参数,需要修改自带配置文件,如下:

vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

将其中的mysql.versionmysql.status[*]mysql.ping注释掉即可

 

3.重启zabbix_agent与配置server

与监控nginx步骤类似,先给编写的脚本文件赋权,然后重启agent,再配置server端即可,具体可以参考nginx配置。zabbix自带mysql模板,因此如果没有自定义需求不需要单独导入模板。

 

四、zabbix-agent监控php-fpm

1.配置fpm

本次测试中,被测试主机(71)和监控主机(213)不是同一台,使用监控主机的nginx连接被监控主机上的php-fpm。监控主机已经安装nginx,被监控主机已经安装了php,因此只需要配置即可。如需安装步骤请移步搜索引擎。

修改被监控主机(71php-fpm配置文件,启用状态功能:

vim /usr/local/php56/etc/php-fpm.conf

路径需要根据实际情况适当修改。

修改如下配置:

其中listen默认可能为127.0.0.1:9000,导致访问不到,因此需要尝试修改为9000,如果需要其他机器访问,则对参数listen.allowed_clients添加需要访问的ip或者直接注释掉(此处为注释,加分号)。参数pm.status_path是开始状态功能的访问的地址,为主要配置项,参数值可以自定义,因为监控主机nginx中已经配置status为监控本身状态,因此此处添加前缀fpm-。

修改完成后检查配置是否正确:

/usr/local/php56/sbin/php-fpm -t

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

检查无误重启php-fpm,因为重启方式有多种,所以根据自己实际情况选择重启方式。(/etc/init.d/php-fpm56 start)

 

2.配置nginx

修改监控主机(213nginx配置:

vim /usr/local/nginx/conf/nginx.conf

路径需要根据实际情况适当修改。

添加如下配置:

其中fastcgi_pass需要根据实际情况修改。

完成后检查配置文件是否正确:

/usr/local/nginx/sbin/nginx -t

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

如果正确则重启:

/usr/local/nginx/sbin/nginx -s reload

 

3.打开status页面

配置并重启完成后打开状态页,地址为nginx主机ip地址+端口+fpm-status。例如此处为:http://172.17.3.213:8888/fpm-status,打开后可以看到如下内容:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

参数含义(图片来源网络):

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

php-fpm 状态页比较个性化的一个地方是它可以带参数,可以带参数json、xml、html并且前面三个参数可以分别和full做一个组合。例如:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

full会多的一些参数(图片来源网络):

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

4.编写php-fpm status数值提取脚本

vim /etc/zabbix/scripts/php-fpm_status.sh

内容如下:

#!/bin/bash
#check php-fpm status
case $1 in
    ping)                  #检测php-fpm进程是否存在
    #/sbin/pidof php-fpm | wc -l
#/usr/bin/curl 127.0.0.1:8888/ping 2>/dev/null  | awk '/pong/{print 1}'
#/usr/bin/curl 127.0.0.1:8888/ping 2>/dev/null  | awk '{if($0~/pong/)print 1}'
/usr/bin/curl 127.0.0.1:8888/ping 2>/dev/null  | awk '{if($1=="pong") {print 1} else {print 0}}'
    ;;
    pool)                  #提取status中的start since数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==1{print $2}'
    ;;
    process_manager)       #提取status中的start since数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==2{print $3}'
    ;;
    start_since)           #提取status中的start since数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==4{print $3}'
    ;;
    conn)                  #提取status中的accepted conn数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==5{print $3}'
    ;;
    listen_queue)          #提取status中的listen queue数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==6{print $3}'
    ;;
     max_listen_queue)     #提取status中的max listen queue数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==7{print $4}'
    ;;
    listen_queue_len)      #提取status中的listen queue len
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==8{print $4}'
    ;;
    idle_processes)        #提取status中的idle processes数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==9{print $3}'
    ;;
    active_processes)      #提取status中的active processes数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==10{print $3}'
    ;;
     total_processes)      #提取status中的total processess数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==11{print $3}'
    ;;
    max_active_processes)  #提取status中的max active processes数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==12{print $4}'
    ;;
     max_children_reached) #提取status中的max children reached数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==13{print $4}'
    ;;
    slow_requests)         #提取status中的slow requests数值
    /usr/bin/curl 127.0.0.1:8888/fpm-status 2>/dev/null  | awk 'NR==14{print $3}'
    ;;
    *)
    echo "Usage: $0 {ping|pool|process_manager|start_since|conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached|slow_requests}"
    exit 1
    ;;
esac

以上脚本可根据实际情况进行修改。完成后赋予执行权限:

chmod +x php-fpm_status.sh

测试,先进入脚本目录:

cd /etc/zabbix/scripts/

然后执行类似如下命令测试:

./php-fpm_status.sh ping

结果类似下图:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

5.修改zabbix_agentd配置文件

vim /etc/zabbix/zabbix_agentd.conf

添加如下内容:

# PHP-FPM
UserParameter=php-fpm.status[*],/etc/zabbix/scripts/php-fpm_status.sh $1
# 因为监控的是另外主机的fpm,因此version不可用
#UserParameter=php-fpm.version,/usr/local/php/sbin/php-fpm -v | awk 'NR==1{print $0}'

保存退出并重启agent服务:

systemctl stop zabbix-agent
systemctl start zabbix-agent

测试zabbix_get能否取到数据:使用类似如下命令:

zabbix_get -s 127.0.0.1 -k 'php-fpm.status[conn]'
zabbix_get -s 127.0.0.1 -k 'php-fpm.status[total_processes]'

成功如下:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

6.在zabbix server上配置模板(自定义)

这里的模板是php-fpm status要监控的item、trigger、Graphs等一系列内容。对于在zabbix server上新增要监控的内容,最好的办法就是从模板开始,然后按照顺序依次配置相应内容,无需去网上找现成的模板导入,对于系统的监控按需配置是最好的。下面是相应的操作过程。

选择【配置】-【模板】-【创建模板】

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

在新页面中填入模板名称并选择群组,点【添加】即可

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

创建完成后在模板列表选择刚刚创建的模板

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

在新的页面选择应用集并创建一个应用集,名称自定义

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

完成后开始创建监控项:点击【监控项】-【创建监控项】

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

在新的页面根据实际输入详细信息,如下:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

按照上述方式添加脚本中定义的监控项,添加完成后类似下图:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

同样的方式,添加触发器:

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

名称自定义,设置告警条件以及恢复条件,此处设置fpm ping值为0时警告,为1时恢复,添加即可。其他项类似,不再赘述。

全部添加完成后,在【配置】-【主机】-选择刚刚添加监控的主机-【模板】-选择刚才创建的php模板-【添加】-【更新】即可在监控项中看到检测结果。

CentOS7中配置Zabbix监控nginx、mysql、php-fpm等-LMLPHP

 

五、其他

1.查看启动状态

查看端口占用情况:

netstat -lnp|grep 10051

 

查看服务进程:

ps -aux | grep zabbix

10-06 18:33