1. 在zabbix-agent端添加键值
-
vim /etc/zabbix/zabbix_agentd.d/mystat.conf
UserParameter=mystat[*],/server/scripts/chk_mysql.sh '$1' # 键值是mystat[]
2. 服务端命令行测试键值
[root@zabbix ~]# zabbix_get -s 172.16.1.51 -k mystat[Uptime]
496
3. 编写脚本
[root@db01 log]# cat /server/scripts/chk_mysql.sh
1 #!/bin/bash 2 # ------------------------------------------------------------------------------- 3 # FileName: check_mysql.sh 4 # Revision: 1.0 5 # Date: 2018/01/31 6 # Author: chunk 7 # Email: 8 # Website: 9 # Description: 10 # Notes: ~ 11 # ------------------------------------------------------------------------------- 12 # Copyright: 13 # License: GPL 14 15 # 用户名 16 MYSQL_USER='root' 17 18 # 密码 19 MYSQL_PWD='oldboy123' 20 21 # 主机地址/IP 22 MYSQL_HOST='10.0.0.51' 23 24 # 端口 25 MYSQL_PORT='3306' 26 27 # 数据连接 28 MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}" 29 30 # 参数是否正确 31 if [ $# -ne "1" ];then 32 echo "arg error!" 33 fi 34 35 # 获取数据 36 case $1 in 37 Uptime) 38 result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` 39 echo $result 40 ;; 41 Com_update) 42 result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` 43 echo $result 44 ;; 45 Slow_queries) 46 result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"` 47 echo $result 48 ;; 49 Com_select) 50 result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3` 51 echo $result 52 ;; 53 Com_rollback) 54 result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` 55 echo $result 56 ;; 57 Questions) 58 result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` 59 echo $result 60 ;; 61 Com_insert) 62 result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3` 63 echo $result 64 ;; 65 Com_delete) 66 result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` 67 echo $result 68 ;; 69 Com_commit) 70 result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3` 71 echo $result 72 ;; 73 Bytes_sent) 74 result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` 75 echo $result 76 ;; 77 Bytes_received) 78 result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` 79 echo $result 80 ;; 81 Com_begin) 82 result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` 83 echo $result 84 ;; 85 86 *) 87 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)" 88 ;; 89 esac
4. web页面操作添加监控项
在对应的主机上创建监控项
创建1个监控项之后就点击克隆修改名称和键值
二, 判断端口是否存活,
1,方法:一:映射值:
0就是down 1就是up
2 ,方法 不用值映射
创建触发器
三,还可以添加远程命令 mysql挂了重启,重启不成功,发送邮件
前提,要改客户端2个配置文件
zabbix-agent配置文件中开启远程命令
EnableRemoteCommands=1
visudo 中添加zabbix用户免密
之前测试这样写成功启动myslq ,启动nginx就失败不知道怎么回事
visudo
## Same thing without a password
#%wheel ALL=(ALL) NOPASSWD: ALL
zabbix ALL=(ALL) NOPASSWD: ALL
停止nginx,没有远程执行命令
[root@web01 ~]# systemctl stop nginx
查看日志
Jan 12 04:03:42 web01 sudo: pam_unix(sudo:auth): conversation failed Jan 12 04:03:42 web01 sudo: pam_unix(sudo:auth): auth could not identify password for [zabbix] Jan 12 04:03:42 web01 sudo: pam_succeed_if(sudo:auth): requirement "uid >= 1000" not met by user "zabbix"
后来改了visudo文件
[root@web01 ~]# visudo ## Same thing without a password #%wheel ALL=(ALL) NOPASSWD: ALL zabbix ALL=NOPASSWD: ALL
查看日志,远程支持命令成功
[root@web01 ~]# tail -f /var/log/secure
ticationAgent, locale en_US.UTF-8) (disconnected from bus)Jan 12 15:04:26 web01 sudo: zabbix : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/bin/systemctl restart nginx.service