04 zabbix自定义监控

1. 自定义监控进程

实例:
现在需要监控客户机的httpd服务的进程

[root@localhost ~]# ps -ef | grep httpd
root       41775       1  1 15:50 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     41776   41775  1 15:50 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     41777   41775  5 15:50 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     41778   41775  2 15:50 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     41779   41775  1 15:50 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root       41992    2204  0 15:50 pts/0    00:00:00 grep --color=auto httpd
[root@localhost ~]# 

1.1 修改被监控机的配置文件

[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
322 UnsafeUserParameters=1   //修改这一行
526 UserParameter=check_process[*],/scripts/check_process.sh $1   //在配置文件最后一行添加
//修改完之后记得重启
[root@localhost ~]# pkill zabbix_agentd 
[root@localhost ~]# zabbix_agentd 

1.2 写脚本,脚本放到统一的位置

//注意在被监控机去写脚本获取数据
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# cat check_process.sh 
#! /bin/bash

count=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1")   //利用正则去过滤httpd的五个进程
if [ $count -eq 0 ];then   //判断值如果等于0输出1,否则输出0
    echo '1'   //1代表进程有问题
else
    echo '0'   //0代表正常
fi
[root@localhost scripts]# 
[root@localhost scripts]# chmod +x check_process.sh   //赋予执行权限
//返回服务端手动获取数据
[root@server ~]# zabbix_get -s 192.168.8.130 -k check_process[httpd]
0
[root@localhost ~]# systemctl stop httpd.service    //关闭httpd
[root@server ~]# zabbix_get -s 192.168.8.130 -k check_process[httpd]   //显示进程有问题
1

1.3 在web界面配置监控项和触发器

选择主机,点击监控项
在这里插入图片描述
查看最新数据,因为关闭了httpd所以数值为1
在这里插入图片描述
选择主机添加触发器
在这里插入图片描述
填写名称以及主机
在这里插入图片描述
添加相应的值
在这里插入图片描述
在这里插入图片描述
可以看到httpd服务报警了,存在故障,因为关闭了httpd服务输出的结果为1就有问题触发报警
在这里插入图片描述

2. 自定义监控日志文件

实例:现在需要监控httpd的日志文件

2.1 写脚本,脚本放到统一的位置

注意!!!
下面使用的是python编写的脚本
作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为Error
[root@localhost scripts]# ls
check_process.sh  log.py
[root@localhost scripts]# chmod +x log.py 
[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 scripts]# 
[root@localhost scripts]# yum -y install python3   //下载python

//手动触发验证一下脚本
[root@localhost scripts]# ./log.py /var/log/httpd/error_log 
0
[root@localhost scripts]# echo "Error" >> /var/log/httpd/error_log 
[root@localhost scripts]# ./log.py /var/log/httpd/error_log 
1
[root@localhost scripts]# 
[root@localhost scripts]# rm -f /tmp/logseek 

2.2 修改被监控机的配置文件

[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3   //在配置文件最后一行加上
[root@localhost scripts]# pkill zabbix_agentd   //重启服务
[root@localhost scripts]# zabbix_agentd 

[root@localhost scripts]# chmod 755 /var/log/httpd/

//返回服务端是否能获取数据
[root@server ~]# zabbix_get -s 192.168.8.130 -k check_log[/var/log/httpd/error_log]
0
[root@localhost scripts]# echo "Error" >> /var/log/httpd/error_log 
[root@server ~]# zabbix_get -s 192.168.8.130 -k check_log[/var/log/httpd/error_log]
1

2.3 在web界面配置监控项和触发器

在主机里面添加监控项
在这里插入图片描述
在这里插入图片描述
在主机里面添加触发器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
查看最新数据
在这里插入图片描述
手动触发验证

[root@localhost scripts]# echo "Error" >> /var/log/httpd/error_log 
[root@localhost scripts]# 

在这里插入图片描述
在监控项里面继续添加其他值
在这里插入图片描述
手动触发验证

[root@localhost scripts]# echo "error" >> /var/log/httpd/error_log 
[root@localhost scripts]# 

在这里插入图片描述

3. 自定义监控MySQL主从

实例:

搭建MySQL主从后,很多时候不知道从的状态是否ok,有时候出现异常不能及时知道,这里通过shell脚本结合zabbix实现监控并告警

一般情况下,在MySQL的从上查看从的运行状态是通过Slave_IO_Running线程和Slave_SQL_Running线程是否ok,通过命令“show slave status\G;”即可查看。所以这里根据这两个值进行判断。

环境说明:首先,要确保两台主机有MySQL主从复制

数据库角色IP
主数据库192.168.8.137(node1)
从数据库192.168.8.130(node2)
[root@node2 scripts]# mysql -uroot -p1 -e 'show slave status\G'
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.8.137
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay-bin.000004
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 

3.1 写脚本,脚本放到统一的位置

//去从数据库编写脚本
[root@node2 scripts]# cat check_replication.sh 
#!/bin/bash

user=root
pass=1

count=$(mysql -u$user -p$pass -e 'show slave status\G' 2>/dev/null |grep '_Running:'|grep -c 'Yes')
if [ $count -eq 2 ];then
        echo '0'
else
        echo '1'
fi

[root@node2 scripts]# 

3.2 修改被监控机的配置文件

[root@node2 scripts]# vim /usr/local/etc/zabbix_agentd.conf
322 UnsafeUserParameters=1   //修改这一行
UserParameter=check_replication,/scripts/check_replication.sh   //在配置文件最后一行添加

[root@node2 scripts]# pkill zabbix_agentd 
[root@node2 scripts]# zabbix_agentd 
//手动触发验证一下脚本
[root@node2 scripts]# bash check_replication.sh 
0
[root@node2 scripts]# 

//返回服务端获取数据
[root@server ~]# zabbix_get -s 192.168.8.130 -k check_replication
0
[root@server ~]# 

//模拟故障
[root@node2 scripts]# mysql -uroot -p1
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

//再次验证
[root@node2 scripts]# bash check_replication.sh 
1
[root@node2 scripts]# 

[root@server ~]# zabbix_get -s 192.168.8.130 -k check_replication
1
[root@server ~]# 

3.3 在web界面配置监控项和触发器

点击主机创建监控项
在这里插入图片描述
在这里插入图片描述
点击主机添加触发器
在这里插入图片描述

在这里插入图片描述

//模拟故障
[root@node2 scripts]# mysql -uroot -p1
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

在这里插入图片描述

4 自定义监控MySQL主从延迟

4.1 写脚本,脚本放到统一的位置

[root@node2 scripts]# cat check_replication_delay.sh 
#!/bin/bash

user=root
pass=1

delay_count=$(mysql -u$user -p$pass -e 'show slave status\G' 2>/dev/null |grep 'Behind'|awk '{print $2}')
if [ $delay_count != NULL ];then
    echo $delay_count
else
    echo '0'
fi
[root@node2 scripts]# 
[root@node2 scripts]# chmod +x check_replication_delay.sh 

4.2 修改被监控机的配置文件

[root@node2 scripts]# vim /usr/local/etc/zabbix_agentd.conf
322 UnsafeUserParameters=1   //修改这一行
UserParameter=check_replication_delay,/scripts/check_replication_delay.sh   //在文件的最后一行添加
//重启服务
[root@node2 scripts]# pkill zabbix_agentd 
[root@node2 scripts]# zabbix_agentd 


//验证脚本效果
[root@node2 scripts]# bash check_replication_delay.sh 
0
[root@node2 scripts]# 

//返回服务端获取客户端数据
[root@server ~]# zabbix_get -s 192.168.8.130 -k check_replication_delay
0
[root@server ~]# 

4.3 在web界面配置监控项和触发器

点击主机添加监控项
在这里插入图片描述
在这里插入图片描述
查看最新数据,查看监控获取的值
在这里插入图片描述
点击主机添加触发器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5 声音报警

在用户设置里面有一个profile,点击进去
在这里插入图片描述
点击messaging消息,将Frontend messaging勾选,默认是没有勾选的。
Play sound表示播放声音,默认只播放一次

这里声音报警有三个选项,第一个是只发一次声音;第二个是每十秒钟发一次;第三个是Message timeout的间隔时间来进行报警的。

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

6 管理用户和组权限

首先登录zabbix服务端的数据库,里面有一个zabbix的数据库

[root@server ~]# mysql -uroot -p1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

切换到zabbix数据库里面去,查看表里面有一个users的表,这里面有两个用户一个是Admin,一个是guest

mysql> use zabbix 
mysql> show tables;
 users 

web界面中他们在Adminstration
在这里插入图片描述

6.1 管理用户

如果想添加用户可以在users这张表里面插入也可以在web界面添加一个用户
在这里插入图片描述
先给用户一个角色,因为我们这边是添加到管理员组所以给他一个管理员角色,添加之后,系统会默认将管理员的权限自动添加
在这里插入图片描述
这里的Username是用户名,Groups是将用户添加到哪个组里面去,然后给用户设置一个密码,最后点击add添加
在这里插入图片描述

mysql> select * from users\G;
*************************** 3. row ***************************
        userid: 3
      username: pengyudong
          name: 
       surname: 
        passwd: $2y$10$tmwOW2nx7wJjh7wxxWOeIOekidbiXG0F9Ocs5r3IMTrPjl9dURhL2
           url: 
     autologin: 0
    autologout: 0
          lang: default
       refresh: 30s
         theme: default
attempt_failed: 0
    attempt_ip: 
 attempt_clock: 0
 rows_per_page: 50
      timezone: default
        roleid: 2
3 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> 

6.2 设置权限

添加某个用户到这个里面之后某个用户就无法访问web界面
在这里插入图片描述
下面的zabbix adminstrators是管理员组
可以给哪个组设置权限
这里列举了你所有的组,可以选择给哪个组什么权限
在这里插入图片描述
这里我们看一下刚才添加的用户的权限
在这里插入图片描述
这里因为我们设置这个用户的角色为管理员角色,所以他有和管理员相同的权限。
在这里插入图片描述
角色有多个,除了管理员角色外还有客户角色,超级管理员角色,用户角色

除此之外我们可以去自定义角色
点击创建用户角色

可以通过需要来定义权限
在这里插入图片描述
在这里插入图片描述

7 设想一下如果zabbix管理员账户的密码忘记了该怎么办呢?

mysql> select * from users\G
*************************** 1. row ***************************
        userid: 1
      username: Admin
          name: Zabbix
       surname: Administrator
        passwd: $2y$10$92nDno4n0Zm7Ej7Jfsz8WukBfgSS/U0QkIuu8WkJPihXBb2A1UrEK

[root@server ~]# echo -n passwd | openssl md5 
(stdin)= 5fce1b3e34b520afeffb37ce08c7cd66
 passwd就是密码,可以根据自己的需要来设置。他会生成一个密码加密后的字符串
然后到数据库里面,更新表格并重新设置密码
mysql> update users set passwd="5fce1b3e34b520afeffb37ce08c7cd66" where userid = 1;
最后刷新一下权限即可
mysql> flush privileges;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值