Environment: |
centos6.6 |
zabbix-2.4.5 |
mysql-5.6.24 |
Zabbix does not have its own monitoring template for MySQL, many templates on the web are fixed listening to a certain port, not flexible enough, if the MySQL port a lot of memory is not clear, we should LLD (Low level discovery) mode monitors mysql.
Steps:
First, the client
1. JSON of monitoring content
in the client, the monitoring content needs to be JSON displayed, and then the server can filter the results through regular. Below is a JSON display of my json-monitored mysql:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A8/wKiom1XV3yyTX-TJAACeP5dZVV8556.jpg "title=" 1.png " alt= "Wkiom1xv3yytx-tjaacep5dzvv8556.jpg"/>
The script reads as follows:
#Fucation: Mysql low-level discovery#script_name mysql_low_discovery.shmysql () { port= ($ (sudo ss -tnlp | grep mysql | awk -f "[ :]+" ' {print $4} ') printf ' {\ n ' printf ' \ t ' data: [\ n ' for key in ${!port[@]} do if [[ "${#port [@]}" -gt 1 && "${key}" -ne "$ ((${#port [@]}-1)]" ]];then socket=$ (ps aux|grep ${port[ ${key}]}|grep -v grep|awk -f ' = ' ' {print $10} ' |cut -d ' ' -f  1) printf ' \t {\n ' printf "\t\t\t\" {#MYSQLPORT}\ ": \" ${port[${key}]}\ "},\n" else [[ "${key}" -eq "((${#port [@]}-1))" ]] socket=$ (ps aux|grep ${port[${key}]}|grep -v grep|awk - f ' = ' '{print $10} ' |cut -d ' ' -f 1 ' printf ' \t {\n ' printf "\t\t\t\" {#MYSQLPORT}\ ": \" ${port[${key}]}\ "}\n" fi done printf ' \ t ]\n ' printf '}\n '}$1
If you want the script to be able to use Zabbix's identity to obtain data, do the following four steps:
Chown Zabbix.zabbix mysql_low_discovery.shchmod +x mysql_low_discovery.shecho "Zabbix all= (Root) nopasswd:/usr/sbin/ SS ">>/etc/sudoerssed-i ' s/^defaults.*.requiretty/#Defaults requiretty/'/etc/sudoers
Finally put the script into a directory that Zabbix user allows to access, define Userparameter when the path remains the same.
2. Custom Key values
In the client to modify the configuration file zabbix_agentd.conf, add the content:
Userparameter=zabbix_low_discovery[*],/bin/bash/usr/local/zabbix-2.4.5/script/mysql_low_discovery.sh $ 1userparameter=mysql_stats[*],/usr/local/mysql/bin/mysql--login-path=local-e "show Global Status" | grep "\<$2\>" | Cut-f2
There's a pit here. To be said in advance, for mysql5.6 above, direct use of MYSQL-UXXX-PXXX-E "show global Status" and other direct password command will be warned:
Warning:using a password on the command line interface can is insecure.
this warning will directly cause the Zabbix server to not recognize the value of mysql_stats[*]. The workaround is to use the Mysql_config_editor tool that comes with MySQL:
Mysql_config_editor set--login-path=local--host=127.0.0.1--user=username--password Enter will prompt for password
Then we can use it in the script:
MySQL--login-path=local-e "statement"
Replace
Mysql-u username-p pass-e "statement"
3.Mysql account for authorized query function
Grant SELECT On * * to [e-mail protected] ' 127.0.0.1 ' identified by "PASSWORD"; flush privleges;
4. Restart the Zabbix client
Killall Zabbix_agentd/usr/local/zabbix/sbin/zabbix_agentd
5. Testing
Test scripts and commands first:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A8/wKiom1XV4UPgK_E3AAE_hw00udw978.jpg "title=" 2.png " alt= "Wkiom1xv4upgk_e3aae_hw00udw978.jpg"/>
The re-test client can not get the key value:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A4/wKioL1XV41KAHQIjAAGYf_sgiX0490.jpg "title=" 3.png " alt= "Wkiol1xv41kahqijaagyf_sgix0490.jpg"/>
Finally, test the server side can not get the key value:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A8/wKiom1XV4USA_UtvAAFXHf1Wy3U171.jpg "title=" 4.png " alt= "Wkiom1xv4usa_utvaafxhf1wy3u171.jpg"/>
Note: The data format received by Zabbix must be in JSON format, and the form should be:
{"Data": [{"{#键}": "Value"}]}
Second, the service side
1. Create a template
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A8/wKiom1XV4eqwtfszAANDqtvsnSk710.jpg "title=" 5.png " alt= "Wkiom1xv4eqwtfszaandqtvsnsk710.jpg"/>
2. Set Discovery Rules
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A4/wKioL1XV4_qSGS8FAAPtHD34S3I707.jpg "title=" 6.png " alt= "Wkiol1xv4_qsgs8faapthd34s3i707.jpg"/>
3. Set the regular
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/A4/wKioL1XV4_rSBKDPAAOSWCrXk1Y843.jpg "title=" 7.png " alt= "Wkiol1xv4_rsbkdpaaoswcrxk1y843.jpg"/>
4. Create a Discovery Item
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/A8/wKiom1XV4evTPX5mAATju_cC-Os390.jpg "title=" 8.png " alt= "Wkiom1xv4evtpx5maatju_cc-os390.jpg"/>
5. Create a trigger for a discovery item
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/A4/wKioL1XV4_rQw20_AAP9w1OYEW4406.jpg "title=" 9.png " alt= "Wkiol1xv4_rqw20_aap9w1oyew4406.jpg"/>
6. Drawing the items you want
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/A8/wKiom1XV4evBd9ubAAVHcoOFmcw565.jpg "title=" 10.png "alt=" Wkiom1xv4evbd9ubaavhcoofmcw565.jpg "/>
7. Linking a template to a MySQL server
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A4/wKioL1XV4_rAQ-AwAAO7ftDqzGY164.jpg "title=" 11.png "alt=" Wkiol1xv4_raq-awaao7ftdqzgy164.jpg "/>
8. Precautions
(1) Discovery rule Set Update duration is 1 hours
Why set 3,600 seconds? The reason is that if the setup time is too short, such as you set 60s, one is the server pressure, one is if you detect the port suddenly when dropped, has not come to the urgent alarm, the host through the JSON to obtain this information, it is considered that there is no this port, the template will automatically close the content of this monitoring item (default reserved 30 days , and then automatically delete), so that the alarm function is basically no, so it is found that the interval longer than some better.
(2) Set the regular
The regular is set to the MySQL listening port range to fixed, so that there is no error, you can not set the regular, this time need to set the discovery rule when the regular item to be emptied.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/A8/wKiom1XV4eyz8gcDAAGyMepJfvk801.jpg "title=" 12.png "alt=" Wkiom1xv4eyz8gcdaagymepjfvk801.jpg "/>
(3) Create Discovery Item (item prototypes) to be aware
Name: You can't do whatever you want to know.
Type: Set to Zabbix agent, which means to submit data proactively
key: Must be mysql_stats[{#MYSQLPORT},$2], the first parameter is the port, the second parameter is the item you specifically want to query, my template has 47 items, These projects are mysql-uxxx-pxxx-e "Show global Status" has been listed and can be viewed on its own.
At last:
Attachment template to serve the
This article is from the "Tongluowan" blog, make sure to keep this source http://wuhf2015.blog.51cto.com/8213008/1686674
Zabbix Monitor MySQL's discovery template