Zabbix monitoring MySQL, Apache, Nginx application monitoring

zabbix monitoring of third-party applications, there are two difficult work, it is to write a custom script monitoring, and the other is in the preparation of the template and import zabbix web, according to this scripted

Monitoring requirements can be customized, template file and write some difficulty, but the Internet has a lot of templates already written, we can be directly used directly, so, zabbix monitoring of the application software is not difficult.

A, zabbix monitoring application mysql combat
this paper is to introduce zabbix monitoring mysql, this is the easiest, because zabbix already comes with mysql monitoring template, we only need to write a script to monitor the mysql, so mysql monitoring can be divided into two steps.

1, zabbix add a custom script monitoring mysql

Here we are given a line running mysql monitoring scripts check_mysql, reads as follows:

#!/bin/bash
# 主机地址/IP
MYSQL_HOST='127.0.0.1' # 端口 MYSQL_PORT='3306' # 数据连接 MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -h${MYSQL_HOST} -P${MYSQL_PORT}" # 参数是否正确 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|C om_begin)" ;; esac

This script is very simple, is to get the running parameters mysql by mysqladmin command, because you want to get mysql running, you need to log in to the mysql acquired status value, but the script does not add a user name and password information to log database, because password to the script is not safe, and the other is in the mysql5.7 version, plain text password on the command line, it will prompt the following information:

mysqladmin: [Warning] Using a password on the command line interface can be insecure.

Take this solution to the problem is the user name and password to log into the database information is written /etc/my.cnf file similar to the following:

[mysqladmin]
user=root
password=xxxxxx

Thus, through the command line mysqladmin operation, and will automatically log into the database by the root user and the corresponding password.

2, zabbix agent terminal configuration modification

To monitor mysql, mysql will need to be installed on the server zabbix agent, and then open a custom monitor mode agent will put zabbix agent script above the end of / etc / zabbix under / shell directory, and then authorize:

chmod o+x check_mysql
chown zabbix.zabbix check_mysql

Next, add the following to /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf file, note the contents before userparameter_mysql.conf delete all the files or commented out.

UserParameter=mysql.status[*],/etc/zabbix/shell/check_mysql.sh $1
UserParameter=mysql.ping,HOME=/etc /usr/local/mysql/bin/mysqladmin ping 2>/dev/null| grep -c alive UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V

In fact, here it is a custom monitor three items, namely mysql.status, mysql.ping and mysql.version, pay attention to the wording of the custom monitor. The three keys to customize monitored items in zabbix web needs to added to it.

Once configured, the last restart zabbix agent services to validate the configuration.

3, Zabbix web interface introduced Template

zabbix mysql monitor comes with a template, so just link to the template corresponding to the host.
Click the web interface "Configuration", select the "host", top right, click "Create Host", add a mysql host, as shown below:

Here to add a mysql host 172.16.213.236, then click on the figure the "template" option, click "link indicator" behind the selection button, select the "Template DB MySQL" template, as shown below:

Finally, click the image above red box marked "Add" button to complete the link template.

Then, click on the web above the "Settings" option, then select the "template", find "Template DB MySQL" template, you can see this template has been added 14 monitored items, a flip-flop, two graphics, an application set, then "monitor key", the key information and to display the name of the monitored item, as shown below:

Here the focus of concern is monitoring each item corresponding to the name "key" to configure one, the key here, must be consistent and agent-side custom monitoring key. In addition, you can see, "mysql status" monitoring this item has a trigger, check the operating status for mysql, finally, also need to focus on these types of surveillance is to monitor the entry is "zabbix clients", all items are monitored stored in the "MySQL" application set.

After all set up, monitor mysql has added a host of 172.16.213.236 completed.

4, view monitoring status data

点击web上面的“监测中”选项,然后选择“最新数据”,即可看到监控项是否获取到了最新数据,如下图所示:

通过过滤器进行过滤,即可查看mysql监控项返回的数据,可以看到“最新数据”一列中,已经获取到了mysql的状态数据,此外,在“名称”一列中,还可以看到“Template DB MySQL”模板中每个监控项对应的键值,例如mysql.status[Com_begin]、mysql.status[Bytes_received]、mysql.status[Bytes_sent],这些监控项键值与zabbix agent端自定义监控项的名称是完全对应的。

有时候由于agent端配置的问题,或者网络、防火墙等问题,可能导致server端无法获取agent端的数据,此时在web界面上就会出现如下图信息:

在此图中,可以从“最近检查记录”一列中查看最近一次的检查时间,如果监控项无法获取到数据,那么这个检查时间肯定不是最新的,此外,最后一列“信息”中也会给出错误提示,我们可以从错误提示中找到无法获取数据的原因,这将非常有助于排查问题。在没有获取到数据时,可以看到每列信息都是灰色的。

5、测试触发器告警功能

MySQL加入zabbix监控后,我们还需要测试一下,触发器告警动作是否正常,点击web上面的“监测中”选项,然后选择“问题”,即可看到有问题的监控项,如下图所示:

在这个界面中,可以看到哪个主机出现了什么问题,以及问题持续的时间,还有问题的严重性,当触发器触发后,会激活触发器动作,也就是发送告警消息的操作,在上面的介绍中,我们配置了邮件告警,那么就来看看是否发送了告警邮件,接下来,点击web导航上面的“报表”选项,然后选择“动作日志”,即可看到动作事件的日志,如下图所示:

此界面显示了监控项在发生故障后,触发器动作发送的消息事件,其中,“类型”一列指定是发送邮件信息,“接收者”一列是消息收件人的地址,“消息”一列是发送消息的详细内容,“状态”一列显示了告警邮件是否发送成功,如果发送不成功,最后一列“消息”会给出错误信息,我们根据错误提示进行排错即可。

二、zabbix 监控 apache应用实战
zabbix对apache的监控,稍微复杂一些,但基本流程还是两个步骤,第一个是编写监控apache的脚本,第二个是创建apache监控模板。

1、开启apache状态页

要监控apache的运行状态,需要在apache的配置中开启一个apache状态页面,然后再通过编写脚本获取这个状态页面的数据即可达到监控apache的目的,这里我们以apache2.4版本为例,如何安装httpd不做介绍,主要介绍下如何打开apache的Server Status页面,要打开状态页面,只需在apache配置文件httpd.conf文件最下边加入如下代码段:

ExtendedStatus On
<location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 172.16.213.132 </location> 或者 ExtendedStatus On <location /server-status> SetHandler server-status Require ip 127.0.0.1 172.16.213.132 </location>

其中:

ExtendedStatus On: 表示开启或关闭扩展的status信息,设置为On后,通过ExtendedStatus指令可以查看更为详细的status信息。但启用扩展状态信息将会导致服务器运行效率降低。

第二行的/server-status表示以后可以用类似http://ip/server-status来访问, 同时也可以通过http://ip/server-status?refresh=N 方式动态访问,此url表示访问状态页面可以每N秒自动刷新一次。

Require是apache2.4版本的一个新特效,可以对来访的IP或主机进行访问控制。 “Require host www.abc.com”表示仅允许www.abc.com访问apache的状态页面。“Require ip 172.16.213.132”表示仅允许172.16.213.132主机访问apache的状态页面。Require类似的用法还要如下几种:

 允许所有主机访问:Require all granted
 拒绝所有主机访问:Require all denied
 允许某个IP访问:Require ip ip地址
 禁止某个IP访问: Require not ip ip地址
 允许某个主机访问:Require host 主机名
 禁止某个主机访问 Require not host 主机名

最后,重启apache服务即可完成httpd状态页面的开启。

2、编写apache的状态监控脚本和zabbix模板

apache状态页面配置完成后,接下来就需要编写获取状态数据的脚本了,脚本代码较多,大家可直接从如下地址下载即可:

[root@iivey /]# wget  https://www.ixdba.net/zabbix/zabbix-apache.zip

接着,就是编写apache的zabbix监控模板了,zabbix默认没有自带apache的监控模板,需要自己编写,这里我们直接将编写好的模板供大家下载,可以从如下地址下载apache zabbix模板:

[root@iivey /]# wget https://www.ixdba.net/zabbix/zabbix-apache.zip

获取监控数据的脚本文件和监控模板都编写完成后,接下来,还需要在要监控的apache服务器上(需要安装zabbix agent)上做两个步骤的操作,第一个步骤是将apache监控脚本放到需要监控的apache服务器上的/etc/zabbix/shell目录下,如果没有shell目录,自行创建一个即可。然后执行授权:

[root@iivey shell]#chmod 755 zapache

当然,zabbix_agentd.conf也是需要配置的,这个文件的配置方式前面已经介绍过,这里就不在多说了。

第二个步骤是在apache服务器上的/etc/zabbix/zabbix_agentd.d目录下创建userparameter_zapache.conf文件,内容如下:

UserParameter=zapache[*],/etc/zabbix/shell/zapache  $1

注意这里/etc/zabbix/shell/zapache的路径。

最后,重启zabbix-agent服务完成agent端的配置:

[root@localhost zabbix]# systemctl  start zabbix-agent

3、Zabbix图形界面导入模板

点击web导航上面的“配置”选项,然后选择“模板”,点击右上角“导入”按钮,开始导入apache模板到zabbix中,如下图所示:

在此界面下,在“导入文件”选项中点击“浏览”,导入apache的模板文件,接着点击最下面的“导入”按钮即可将apache模板导入zabbix中。

模板导入后,还需要将此模板关联到某个主机下,这里我们仍然选择将此模板关联到172.16.213.236这个主机下,点击web导航上面的“配置”选项,然后选择“主机”,接着点开172.16.213.236主机链接,然后选择“模板”这个二级选项,链接一个新的模板,如下图所示:

在此界面下点击“链接指示器”后面的“选择”按钮,选择刚刚上传上来的模板,这样就把apache模板链接到了172.16.213.236主机上了。这样172.16.213.236主机已经链接了两个模板了,如下图所示:

点击“更新”按钮,完成模板的链接。

点击web上面的“设置”选项,然后选择“模板”,找到“Template App Apache Web Server zapache”模板,可以看到此模板已经添加了23个监控项,1个触发器,5个图形,1个应用集,然后点 “监控项”,即可显示监控项的名称和键值信息。请注意一下监控项中每个键值的名称。

4、查看apache状态数据

点击web上面的“监测中”选项,然后选择“最新数据”,根据过滤器指定条件,即可看到“Apache Web Server”这个应用集下每个监控项是否获取到了最新数据,如下图所示:

从此图中可以看出,已经获取到了apache的监控状态数据,重点关注监控项对应的键值名称,每个监控项最后的检查时间以及最新数据信息。

三、zabbix 监控 nginx应用实战
Zabbix对Nginx的监控,与监控apache的方式完全一样,基本流程还是两个步骤,第一个是编写监控Nginx的脚本,第二个是创建Nginx监控模板。这里我们以监控远程主机172.16.213.236上的Nginx服务为例,详细介绍下如何对nginx进行状态监控。

1、 开启nginx状态页

这个操作是在Nginx服务器172.16.213.236上完成的。Nginx跟Apache一样,也提供了状态监控页面,所以,第一步也是开启Nginx的状态监控页面,然后再通过脚本去状态页面获取监控数据即可,这里我们以Nginx1.14版本为例,首先在nginx的配置文件的server段(想监控哪个虚拟主机,就放到哪个server段中)中添加如下配置:

location /nginx-status {
  stub_status on; access_log off; allow 127.0.0.1; allow 172.16.213.132; deny all; }

这段location是打开nginx的状态监控页面,“stub_status”为“on”表示开启状态监控模块,“access_log off”表示关闭这个页面的访问日志,接下来的“allow”表示这个状态监控页面允许哪些客户端访问,一般允许本机(127.0.0.1)和你的客户端电脑就行,这里172.16.213.132就是我的客户端电脑,为了调试方便,我允许自己电脑访问nginx的状态页面,除了允许访问的客户端外,其它都通过“deny all”禁止访问即可。这样,nginx状态页面就设置好了。

2、访问设置好的nginx-status链接

要访问nginx状态页面,可通过如下方式获取nginx状态页面信息:http://172.16.213.236/nginx-status, 其中,172.16.213.236就是nginx服务器,访问这个页面后,会输出如下信息:

Active connections: 22
server accepts handled requests
502254 502254 502259
Reading: 0 Writing: 2 Waiting: 20
对上面输出中每个参数含义详细说明如下:

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

3、编写nginx状态监控脚本

编写nginx状态监控脚本的思路,主要是对状态页面获取的信息进行抓取,下面是通过shell编写的一个抓取nginx状态数据的脚本文件nginx_status.sh,内容如下:

#!/bin/bash
# Set Variables
HOST=127.0.0.1
PORT="80" if [ $# -eq "0" ];then echo "Usage:$0(active|reading|writing|waiting|accepts|handled|requests|ping)" fi # 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 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}' } function ping { /sbin/pidof nginx | wc -l } # Run the requested function $1

脚本内容很简单,基本不需要修改即可使用,如果你要修改主机和端口,可修改脚本中的“HOST”和“PORT”变量。

4、在zabbix agent端修改配置

将编写好的nginx_status.sh脚本放到172.16.213.236服务器上zabbix agent的一个目录下,这里是/etc/zabbix/shell,然后做如下操作:

[root@zabbix agent1 shell]#chmod o+x /etc/zabbix/shell/nginx_status.sh
[root@ zabbix agent1 shell]#chown zabbix:zabbix /etc/zabbix/shell /nginx_status.sh

接着,创建一个名为userparameter_nginx.conf的文件,放到/etc/zabbix/zabbix_agentd.d目录下,内容如下:

UserParameter=nginx.status[*],/etc/zabbix/shell/nginx_status.sh  $1

这个内容其实就是自定义了一个监控项nginx.status[], 其中,“[]”代表参数,这个参数是通过nginx_status.sh脚本的参数传进来的。

所有配置完成,还需要重启zabbix agent服务,以保证配置生效。

5、Nginx模板导入与链接到主机

zabbix默认没有自带Nginx的监控模板,需要自己编写,这里我们直接将编写好的模板供大家下载,可以从如下地址下载Nginx zabbix模板:

[root@iivey /]# wget https://www.ixdba.net/zabbix/zabbix-nginx.zip

模板下载完成后,点击zabbix web导航上面的“配置”选项,然后选择“模板”,点击右上角“导入”按钮,开始导入Nginx模板到zabbix中。
模板导入后,点击web上面的“设置”选项,然后选择“模板”,找到“Template App NGINX”模板,可以看到此模板包含8个监控项,1个触发器,2个图形,1个应用集,重点看一下监控项和键值信息,如下图所示:

最后,还需要将此模板链接到需要监控的主机下,点击web导航上面的“配置”选项,然后选择“主机”,接着点开172.16.213.236主机链接,然后选择“模板”这个二级选项,通过“链接指示器”选择一个模板“Template App NGINX”,添加进去即可。

其实,要对主机的基础信息(CPU、磁盘、内存、网络等)做监控的话,只需要链接一个基础模板“Template OS Linux”到此主机即可,这样,172.16.213.236主机已经链接了四个模板了,如下图所示:

After you add the template, basic information on the host 172.16.213.236, apache information, nginx information, MySQL information have been incorporated into the zabbix monitoring.

6, zabbix server side acquired test data

In join the host zabbix process, some problems may occur, for example, Zabbix server has not been acquired to agent-side data, then, how to troubleshoot it, introduce a simple and effective method here, it is through the implementation of zabbix_get manual tests on zabbix server, If zabbix_get can get to the data, it shows the normal communication between zabbix server and zabbix agent, if not obtain the data, it will error, we can carry out the purpose of the error troubleshooting tips.

In the present embodiment, the following commands may be performed to troubleshoot:

[root@zabbix server ~]# /usr/local/zabbix/bin/zabbix_get -s 172.16.213.236 -p 10050 -k "nginx.status[active]"
16

Which, "nginx.status [active]" is a key term monitoring. Note that this operation is performed on zabbix server, and to process acquired data on zabbix agent.

We are here as long as the tests are normal, are generally able to immediately see the nginx monitor the status of data on zabbix web. How to view nginx monitoring status data and test the alarm trigger action is normal, this had previously been described in detail, I will not repeat here presented.

Guess you like

Origin www.cnblogs.com/flytor/p/11440776.html