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;
在MySQL下查看当前使用的是哪个库:
mysql> Select database();
+------------+
| database() |
+------------+
| db1 |
+------------+
1 row in set (0.00 sec)
mysql>
输出当前库的表Table1中的所有记录:
select * from Table1;
######
select Name,Age,ABS(Age) from Table1;
注释:ABS()函数会返回数值的绝对值。
select Name,UPPER(Name) from Table1;
注释:UPPER()函数会把英文字母转换为大写。
select Name,LOWER(Name) from Table1;
注释:LOWER()函数会把英文字母转换为小写。
select Name,LENGTH(Name) AS 字符串的长度 from Table1;
注释: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
如下图:
保存好配置文件后,重启一下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中创建监控项:
给“被监控主机的MySQL最大连接数”创建监控项:
自定义名称: Oracle command_1
键值: command_1
信息类型: 数字(无正负)
备注:其他地方保持默认,点击底下的“添加”按键。
给监控项“Oracle command_1”创建图形“Oracle command_1 image”:
备注:其他地方保持默认,点击底下的“添加”按键。
查看图形“Oracle command_1 image”:
监测》主机》左击主机名称Zabbix server》图形:
选择时间范围:
使用“过滤器”,选择显示的图形:(默认会显示所有图形)
##################
在主机Zabbix server中再创建一个监控项:
给“Zabbix服务器使用Shell脚本输出SQL语句执行后的返回值”创建监控项:
自定义名称: Oracle command_2
键值: command_2
信息类型: 数字(无正负)
备注:其他地方保持默认,点击底下的“添加”按键。
给监控项“Oracle command_2”创建图形“Oracle command_2 image”:
备注:其他地方保持默认,点击底下的“添加”按键。
查看图形“Oracle command_2 image”:
#############
#############
在库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”:
相关文章:
MySQL的SQL语句
Zabbix自定义监控Linux下的MySQL
Zabbix5.0.12_调用Python3脚本监控Linux下的MySQL |