zabbix自定义监控

zabbix命令

在客户端查看自定义的key值

[root@localhost ~]# zabbix_agentd -p|grep iotps
iotps                                         [t|2.62]

在服务端查看客户端定义的key值

[root@localhost ~]# zabbix_get -s 192.168.100.131 -p10050 -k iotps
2.74

zabbix自定义监控进程

1.在zabbix客户端编辑/usr/local/etc/zabbix_agentd.conf配置文件,UnsafeUserParameters(自定义监控功能)设为1

Server=192.168.100.131
ServerActive=192.168.100.131
Hostname=192.168.100.130
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_p.sh $1

2.写一个脚本名字叫做/scripts/check_p.sh

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


status=$(ps -ef|grep -Ev "grep|$0"|grep -w "$1"|wc -l)


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

3.将脚本所属主和组权限改为zabbix

[root@localhost scripts]# ll
总用量 12
-rwxr-xr-x. 1 zabbix zabbix  125 12月 25 10:52 check_p.sh

4.重启服务

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

5.点击Configuration然后点击hosts,再点击监控项itemis
在这里插入图片描述

6.点击创建监控项create item
在这里插入图片描述

7.如图配置点击确定
在这里插入图片描述

8.添加触发器
在这里插入图片描述
在这里插入图片描述

9.如图添加点击确定
在这里插入图片描述

10.在客户端关闭mysqld服务,查看zabbix监控已经报警

[root@localhost scripts]# service mysqld stop
Shutting down MySQL.. SUCCESS!

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

zabbix自定义监控日志

1.编辑日志脚本

[root@localhost ~]# cat /scripts/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)

2.给/scripts/check_p.sh权限的主和组设置为zabbix

15[root@localhost ~]# ll /scripts/
总用量 12
-rwxr-xr-x. 1 zabbix zabbix  125 12月 25 10:52 check_p.sh

3.在/usr/local/etc/zabbix_agentd.conf添加配置文件并重启服务

vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/usr/bin/python /scripts/log.py $1 $2 $3

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

4.配置监控项监控httpd的错误日志,设置目录/tmp/myseek的数值是否发生改变,发生改变则有错误日志生成,而且错误日志的名字是hehe

在这里插入图片描述

5.查看/tmp/myseek文件,目前数值为30,属主和组是zabbix

[root@localhost ~]# cat /tmp/myseek 
30[root@localhost ~]# 
30[root@localhost ~]# ll /tmp/myseek
-rw-rw-r--. 1 zabbix zabbix 2 12月 25 18:59 /tmp/myseek

6.向` /var/log/httpd/error_log 追加内容呵呵,发现/tmp/myseek的数值发生改变

[root@localhost ~]# echo 'hehe' >> /var/log/httpd/error_log 

[root@localhost ~]# cat /tmp/myseek 
35[root@localhost ~]# 

7.在查看zabbix监控的数值已经从0变成1,表示着有错误日志生成
在这里插入图片描述

zbbix自定义监控mysqld主从

1.检测从数据库的Slave_IO_Running和 Slave_SQL_Running是否为yes

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000014
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 527
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: d19b859b-2625-11ea-87ab-000c29542b8b
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

2.用脚本来监控着两个参数

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

status=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null|grep -E 'Slave_IO_Running|Slave_SQL_Running:'|grep -c Yes
)

if [ $status -ne 2 ];then
	echo '1'
else
	echo '0'
fi

3.给脚本的属主和组改为zabbix

[root@localhost scripts]# ll
总用量 12
-rwxr-xr-x. 1 zabbix zabbix  196 12月 25 11:49 mysql_repl.sh

4.在/usr/local/etc/zabbix_agentd.conf配置文件加上UserParameter=check_mr,/bin/bash /scripts/mysql_repl.sh

[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mr,/bin/bash /scripts/mysql_repl.sh

5.在item监控项上来监控这个脚本
在这里插入图片描述
6.将mysqld的主从数据出现问题时就会变成1

[root@localhost ~]# service mysqld stop
Shutting down MySQL.. SUCCESS! 

在这里插入图片描述

自定义监控mysql的延迟

1.查看mysql的Seconds_Behind_Master:是否大于0,等于0代表没有延迟

mysql> show salve status\G;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'salve status' at line 1
ERROR: 
No query specified

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000016
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 527
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: d19b859b-2625-11ea-87ab-000c29542b8b
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

2.写一个脚本

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

status=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null| grep 'Seconds_Behind_Master'|awk '{print $2}')

echo $status

3.给脚本属主属组为zabbix并给其执行权限

[root@localhost scripts]# chown zabbix.zabbix mysql_delay.sh 
[root@localhost scripts]# chmod +x mysql_delay.sh
[root@localhost scripts]# ll
总用量 16
-rwxr-xr-x. 1 zabbix zabbix  143 12月 26 09:35 mysql_delay.sh

4.在配置文件/usr/local/etc/zabbix_agentd.conf追加内容

UserParameter=check_delay,/bin/bash /scripts/mysql_delay.sh

5.重启服务

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

6。添加一个监控选项
在这里插入图片描述

7.然后既可以监控延迟了,目前这里是0,无法测试延迟
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值