zabbix 自定义监控!!!!

1.  开启自定义监控功能!!!

    1.1   将zabbix客户端  自定义监控功能打开!

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


# Default:
UnsafeUserParameters=1

### Option: UserParameter

    1.2 创建脚本存放目录!!

[root@localhost ~]# mkdir /scripts

2. 自定义监控进程!

    2.1  编写进程是否存在的脚本!

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

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

if {$process_status!=0};then
        echo '1'
else
        echo 'o'
fi

    2.2 给脚本加上执行权限!

[root@localhost ~]# chmod +x /scripts/check_process.sh
[root@localhost ~]# ll /scripts/
总用量 4
-rwxr-xr-x. 1 root root 131 5月  19 12:06 check_process.sh

    2.3 zabbix用户测试脚本!!

[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port   Process   
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                0.0.0.0:111              0.0.0.0:*                
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*                
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                
LISTEN    0         5                    [::1]:631                 [::]:*                
LISTEN    0         128                   [::]:111                 [::]:*                
LISTEN    0         128                      *:80                     *:*                
LISTEN    0         128                   [::]:22                  [::]:*                
[root@localhost ~]# su - zabbix -s /bin/bash
[zabbix@localhost root]$ /scripts/check_process.sh httpd
1
[zabbix@localhost root]$ exit
注销
[root@localhost ~]# systemctl stop httpd
ss[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port   Process   
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                0.0.0.0:111              0.0.0.0:*                
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*                
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                
LISTEN    0         5                    [::1]:631                 [::]:*                
LISTEN    0         128                   [::]:111                 [::]:*                
LISTEN    0         128                   [::]:22                  [::]:*                
[root@localhost ~]# su - zabbix -s /bin/bash
[zabbix@localhost root]$ /scripts/check_process.sh httpd
0
[zabbix@localhost root]$ 

      2.4 在zabbix 客户端写入自定义监控!

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

# UserParameter=

UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1

### Option: UserParameterDir

[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port   Process   
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                0.0.0.0:111              0.0.0.0:*                
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*                
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                
LISTEN    0         5                    [::1]:631                 [::]:*                
LISTEN    0         128                   [::]:111                 [::]:*                
LISTEN    0         128                   [::]:22                  [::]:*                
[root@localhost ~]# 

     2.5  在服务端测试此条脚本!

[root@localhost ~]# zabbix_get -s 192.168.180.132 -k check_process['httpd']
0

     2.6 在网页上创建监控项!

     2.7  关闭 httpd进行测试!

[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port   Process   
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                0.0.0.0:111              0.0.0.0:*                
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*                
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                
LISTEN    0         5                    [::1]:631                 [::]:*                
LISTEN    0         128                   [::]:111                 [::]:*                
LISTEN    0         128                   [::]:22                  [::]:*                
[root@localhost ~]# 

3. 自定义监控日志文件!

     3.1 编写日志判断是否报错脚本!

[root@localhost ~]# vim /scripts/log.py
[root@localhost ~]# cat /scripts/log.py
#!/usr/bin/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 ~]# 

       3.2 给脚本增加权限!

[root@localhost ~]# chmod +x /scripts/log.py
[root@localhost ~]# ll /scripts/log.py 
-rwxr-xr-x. 1 root root 1841 5月  19 13:09 /scripts/log.py

        3.2 测试zabbix用户使用脚本!

[root@localhost ~]# dnf -y install module python38
上次元数据过期检查:1:03:40 前,执行于 2021年05月19日 星期三 12时08分51秒。
[root@localhost ~]# setfacl -m u:zabbix:rx /var/log/httpd/
[root@localhost ~]# mkdir /zabbix_item_log
[root@localhost ~]# chown -R zabbix.zabbix /zabbix_item_log/
[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

        3.3 在zabbix客户端写入自定义脚本!

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

# UserParameter=

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

     3.4  在服务端进行测试!!!

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



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

服务端!!
[root@localhost ~]# zabbix_get -s 192.168.180.132 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
1

客户端!!
[root@localhost ~]# cat /zabbix_item_log/logseek 
9411
[root@localhost ~]# 

     3.5 在网页上创建监控项!

  3.6  验证!

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

4. mysql主从监控!

实验环境说明:

主机名:主机IP角色
zabbix:192.168.180.129zabbix_server端
从:192.168.180.134zabbix_agent端、mysql从数据库
主:192.168.180.131mysql主数据库

   4.1 主:  (配置root帐号在从数据库阔远程登录!)

mysql> create user 'root'@'192.168.180.134' identified by 'lpll';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on *.* to 'root'@'192.168.180.134';
Query OK, 0 rows affected (0.01 sec)

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

mysql> quit
Bye

     4.2 从:

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id=20
log-bin=mysql-bin
relay-log=mysql-relay-bin
symbolic-links=0
log-error=/var/log/mysqld.log

[cong2]
user=root
password=lpll

     4.3 编写主从脚本!

[root@localhost ~]# vim /scripts/check_mysql_replistatus.sh
[root@localhost ~]# cat /scripts/check_mysql_replistatus.sh
#!/bin/bash
status=$(mysql -e 'show slave status\G' | grep "Running: Yes" |awk '{print $2}' | grep -c 'Yes')
if  [ $status == 2 ];then
        echo '0'
else
        echo '1'
fi
[root@localhost ~]# chmod +x /scripts/check_mysql_replistatus.sh

     4.4 使用zabbix用户使用脚本!  将脚本写入文件!!!

[root@localhost ~]# su - zabbix -s /bin/bash
[zabbix@localhost root]$ /scripts/check_mysql_replistatus.sh
0

# Default:
UnsafeUserParameters=1
UserParameter=check_mysql_replication,/scripts/check_mysql_replistatus.sh

       4.5 在服务机上测试!!!

[root@localhost zabbx]# zabbix_get -s 192.168.180.134 -k check_mysql_replication
0

        4.6 在zabbix页面上创建监控!!!

       4.7  测试  在被监控的主机上关掉 服务!

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.180.131
                  Master_User: lj
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000009
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000014
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000009
             Slave_IO_Running: No
            Slave_SQL_Running: No

5. 主从延迟监控!

     5.1  编写脚本给予权限然后使用zabbix用户测试!!!

[root@localhost ~]# vim /scripts/check_mysql_delay.sh
[root@localhost ~]# cat /scripts/check_mysql_delay.sh
#!/bin/bash
delay=$(mysql -e 'show slave status\G' | grep "Seconds_Behind_Master" |awk '{print $2}')
echo $delay
[root@localhost ~]# 
[root@localhost ~]# chmod +x /scripts/check_mysql_delay.sh
[root@localhost ~]# su - zabbix -s /bin/bash
su: 警告:无法更改到 /home/zabbix 目录: 没有那个文件或目录
[zabbix@localhost root]$ /scripts/check_mysql_delay.sh
0

       5.2  将脚本监控写入配置文件!

[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
# Default:
UnsafeUserParameters=1
UserParameter=check_mysql_delay,/scripts/check_mysql_delay.sh
UserParameter=check_mysql_replication,/scripts/check_mysql_replistatus.sh
[root@localhost ~]# pkill zabbix_agent
[root@localhost ~]# zabbix_agentd 

        5.3  在服务端上测试!

[root@localhost zabbx]# zabbix_get -s 192.168.180.134 -k check_mysql_delay
0

     5.4  在zabbix页面上创建监控!

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值