手把手教学zabbix的安装过程及添加监控机器的过程,设置钉钉告警和邮箱告警

1.安装zabbix

1.进入zabbix官网下载所需要的zabbix版本

2.下载zabbix源

# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
# yum clean all

3.安装Zabbix server,Web前端,agent

yum install zabbix-server-mysql zabbix-agent

 4.安装zabbix前端

 yum install centos-release-scl

 5.编辑配置文件 /etc/yum.repos.d/zabbix.repo and enable zabbix-frontend repository.

[zabbix-frontend]
...
enabled=1
...

 6.安装zabbix前端包

yum install zabbix-web-mysql-scl zabbix-nginx-conf-scl

 7.创建初始化数据库

mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user zabbix@localhost identified by '123456';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> set global log_bin_trust_function_creators = 1;
mysql> quit;


#导入初始架构和数据,系统将提示您输入新创建的密码。
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

**导入数据库模式后禁用log_bin_trust_function_creators选项。
mysql> set global log_bin_trust_function_creators = 0;
mysql> quit;



8. 为Zabbix server配置数据库

编辑配置文件 /etc/zabbix/zabbix_server.conf
DBPassword=123456



为Zabbix前端配置PHP
编辑配置文件 /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf


listen.acl_users = apache,nginx

php_value[date.timezone] = Asia/shanghai

9. 启动服务之前做的操作

# 修改php-fpm服务启动用户
root@zabbix-server ~]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
[zabbix]
user = nginx
group = nginx
listen= /var/opt/rh/rh-php72/run/php-fpm/zabbix.sock
listen.acl_users = nginx
listen.allowed clients = 127.0.0.1


[root@zabbix-server ~]# chown .nginx /var/opt/rh/rh-php72/lib/php/session/
[root@zabbix-server ~]# chown nginx.nginx -R /etc/zabbix/web


root@zabbix-server ~]# vim /etc/opt/rh/rh-nginx116/nginx/nginx.conf
#将配置文件中的server模块删除掉
server 
# 删除

10 启动Zabbix server和agent进程

启动Zabbix server和agent进程,并为它们设置开机自启:

# systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
# systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm

2.设置钉钉告警

2.1在钉钉中创建群聊(同组的同学创建一个群聊),在群里面添加自定义机器人

 2.2 配置钉钉告警脚本

**安装python依赖模块python-requests**
yum -y  install python3 python3-requests

 2.3配置钉钉告警配置脚本zabbix_ding.conf

##首先在创建目录/etc/zabbix/创建zabbix_ding.conf 文件
[root@qfedu ~]# vim /etc/zabbix/zabbix_ding.conf
[config]
log_path=/var/log/zabbix/zabbix_ding.log
#注意下面的链接是钉钉生成的链接
webhook=https://oapi.dingtalk.com/robot/send?access_token=c1d11ecf6704f008eb25de644ed5cead752c065c0d11f9f89b58273cf0c78a96
#钉钉生成的
secret=SECc26e5bc55c64d454f15a8bdcce6825b7e5bb4f6fecc38025d2845837415192d7

 2.4在目录/var/log/zabbix中创建钉钉告警日志文件zabbix_ding.log

[root@qfedu ~]# touch /var/log/zabbix/zabbix_ding.log 

2.5 设置钉钉告警日志文件zabbix_ding.log的所有权

[root@qfedu ~]# chown zabbix.zabbix /var/log/zabbix/zabbix_ding.log 

 2.6 配置钉钉告警执行脚本dingding.py

#在zabbix_server.conf中配置告警脚本的路径
vim /etc/zabbix/zabbix_server.conf
在zabbix_server.conf中配置告警脚本的路径
# AlertScriptsPath=/usr/lib/zabbix/alertscripts
我这里修改为
AlertScriptsPath=/usr/lib/zabbix/alertscripts

 2.7 在/usr/lib/zabbix/alertscripts目录中执行的脚本dingding.py的内容

# cd /usr/lib/zabbix/alertscripts
# vim dingding.py


#以下内容全部写在dingding.py内
#!/usr/bin/env python3
# coding:utf8
#
import configparser
import os
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json
import sys

config = configparser.ConfigParser()
config.read('/etc/zabbix/zabbix_ding.conf', encoding='utf-8')
log_path = config.get('config', 'log_path')
api_url = config.get('config', 'webhook')
api_secret = config.get('config', 'secret')
log_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


# 钉钉机器人文档说明
# https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
def get_timestamp_sign():
    timestamp = str(round(time.time() * 1000))
    secret = api_secret
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return timestamp, sign

# 获取加签后的链接
def get_signed_url():
    timestamp, sign = get_timestamp_sign()
    webhook = api_url + "&timestamp=" + timestamp + "&sign=" + sign
    return webhook

# 定义消息模式
def get_webhook(mode):
    if mode == 0:  # only 关键字
        webhook = api_url
    elif mode == 1 or mode == 2:  # 关键字和加签 或 # 关键字+加签+ip
        webhook = get_signed_url()
    else:
        webhook = ""
        print("error! mode:   ", mode, "  webhook :  ", webhook)
    return webhook


def get_message(text, user_info):
    # 和类型相对应,具体可以看文档 :https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
    # 可以设置某个人的手机号,指定对象发送
    message = {
        "msgtype": "text",  # 有text, "markdown"、link、整体跳转ActionCard 、独立跳转ActionCard、FeedCard类型等
        "text": {
            "content": text  # 消息内容
        },
        "at": {
            "atMobiles": [
                user_info,
            ],
            "isAtAll": False  # 是否是发送群中全体成员
        }
    }
    return message


# 消息发送日志
def log(info):
    if os.path.exists(log_path):
        log_file = open(log_path, "a+")
    else:
        log_file = open(log_path, "w+")
    log_file.write(info)


def send_ding_message(text, user_info):
    # 请求的URL,WebHook地址
    # 主要模式有 0 : 关键字 1:# 关键字 +加签 3:关键字+加签+IP
    webhook = get_webhook(1)
    # 构建请求头部
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    # 构建请求数据
    message = get_message(text, user_info)
    # 对请求的数据进行json封装
    message_json = json.dumps(message)
    # 发送请求
    info = requests.post(url=webhook, data=message_json, headers=header).json()
    code = info["errcode"]
    errmsg = info["errmsg"]
    if code == 0:
        log(log_time + ":消息已发送成功 返回信息:%s %s\n" % (code, errmsg))
    else:
        log(log_time + ":消息发送失败 返回信息:%s %s\n" % (code, errmsg))
        print(log_time + ":消息发送失败 返回信息:%s %s\n" % (code, errmsg))
        exit(3)


if __name__ == "__main__":
    text = sys.argv[3]
    user_info = sys.argv[1]
    send_ding_message(text, user_info)

 2.8设置脚本目录的所有权

chown -R zabbix.zabbix /usr/lib/zabbix/alertscripts/

 2.9 给脚本x权限后测试

chmod +x dingding.py 
./dingding.py  user subject mesages

 2.10 配置脚本告警

 

 为用户添加报警媒介

 

 

创建动作 

 

 

3.设置邮箱告警

3.1 进入Email

获取邮箱授权码

点击进去

 设置内容,如果是163邮箱,那么就写smtp163.com

密码为刚刚的授权码

 选择需要告警的服务

 

 

 配置完成.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值