Board logo

标题: Zabbix5.0.12_调用Shell脚本监控Linux下的MySQL [打印本页]

作者: admin    时间: 2022-8-18 14:47     标题: Zabbix5.0.12_调用Shell脚本监控Linux下的MySQL

笺注: 这是在 CentOS8_LAMP_编译安装Zabbix5.0.12 的基础上进行的。 被监控主机的MySQL的安装可参考 LNMP一键安装包(lamp_Apache2.4用户验证+phpMyAdmin) 笺注:使用以下方法,被监控主机不用安装zabbix-agent 被监控主机的信息: [root@localhost ~]# cat /etc/issue |head -1 CentOS release 6.9 (Final) [root@localhost ~]# [root@localhost ~]# uname -r 2.6.32-696.el6.x86_64 [root@localhost ~]# [root@localhost ~]# ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}' 192.168.168.130 [root@localhost ~]# 被监控主机配置MySQL数据库:( 使用数据库管理员root@localhost ) [root@localhost ~]# mysql -u"root" -p"123" -h"localhost" Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.48-log Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> 查看当前登录的数据库用户: mysql> Select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) mysql> 查看MySQL的版本: mysql> Select version(); +------------+ | version() | +------------+ | 5.5.48-log | +------------+ 1 row in set (0.00 sec) mysql> 查看MySQL的最大连接数: mysql> show variables like '%max_connections%'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 500 | +-----------------+-------+ 1 row in set (0.01 sec) mysql> 创建一个库db1: mysql> Create database db1; Query OK, 1 row affected (0.17 sec) mysql> mysql> show create database db1; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | db1 | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> 注释:库db1的字符集为 utf8mb4_general_ci 在库db1中创建表、插入测试的数据: mysql> use db1; Database changed mysql> mysql> Create table Table1(Name varchar(20),Age tinyint); Query OK, 0 rows affected (0.12 sec) mysql> INSERT into Table1(Name,Age) values ('Zhuohua',8); Query OK, 1 row affected (1.00 sec) mysql> INSERT into Table1(Name,Age) values ('Python',3); Query OK, 1 row affected (0.00 sec) mysql> INSERT into Table1(Name,Age) values ('李大杰',-6); Query OK, 1 row affected (0.00 sec) 输出库db1的表Table1中的所有记录: select * from db1.Table1; 图片1.png 在MySQL下查看当前使用的是哪个库: mysql> Select database(); +------------+ | database() | +------------+ | db1 | +------------+ 1 row in set (0.00 sec) mysql> 输出当前库的表Table1中的所有记录: select * from Table1; 图片2.png ###### select Name,Age,ABS(Age) from Table1; 图片3.png 注释:ABS()函数会返回数值的绝对值。 select Name,UPPER(Name) from Table1; 图片4.png 注释:UPPER()函数会把英文字母转换为大写。 select Name,LOWER(Name) from Table1; 图片5.png 注释:LOWER()函数会把英文字母转换为小写。 select Name,LENGTH(Name) AS 字符串的长度 from Table1; 图片6.png 注释:LENGTH()函数会返回字符串的长度;假如MySQL的字符编码是UTF-8,那么一个汉字将返回 3 ###### 再授权数据库用户zhuohua@'%'(密码168),可以从任意IP进行访问,对库db1有完全控制的权限: mysql> grant all on db1.* to zhuohua@'%' identified by '168'; Query OK, 0 rows affected (0.00 sec) mysql> 查看远程数据库用户zhuohua@'%'的权限: mysql> show grants for zhuohua@'%'; +--------------------------------------------------------------------------------------------------------+ | Grants for zhuohua@% | +--------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'zhuohua'@'%' IDENTIFIED BY PASSWORD '*242E46A1E8D30FE06F7CE37B55BFC25BA981D70C' | | GRANT ALL PRIVILEGES ON `db1`.* TO 'zhuohua'@'%' | +--------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> 被监控主机的防火墙配置:(打开TCP 3306端口) [root@localhost ~]# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT [root@localhost ~]# iptables-save > /etc/sysconfig/iptables [root@localhost ~]# ############# ############# Zabbix服务器使用Shell脚本获取被监控主机的MySQL版本: [root@centos8 ~]# cat /script/xx.sh #!/bin/bash mysql -u"zhuohua" -p"168" -P"3306" -h"192.168.168.130" -sN -e"Select version()" 脚本运行的结果: [root@centos8 ~]# bash /script/xx.sh 5.5.48-log [root@centos8 ~]# ############# Zabbix服务器使用Shell脚本获取被监控主机的MySQL最大连接数: [root@centos8 ~]# cat /script/xx.sh #!/bin/bash mysql -u"zhuohua" -p"168" -P"3306" -h"192.168.168.130" -sN -e"Show variables like '%max_connections%'" |awk '{print $2} ' 脚本运行的结果: [root@centos8 ~]# bash /script/xx.sh 500 [root@centos8 ~]# 设置脚本权限: [root@centos8 ~]# chmod a+x /script/xx.sh [root@centos8 ~]# ############# Zabbix服务器使用Shell脚本输出SQL语句执行后的返回值: [root@centos8 ~]# cat /script/yy.sh #!/bin/bash mysql -u"zhuohua" -p"168" -P"3306" -h"192.168.168.130" -sN -e"Select Age from db1.Table1 where Name = 'Zhuohua'" 脚本运行的结果: [root@centos8 ~]# bash /script/yy.sh 8 [root@centos8 ~]# 设置脚本权限: [root@centos8 ~]# chmod a+x /script/yy.sh [root@centos8 ~]# ############# Zabbix服务器使用Shell脚本输出SQL语句执行后的返回值: [root@centos8 ~]# cat /script/zz.sh #!/bin/bash mysql -u"zhuohua" -p"168" -P"3306" -h"192.168.168.130" -sN -e"Select Age from db1.Table1 where Name = 'Python'" 脚本运行的结果: [root@centos8 ~]# bash /script/zz.sh 3 [root@centos8 ~]# 设置脚本权限: [root@centos8 ~]# chmod a+x /script/zz.sh [root@centos8 ~]# ############# ############# 修改Zabbix服务器的Agent配置文件: [root@centos8 ~]# find / -name zabbix_agentd.conf /root/zabbix-5.0.12/conf/zabbix_agentd.conf /usr/local/zabbix/etc/zabbix_agentd.conf [root@centos8 ~]# [root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf # UnsafeUserParameters=0 修改为:(启用该功能) UnsafeUserParameters=1 接着插入: UserParameter=command_1,/bin/bash /script/xx.sh UserParameter=command_2,/bin/bash /script/yy.sh UserParameter=command_3,/bin/bash /script/zz.sh 如下图: 图片7.png 保存好配置文件后,重启一下Zabbix本机的客户端和服务端: pkill -9 -U zabbix /usr/local/zabbix/sbin/zabbix_agentd /usr/local/zabbix/sbin/zabbix_server ###### 在Zabbix服务器测试,验证能否获取本机(127.0.0.1)的key: [root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_1 500 [root@centos8 ~]# [root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_2 8 [root@centos8 ~]# [root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_3 3 [root@centos8 ~]# ################## 配置》主机: 在主机Zabbix server中创建监控项: 图片8.png 给“被监控主机的MySQL最大连接数”创建监控项: 自定义名称: Oracle command_1 键值: command_1 信息类型: 数字(无正负) 图片9.png 备注:其他地方保持默认,点击底下的“添加”按键。 给监控项“Oracle command_1”创建图形“Oracle command_1 image”: 图片10.png 备注:其他地方保持默认,点击底下的“添加”按键。 查看图形“Oracle command_1 image”: 监测》主机》左击主机名称Zabbix server》图形: 图片11.png 选择时间范围: 图片12.png 使用“过滤器”,选择显示的图形:(默认会显示所有图形) 图片13.png 图片14.png 图片15.png ################## 在主机Zabbix server中再创建一个监控项: 给“Zabbix服务器使用Shell脚本输出SQL语句执行后的返回值”创建监控项: 自定义名称: Oracle command_2 键值: command_2 信息类型: 数字(无正负) 图片16.png 备注:其他地方保持默认,点击底下的“添加”按键。 给监控项“Oracle command_2”创建图形“Oracle command_2 image”: 图片17.png 备注:其他地方保持默认,点击底下的“添加”按键。 查看图形“Oracle command_2 image”: 图片18.png 图片19.png 图片20.png ############# ############# 在库db1的表Table1中更改数据: mysql> use db1; Database changed mysql> mysql> Update Table1 set Age = 18 where Name = 'Zhuohua'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> mysql> select * from Table1 where Name = 'Zhuohua'; +---------+------+ | Name | Age | +---------+------+ | Zhuohua | 18 | +---------+------+ 1 row in set (0.00 sec) mysql> 在Zabbix服务器测试,验证能否获取本机(127.0.0.1)的键值: [root@centos8 ~]# find / -name "zabbix_get" /root/zabbix-5.0.12/src/zabbix_get /root/zabbix-5.0.12/src/zabbix_get/zabbix_get /usr/local/zabbix/bin/zabbix_get [root@centos8 ~]# 做个软链接: [root@centos8 ~]# ln -s /usr/local/zabbix/bin/zabbix_get /usr/sbin [root@centos8 ~]# [root@centos8 ~]# which zabbix_get /usr/sbin/zabbix_get [root@centos8 ~]# [root@centos8 ~]# zabbix_get -s 127.0.0.1 -k command_2 18 [root@centos8 ~]# 再次查看图形“Oracle command_2 image”: 图片21.png 图片22.png 相关文章: MySQL的SQL语句 Zabbix自定义监控Linux下的MySQL Zabbix5.0.12_调用Python3脚本监控Linux下的MySQL

图片附件: 图片1.png (2022-8-18 14:34, 35.15 KB) / 下载次数 38
http://blog.zhuohua.store/attachment.php?aid=20250&k=504c2a1b6e39588e317a811ca13e878b&t=1714707331&sid=zW5rGr



图片附件: 图片2.png (2022-8-18 14:35, 34.61 KB) / 下载次数 40
http://blog.zhuohua.store/attachment.php?aid=20251&k=3e2c8f69c3a9f7b1027d8daf79f8feb2&t=1714707331&sid=zW5rGr



图片附件: 图片3.png (2022-8-18 14:36, 41.87 KB) / 下载次数 27
http://blog.zhuohua.store/attachment.php?aid=20252&k=a4a6467e263fe2fac350d7c7ecc19387&t=1714707331&sid=zW5rGr



图片附件: 图片4.png (2022-8-18 14:36, 41.64 KB) / 下载次数 43
http://blog.zhuohua.store/attachment.php?aid=20253&k=5919c1ca9945e077cfa830b2d96e2bd8&t=1714707331&sid=zW5rGr



图片附件: 图片5.png (2022-8-18 14:36, 39.98 KB) / 下载次数 42
http://blog.zhuohua.store/attachment.php?aid=20254&k=347a37c197f9803fffea686e94b1b1f8&t=1714707331&sid=zW5rGr



图片附件: 图片6.png (2022-8-18 14:37, 65.2 KB) / 下载次数 41
http://blog.zhuohua.store/attachment.php?aid=20255&k=7721a215603c5cf2fa0c1d4bdb748e13&t=1714707331&sid=zW5rGr



图片附件: 图片7.png (2022-8-18 14:41, 51.86 KB) / 下载次数 42
http://blog.zhuohua.store/attachment.php?aid=20256&k=be653bad5585f0aba6a0c8f0d95ac661&t=1714707331&sid=zW5rGr



图片附件: 图片8.png (2022-8-18 14:42, 14.42 KB) / 下载次数 43
http://blog.zhuohua.store/attachment.php?aid=20257&k=aa01a3f38da55e9d19cbcf89bdecb4f5&t=1714707331&sid=zW5rGr



图片附件: 图片9.png (2022-8-18 14:42, 27.98 KB) / 下载次数 36
http://blog.zhuohua.store/attachment.php?aid=20258&k=e5dbc3ed58ec6beff56969eef6a3f4d6&t=1714707331&sid=zW5rGr



图片附件: 图片10.png (2022-8-18 14:42, 76.3 KB) / 下载次数 34
http://blog.zhuohua.store/attachment.php?aid=20259&k=04c796b47b8f3bf260efb26b53be2962&t=1714707331&sid=zW5rGr



图片附件: 图片11.png (2022-8-18 14:43, 46.06 KB) / 下载次数 33
http://blog.zhuohua.store/attachment.php?aid=20260&k=8da64e484f2bf852e764bfbb5e37c67b&t=1714707331&sid=zW5rGr



图片附件: 图片12.png (2022-8-18 14:43, 61.54 KB) / 下载次数 27
http://blog.zhuohua.store/attachment.php?aid=20261&k=619fae1b580e700369425580fdf46151&t=1714707331&sid=zW5rGr



图片附件: 图片13.png (2022-8-18 14:44, 14.79 KB) / 下载次数 37
http://blog.zhuohua.store/attachment.php?aid=20262&k=aad16489ed7fb012e5aeaea23c84bbf7&t=1714707331&sid=zW5rGr



图片附件: 图片14.png (2022-8-18 14:44, 78.54 KB) / 下载次数 36
http://blog.zhuohua.store/attachment.php?aid=20263&k=e484733bef8040bd5d47944a9755bc70&t=1714707331&sid=zW5rGr



图片附件: 图片15.png (2022-8-18 14:44, 38.03 KB) / 下载次数 44
http://blog.zhuohua.store/attachment.php?aid=20264&k=00529eb77488f451f873b8df7877b9cc&t=1714707331&sid=zW5rGr



图片附件: 图片16.png (2022-8-18 14:45, 24.4 KB) / 下载次数 35
http://blog.zhuohua.store/attachment.php?aid=20265&k=38ef86c8626603e50baea86c35366a64&t=1714707331&sid=zW5rGr



图片附件: 图片17.png (2022-8-18 14:45, 74.74 KB) / 下载次数 51
http://blog.zhuohua.store/attachment.php?aid=20266&k=33096ac9de7cfbd37fb7ae61523382a8&t=1714707331&sid=zW5rGr



图片附件: 图片18.png (2022-8-18 14:45, 17.29 KB) / 下载次数 31
http://blog.zhuohua.store/attachment.php?aid=20267&k=841e77c138c3104f1acd2e6dc5cfe486&t=1714707331&sid=zW5rGr



图片附件: 图片19.png (2022-8-18 14:45, 80.26 KB) / 下载次数 33
http://blog.zhuohua.store/attachment.php?aid=20268&k=590e093198f1825b57995b7ea7cc4226&t=1714707331&sid=zW5rGr



图片附件: 图片20.png (2022-8-18 14:46, 34.56 KB) / 下载次数 28
http://blog.zhuohua.store/attachment.php?aid=20269&k=0da92d642d5a1d69f4582ada1b571580&t=1714707331&sid=zW5rGr



图片附件: 图片21.png (2022-8-18 14:47, 74.27 KB) / 下载次数 24
http://blog.zhuohua.store/attachment.php?aid=20270&k=081256f5140b83cdfe8b8af01c049815&t=1714707331&sid=zW5rGr



图片附件: 图片22.png (2022-8-18 14:47, 36.14 KB) / 下载次数 27
http://blog.zhuohua.store/attachment.php?aid=20271&k=852f0af9e1b17d154c5d623c57fab5b6&t=1714707331&sid=zW5rGr






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2