自定义监控MySQL

自定义监控MySQL

进程

在客户端打开自定义功能

[root@agent etc]# vim zabbix_agentd.conf
//搜索UnsafeUserParameters,打开自定义监控
UnsafeUserParameters=1     //取消注释,0改为1

//在文件最后添加下面一行
UserParameter=check_process_httpd,bin/bash /scripts/check_process.sh

//编写检查进程的脚本
[root@agent scripts]# pwd
/scripts
[root@agent scripts]# vim check_process.sh
#!/bin/bash

count=$(ps -ef |grep -v grep|grep -c httpd)

if [ $count -ne 6 ];then
    echo '1'
fi 

[root@agent etc]# chmod +x /scripts/check_process.sh   //添加执行权限
[root@agent etc]# bash /scripts/check_process.sh  //执行
[root@agent etc]# pkill zabbix
[root@agent etc]# zabbix_agentd
[root@agent etc]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:111               0.0.0.0:*                  
LISTEN    0         32           192.168.122.1:53                0.0.0.0:*                  
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         5                127.0.0.1:631               0.0.0.0:*                  
LISTEN    0         128                0.0.0.0:10050             0.0.0.0:*                  
LISTEN    0         128                   [::]:111                  [::]:*                  
LISTEN    0         128                   [::]:22                   [::]:*                  
LISTEN    0         5                    [::1]:631                  [::]:*  

//在服务端测试
[root@zabbix ~]# zabbix_get -s 192.168.170.129 -k check_process_httpd
1

web添加监控项进行监控

创建监控项



创建触发器



日志

//脚本配置
[root@localhost 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@agent scripts]# vim check_process.sh 
#!/bin/bash
  
count=$(ps -ef |grep -Ev "grep|$0"|grep -c $1)

if [ $count -eq 0 ];then
    echo '1'
else
    echo '0'
fi

[root@agent etc]# vim zabbix_agentd.conf
UserParameter=check_process[*],bin/bash /scripts/check_process.sh [$1]   //修改此行
UserParameter=check_log[*],python3 /scripts/log.py $1 $2 $3    //添加此行
//更改权限
[root@agent ~]# chmod 755 /var/log/httpd/

//测试
[root@zabbix etc]# zabbix_get -s 192.168.100.120 -k check_log[/var/log/httpd/error_log]
0

posted @ 2022-07-11 00:28  夏天的海  阅读(61)  评论(0)    收藏  举报