zabbix自定义监控

zabbix自定义监控

  1. 进程监控
  2. 日志文件监控
  3. mysql主从状态监控
  4. mysql主从延迟监控

环境

server端:192.168.147.44
agent端:192.168.147.10

在agent端安装agent

将server端的zabbix-5.2.6的包传到agent端上

[root@localhost ~]# scp zabbix-5.2.6.tar.gz 192.168.147.10:/root/
The authenticity of host '192.168.147.10 (192.168.147.10)' can't be established.
ECDSA key fingerprint is SHA256:gSsPLJ32lTw4VyltfgKSGM3YOoC/tgzVRHXPrX8hyVY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.147.10' (ECDSA) to the list of known hosts.
root@192.168.147.10's password: 
zabbix-5.2.6.tar.gz                           100%   20MB  16.8MB/s   00:01    

在agent端解压传过去的文件

[root@localhost ~]# tar xf zabbix-5.2.6.tar.gz 
[root@localhost ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg       zabbix-5.2.6
模板  图片  下载  桌面  initial-setup-ks.cfg  zabbix-5.2.6.tar.gz

安装需要的安装包

[root@localhost zabbix-5.2.6]# useradd -r -M -s /sbin/nologin zabbix
[root@localhost ~]# dnf -y install gcc gcc-c++ make pcre-devel

编译安装agent

[root@localhost ~]# cd zabbix-5.2.6/
[root@localhost zabbix-5.2.6]# ./configure --enable-agent
........
***********************************************************
*            Now run 'make install'                       *
*                                                         *
*            Thank you for using Zabbix!                  *
*              <http://www.zabbix.com>                    *
***********************************************************

[root@localhost zabbix-5.2.6]# make install

[root@localhost etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d

[root@localhost etc]# vim zabbix_agentd.conf
113 Server=192.168.147.44  //server端的ip
154 ServerActive=192.168.147.44  //server端的ip
165 Hostname=node3  //可是任意指定,最好是随机数

进程监控

在web界面创建一个主机
zabbix自定义监控_第1张图片
zabbix自定义监控_第2张图片

[root@localhost ~]# dnf -y install httpd   //安装httpd

[root@localhost ~]# systemctl start httpd  //启动httpd
[root@localhost ~]# ss -antl   //有80端口
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                    *:80                   *:*              
LISTEN  0        80                     *:2001                 *:*              
LISTEN  0        80                     *:2002                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*              

zabbix自定义监控_第3张图片
httpd安装完成

[root@localhost ~]# cd /usr/local/etc/ 
[root@localhost etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@localhost etc]# vim zabbix_agentd.conf

UnsafeUserParameters=1  //将这行取消注释 并将数值改为1

在agent端

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts
[root@localhost scripts]# ls
[root@localhost scripts]# vim check_process.sh    //写进程监控的脚本

#!/bin/bash

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

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

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

[root@localhost scripts]# chmod +x check_process.sh   //给脚本x的权限
[root@localhost scripts]# ll
总用量 4
-rwxr-xr-x. 1 root root 133 519 19:09 check_process.sh

测试脚本

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# su - zabbix -s /bin/bash
su: 警告:无法更改到 /home/zabbix 目录: 没有那个文件或目录

[zabbix@localhost root]$  /scripts/check_process.sh httpd
0
[zabbix@localhost root]$ ps -ef|grep -v grep|grep -c httpd
5

[root@localhost etc]# vim zabbix_agentd.conf  //在文本最后添加下列一行内容

UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd 

在server端

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# zabbix_get -s 192.168.147.10 -k check_process['httpd']
0

添加监控项

zabbix自定义监控_第4张图片

zabbix自定义监控_第5张图片

zabbix自定义监控_第6张图片
查看是否有数据

zabbix自定义监控_第7张图片
再关闭httpd再次查看

zabbix自定义监控_第8张图片
添加触发器

zabbix自定义监控_第9张图片

zabbix自定义监控_第10张图片
开启声音报警
zabbix自定义监控_第11张图片
测试

[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        80                     *:2001                 *:*              
LISTEN  0        80                     *:2002                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*              

报警启动
在这里插入图片描述

日志文件监控

将写好的脚本拖入到scripts里

zabbix自定义监控_第12张图片

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

查看日志文件

[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 ~]# chmod +x /scripts/log.py
[root@localhost ~]# ll /scripts/log.py
-rwxr-xr-x. 1 root root 1854 519 20:31 /scripts/log.py
[root@localhost scripts]# which python3  //这里如果没有,就需要安装
/usr/bin/python3

测试脚本

[root@localhost ~]# ls /var/log/httpd/
access_log  error_log

[root@localhost scripts]# setfacl -m u:zabbix:rx /var/log/httpd/
[root@localhost scripts]# mkdir /zabbix_item_log
[root@localhost scripts]# chown -R zabbix.zabbix /zabbix_item_log/
[root@localhost scripts]# cd
[root@localhost ~]# su - zabbix -s /bin/bash
su: 警告:无法更改到 /home/zabbix 目录: 没有那个文件或目录
[zabbix@localhost root]$ python3 /scripts/log.py /var/log/httpd/error_log /zabbix_item_log/logseek Failed
0

编写配置文件

[root@localhost etc]# vim zabbix_agentd.conf  //在配置文件中添加下列这一行

UserParameter=check_logs[*],/scripts/log.py $1 $2 $3

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

server端

[root@localhost ~]#  zabbix_get -s 192.168.147.10 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
0

agent端

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

[root@localhost ~]# cat /zabbix_item_log/logseek 
6389

server端

[root@localhost ~]#  zabbix_get -s 192.168.147.10 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
1

zabbix自定义监控_第13张图片

zabbix自定义监控_第14张图片

zabbix自定义监控_第15张图片

zabbix自定义监控_第16张图片

手动触发

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

zabbix自定义监控_第17张图片

mysql主从状态监控

使用多实例部署来做

有实例2001、2002

将实例2001作为主
2002作为从

[root@localhost ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                    *:80                   *:*              
LISTEN  0        80                     *:2001                 *:*              
LISTEN  0        80                     *:2002                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*              

进入实例2001

[root@localhost ~]# mysql -uroot -p'123.com.' -P2001 -h127.0.0.1

mysql> grant replication slave on *.* to 'repl'@'127.0.0.1' identified by 'repl123!';  
Query OK, 0 rows affected, 1 warning (1.26 sec)

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

[root@localhost ~]# vim /etc/my.cnf

[mysqld2001]
datadir = /opt/data/2001
port = 2001
socket = /tmp/mysql2001.sock
pid-file = /opt/data/2001/mysql_2001.pid
log-error=/var/log/2001.log
server-id = 10   //加入这一行
log-bin = mybin   //加入这一行

重启实例2001

[root@localhost ~]#  ps -ef|grep 2001
root         973       1  0 15:22 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data/2001 --port=2001 --socket=/tmp/mysql2001.sock --pid-file=/opt/data/2001/mysql_2001.pid --log-error=/var/log/2001.log
mysql       1381     973  0 15:22 ?        00:00:22 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data/2001 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/2001.log --pid-file=/opt/data/2001/mysql_2001.pid --socket=/tmp/mysql2001.sock --port=2001
root      902282  189591  0 21:13 pts/0    00:00:00 grep --color=auto 2001
[root@localhost ~]# kill -9  973  1381 
[root@localhost ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                    *:80                   *:*              
LISTEN  0        80                     *:2002                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*       

[root@localhost ~]# mysqld_multi start 2001
[root@localhost ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                    *:80                   *:*              
LISTEN  0        80                     *:2001                 *:*              
LISTEN  0        80                     *:2002                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*              
mysql> show master status;
+--------------+----------+--------------+------------------+-------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------+----------+--------------+------------------+-------------------+
| mybin.000001 |      154 |              |                  |                   |
+--------------+----------+--------------+------------------+-------------------+
1 row in set (0.12 sec)


进入2002

[root@localhost ~]# mysql -uroot -p'123.com.' -P2002 -h127.0.0.1

[root@localhost ~]# vim /etc/my.cnf

[mysqld2002]
datadir = /opt/data/2002
port = 2002
socket = /tmp/mysql2002.sock
pid-file = /opt/data/2002/mysql_2002.pid
log-error=/var/log/2002.log
server-id = 20    //加入这行
relay-log = myrelay  //加入这行

重启实例2002

[root@localhost ~]# ps -ef|grep 2002
root         979       1  0 15:22 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data/2002 --port=2002 --socket=/tmp/mysql2002.sock --pid-file=/opt/data/2002/mysql_2002.pid --log-error=/var/log/2002.log
mysql       1378     979  0 15:22 ?        00:00:22 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data/2002 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/2002.log --pid-file=/opt/data/2002/mysql_2002.pid --socket=/tmp/mysql2002.sock --port=2002
root      923555  191915  0 21:34 pts/3    00:00:00 mysql -uroot -px xxxxxx -P2002 -h127.0.0.1
root      924832  189591  0 21:37 pts/0    00:00:00 grep --color=auto 2002
[root@localhost ~]# kill -9 979  1378  923555
[root@localhost ~]#  ss -antl
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                    *:80                   *:*              
LISTEN  0        80                     *:2001                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*       


root@localhost ~]# /usr/local/mysql/bin/mysqld_multi start 2002
Wide character in print at /usr/local/mysql/bin/mysqld_multi line 678.
[root@localhost ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                    *:80                   *:*              
LISTEN  0        80                     *:2001                 *:*              
LISTEN  0        80                     *:2002                 *:*              
LISTEN  0        80                     *:2003                 *:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*              
       

配置2002

mysql> change master to \
    -> master_host='127.0.0.1',
    -> master_user='repl',
    -> master_password='repl123!',
    -> master_log_file='mybin.000001',
    -> master_log_pos=154;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    2
Current database: *** NONE ***

Query OK, 0 rows affected, 1 warning (0.02 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 2002
                Connect_Retry: 60
              Master_Log_File: mybin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 316
        Relay_Master_Log_File: mybin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

[root@localhost ~]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G' | grep "Running: Yes"
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

[root@localhost ~]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G' | grep "Running: Yes"|awk '{print $2}'|grep -c 'Yes'
mysql: [Warning] Using a password on the command line interface can be insecure.
2
-c 取出数值

编写脚本

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

mysql=/usr/local/mysql/bin/mysql

status=$($mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G' | grep "Running: Yes"|awk '{print $2}'|grep -c 'Yes')

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

给脚本权限

[root@localhost scripts]# chmod +x check_mysql_replistatus.sh 
[root@localhost ~]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G' | grep "Running: Yes"|awk '{print $2}'|grep -c 'Yes'
mysql: [Warning] Using a password on the command line interface can be insecure.
2

启动脚本

[zabbix@localhost scripts]$ ./check_mysql_replistatus.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
0    //数值为0,说明没有问题

测试

[root@localhost scripts]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'stop slave;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost scripts]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G'
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mybin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 316
        Relay_Master_Log_File: mybin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No

//停止从库
[zabbix@localhost scripts]$ ./check_mysql_replistatus.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
1   

编写配置文件

[root@localhost ~]# vim zabbix_agentd.conf
//在配置文件中添加这一行
UserParameter=check_mysql_replication,/scripts/check_mysql_replistatus.sh

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

server端

[root@localhost ~]# zabbix_get -s 192.168.147.10 -k check_mysql_replication
mysql: [Warning] Using a password on the command line interface can be insecure.
1

zabbix自定义监控_第18张图片
在这里插入图片描述

mysql主从延迟监控

取出主从延迟的值

[root@localhost scripts]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G'|grep 'Seconds_Behind_Master'
mysql: [Warning] Using a password on the command line interface can be insecure.
        Seconds_Behind_Master: 0
[root@localhost scripts]# mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G'|grep 'Seconds_Behind_Master'|awk  '{print $2}'
mysql: [Warning] Using a password on the command line interface can be insecure.
0

编辑脚本

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

delay=$(mysql -uroot -p'123.com' -P2002 -h127.0.0.1 -e 'show slave status\G'|grep 'Seconds_Behind_Master'|awk  '{print $2}')

echo $delay

给脚本权限

[root@localhost scripts]# chmod +x check_mysql_delay.sh 
[root@localhost scripts]# ./check_mysql_delay.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
0

编辑配置文件

[root@localhost etc]# vim zabbix_agentd.conf
UserParameter=check_mysql_delay,/scripts/check_mysql_delay.sh
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd

server端执行

[root@localhost ~]# zabbix_get -s 192.168.147.10 -k check_mysql_delay
mysql: [Warning] Using a password on the command line interface can be insecure.
0

zabbix自定义监控_第19张图片

你可能感兴趣的:(zabbix,zabbix)