Two methods for zabbix to collect data using shell scripts

    Goal: monitor the number of connections currently opened by mysql (Threads_connected).
    Environment:
    Zabbix 4.0.29 zabbix
    server: 192.168.149.149 zabbix
    agent: 192.168.149.150

Method 1. Execute shell script through zabbix-agent

    A simple summary is that you need to customize the key-value pair, grab the return value of the shell script through zabbix-agent, and then pass it to zabbix-server.
    step:

  1. Write the script monitor_mysql.sh on the agent side, the content is:
#!/bin/bash
export MYSQL_PWD=123456
/usr/bin/mysql -h 192.168.149.150 -uzabbix -e "show status like '%Threads_connected%';"|grep 'Threads_connected'|awk '{print $2}'

    MYSQL_PWD is the database password, and I put the script in the /tmp directory.

  1. Modify user parameters. Because I have configured in the zabbix_agentd.conf configuration file:
Include=/etc/zabbix/zabbix_agentd.d/*.conf

    So add it to userparameter_mysql.conf in this directory (if there is no userparameter_mysql.conf, just create a new one):

UserParameter=mysql.Threads_connected,sh /tmp/monitor_mysql.sh

    This is a key-value pair, the key is mysql.Threads_connected, and the value is the return value of the script, which is the number of connections currently opened by mysql.

  1. Use zabbix-get to test on the server side. The following problems may be encountered:
# ./zabbix_get -s 192.168.149.150 -k "mysql.Threads_connected"
zabbix_get [9357]: Check access restrictions in Zabbix agent configuration

    Need to modify the server option of zabbix_agentd.conf, in order to save trouble, I set it to Server=0.0.0.0/0.

# ./zabbix_get -s 192.168.149.150 -k "mysql.Threads_connected"
sh: /root/test/monitor_mysql.sh: 权限不够

    Check the permissions, I just moved the script from the /root/test directory to the /tmp directory.

  1. Configure item. Because the return value is a number, select Numeric.
    Insert picture description here
  2. Check the latest data.
    Insert picture description here
Method 2. Execute shell script through zabbix-server

    The simple summary is to execute the script through the External checks function of zabbix-server and record the return value. The advantage is flexibility, the client does not need any configuration, and zabbix-agent is not required; the disadvantage is that excessive use will seriously reduce the performance of the zabbix system.
    step:

  1. Query zabbix_server.conf, the configuration is as follows:
# ExternalScripts=${datadir}/zabbix/externalscripts

    Because the zabbix-server installation directory in the experiment is /usr/local/zabbix, the default path of the external script is /usr/local/zabbix/share/zabbix/externalscripts. Put monitor_mysql.sh in this directory.

  1. Test on the command line.
# sh /usr/local/zabbix/share/zabbix/externalscripts/monitor_mysql.sh 
1

    No problem, go to the next step of the graphical interface configuration.

  1. Graphical interface configuration.
    Insert picture description here
        Type is External check and Key is the script name. If the script has parameters, write them in square brackets, similar to check_oracle.sh["-h","{HOST.CONN}"].

  2. Check the latest data.
    Insert picture description here

Reference documents

[1]Zabbix SIA.EXTERNAL CHECKS[EB/OL].https://www.zabbix.com/documentation/current/manual/config/items/itemtypes/external,2020-01-01.
[2]sfzhang.Zabbix +Shell script monitoring online service [EB/OL].https://blog.51cto.com/sfzhang88/999523,2012-09-20.

Guess you like

Origin blog.csdn.net/zsx0728/article/details/114527987