zabbix监控部署之自定义监控
自定义监控进程
自定义监控日志
自定义监控MySQL主从状态
自定义监控MySQL主从延迟
主机名 | 系统版本 | ip地址 | 主机 | zabbix版本 |
---|---|---|---|---|
kiwi.server.com | CentOS8 | 192.168.234.33 | 服务端 | zabbix-6.4.6 |
kiwi111.client.com | CentOS8 | 192.168.234.22 | 客户端 | zabbix-6.4.6 |
1.配置自定义监控进程
1.1 在客户端打开自定义监控功能
[root@kiwi111 ~]# vim /usr/local/etc/zabbix_agentd.conf
··························略
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1 ## 添加这一行
### Option: UserParameter
····················略
## 重启服务
[root@kiwi111 ~]# systemctl restart zabbix_agentd
[root@kiwi111 ~]#
1.2 编写检查进程的脚本
## 创建一个专门用来放脚本的目录
[root@kiwi111 ~]# mkdir /kiwi_scripts
[root@kiwi111 ~]# cd /kiwi_scripts/
[root@kiwi111 kiwi_scripts]# vim process.sh
[root@kiwi111 kiwi_scripts]# cat process.sh
#!/bin/bash
content=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1")
if [ $content -eq 0 ];then
echo 1
else
echo 0
fi
[root@kiwi111 kiwi_scripts]# chmod +x process.sh
[root@kiwi111 kiwi_scripts]# ./process.sh httpd
1
[root@kiwi111 kiwi_scripts]# ./process.sh zabbix
0
## 结果为1就是服务进程有问题,0则没有问题
1.3 修改zabbix配置文件
让脚本能够绑定一个key
[root@kiwi111 kiwi_scripts]# vim /usr/local/etc/zabbix_agentd.conf
································略
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
····································略
UserParameter=check_process[*],/bin/bash /kiwi_scripts/process.sh $1 ## 在此文件加入这一行,$1表示要监控的进程,路径为脚本路径
## 此行可以在上面复制这一行UserParameter=<key>,<shell command>
## 重启服务
[root@kiwi111 kiwi_scripts]# systemctl restart zabbix_agentd
1.4 在服务端进行测试
[root@kiwi etc]# zabbix_get -s 192.168.234.22 -k check_process[mysql]
0
[root@kiwi etc]# zabbix_get -s 192.168.234.22 -k check_process[httpd]
1
[root@kiwi etc]#
1.5 在web端添加监控项
创建监控项
完成之后点添加
这样就是添加好了
接着再去创建触发器
填完之后点insert
完成之后再点add
添加完成
现在将客户端的MySQL服务给关闭
[root@kiwi111 ~]# systemctl stop mysqld
[root@kiwi111 ~]#
成功报警
注:这里邮件发送不了是因为我把邮箱告警给关闭了
监控完成
2.配置自定义监控日志
2.1 在客户端配置一个py脚本
## 注意:该脚本检测到一个Error之后为0,当它再检查一遍之后发现同一个Error就不会再为0
[root@kiwi111 kiwi_scripts]# vim log.py
[root@kiwi111 kiwi_scripts]# cat log.py
#!/usr/bin/env python3
import sys
import re
def prePos(seekfile):
global curpos
try:
cf = open(seekfile)
except IOError:
curpos = 0
return curpos
except FileNotFoundError:
curpos = 0
return curpos
else:
try:
curpos = int(cf.readline().strip())
except ValueError:
curpos = 0
cf.close()
return curpos
cf.close()
return curpos
def lastPos(filename):
with open(filename) as lfile:
if lfile.readline():
lfile.seek(0,2)
else:
return 0
lastPos = lfile.tell()
return lastPos
def getSeekFile():
try:
seekfile = sys.argv[2]
except IndexError:
seekfile = '/tmp/logseek'
return seekfile
def getKey():
try:
tagKey = str(sys.argv[3])
except IndexError:
tagKey = 'Error'
return tagKey
def getResult(filename,seekfile,tagkey):
destPos = prePos(seekfile)
curPos = lastPos(filename)
if curPos < destPos:
curpos = 0
try:
f = open(filename)
except IOError:
print('Could not open file: %s' % filename)
except FileNotFoundError:
print('Could not open file: %s' % filename)
else:
f.seek(destPos)
while curPos != 0 and f.tell() < curPos:
rresult = f.readline().strip()
global result
if re.search(tagkey, rresult):
result = 1
break
else:
result = 0
with open(seekfile,'w') as sf:
sf.write(str(curPos))
finally:
f.close()
return result
if __name__ == "__main__":
result = 0
curpos = 0
tagkey = getKey()
seekfile = getSeekFile()
result = getResult(sys.argv[1],seekfile,tagkey)
print(result)
[root@kiwi111 kiwi_scripts]#
[root@kiwi111 kiwi_scripts]# chmod +x log.py
[root@kiwi111 kiwi_scripts]#
2.2 在客户端修改配置文件
让脚本能够绑定一个key
[root@kiwi111 kiwi_scripts]# vim /usr/local/etc/zabbix_agentd.conf
······························略
UserParameter=check_process[*],/bin/bash /kiwi_scripts/process.sh $1
UserParameter=check_logs[*],python3 /kiwi_scripts/log.py $1 $2 $3 ## 添加此行
## 安装Python3
[root@kiwi111 kiwi_scripts]# yum install -y python3
## 重启服务
[root@kiwi111 kiwi_scripts]# systemctl restart zabbix_agentd
[root@kiwi111 kiwi_scripts]#
2.3 测试
## 在客户端给权限
[root@kiwi111 ~]# chmod 755 /var/log/mysql/mysqld.log
## 在服务端测试
[root@kiwi ~]# zabbix_get -s 192.168.234.22 -k check_logs['/var/log/mysql/mysqld.log']
0
[root@kiwi ~]#
## 当日志文件里有Error时则为出现问题即为1
2.4 在web端配置
添加监控项
这样就是添加好了
添加触发器
选择一个监控项‘
将值修改为1
添加
添加好了
手动添加Error进行测试
[root@kiwi111 kiwi_scripts]# echo Error >> /var/log/mysql/mysqld.log
触发成功
监控完成
3. 自定义监控MySQL主从状态
注:需先部署MySQL的主从服务,可以看本篇博客作者的另一个博客《MySQL主从》。在此不多做赘述
3.1编写脚本
## 这个脚本能够查看数据库主从是否正常
[root@kiwi111 kiwi_scripts]# vim mysql_master-slave.sh
[root@kiwi111 kiwi_scripts]# cat mysql_master-slave.sh
#!/bin/bash
count=$(mysql -uroot -p'1' -e 'show slave status\G' | grep -i 'running:' | grep -c 'Yes')
if [ $count -ne 2 ];then
echo "1"
else
echo "0"
fi
[root@kiwi111 kiwi_scripts]# chmod +x mysql_master-slave.sh
[root@kiwi111 kiwi_scripts]#
3.2 在zabbix客户端修改配置文件
让脚本能够绑定一个key
[root@kiwi111 kiwi_scripts]# vim /usr/local/etc/zabbix_agentd.conf
······························略
UserParameter=check_mysql_master-slave,/kiwi_scripts/mysql_master-slave.sh ## 追加此行
##重启服务
[root@kiwi111 kiwi_scripts]# systemctl restart zabbix_agentd
在服务端测试
[root@kiwi ~]# zabbix_get -s 192.168.234.22 -k check_mysql_master-slave
0
[root@kiwi ~]#
## 服务正常为0,异常为1
3.3 在zabbix web端配置
添加监控项
这样就是添加好了
添加触发器
添加完成
3.4 测试
## 在从服务器关闭主从同步
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.001 sec)
可以看到已经触发
监控完成
4. 自定义监控MySQL主从延迟
当主从延迟达到了一定值,就会报警
4.1 编写脚本
[root@kiwi111 kiwi_scripts]# vim mysql_delay.sh
[root@kiwi111 kiwi_scripts]# cat mysql_delay.sh
#!/bin/bash
mysql -uroot -p'1' -e 'show slave status\G' | grep 'Seconds_Behind_Master' | awk '{print $NF}'
[root@kiwi111 kiwi_scripts]# chmod +x mysql_delay.sh
4.2 修改客户端配置文件
给脚本绑定key
[root@kiwi111 ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_delay,/kiwi_scripts/mysql_delay.sh ##添加此行
##重启服务
[root@kiwi111 ~]# systemctl restart zabbix_agentd
[root@kiwi111 ~]#
在服务端进行测试
[root@kiwi ~]# zabbix_get -s 192.168.234.22 -k check_mysql_delay
0
[root@kiwi ~]#
4.3 在web端添加监控项与触发器
监控项
添加成功
触发器
当延迟超过两百报警
4.4 手动触发进行测试
可以看到已经报警