zabbix報警(向消息中心發送報警信息)


一、zabbix web界面的配置

重點:zabbix執行報警腳本時,有對腳本的執行權限,但是沒有對腳本中的命令執行的權限,需要在sudoers文件中為zabbix用戶添加用戶權限。

1.創建腳本的報警媒介

腳本路徑可以在server的配置文件中配置

[root@localhost]# cat /etc/zabbix/zabbix_server.conf |grep "alertscripts"
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts

[root@localhost]# ls /usr/lib/zabbix/alertscripts/
zabbix_alert.sh

輸入腳本名稱,類型選擇腳本並添加以下3個參數,分別對應sendEmail.sh腳本需要的3個參數:收件人地址、主題、詳細內容

{ALERT.SENDTO}  
{ALERT.SUBJECT}  
{ALERT.MESSAGE}

2.為用戶添加報警媒介,這里就不創建新的用戶,而是用admin用戶報警

3.創建動作並進行配置

動作參數說明

告警主機:{HOSTNAME1}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項目:{TRIGGER.KEY1}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
環境信息:UAT

二.腳本的編寫以及說明

  由於預生產環境不能連接外網,所以需要把報警信息推送到項目系統的消息中心模塊。而向消息中心發送消息需要攜帶token,采用python3 + requests的方式獲取token,向消息中心發送消息。(對於shell熟悉程度不高)
  由於編譯安裝python3需要的依賴包很多,在沒有外網的情況下安裝依賴包非常DT。所以采用docker容器的方式,啟動一個python3的容器來執行腳本。

1.腳本如下

#!/usr/bin/env python3
import requests
import json
import sys
class ZabbixAlert(object):
    login_url = 'https://xx.xxx.com/api/login'
    login_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
    }
    login_data = {
        'username': 'admin',
        'password': 'xxxxxx',
    }

    submit_url = 'https://xx.xxx.com/message/submit'

    # 獲取token
    def get_auth_token(self):
        response = requests.post(url=self.login_url,data=self.login_data,headers=self.login_headers)
        response_dict = json.loads(response.text)
        return response_dict['X-Auth-Token']

    # 攜帶token發送報警信息
    def send_message(self,sendto,subject,message):
        x_auth_token = self.get_auth_token()
        headers = {
            'Content-Type':'application/json;charset=UTF-8',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
            'x-auth-token':x_auth_token,
        }
        data = {
            'appname':"GCREPORT-新聞中心",
            'content': "{}<p><br></p>{}<p><br></p>{}<p><br></p>".format(sendto,subject,message),
            'createuser': "admin",
            'destusers': [],
            'title': "zabbix測試",
            'type': "新聞",
        }
        response = requests.post(url=self.submit_url,headers=headers,data=json.dumps(data))
        return response.text


sendto = sys.argv[1]
subject = sys.argv[2]
message = sys.argv[3]
zbx = ZabbixAlert()
print(zbx.send_message(sendto,subject,message))

2.生成docker鏡像並上傳服務器

在一台能上外網的機器生成鏡像
[root@common docker]# cat Dockerfile 
FROM python:3.6
RUN pip3 install requests 

[root@common docker]# docker build -t python3:v1 .
[root@common docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python3             v1                  0f4e54b7f396        8 minutes ago       922 MB
docker.io/python    3.6                 1c515a624542        36 hours ago        913 MB

導出鏡像並上傳到預生產環境
[root@common docker]# docker save -o python3.tar python3:v1

在預生產機器上導入鏡像,將本地的腳本目錄映射到容器中的某個目錄,方便修改python腳本的參數。

腳本說明:send_message.py為發送告警信息的腳本,zabbix_alert.sh為zabbix觸發動作后調用的腳本。
[root@localhost alertscripts]# ls /usr/lib/zabbix/alertscripts/
send_message.py  zabbix_alert.sh

命令一定要加上絕對路徑,不加zabbix調用時會找不到命令,$1 $2 $3需要加上雙引號,不加 python通過sys.argv取參數時會出現問題。
[root@localhost alertscripts]# cat zabbix_alert.sh 
#!/bin/bash
/bin/sudo /usr/bin/docker exec python /alertscripts/send_message.py "$1" "$2" "$3"

導入鏡像
[root@localhost]# docker load -i /usr/local/src/python3.tar 
660314270d76: Loading layer [==================================================>]  119.2MB/119.2MB
6d5a64ea8f37: Loading layer [==================================================>]  17.11MB/17.11MB
74e2ede3b29c: Loading layer [==================================================>]  17.83MB/17.83MB
97e8dd85db4e: Loading layer [==================================================>]  149.8MB/149.8MB
6e302bbcacce: Loading layer [==================================================>]  520.7MB/520.7MB
1911313536c8: Loading layer [==================================================>]  17.57MB/17.57MB
efb9a1092694: Loading layer [==================================================>]  87.38MB/87.38MB
2ea1378e6b91: Loading layer [==================================================>]  4.608kB/4.608kB
2fcfc4f651ee: Loading layer [==================================================>]  6.677MB/6.677MB
b4e47ec3053a: Loading layer [==================================================>]  9.316MB/9.316MB
Loaded image: python3:v1

啟動容器
docker run -ti -d --name python -v /usr/lib/zabbix/alertscripts/:/alertscripts python3:v1

  配置至此,我以為會很順利的觸發動作,發送報警消息。但是每次zabbix主界面顯示已發送時,在消息中心都收不到消息。於是開始排錯。

  首先,開啟詳細的日志記錄,將日志的等級設置為4。

[root@common]# cat /etc/zabbix/zabbix_server.conf
### Option: DebugLevel
#       Specifies debug level:
#       0 - basic information about starting and stopping of Zabbix processes
#       1 - critical information
#       2 - error information
#       3 - warnings
#       4 - for debugging (produces lots of information)
#       5 - extended debugging (produces even more information)
#
# Mandatory: no
# Range: 0-5
# Default:
DebugLevel=4

發現權限不夠,需要在/etc/sudoers文件中為zabbix用戶添加命令權限。(如果在zabbix_alert.sh中命令不加絕對路徑會提示找不到命令)

日志內容
29074:20190829:093122.148 In zbx_popen() command:'/usr/lib/zabbix/alertscripts/zabbix_alert.sh 'Administrator' 'PROBLEM: Too many processes on Zabbix server' '告警主機:Zabbix server^M
告警時間:2019.08.29 09:30:52^M
告警等級:Warning^M
告警信息: Too many processes on Zabbix server^M
告警項目:proc.num[]^M
問題詳情:Number of processes:198^M
當前狀態:PROBLEM:198^M
事件ID:122''
 29074:20190829:093122.149 End of zbx_popen():9
 29870:20190829:093122.149 zbx_popen(): executing script
 29074:20190829:093122.155 In zbx_waitpid()
 29074:20190829:093122.155 zbx_waitpid() exited, status:126
 29074:20190829:093122.155 End of zbx_waitpid():29870
 29074:20190829:093122.155 cc.sh output:
/usr/lib/zabbix/alertscripts/zabbix_alert.sh:行5: /usr/bin/docker: 權限不夠

 29074:20190829:093122.155 End of execute_action():SUCCEED
 29074:20190829:093122.155 alert ID [44] was sent successfully
 29074:20190829:093122.155 query without transaction detected
 29074:20190829:093122.155 query [txnlev:0] [update alerts set status=1,error='' where alertid=44]
 29084:20190829:093122.156 __zbx_zbx_setproctitle() title:'proxy poller #1 [exchanged data with 0 proxies in 0.000098 sec, exchanging data]'
 
添加zabbix執行命令的權限(一定要關閉selinux,這是一個大坑)
[root@common]# cat /etc/sudoers
## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL
zabbix  ALL=(root)      NOPASSWD:/usr/bin/docker,/bin/sudo

至此,zabbix向消息中心發送報警信息配置完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM