Zabbix Monitors Mysql, Apache, Nginx Applications

Zabbix's monitoring of third-party application software has two main difficulties: one is to write a custom monitoring script; the other is to write a template and import it into the zabbix web, which is customized to the monitoring requirements, while writing a template file is relatively difficult, but there are already many templates written on the web that can be used directly.So it's not really difficult for Zabbix to monitor application software.

1. zabbix monitoring mysql application

This blog post starts with Zabbix's monitoring of Mysql, which is the simplest because Zabbix already has its own template for Mysql monitoring. We just need to write a script to monitor Mysql!The following actions are implemented on the zabbix agent side as follows:

1) Install mysql on the zabbix agent side

Because mysql installation is too time consuming, I provide One-click installation of mysql script As follows:

[root@agent ~]# ls mysql*
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  mysql.sh
[root@agent ~]# sh mysql.sh 
Starting MySQL. SUCCESS! 
mysql: [Warning] Using a password on the command line interface can be insecure.
//This message indicates that the installation was successful!
[root@agent ~]# The root password for mysql-u root-p123//script is 123
mysql> grant all on *.* to zabbix@'192.168.1.%' identified by 'zabbix';
//Create Test User

2) Write a custom monitoring mysql script on the zabbix agent side

[root@agent ~]# mkdir /etc/zabbix/shell
[root@agent ~]# vim /etc/zabbix/shell/check_mysql.sh 
#!/bin/bash
MYSQL_HOST='192.168.1.8'     #Specify the IP address of the mysql host
MYSQL_PORT='3306'            #Specify the listening address of the mysql host
MYSQL_CONN="/usr/local/bin/mysqladmin -h${MYSQL_HOST} -P${MYSQL_PORT}"     #Specify the tool to use for the connection
if [ $# -ne "1" ];then
    echo "arg error!"
fi
# get data
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
//Because it is not safe to write user name and password information into the script, write user name and password information into the / etc/my.cnf file
[root@agent ~]# vim /etc/my.cnf 
[mysqladmin]
user=zabbix
password=zabbix
[root@agent ~]# chmod +x /etc/zabbix/shell/check_mysql.sh 
[root@agent ~]# chown zabbix.zabbix /etc/zabbix/shell/check_mysql.sh 
[root@agent ~]# sh /etc/zabbix/shell/check_mysql.sh Uptime
5260
//agent-side test scripts can be used

3) Modify configuration on zabbix agent side

[root@agent ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
//Comment all of the original UserParameter s and add the following
UserParameter=mysql.status[*],/etc/zabbix/shell/check_mysql.sh $1
UserParameter=mysql.ping,HOME=/etc /usr/local/bin/mysqladmin ping | grep -c alive
UserParameter=mysql.version,/usr/local/bin/mysql -V
[root@agent ~]# systemctl restart zabbix-agent
[root@zabbix ~]# zabbix_get -s 192.168.1.8 -p 10050 -k "mysql.status[Uptime]"
5372
//zabbix server-side test to see if data is available

4) Introduction of template to zabbix web interface

zabbix comes with a template for mysql monitoring, so just link the template to the host!The following:





The focus here is on the configuration of the "key value" column for each monitoring item name, where the key values must be consistent with the agent-side customized monitoring key values.In addition, you can see that the monitor item "mysql status" has a trigger to check the running status of mysql.Finally, it is important to note that the monitoring type of these monitors is the zabbix client, and that all monitors are stored in the MYSQL application set.

The above setup is complete, the host that monitors mysql has been added!

5) View monitoring status data

Figure:


You can see from the diagram that the data is already available!

Configuring mysql monitoring is complete!

2. zabbix monitoring apache application

zabbix's monitoring of apache is slightly more complex, but the basic process consists of two steps: writing a monitoring script to monitor apache and creating an apache monitoring template.The following actions are implemented on the zabbix agent side as follows:

1) zabbix agent opens apache status page

To monitor the status of apache, you need to open an Apache status page in the Apache configuration, and then script to get the data of this status page to monitor apache!

[root@agent ~]# yum -y install httpd
[root@agent ~]# vim /etc/httpd/conf/httpd.conf 
//Add the following at the end
EnableSendfile on
<location /server-status>
SetHandler server-status
Require ip 127.0.0.1 192.168.1.10
</location>
[root@agent ~]# systemctl start httpd

The configuration items are explained as follows:

1) EnableSendfile on: Indicates that the extended status information is turned on, but when turned on, the server will be less efficient;
2)/server-status: means pass" http://ip/server-status "To access, but also through" http://ip/server-status?refresh=N "For dynamic access, N means an automatic refresh in N seconds;
3) Require ip 127.0.0.1 192.168.1.10: This is a new special effect added to Apache version 2.4.That is, the IP address that needs to be accessed;

The common uses of Require are as follows:

1) Require all granted: Allow access to all hosts;
2) Require all denied: deny access to all hosts;
3) Require IP address: Allow an IP access;
4) Require not ip IP address: Prohibit an IP access;
5) Require host host name: allow access to a host;
6) Require not host host host name: prohibit access to a host;

Restart apache service after modification takes effect!

2) zabbix agent to get apache state detection scripts and zabbix templates

After configuring the apache status page, you need to write a script to get the status data. There are a lot of script code, you can download it as follows!

[root@agent ~]# wget https://www.ixdba.net/zabbix/zabbix-apache.zip
[root@agent ~]# unzip zabbix-apache.zip
[root@agent ~]# ll zapache*
-rw-r--r-- 1 root root  5792 1 Month 31 23:00 zapache
-rw-r--r-- 1 root root 52024 4 February 22, 2016 zapache-template.xml
//zapache: is the script needed to monitor apache;
//zapache-template.xml: is the template required to monitor apache;
[root@agent ~]# mv zapache /etc/zabbix/shell/
[root@agent ~]# chmod 755 /etc/zabbix/shell/zapache 
[root@agent ~]# chown zabbix.zabbix /etc/zabbix/shell/zapache 
[root@agent ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
//Write apache template files to invoke apache monitoring scripts
UserParameter=zapache[*],/etc/zabbix/shell/zapache $1
[root@agent ~]# systemctl restart zabbix-agent
[root@zabbix ~]# zabbix_get -s 192.168.1.8 -p 10050 -k "zapache[BusyWorkers]"
1
//zabbix server tests whether data can be obtained

3) zabbix web page import template

Figure:



4) View apache status data

Figure:


You can see from the diagram that the data is already available!

Configure apache monitoring complete!

3. zabbix Monitoring Nginx Application

zabbix monitors Nginx in exactly the same way apache is monitored. The basic process is two steps: the first is to script Nginx; the second is to create a Nginx monitoring template.The following actions are implemented on the zabbix agent side as follows:

1) zabbix agent opens Nginx status page

[root@agent ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@agent ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@agent ~]# tar zxf nginx-1.14.2.tar.gz -C /usr/src
[root@agent ~]# cd /usr/src/nginx-1.14.2/
[root@agent nginx-1.14.2]#  ./configure --with-http_stub_status_module && make && make install
[root@agent ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
//Add loaction Rule
        location /nginx-status {
          stub_status on;            //Open Status Monitoring Module
          access_log off;            //Close Access Log
          allow 127.0.0.1; 
          allow 192.168.1.10;       //IP Address Allowed Access
          deny all;                         //Deny all IP addresses
        }
[root@agent ~]# nginx -t
[root@agent ~]# nginx               
[root@agent ~]# curl 127.0.0.1/nginx-status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 

Output information description:

1) Active connections: the number of active links initiated to the backend;
2) how many connections have been processed by accepts:nginx in total;
3) handled:nginx successfully created several handshakes;
4) Total number of requests processed by requests:nginx;
5) Number of header s of Reading:nginx reading clients;
6) The number of header s returned by Writing:nginx to the client;
7) Waiting:nginx request processing is complete, waiting for the next request instruction connection;

2) zabbix agent to get apache state detection scripts and zabbix templates

[root@agent ~]# wget https://www.ixdba.net/zabbix/zabbix-nginx.zip
[root@agent ~]# unzip zabbix-nginx.zip 
[root@agent ~]# ll nginx_status.sh zbx*
-rw-r--r-- 1 root root  1106 11 January 2018 nginx_status.sh
-rw-r--r-- 1 root root 20703 11 January 2018 zbx_nginx_templates.xml
//nginx_status.sh: Required script
// zbx_nginx_templates.xml: required template
[root@agent ~]# mv nginx_status.sh /etc/zabbix/shell/
[root@agent ~]# chmod o+x /etc/zabbix/shell/nginx_status.sh 
[root@agent ~]# chown zabbix.zabbix /etc/zabbix/shell/nginx_status.sh 
[root@agent ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
//Write a template file for nginx to invoke monitoring scripts for nginx
UserParameter=nginx.status[*],/etc/zabbix/shell/nginx_status.sh $1
[root@agent ~]# systemctl restart zabbix-agent
[root@zabbix ~]# zabbix_get -s 192.168.1.8 -p 10050 -k "nginx.status[active]"
1
//zabbix server tests whether data can be obtained

3) zabbix web page import template

Figure:



4) View nginx status data

Figure:


You can see from the diagram that the data is already available!

Configure nginx monitoring complete!

This is the end of the blog. Thank you for reading it.

Tags: Linux Zabbix Nginx MySQL Apache

Posted on Sat, 01 Feb 2020 13:12:31 -0800 by Keith Scott