自定义监控
自定义监控mysql进程
开启自定义监控(在客户端进行)
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@client etc]# vi zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1 //搜索Parameters 取消注释 并将值改为1
UserParameter=check_process[*],/bin/bash /scropts/check_process.sh $1 //文档最后添加自定义监控
[root@client etc]# mkdir /scropts/
[root@client etc]# cd /scropts/
[root@client scropts]# cat check_process.sh
#!/bin/bash
count=$(ps -ef | grep -Ev "grep|$0" | grep -c $1) //"grep|$0" 过滤脚本自身 $1脚本执行时后面的参数
if [ $count -eq 0 ];then //当count 等于0 时打印1 因为正常程序执行都会有进程 所以不可能计数为0
echo '1'
else
echo '0' //没问题打印0
fi
[root@client scropts]# chmod +x check_process.sh //赋予脚本执行权限
[root@client scropts]# ll
total 4
-rwxr-xr-x. 1 root root 132 Jul 10 17:19 check_process.sh
[root@client scropts]# pkill zabbix //杀死zabbix的进程
[root@client scropts]# zabbix_agentd //杀死zabbix的进程
[root@client scropts]# yum -y install mariadb*
[root@client scropts]# systemctl start mariadb.service
[root@client scropts]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
服务端测试
[root@lamp ~]# zabbix_get -s 192.168.200.6 -k check_process[mysqld]
0
[root@lamp ~]# zabbix_get -s 192.168.200.6 -k check_process[zabbix]
0
[root@client scropts]# systemctl stop mariadb.service
[root@client scropts]# ss -atnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
自定义日志监控
准备工作
[root@client scropts]# vi 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)
"log.py" [New] 85L, 1854C written
[root@client scropts]# ls
check_process.sh log.py
[root@client scropts]# chmod +x log.py //给脚本log执行权限
[root@client scropts]# ll
total 8
-rwxr-xr-x. 1 root root 132 Jul 10 18:02 check_process.sh
-rwxr-xr-x. 1 root root 1854 Jul 10 18:24 log.py
[root@client scropts]# setfacl -m u:zabbix:rx /var/log/mariadb //让zabbix用户对mariadb的日志目录有读权限
[root@client scropts]# ll /var/log/mariadb/
total 8
-rw-rwx---+ 1 mysql mysql 6385 Jul 10 18:39 mariadb.log
[root@client scropts]# getfacl /var/log/mariadb
getfacl: Removing leading '/' from absolute path names
# file: var/log/mariadb
# owner: mysql
# group: mysql
user::rw-
user:zabbix:r-x
group::rw-
mask::rwx
other::---
测试脚本
[root@client scropts]# yum -y install python3 //安装python
//正常执行结果为0
[root@client scropts]# python3 log.py /var/log/mariadb/mariadb.log
0
//向日志中添加Error
[root@client scropts]# echo 'Error' >> /var/log/mariadb/mariadb.log
//脚本检查到Error 打印1
[root@client scropts]# python3 log.py /var/log/mariadb/mariadb.log
1
[root@client scropts]# python3 log.py /var/log/mariadb/mariadb.log
0
[root@client scropts]# cat /tmp/logseek
3862[root@client scropts]# python3 log.py /var/log/mariadb/mariadb.log
0
[root@client scropts]# cat /tmp/logseek
3862[root@client scropt
[root@client scropts]# echo 'Error' >> /var/log/mariadb/mariadb.log
[root@client scropts]# python3 log.py /var/log/mariadb/mariadb.log
1
[root@client scropts]# cat /tmp/logseek //再次执行 因为日志是实时刷新的所以这次 显示无错误 打印0
3868[root@client scropts]# rm -rf /tmp/logseek //当网页使用时没有权限
配置zabbix_agentd.conf文件
[root@client scropts]# vi /usr/local/etc/zabbix_agentd.conf
# ListenBacklog=
UserParameter=check_process[*],/bin/bash /scropts/check_process.sh $1
UserParameter=check_logs[*],/usr/bin/python3 /scropts/log.py $1 $2 $3
//可以传更多的参数
[root@client scropts]# pkill zabbix
[root@client scropts]# zabbix_agentd
[root@client scropts]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@client scropts]# echo 'Error' >> /var/log/mariadb/mariadb.log