Linux实战教学笔记51:Zabbix监控平台3.2.4(三)生产环境案例

一,Zabbix生产环境监测案例概述

1.1 项目规划

  • [x] :主机分组
    • 交换机
    • Nginx
    • Tomcat
    • MySQL
    • Apache
    • PHP-fpm
    • redis(也有状态页,同学们自己研究)
    • memcache(也有状态页,同学们自己研究)
  • [x] :监控对象识别:
    • 使用SNMP监控交换
    • 使用IPMI监控服务器硬件
    • 使用Agent监控服务器
    • 使用JMX监控JAVA
    • 监控MySQL
    • 监控Web状态
    • 监控Nginx状态
    • 监控Apache状态
    • 监控Tomcat状态

1.2 SNMP监控流程

  1. 交换机上开启snmp
  2. 在zabbix上添加监控(设置SNMP interfaces)
  3. 关联监控模版

1.3 IPMI

建议使用自定义item监控服务器硬件,在本地执行ipmitool命令来获取数据

可以在本地执行ipmitool命令获取数据(命令需要安装)

1.4 JMX(使用Zabbix Java Gateway代理)

用来获取java数据,本次的实战内容

1.5 实战监控Nginx,Apache,MySQL,PHP-fpm

今天的实战内容,请同学们注意总结过程

1.6 Zabbix的Web监测

今天的实战内容,请同学们注意总结过程

二,Zabbix监控MySQL数据库操作实战

我们需要部署一个MySQL服务和一个zabbix-agentd客户端

2.1 编写监控脚本

在nginx服务器(192.168.0.221)上安装一个MySQL
下面是监控mysql状态的一个shell脚本,内容如下:

[root@nginx scripts]# pwd
/server/scripts
[root@nginx scripts]# cat check_mysql.sh 
#!/bin/bash
# author:Mr.chen

MySQL_USER="root"
MySQL_PWD="123123"
MySQL_HOST="127.0.0.1"
MySQL_PORT="3306"

MySQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MySQL_USER} -h${MySQL_HOST} -P${MySQL_PORT} -p${MySQL_PWD}"

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_querles)
        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_querles|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
        ;;
esac

2.2 在zabbix-agent端创建自定义键值配置文件

#创建mysql.status的键值
[root@nginx scripts]# chmod +x check_mysql.sh 
[root@nginx scripts]# chown zabbix.zabbix check_mysql.sh
[root@nginx zabbix]# cd /etc/zabbix/zabbix_agentd.d/
[root@nginx zabbix_agentd.d]# cat mysql_status.conf 
UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1

#删除旧键值模版
[root@nginx zabbix_agentd.d]# rm -rf userparameter_mysql.conf 

#创建mysql.ping和mysql.version的键值
[root@nginx zabbix_agentd.d]# cat mysql_status.conf 
UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -p123123 ping | grep -c alive
UserParameter=mysql.version,/usr/local/bin/mysql -V

#重启zabbix-agentd服务
[root@nginx zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

2.3 在zabbix-server端测试键值

[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Uptime]"
2791
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Com_commit]"
0
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_sent]"
144628
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_received]"
3934
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.ping"
1
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.version"
/usr/local/bin/mysql  Ver 14.14 Distrib 5.5.32, for linux2.6 (x86_64) using readline 5.1

2.4 在zabbix-server端Web前台引入zabbix监控MySQL的模版

zabbix自带监控MySQL的模版,我们只需要将模版引入到主机即可
配置-->主机-->选择主机-->模板,然后将MySQL模版连接到此主机即可

QQ截图20180122210222.png-42.2kB

QQ截图20180122210235.png-25.5kB

QQ截图20180122210257.png-20.8kB

QQ截图20180122210610.png-36.3kB

QQ截图20180122210625.png-88kB

从图中可以发现模版里的键值都是我们之前已经定义好了的键值。

三,Zabbix监控Apache、Nginx操作实战

3.1 实战监控Apache

3.1.1 源码编译构建apache服务

#为了避免冲突,若系统中已经安装httpd服务,务必删除
[root@apache ~]# rpm -q httpd
package httpd is not installed
[root@apache ~]# /etc/init.d/iptables stop
[root@apache ~]# setenforce 0
setenforce: SELinux is disabled

#源码构建apache
[root@apache httpd-2.2.17]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
[root@apache httpd-2.2.17]# cd /usr/src/httpd-2.2.17/
[root@apache httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
[root@apache httpd-2.2.17]# cd /usr/local/httpd/
[root@apache httpd]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
[root@apache httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@apache httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@apache httpd]# chmod +x /etc/init.d/httpd
[root@apache ~]# cd /usr/local/httpd/htdocs/
[root@apache htdocs]# echo "`hostname -I` I am apache" > index.html
[root@apache htdocs]# cat index.html 
192.168.0.222  I am apache
[root@apache conf]# awk '/#ServerName/{print NR,$0}' httpd.conf 
97 #ServerName www.example.com:80
[root@apache conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf 
[root@apache conf]# sed -n '97p' httpd.conf 
ServerName www.yunjisuan.com:80
[root@apache conf]# /etc/init.d/httpd start
[root@apache conf]# netstat -antup | grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      53288/httpd         
[root@apache conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
[root@apache conf]# tail -1 /etc/hosts
192.168.0.222  www.yunjisuan.com
[root@apache conf]# curl www.yunjisuan.com
192.168.0.222  I am apache

3.1.2 开启apache状态页

要打开apache的ServerStatus页面,需要在httpd.conf文件最下边加入代码段

ExtendedStatus On
<location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</location>

最后,重启动apache即可

3.1.3 下载apache zabbix模版并解压

#从如下地址下载apache zabbix模版:
[root@apache ~]# wget https://github.com/lorf/zapache/archive/master.zip
#解压zip格式压缩文件
[root@apache ~]# unzip master
[root@apache ~]# ls -d zapache-master
zapache-master
[root@apache ~]# cd zapache-master/
[root@apache ~]# tree zapache-master/
zapache-master/
├── httpd-server-status.conf.sample
├── README.md
├── userparameter_zapache.conf.sample
├── zapache
├── zapache-template-active.xml
└── zapache-template.xml

0 directories, 6 files
  • 此模版文件中包含了监控apache的脚本文件,zabbix的apache模版文件,还有客户端zabbix自定义传key文件。
  • 首先将zapache放到需要监控的apache服务器上的/etc/zabbix/scripts目录下,如果没有scripts目录,自行创建一个即可。然后执行授权:
[root@apache zapache-master]# chmod 755 /etc/zabbix/scripts/zapache
[root@apache scripts]# cd /etc/zabbix/scripts/
[root@apache scripts]# ./zapache 
zapache version: 1.5
usage:
  ./zapache [<url>] TotalAccesses                 - Check total accesses.
  ./zapache [<url>] TotalKBytes                   - Check total KBytes.
  ./zapache [<url>] CPULoad                       - Check CPU load.
  ./zapache [<url>] Uptime                        - Check uptime.
  ./zapache [<url>] ReqPerSec                     - Check requests per second.
  ./zapache [<url>] BytesPerSec                   - Check Bytes per second.
  ./zapache [<url>] BytesPerReq                   - Check Bytes per request.
  ./zapache [<url>] BusyWorkers                   - Check busy workers.
  ./zapache [<url>] IdleWorkers                   - Check idle workers.
  ./zapache [<url>] version                       - Version of this script.
  ./zapache [<url>] ping                          - Check if Apache is up.
  ./zapache [<url>] WaitingForConnection          - Check Waiting for Connection processess.
  ./zapache [<url>] StartingUp                    - Check Starting Up processess.
  ./zapache [<url>] ReadingRequest                - Check Reading Request processess.
  ./zapache [<url>] SendingReply                  - Check Sending Reply processess.
  ./zapache [<url>] KeepAlive                     - Check KeepAlive Processess.
  ./zapache [<url>] DNSLookup                     - Check DNSLookup Processess.
  ./zapache [<url>] ClosingConnection             - Check Closing Connection Processess.
  ./zapache [<url>] Logging                       - Check Logging Processess.
  ./zapache [<url>] GracefullyFinishing           - Check Gracefully Finishing Processess.
  ./zapache [<url>] IdleCleanupOfWorker           - Check Idle Cleanup of Worker Processess.
  ./zapache [<url>] OpenSlotWithNoCurrentProcess  - Check Open Slots with No Current Process.
  • 接着将userparameter_zapache.conf.sample文件放到/etc/zabbix/zabbix_agentd.d目录下,并重命名为userparameter_zapache.conf文件,然后修改键值命令的路径。
  • 最后,重启zabbix-agent服务
[root@apache zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
[root@apache zabbix_agentd.d]# tail -1 userparameter_zapache.conf 
UserParameter=zapache[*],/etc/zabbix/scripts/zapache \$1    #本行要修改
[root@apache zapache-master]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

3.1.4 测试键值

#在zabbix-agent端查看脚本用法
[root@apache zabbix_agentd.d]# cd /etc/zabbix/scripts/
[root@apache scripts]# ./zapache 
zapache version: 1.5
usage:
  ./zapache [<url>] TotalAccesses                 - Check total accesses.
  ./zapache [<url>] TotalKBytes                   - Check total KBytes.
  ./zapache [<url>] CPULoad                       - Check CPU load.
  ./zapache [<url>] Uptime                        - Check uptime.
  ./zapache [<url>] ReqPerSec                     - Check requests per second.
  ./zapache [<url>] BytesPerSec                   - Check Bytes per second.
  ./zapache [<url>] BytesPerReq                   - Check Bytes per request.
  ./zapache [<url>] BusyWorkers                   - Check busy workers.
  ./zapache [<url>] IdleWorkers                   - Check idle workers.
  ./zapache [<url>] version                       - Version of this script.
  ./zapache [<url>] ping                          - Check if Apache is up.
  ./zapache [<url>] WaitingForConnection          - Check Waiting for Connection processess.
  ./zapache [<url>] StartingUp                    - Check Starting Up processess.
  ./zapache [<url>] ReadingRequest                - Check Reading Request processess.
  ./zapache [<url>] SendingReply                  - Check Sending Reply processess.
  ./zapache [<url>] KeepAlive                     - Check KeepAlive Processess.
  ./zapache [<url>] DNSLookup                     - Check DNSLookup Processess.
  ./zapache [<url>] ClosingConnection             - Check Closing Connection Processess.
  ./zapache [<url>] Logging                       - Check Logging Processess.
  ./zapache [<url>] GracefullyFinishing           - Check Gracefully Finishing Processess.
  ./zapache [<url>] IdleCleanupOfWorker           - Check Idle Cleanup of Worker Processess.
  ./zapache [<url>] OpenSlotWithNoCurrentProcess  - Check Open Slots with No Current Process.

我们发现脚本需要传递两个参数,即url和选项
然后我们在zabbix-server端进行键值测试

[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"
9
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com version]"
1.5
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com ping]"
1

3.1.5 在zabbix-server的Web页面上监控apache

(1)创建一个监控apache的主机

QQ截图20180122233702.png-36.7kB

QQ截图20180122233846.png-28.8kB

(2)导入带有apache监控项的模版

zabbix默认并没有监控apache的监控项模版。我们需要将它导入到apache中。在之前解压出来的目录中,有待导入的web模版

[root@apache ~]# tree zapache-master/
zapache-master/
├── httpd-server-status.conf.sample
├── README.md
├── zapache-template-active.xml #Web模版
└── zapache-template.xml    #Web模版

0 directories, 4 files

我们将这两个.xml后缀的拷贝到windows上,然后从Web界面导入进去

QQ截图20180122234457.png-17.2kB

QQ截图20180122234649.png-32.8kB

导入以后Web端就有监控apache的监控项模版了

(3)将模版链接到apache主机

QQ截图20180122234811.png-27.5kB

QQ截图20180122234841.png-23.9kB

QQ截图20180122235146.png-26.2kB

3.2 实战监控nginx

3.2.1 开启nginx状态页

#在nginx的配置文件中,添加status配置
location = /nginx-status {
    stub_status on;
    access_log  off;
    allow 127.0.0.1;
    allow 192.168.0.220;    #zabbix服务器地址一般是内网地址
    deny all;
}

3.2.2 访问设置好的nginx-status链接

#在zabbix-server上通过如下方式获取nginx状态页面信息:
root@Zabbix_Server ~]# curl 192.168.0.221/nginx-status
Active connections: 1 
server accepts handled requests
 634 634 618 
Reading: 0 Writing: 1 Waiting: 0 

nginx status详细说明如下:
Active connections:对后端发起的活动连接数;
accepts:nginx总共处理了多少个连接;
handled:nginx成功创建了几次握手;
requests:nginx总共处理了多少请求。
Reading:nginx读取客户端的header数;
Writing:nginx返回给客户端的header数;
Waiting:nginx请求处理完成,正在等待下一请求指令的连接

3.2.3 编写nginx状态监控脚本

#下面是nginx状态监控抓取脚本,内容如下:
[root@nginx scripts]# cat nginx_status.sh 
#!/bin/bash
# Set Variables

HOST="127.0.0.1"
PORT="80"

#Functions to return nginx stats

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 "Reading" | awk '{print $2}'
}

function writing(){

    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Writing" | awk '{print $4}'
}

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}'
}

function ping(){

    /sbin/pidof nginx | wc -l
}

# Run the requested function

$1

3.2.4 给脚本可执行权限并且授权

[root@nginx scripts]# chmod +x nginx_status.sh 
[root@nginx scripts]# chown zabbix.zabbix nginx_status.sh

3.2.5 在zabbix-agent端配置nginx键值配置文件

[root@nginx zabbix_agentd.d]# pwd
/etc/zabbix/zabbix_agentd.d
[root@nginx zabbix_agentd.d]# cat nginx.conf 
UserParameter=nginx.active,/usr/bin/curl -s "http://127.0.0.1:80/nginx-status" | grep "Active" | awk '{print $NF}'
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1
#重启agent客户端
[root@nginx zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

3.2.6 在zabbix-server端测试键值

[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[reading]" -p 10050
0
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[active]" -p 10050
1
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[reading]" -p 10050
0
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[writing]" -p 10050
1
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[ping]" -p 10050
1

3.2.7 接下来我们需要在Web端导入nginx监控模版

QQ截图20180126105441.png-26.7kB

QQ截图20180126105511.png-49.3kB

QQ截图20180126105523.png-34.5kB

QQ截图20180126105544.png-29.5kB

3.2.8 异常调试

如果Zabbix图形界面一直没有获取到数据,可以通过在zabbix server上执行zabbix_get手动测试:
/usr/local/zabbix/bin/zabbix_get -s 192.168.0.221 -p 10050 -k "nginx.status[active]"
如果获取数据有问题,会给出相应提示,如果能够获取到数据,那就没问题了。

四,Zabbix监控Tomcat操作实战

zabbix监控tomcat,需要zabbix_server开启java poller,zabbix_java开启javaGateway,端口10052,Tomcat JMX开启12345端口,提供性能数据。

数据获取流程为:java poller<-->javaGateway:10052<-->Tomcat:12345

QQ截图20180126111607.png-76.3kB

4.1 配置Tomcat JMX

#配置jmx,在需要监控的tomcat服务器上,编辑catalina.sh,加入如下配置
[root@Tomcat bin]# sed -n '249p' /usr/local/tomcat/bin/catalina.sh 
CATALINA_OPTS="-server -Xms256m -Xmx512m -XX:PermSize=64M -XX:MaxPermSize=128m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.0.224 -Dcom.sun.management.jmxremote.port=12345"

这里需要注意,必须增加-Djava.rmi.server.hostname选项,并且后面的IP就是tomcat服务器的IP

4.2 重启Tomcat

[root@Tomcat bin]# /usr/local/tomcat/bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@Tomcat ~]# netstat -antup | grep java
[root@Tomcat bin]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@Tomcat zabbix]# netstat -antup | grep java
tcp        0      0 :::54031                    :::*                        LISTEN      1521/java           
tcp        0      0 :::8080                     :::*                        LISTEN      1521/java           
tcp        0      0 :::53881                    :::*                        LISTEN      1521/java           
tcp        0      0 :::12345                    :::*                      LISTEN      1521/java          #新增端口 
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      1521/java           
tcp        0      0 :::8009                     :::*                        LISTEN      1521/java       

4.3 编译zabbix,加入java支持

在zabbix server服务器上,编译安装zabbix-server,需要加上--enable-java,以支持jmx监控,如果之前的zabbix server没加此选项,那么需要重新编译安装,编译参数如下:

./configure --prefix=/usr/local/zabbix --with-mysql --with-net-snmp --with-libcurl --enable-server --enable-agent --enable-proxy --enable-java --with-libxml2

特别提示:
在启用--enable-java编译zabbix之前,zabbix server服务器上需要安装JDK,并需要设置JAVA_HOME,以让系统能够识别到jdk的路径

4.3.1 在zabbix-server端安装JDK

#解压jdk
[root@Zabbix_Server ~]# tar xf jdk-8u60-linux-x64.tar.gz -C /usr/local/
[root@Zabbix_Server ~]# ln -s /usr/local/jdk1.8.0_60/ /usr/local/jdk
#配置java环境变量
[root@Zabbix_Server ~]# sed -i.ori '$a export JAVA_HOME=/usr/local/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
[root@Zabbix_Server ~]# tail -3 /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
#让环境变量立刻生效
[root@Zabbix_Server ~]# source /etc/profile
#检查java环境变量情况
[root@Zabbix_Server ~]# which java
/usr/local/jdk/bin/java
[root@Zabbix_Server ~]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

4.3.2 重新编译zabbix-server服务器端

#重新编译zabbix-server服务器端之前
[root@Zabbix_Server ~]# ll /usr/local/zabbix/sbin/
total 8604
-rwxr-xr-x 1 root root 1165774 Jan 20 15:16 zabbix_agentd
-rwxr-xr-x 1 root root 3672055 Jan 20 15:16 zabbix_proxy
-rwxr-xr-x 1 root root 3966037 Jan 20 15:16 zabbix_server

#开始重新编译zabbix-server服务器端
[root@Zabbix_Server ~]# 
[root@Zabbix_Server ~]# /etc/init.d/zabbix_server stop
Shutting down zabbix_server:                               [  OK  ]
[root@Zabbix_Server ~]# cd /usr/src/zabbix-3.2.4/
[root@Zabbix_Server zabbix-3.2.4]# ./configure --prefix=/usr/local/zabbix --with-mysql --with-net-snmp --with-libcurl --enable-server --enable-agent --enable-proxy --enable-java --with-libxml2
[root@Zabbix_Server zabbix-3.2.4]# make && make install

#重新编译zabbix-server服务器端之后
[root@Zabbix_Server zabbix-3.2.4]# cd /usr/local/zabbix/sbin/
[root@Zabbix_Server sbin]# ll
total 8608
-rwxr-xr-x 1 root root 1165774 Jan 25 22:22 zabbix_agentd
drwxr-xr-x 4 root root    4096 Jan 25 22:22 zabbix_java     #多了一个目录
-rwxr-xr-x 1 root root 3672055 Jan 25 22:22 zabbix_proxy
-rwxr-xr-x 1 root root 3966037 Jan 25 22:22 zabbix_server
[root@Zabbix_Server sbin]# cd zabbix_java/
[root@Zabbix_Server zabbix_java]# ll
total 20
drwxr-xr-x 2 root root 4096 Jan 25 22:22 bin
drwxr-xr-x 2 root root 4096 Jan 25 22:22 lib
-rw-r--r-- 1 root root  791 Jan 25 22:22 settings.sh    #java gateway配置脚本
-rwxr-xr-x 1 root root  545 Jan 25 22:22 shutdown.sh    #停止java gateway服务
-rwxr-xr-x 1 root root 2025 Jan 25 22:22 startup.sh     #启动java gateway服务

提示:
java gateway服务的配置脚本settings.sh不需要修改任何配置。

4.3.3 启动java Gateway服务

[root@Zabbix_Server zabbix_java]# /usr/local/zabbix/sbin/zabbix_java/startup.sh 
[root@Zabbix_Server zabbix_java]# netstat -antup | grep 10052
tcp        0      0 :::10052                    :::*                        LISTEN      13126/java 

4.4 修改zabbix server 配置

默认的情况下,zabbix server未启用javaPollers,需要修改zabbix_server.conf的如下配置

[root@Zabbix_Server zabbix]# cd /usr/local/zabbix/etc/

#修改以下配置文件的内容
[root@Zabbix_Server etc]# cat -n zabbix_server.conf | sed -n '215p;223p;231p'
   215  # JavaGateway=
   223  # JavaGatewayPort=10052
   231  # StartJavaPollers=0

#将上述配置文件内容修改成如下
[root@Zabbix_Server etc]# cat -n zabbix_server.conf | sed -n '215p;223p;231p'
   215  JavaGateway=127.0.0.1   #JavaGateway的地址
   223  JavaGatewayPort=10052
   231  StartJavaPollers=5

#查看已经打开的java poller进程
[root@Zabbix_Server ~]# ps -ef | grep "java poller" | grep -v grep
zabbix    13233  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #1 [got 0 values in 0.000001 sec, idle 5 sec]
zabbix    13234  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #2 [got 0 values in 0.000015 sec, idle 5 sec]
zabbix    13235  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #3 [got 0 values in 0.000001 sec, idle 5 sec]
zabbix    13236  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #4 [got 0 values in 0.000001 sec, idle 5 sec]
zabbix    13237  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #5 [got 0 values in 0.000001 sec, idle 5 sec]

4.5 重启zabbix_server服务

[root@Zabbix_Server ~]# /etc/init.d/zabbix_server start
Starting zabbix_server:                                    [  OK  ]
[root@Zabbix_Server ~]# netstat -antup | grep 10051
tcp        0      0 0.0.0.0:10051               0.0.0.0:*                   LISTEN      13172/zabbix_server 
tcp        0      0 192.168.0.220:10051         192.168.0.221:56752         TIME_WAIT   -                   
tcp        0      0 192.168.0.220:10051         192.168.0.221:56754         TIME_WAIT   -                   
tcp        0      0 192.168.0.220:10051         192.168.0.221:56751         TIME_WAIT   -                   
tcp        0      0 192.168.0.220:10051         192.168.0.220:47162         TIME_WAIT   -           

4.6 Zabbix图形界面配置

4.6.1 创建一个主机利用JMX接口监控tomcat被监控端

1.png-49.1kB

2.png-44.5kB

4.6.2 导入zabbix监控tomcat的模版

因为Zabbix自带的Tomcat模版有兼容性问题,并不好用,因此,我们导入自己制作的监控项模版

1.png-28.6kB

2.png-53.5kB

3.png-29.9kB

QQ截图20180128192111.png-42.8kB

最后我们关联模版即可。

QQ截图20180128211056.png-60.3kB

五,Zabbix监控PHP-FPM操作实战

php-fpm和nginx一样内建了一个状态页,对于想了解php-fpm的状态以及监控php-fpm非常有帮助

5.1 启用php-fpm状态功能

修改php-fpm配置文件,确保如下配置是打开状态:
pm.status_path = /status,具体操作如下

#在Zabbix-server端操作如下
[root@Zabbix_Server etc]# pwd
/usr/local/php/etc
[root@Zabbix_Server etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[root@Zabbix_Server etc]# sed -n '360p' php-fpm.conf
;pm.status_path = /status
[root@Zabbix_Server etc]# vim php-fpm.conf +360
[root@Zabbix_Server etc]# sed -n '360p' php-fpm.conf
pm.status_path = /status

#重启php-fpm服务
[root@Zabbix_Server ~]# netstat -antup | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      1145/php-fpm
[root@Zabbix_Server ~]# pkill php-fpm
[root@Zabbix_Server ~]# netstat -antup | grep 9000
[root@Zabbix_Server ~]# /usr/local/php/sbin/php-fpm 
[root@Zabbix_Server ~]# netstat -antup | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      18918/php-fpm

5.2 nginx配置php-fpm状态页面

在nginx配置文件nginx.conf里配置php状态页,操作如下

[root@Zabbix_Server ~]# cd /usr/local/nginx/
[root@Zabbix_Server nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
#配置php-fpm状态页面
[root@Zabbix_Server nginx]# vim conf/nginx.conf
[root@Zabbix_Server nginx]# cat conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location = /nginx-status {
            stub_status on;
            access_log off;
        }
        location = /status {         #添加此location
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        }
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}
#重启动nginx服务
[root@Zabbix_Server nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Zabbix_Server nginx]# /usr/local/nginx/sbin/nginx -s reload

5.3 状态页浏览器访问测试

php-fpm状态页比较个性化的一个地方是它可以带参数,可以带的参数有json、xml、html,使用zabbix或者nagios监控可以考虑使用xml或者默认方式。

QQ截图20180130230603.png-19kB

#具体含义说明如下
[root@Zabbix_Server nginx]# curl 127.0.0.1/status
pool:                 www           #fpm进程池名称,大多数为www
process manager:      dynamic       #进程管理方式(static,dynamic or nodemand.dynamic)
start time:           26/Jan/2018:02:56:58 -0500    #启动日期
start since:          2972          #运行时长
accepted conn:        517           #当前FPM进程池接受的请求数
listen queue:         0             #请求等待队列,如果值不是0,那么要增加FPM的进程数
max listen queue:     4             #请求等待队列最高的数量
listen queue len:     128           #socket等待队列长度
idle processes:       2             #空闲进程数量
active processes:     1             #活跃进程数量
total processes:      3             #总进程数量
max active processes: 3             #最大的活跃进程数量(FPM启动开始算)
max children reached: 0             #达到最大子进程的次数,如果值不为0,那么需要调高最大进程数
slow requests:        0             #当启用了php-fpm的slow-log功能时,如果出现php-fpm慢请求这个计数器会增加,一般不当的MySQL查询会触发这个值

其他状态参数显示形式的数据(json,xml,html)

#以xml的格式输出PHP-fpm状态页
[root@Zabbix_Server nginx]# curl 127.0.0.1/status?xml
<?xml version="1.0" ?>
<status>
<pool>www</pool>
<process-manager>dynamic</process-manager>
<start-time>1516953418</start-time>
<start-since>3731</start-since>
<accepted-conn>647</accepted-conn>
<listen-queue>0</listen-queue>
<max-listen-queue>4</max-listen-queue>
<listen-queue-len>128</listen-queue-len>
<idle-processes>2</idle-processes>
<active-processes>1</active-processes>
<total-processes>3</total-processes>
<max-active-processes>3</max-active-processes>
<max-children-reached>0</max-children-reached>
<slow-requests>0</slow-requests>

#以json的格式输出PHP-fpm的状态页
</status>[root@Zabbix_Server nginx]# curl 127.0.0.1/status?json
{"pool":"www","process manager":"dynamic","start time":1516953418,"start since":3736,"accepted conn":648,"listen queue":0,"max listen queue":4,"listen queue len":128,"idle processes":2,"active processes":1,"total processes":3,"max active processes":3,"max children reached":0,"slow requests":0}[root@Zabbix_Server nginx]# 

#以html的格式输出PHP-fpm的状态页
[root@Zabbix_Server nginx]# curl 127.0.0.1/status?html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>PHP-FPM Status Page</title></head>
<body>
<table>
<tr><th>pool</th><td>www</td></tr>
<tr><th>process manager</th><td>dynamic</td></tr>
<tr><th>start time</th><td>26/Jan/2018:02:56:58 -0500</td></tr>
<tr><th>start since</th><td>3748</td></tr>
<tr><th>accepted conn</th><td>650</td></tr>
<tr><th>listen queue</th><td>0</td></tr>
<tr><th>max listen queue</th><td>4</td></tr>
<tr><th>listen queue len</th><td>128</td></tr>
<tr><th>idle processes</th><td>2</td></tr>
<tr><th>active processes</th><td>1</td></tr>
<tr><th>total processes</th><td>3</td></tr>
<tr><th>max active processes</th><td>3</td></tr>
<tr><th>max children reached</th><td>0</td></tr>
<tr><th>slow requests</th><td>0</td></tr>
</table>
</body></html>

5.4 在Zabbix agent端修改配置

在创建Zabbix的agent端键值配置文件的时候,我们可以考虑利用PHP-fpm状态页的不同输出格式来抓取数据。例如我们利用xml格式获取数据,操作如下:

#查看xml格式PHP-fpm状态页数据
root@Zabbix_Server nginx]# curl 127.0.0.1/status?xml
<?xml version="1.0" ?>
<status>
<pool>www</pool>
<process-manager>dynamic</process-manager>
<start-time>1516953418</start-time>
<start-since>3731</start-since>
<accepted-conn>647</accepted-conn>
<listen-queue>0</listen-queue>
<max-listen-queue>4</max-listen-queue>
<listen-queue-len>128</listen-queue-len>
<idle-processes>2</idle-processes>
<active-processes>1</active-processes>
<total-processes>3</total-processes>
<max-active-processes>3</max-active-processes>
<max-children-reached>0</max-children-reached>
<slow-requests>0</slow-requests>

#创建zabbix监控的agent端键值配置文件
[root@Zabbix_Server ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@Zabbix_Server zabbix_agentd.d]# cat userparameter_php-fpm.conf 
UserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1/status?xml" | grep "<$1>" | awk -F "[>|<]" '{print $$3}'    #这里需要注意在脚本里如果写成$3的话,脚本会认识是一个变量,因此我们需要用$$3

#重启动agent客户端
[root@Zabbix_Server ~]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

5.5 进行键值配置文件测试

[root@Zabbix_Server zabbix_agentd.d]# zabbix_get -s 192.168.0.220 -p 10050 -k "php-fpm.status[process-manager]"
dynamic
[root@Zabbix_Server zabbix_agentd.d]# zabbix_get -s 192.168.0.220 -p 10050 -k "php-fpm.status[start-since]"
5634
[root@Zabbix_Server zabbix_agentd.d]# zabbix_get -s 192.168.0.220 -p 10050 -k "php-fpm.status[active-processes]"
1

5.6 Zabbix图形界面导入我们的监控模版

QQ截图20180130235651.png-51.5kB

QQ截图20180130235713.png-28kB

六,Zabbix的Web监测功能

Zabbix的Web监测功能,没有任何依赖,不需要agent端,全部依赖于Server的主动监测

利用Web监测,监测Server端的nginx首页

QQ截图20180131001529.png-53.9kB

QQ截图20180131001542.png-34.3kB

QQ截图20180131002003.png-47.8kB

QQ截图20180131002105.png-22kB

QQ截图20180131002314.png-23.3kB

QQ截图20180131002455.png-25.5kB

QQ截图20180131002555.png-32kB

到这里Web监测我们就创建好了,最后再看一下监控结果

QQ截图20180131002646.png-22.9kB

七,思考与作业

请同学们在Zabbix监控MySQL的模版示例的基础上,自定义一个监控数据库主从复制的监控项

IP地址主机名用途
192.168.0.220 Zabbix_Server服务器 Zabbix监控端/主MySQL
192.168.0.221 Nginx服务器 Nginx服务器/从MySQL
金牌IT职业再教育培训机构,欢迎来校资源。QQ:215379068
posted @ 2019-04-04 08:54  无情站长  阅读(468)  评论(0编辑  收藏  举报