自定义监控mysql进程以及日志

客户端操作开启自定义监控

[root@zabbix-agent ~]# cd /usr/local/etc/
[root@zabbix-agent etc]# ls
zabbix_agentd.conf    zabbix_server.conf
zabbix_agentd.conf.d  zabbix_server.conf.d
[root@zabbix-agent etc]# vi zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1   #此处取消注释修改值为1

UserParameter=check_process[*],/bin/bash /scropts/check_process.sh $1  #加到文章底部。
[root@zabbix-agent etc]# mkdir /scropts/ #创建一个目录
[root@zabbix-agent etc]# cd /scropts/
[root@zabbix-agent scropts]# vim check_process.sh  #写一个脚本

#!/bin/bash

count=$(ps -ef | grep -Ev "grep|$0" | grep -c $1) #过滤脚本自身
if  [ $count -eq 0 ];then   #当count等于0时打印1,
     echo '1'
else
     echo '0'  #没有问题打印0
fi
[root@zabbix-agent scropts]# chmod +x check_process.sh  #增加自行权限
[root@zabbix-agent scropts]# ll
总用量 4
-rwxr-xr-x 1 root root 125 7月  10 23:15 check_process.sh
[root@zabbix-agent scropts]# pkill zabbix  #杀进程
[root@zabbix-agent scropts]# zabbix_agentd
[root@zabbix-agent scropts]# systemctl start mariadb.service #启动数据库
[root@zabbix-agent scropts]# ss -antl  #有端口号了
State  Recv-Q  Send-Q   Local Address:Port    Peer Address:PortProcess
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            0.0.0.0:22           0.0.0.0:*
LISTEN 0       128               [::]:22              [::]:*

测试服务端

root@zabbix-server ~]# zabbix_get -s 192.168.6.132 -k check_process[MysqLd]
0
[root@zabbix-server ~]# zabbix_get -s 192.168.6.132 -k check_process[zabbix]
0
[root@zabbix-agent scropts]# systemctl stop mariadb.service
[root@zabbix-agent scropts]# ss -atnl
State  Recv-Q  Send-Q   Local Address:Port    Peer Address:PortProcess
LISTEN 0       128            0.0.0.0:10050        0.0.0.0:*
LISTEN 0       128            0.0.0.0:22           0.0.0.0:*
LISTEN 0       128               [::]:22              [::]:*

 

   添加监控项,保存,更新。

 

 添加触发器

 

 取到值了,

[root@zabbix-agent scropts]# systemctl stop mariadb.service  # 停掉服务测试
[root@zabbix-agent scropts]# ss -antl
State  Recv-Q  Send-Q   Local Address:Port    Peer Address:PortProcess
LISTEN 0       128            0.0.0.0:10050        0.0.0.0:*
LISTEN 0       128            0.0.0.0:22           0.0.0.0:*
LISTEN 0       128               [::]:22              [::]:*

 

 

 收到邮件

自定义监控日志

[root@zabbix-agent scropts]# vi log.py
[root@zabbix-agent scropts]# 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@zabbix-agent scropts]# ls
check_process.sh  log.py
[root@zabbix-agent scropts]# chmod +x log.py  #添加权限
[root@zabbix-agent scropts]# ll
总用量 8
-rwxr-xr-x 1 root root  125 7月  10 23:15 check_process.sh
-rwxr-xr-x 1 root root 1854 7月  11 00:13 log.py
[root@zabbix-agent scropts]# setfacl -m u:zabbix:rx /var/log/mariadb/mariadb.log   # 让zabbix用户对这个日志有读和执行的权限
[root@zabbix-agent scropts]# ll /var/log/mariadb/
总用量 8
-rw-rwx---+ 1 MysqL MysqL 6983 7月  11 00:03 mariadb.log
[root@zabbix-agent scropts]# getfacl /var/log/mariadb/mariadb.log  # 查看
getfacl: Removing leading '/' from absolute path names
# file: var/log/mariadb/mariadb.log
# owner: MysqL
# group: MysqL
user::rw-
user:zabbix:r-x
group::rw-
mask::rwx
other::---

测试脚本

[root@zabbix-agent scropts]# yum -y install python3
[root@zabbix-agent scropts]# python3 log.py /var/log/mariadb/mariadb.log
0
[root@zabbix-agent scropts]# echo 'Error' >> /var/log/mariadb/mariadb.log
[root@zabbix-agent scropts]# python3 log.py /var/log/mariadb/mariadb.log
1
[root@zabbix-agent scropts]# cat /tmp/logseek
6989[root@zabbix-agent scropts]# echo 'Error' >> /var/log/mariadriadb.log
[root@zabbix-agent scropts]# python3 log.py /var/log/mariadb/mariadb.log
1
[root@zabbix-agent scropts]# cat /tmp/logseek
6995[root@zabbix-agent scropts]#
[root@zabbix-agent scropts]# rm -rf /tmp/logseek
# 测试脚本。

配置zabbix_agentd.conf文件

[root@zabbix-agent scropts]# vi /usr/local/etc/zabbix_agentd.conf

UserParameter=check_logs[*],/usr/bin/python3 /scropts/log.py $1$2 $3   #添加到最后
[root@zabbix-agent scropts]# pkill zabbix  # 啥进程
[root@zabbix-agent scropts]# zabbix_agentd
[root@zabbix-agent scropts]# systemctl start mariadb
[root@zabbix-agent scropts]# ss -antl
State  Recv-Q  Send-Q   Local Address:Port    Peer Address:PortProcess
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            0.0.0.0:22           0.0.0.0:*
LISTEN 0       128               [::]:22              [::]:*

手动添加监控项

 

 添加触发器

 

 手动促发测试

 

相关文章

优化MySQL数据库发布系统存储的方法有:1.mysql库主从读写分...
使用mysql的方法:在“我的电脑”→右键→“管理”→“服务”...
在mysql中查看root用户权限的方法:1.命令行启动mysql服务;...
MySQL主从复制是用来备份一个与主数据库一样环境的从数据库,...
运行mysql的方法1.启动mysql服务,在“我的电脑”→右键→“...
开启mysql的方法1.可以通过快捷键win+r,输入cmd,打开窗口,...