zabbix自定义监控
1:自定义监控进程
#:在被监控主机端配置
#:1:创建脚本存放目录并编写脚本
mkdir /script
vim /script/showprocess.sh
#!/bin/bash
process=$(ps -ef |grep "$1" |grep -Ev "grep|$0" |wc -l )
if [ $process -eq 0 ];then
echo "1"
else
echo "0"
fi
#:给脚本加上执行权限
#:编辑配置文件 vim /usr/local/etc/zabbix_agentd.conf 将 ()(不安全的用户参数)UnsafeUserParameters=0 改为{UnsafeUserParameters=1 在文件最后添加 UserParameter=<自定义 key名>,</bin/bash (脚本绝对路径)> }
UserParameter=show_process[*],/bin/bash /script/showprocess.sh $1
#:写完后重启zabbix_agentd 服务
#:在服务端测试
zabbix_get -s (指定受控端ip) -k show_process['<服务名>']
2:自定义监控日志
#:安装python 3
yum -y install python3
#:编写一个python脚本
vim /script/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)
#:在/tmp目录下默认会生成一个logseek的文件存放日志读取记录,下一次执行自动从这一条往后读取
#:编辑配置文件 vim /usr/local/etc/zabbix_agentd.conf 最后一行书写
UserParameter=check_logs[*],python3 /script/showprocess.sh $1 $2 $3
3: 监控mysql主从状态
#: 编写脚本
vim /script/mysql_status.sh
#!/bin/bash
count=$(mysql -uroot -p'<密码>' -e 'show salve status\G |grep -i 'running:' |grep -c 'Yes')
if [ $count -ne 2 ];then
echo '1'
else
echo '0'
fi
#:编辑配置文件 vim /usr/local/etc/zabbix_agentd.conf 最后一行书写
UserParameter=check_mysql_status,/bin/bash /script/mysql_status.sh
4: 自定义监控主从延迟
#: 编写脚本
vim /script/mysql_delay.sh
#!/bin/bash
count=$(mysql -uroot -p'<密码>' -e 'show salve status\G |grep -i 'Seconds_Behind_Master' |awk '{print $NF}')
编写脚本
vim /script/mysql_delay.sh
#!/bin/bash
count=$(mysql -uroot -p’<密码>’ -e 'show salve status\G |grep -i ‘Seconds_Behind_Master’ |awk ‘{print $NF}’)
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=C%3A%5CUsers%5Cliuhongsheng%5CDesktop%5Czabbix%E8%87%AA%E5%AE%9A%E4%B9%89%E7%9B%91%E6%8E%A7.assets%5Cimage-20231007083726544.png&pos_id=img-vUOYVx4F-1696639085462)