zoukankan      html  css  js  c++  java
  • zabbix自定义监控项

    zabbix自带的默认模版里包括了很多监控项,有时候为了满足业务需求,需要根据自己的监控项目自定义监控项,需要在agentd端进行配置。

    修改zabbix_agentd.conf配置文件

      第一个:默认为0,此处将它改为1

      第二个:自定义监控项,格式为:UserParameter=<键值>,<命令>

    [root@noed1 etc]# sed -n "322p"  zabbix_agentd.conf
    UnsafeUserParameters=1
    

    首先编写自定义监控脚本,这里以监控进程是否存在为例。

      脚本名字:process_check.sh

      脚本目录(这个目录可以自定义):/usr/local/etc/zabbix_scripts

      脚本内容:

    [root@noed1 zabbix_scripts]# cat process_check.sh 
    #!/bin/bash
    
    result=$(ps -ef |grep -Ev "grep|$0" | grep -c $1)
    
    if [ $result -eq 0 ];then
    	echo "1"
    else
    	echo "0"
    fi
    

     通过ps检查“传参”进程是否存在,如果存在则脚本反馈1,如果不存在则返回0

    修改zabbix_agentd.conf配置文件

    自定义监控项,格式为:UserParameter=<键值>,<命令>。

    添加完成以后,重启zabbix_agentd才会生效。

    root@noed1 etc]# sed -n "332p" zabbix_agentd.conf
    UserParameter=process_check[*],/usr/local/etc/zabbix_scripts/process_check.sh $1
    

    配置zabbix监控

    选择需要监控的主机

    创建监控项

    根据自己自定义填写监控项

    添加完成后查看是否能获得数据

    配置触发器

    添加Actions

    测试告警

    自定义监控日志

    python脚本

    作用:检查日志文件中是否有指定的关键字

    第一个参数为日志文件名(必须有,相对路径、绝对路径均可)

    第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)

    第三个参数为搜索关键字,默认为 Error

    [root@noed1 zabbix_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@noed1 etc]# sed -n "334p" zabbix_agentd.conf
    UserParameter=error_log[*],/usr/local/etc/zabbix_scripts/log.py $1 $2 $3
    

    在server端取值

    [root@zabbix_server ~]# zabbix_get  -s 192.168.248.200 -k "error_log["/tmp/logs","/tmp/testseek", "Error"]"
    1
    

    测试输入一些内容到被监控端/tmp/logs文件中再次取值

    root@noed1 etc]# for i in `seq 10`;do echo "$i" >> /tmp/logs;done
    
    #到监控端取值
    [root@zabbix_server ~]# zabbix_get  -s 192.168.248.200 -k "error_log["/tmp/logs","/tmp/testseek", "Error"]"
    0
    
    #输出Error至/tmp/logs文件中再次取值
    [root@noed1 etc]# echo  "Error" >> /tmp/logs 
    
    #取值
    [root@zabbix_server ~]# zabbix_get  -s 192.168.248.200 -k "error_log["/tmp/logs","/tmp/testseek", "Error"]"
    1
    

    配置监控项

    配置触发器

    配置动作

    触发动作测试

    监控MySQL主从

    在从库配置

    [root@noed1 ~]# cat /usr/local/etc/zabbix_scripts/mysql_repliction_status.sh 
    #!/bin/bash
      
    USER="root"
    PASSWD="123123123"
    
    result=`mysql -u${USER}  -p${PASSWD}  -e "show slave statusG" | grep "Running:"|awk '{print$2}'|grep -c Yes`
    
    if [ $result -eq 2 ];then
           echo "0"
    else
           echo "1"
    fi
    

    修改被监控端配置文件

    [root@noed1 zabbix_scripts]# sed -n '335p' ../zabbix_agentd.conf
    UserParameter=mysql_replication,/usr/local/etc/zabbix_scripts/mysql_repliction_status.sh
    

    在监控端取值

    [root@zabbix_server ~]# zabbix_get  -s 192.168.248.200 -k "mysql_replication"
    0
    

    配置监控项

    添加触发器

    查看最新数据

    配置动作

    触发动作

    进入从库stop slave;

    MariaDB [(none)]> stop slave;
    Query OK, 0 rows affected (0.002 sec)
    

    收到告警

  • 相关阅读:
    项目笔记三
    ASP.NET小收集<9>:HTML解析
    TSQL数据维护:更改表所有者
    [转贴]SQL2005:数据类型最大值
    TSQL存储过程:ROW_NUMBER()分页
    JS收集<7>:浏览器event兼容
    统计SQLServer2005表记录数
    ASP.NET小收集<8>:JS创建对象
    JS收集<8>:HTML控件的坐标
    MySql按指定天数进行分组数据统计分析 1
  • 原文地址:https://www.cnblogs.com/diqiyao/p/14715830.html
Copyright © 2011-2022 走看看