栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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.10.202 -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.10.202 -k check_log[/var/log/httpd/error_log] 
0

在网页上添加监控项来查看

mysql
##编写脚本
[root@localhost scripts]# cat check_mysql.sh
#!/bin/bash
  
User=root
Passwd=123

io=$(mysql -u"$User" -p"$Passwd" -e 'show slave status G' 2>&1 |grep -w Slave_IO_Running | awk -F': ' '{print $2}')
sql=$(mysql -u"$User" -p"$Passwd" -e 'show slave status G' 2>&1 |grep -w Slave_SQL_Running | 
awk -F': ' '{print $2}')

if [[ $io == 'Yes' ]] && [[ $sql == 'Yes' ]];then
        echo 0
else
        echo 1
fi

##设置配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
······
# Default: SOMAXCONN (hard-coded constant, depends on system)
# ListenBacklog=
UserParameter=check_mysql_slave,/scripts/check_mysql.sh

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

##测试
[root@localhost ~]# zabbix_get -s 192.168.10.203 -k check_mysql_slave
0

在网站上添加监控项

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号