zabbix自定义监控进程、日志、mysql主从

环境说明:

zabbix服务器: 192.168.100.1 服务端 (已经装好zabbix_agentd和zabbix_server,且为MySQL主)

zabbix客户端: 192.168.100.128 客户端 (已经装好zabbix_agentd,已经安装好apache服务,且为MySQL从,数据库没有密码)

监视客户端的apache进程:

关闭两台机器的防火墙和selinux:

[root@zabbix ~]# systemctl stop firewalld
[root@zabbix ~]# setenforce 0

以查看进程方式过滤apache进程,输出1表示进程有问题,0表示没问题

[root@zabbix ~]# cat /scripts/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@zabbix ~]# chmod +x /scripts/check_process.sh 

//源码安装执行此脚本要加绝对路径
[root@zabbix ~]# /scripts/check_process.sh httpd
0
[root@zabbix ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port            Peer Address:Port     
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                   0.0.0.0:9000                 0.0.0.0:*        
LISTEN    0          128                      [::]:22                      [::]:*        
LISTEN    0          80                          *:3306                       *:*        
LISTEN    0          128                         *:80                         *:*   

//在文件的最后面加上两行开启自定义监控的配置
UnsafeUserParameters=1
UserParameter=check_apache,/scripts/check_process.sh httpd


//客户端这边关闭了apache的进程
[root@zabbix ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port            Peer Address:Port     
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                   0.0.0.0:9000                 0.0.0.0:*        
LISTEN    0          128                      [::]:22                      [::]:*        
LISTEN    0          80                          *:3306                       *:* 

//在服务端上查看能否取到刚才配置脚本的值
[root@zabbix ~]# zabbix_get -s 192.168.100.128 -k check_apache
1
//能取到1说明没问题了

配置网页zabbix的流程

添加主机监控之类的操作请参考zabbix监控配置

 

 

 zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

这里的HOST.NAME别写错了 

zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

//受控机手动触发报警
[root@zabbix ~]# apachectl start
[root@zabbix ~]# apachectl stop

zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

 配置监控日志流程

//当前在受控机
[root@zabbix scripts]# pwd
/scripts
[root@zabbix ~]# dnf -y install python36
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为 Error

[root@zabbix scripts]# vim 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 scripts]# chmod +x log.py 
[root@zabbix scripts]# touch /usr/local/apache/logs/error.log
//在文件最后添加
[root@zabbix scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3

[root@zabbix scripts]# pkill zabbix
[root@zabbix scripts]# zabbix_agentd

//在服务端上查看能否取到刚才配置py脚本的值
[root@zabbix ~]# zabbix_get -s 192.168.100.128 -k check_logs["/usr/local/apache/logs/error.log","/tmp/seek","Error"]

//手动触发报警
[root@zabbix scripts]# echo Error >> /usr/local/apache/logs/error.log

网页监控配置流程

zabbix自定义监控进程、日志、mysql主从

zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从zabbix自定义监控进程、日志、mysql主从

zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

监控客户端的mysql主从状态

//当前在受控机
[root@zabbix scripts]# vim check_mysql_repl.sh 

#!/bin/bash
  
count=$(mysql -uroot -e show slave status\G|grep  Running:|awk {print $2}|grep -c Yes)
if [ $count -ne 2 ];then
        echo 1
else
        echo 0
fi

//输出1表示主从有问题,0表示没问题
[root@zabbix scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_repl,/scripts/check_mysql_repl.sh

[root@zabbix scripts]# pkill zabbix
[root@zabbix scripts]# zabbix_agentd

//在服务端上查看能否取到刚才配置脚本的值
[root@zabbix ~]# zabbix_get -s 192.168.100.128 -k check_mysql_repl

网页监控配置流程

zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

zabbix自定义监控进程、日志、mysql主从

 zabbix自定义监控进程、日志、mysql主从

 

zabbix自定义监控进程、日志、mysql主从

上一篇:MySQL 8.0的MGR多主搭建与测试


下一篇:sql取结果集中的第一条记录