zabbix自定义监控

自定义监控进程

  1. 写脚本,脚本放在统一位置
  2. 修改被监控机zabbix_agentd.conf配置文件
    UnxsafeParameters=1
    UnserParameter=<key>,<command>
  3. 重启zabbix_agent
  4. 在web界面配置监控项和触发器

修改客户端zabbix_agentd文件zabbix_agentd.conf

[root@localhost etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1 #默认为0。修改为1,打开自定义监控功能
······
UserParameter=check_process[*],/script/check_process.sh $1

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

#写自定义监控脚本
[root@localhost scripts]# cat 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@server ~]# zabbix_get -s 192.168.8.131 -k check_process[httpd]

配置监控项
在这里插入图片描述
在这里插入图片描述
添加触发器
在这里插入图片描述
在这里插入图片描述

自定义监控日志

  1. 写脚本,脚本放在统一位置
  2. 修改被监控机zabbix_agentd.conf配置文件
    UnxsafeParameters=1
    UnserParameter=<key>,<command>
  3. 重启zabbix_agent
  4. 在web界面配置监控项和触发器
#日志脚本
#!/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)

log.py作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件,相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为Error

#执行脚本需安装python
[root@localhost scripts]# yum -y install python3

#修改文件
[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3

[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd

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

配置监控项在这里插入图片描述
添加触发器
在这里插入图片描述
在这里插入图片描述

#写入错误
[root@localhost ~]# echo "Error" >> /var/log/httpd/error_log

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

自定义监控mysql主从

  1. 写脚本,脚本放在统一位置
  2. 修改被监控机zabbix_agentd.conf配置文件
    UnxsafeParameters=1
    UnserParameter=<key>,<command>
  3. 重启zabbix_agent
  4. 在web界面配置监控项和触发器
#在家目录下写存放数据库密码文件
[root@localhost ] vim /scripts/.password
[client]
user=root
password=123

#编写脚本
[root@localhost scripts]# vim mysql_io.sh
#!/bin/bash

IO=$(mysql --defaults-file=/scripts/.password  -e"show slave status\G;"  | grep Slave_IO_Running: | awk   '{print $2}')
if [ $IO == "Yes" ];then
    echo "0"
else
    echo "1"
fi

[root@localhost scripts]# cat mysql_sql.sh 
#!/bin/bash

SQL=$(mysql --defaults-file=/scripts/.password  -e"show slave status\G;"  | grep Slave_SQL_Running: | awk   '{print $2}')
if [ $SQL == 'Yes' ];then
    echo "0"
else
    echo "1"
fi

# 修改zabbix_agentd文件
[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_io,/scripts/mysql_io.sh
UserParameter=check_mysql_sql,/scripts/mysql_sql.sh

#重启服务
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd

#在服务端上查看
[root@server ~]# zabbix_get -s 192.168.8.131 -k check_mysql_io
0
[root@server ~]# zabbix_get -s 192.168.8.131 -k check_mysql_sql
0


监控IO进程
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
监控SQL进程

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

#关闭主从同步
[root@localhost scripts]# mysql -uroot -p12
mysql> stop slave
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.8.129
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: 
          Read_Master_Log_Pos: 4
               Relay_Log_File: mysql_relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: 
             Slave_IO_Running: No
            Slave_SQL_Running: No

在这里插入图片描述

mysql主从延迟

  1. 写脚本,脚本放在统一位置
  2. 修改被监控机zabbix_agentd.conf配置文件
    UnxsafeParameters=1
    UnserParameter=<key>,<command>
  3. 重启zabbix_agent
  4. 在web界面配置监控项和触发器
#脚本
[root@localhost scripts]# cat mysql_behind.sh 
#!/bin/bash

delay=$(mysql --defaults-file=/scripts/.password  -e"show slave status\G;"  | grep Behind | awk   '{print $2}')
if [ $delay -eq 0 ];then
    echo '0'
else
    echo '1'
fi

#

添加监控项
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
测试=0时,报警效果
在这里插入图片描述

用户和组权限设置

guest用户此时为禁用
在这里插入图片描述
在用户组里将guest用户踢出
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此时guest用户启用
在这里插入图片描述
在这里插入图片描述
guest用户即可访问
在这里插入图片描述
设置guest用户不能访问
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
给guest用户读权限

在这里插入图片描述

声音报警

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值