zabbix自定义监控(进程、日志文件、mysql)

5 篇文章 0 订阅
4 篇文章 0 订阅

zabbix自定义监控(进程、日志文件、mysql)

进程

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

content=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1")

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

##修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
·····
# Default: SOMAXCONN (hard-coded constant, depends on system)
# ListenBacklog=
UserParameter=check_process[*],/scripts/check_process.sh $1  ##追加此行(监控名称与脚本路径)

##在服务端测试
[root@localhost etc]# zabbix_get -s 192.168.240.50 -k check_process[httpd]
0

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@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
·····
# Default: SOMAXCONN (hard-coded constant, depends on system)
# ListenBacklog=
UserParameter=check_process[*],/scripts/check_process.sh $1
UserParameter=check_log[*],/scripts/log.py $1 $2 $3 ##添加此行

##更改权限
[root@localhost ~]# chmod 755 /var/log/httpd/

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

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

mysql主从

##编写脚本
[root@localhost scripts]# cat mysql_status.sh 
#!/bin/bash
USER=zabbix
PASSWD=zabbix123!
qs=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null | grep _Running | grep -c Yes`
if [ $qs -eq 2 ];then
    echo 0
else
    echo 1
fi


##设置权限
mysql> grant select on *.* to 'zabbix'@'localhost' identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> grant SUPER, REPLICATION CLIENT on *.* to 'zabbix'@'localhost' identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

##设置配置文件
[root@localhost etc]# vim zabbix_agentd.conf
······
# Default: SOMAXCONN (hard-coded constant, depends on system)
# ListenBacklog=
UserParameter=check_process[*],/scripts/check_process.sh $1
UserParameter=check_log[*],/scripts/log.py $1 $2 $3
UserParameter=mysql.slave[*],/scripts/mysql_status.sh $1

##重启
[root@localhost etc]# pkill zabbix_agentd 
[root@localhost etc]# zabbix_agentd 

##测试
[root@localhost ~]# zabbix_get -s 192.168.240.50 -k mysql.slave
0

##停止主从,进行web测试
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

mysql延迟

##脚本
[root@localhost scripts]# cat check_delay.sh 
#!/bin/bash
USER=zabbix
PASSWD=zabbix123!
delay_count=$(mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null | grep 'Behind' | awk '{print $2}')
if [ $delay_count != NULL ];then
    echo $delay_count
else
    echo 0
fi

##添加配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
·······
# Default: SOMAXCONN (hard-coded constant, depends on system)
# ListenBacklog=
UserParameter=check_process[*],/scripts/check_process.sh $1
UserParameter=check_log[*],/scripts/log.py $1 $2 $3
UserParameter=mysql.slave,/scripts/mysql_status.sh
UserParameter=mysql.delay,/scripts/check_delay.sh  ##添加该行

##重启
[root@localhost etc]# pkill zabbix_agentd 
[root@localhost etc]# zabbix_agentd 

##测试
[root@localhost ~]# zabbix_get -s 192.168.240.50 -k mysql.delay
0

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值