A、アラームメッセージZABBIXの
原則:操作は、電子メール、マイクロチャネル、釘、およびその他のメッセージを送ることができる場所、コミットした後、警報装置の動作を設定することができます。
アラームの設定スクリプトを置き、/usr/local/zabbix/etc/zabbix_server.confを参照してください
LogFile=/usr/local/zabbix/zabbix_server.log
DBHost=127.0.0.1
DBName=zabbix
DBUser=zabbix
DBPassword=zabbixpwd
DBPort=3306
Timeout=30
AlertScriptsPath=/usr/local/zabbix/alertscripts ##触发脚本放置位置
ExternalScripts=/usr/local/zabbix/externalscripts
LogSlowQueries=3000
送信メールのPythonスクリプト
/usr/local/zabbix/alertscripts/zabbix_sendmail.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
import sys
smtpaddr = 'smtp.qq.com'
myemail='[email protected]' ##发送邮件的QQ
password='mypassword' ##填写登陆客户端的授权码,在邮箱里生成同时需要打开POP3,IMAP服务
#f = open('/usr/local/zabbix/.passwd','r')
#password = f.readline().strip()
recvmail=sys.argv[1] ##发送邮件给谁
subject=sys.argv[2] ## 发送的主题
content=sys.argv[3] ##发送的内容
msg = MIMEText("""%s"""%(content), "plain", "utf-8") ##发送的内容
msg['Subject'] = Header(subject, 'utf-8').encode()
msg['From'] = myemail
msg['To'] = recvmail
try:
smtp = SMTP_SSL( smtpaddr )
smtp.login(myemail, password)
smtp.sendmail(myemail, recvmail.split(','), msg.as_string())
smtp.quit()
print("success")
except Exception as e:
print("fail: "+str(e))
##增加执行权限,测试脚本是否运行正常能发送邮件
/usr/local/zabbix/alertscripts/zabbix_sendmail.py [email protected] 'zabbix disk' 'content: disk > 90%'
2、設定電子メールアラートアラートメディア
スクリプトの配置名、注意が、alertscriptsディレクトリ/の下には/ usr / local /のZABBIXに配置された対象が何であるかを誰に送信された3つの許容パラメータを、追加され、内容はこの場所には三つのパラメータを追加する必要があるものである
{ALERT.SENDTOを}
ALERT.SUBJECT} {
{} ALERT.MESSAGE
メディアは、ユーザー設定を送信する必要が追加
誰、時間、レベルに送信され実行されるスクリプトを、送信、カンマで区切られた複数あります
テンプレートまたはメインフレームでのトリガーアクションを作成します。
発射するトリガーを設定した後のアクション(行動)
スクリプトが実行され、ユーザーの問題の内容を送りました
電子メールのリカバリを送信するために、障害回復アクションの後
第二、カスタム警報の内容とアラーム抑制
アラームパラメータの内容に加えて、カスタムコンテンツhost.nameホストIP1
抑制トリガ後の動作デフォルト動作ステップの継続時間は、操作は、例えば、30代は、上記の配置は、次の手順を介して実施される
ステップ4-4それは4 * 30 2分の工程の後に郵送アラームを送信4
第三に、マイクロチャネルのアラームは、
企業がマイクロ手紙を作成して登録し
、自己構築されたアプリケーションの管理に背景を追加する
スクリプト内agentIdが及びsecrct塗りの手紙を得るために良い零細企業を作成した後に
同じ位置に同じメッセージアドマイクロチャネルのアラームスクリプトで
vim /usr/local/zabbix/alertscripts/zabbix_wx.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2
agentid = 'xxx' ###微信自建应用ID
corpid = 'xxx' ###微信企业ID 在我的企业里面查看
corpsecret = 'xxx' ### 微信自建应用secret
#get tocken
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']
#send wechart
touser=sys.argv[1] #many user: 'zhangsan|wangwu'
content=sys.argv[2] #content
post_content = {
"touser":touser,
"agentid":agentid,
"msgtype": "text",
"text":{
"content":content,
}
}
json_content = json.dumps(post_content)
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)
print(response.read().decode('utf-8'))
##测试
/usr/local/zabbix/alertscripts/zabbix_wx.py '通讯录里面的微信名' 'disk is not enough'
送信されたコンテンツに送信された二つの変数を作成するために追加
のアラーム(注変更スクリプトれ送信の名前)の後にアクションを
ユーザー追加注意を名前だけでなく、警報レベルに送信します。
4:アラームネイル
マイクロチャネルと同様の警告と釘警報
アラームスクリプトは/ usrを/ローカル/ ZABBIX / alertscripts /
###告警机器人脚本
###安装pip,安装request模块
yum -y install epel-release
yum -y install python-pip
pip install requests
#!/usr/bin/env python
import json
import requests
import sys
def send_msg(url, remiders, msg):
headers = {'Content-Type': 'application/json; charset=utf-8'}
data = {
"msgtype": "text",
"at": {
"atMobiles": remiders,
"isAtAll": False,
},
"text": {
"content": msg,
}
}
r = requests.post(url, data=json.dumps(data), headers=headers)
return r.text
if __name__ == '__main__':
msg = sys.argv[1]
remiders = []
url = '告警机器人webrooturl'
print(send_msg(url, remiders, msg))
設定パラメータ(管理者が記入するユーザーに送信することができます)
釘作業アラーム通知スクリプト
#!/usr/bin/python
# -*- coding: utf-8 -*-
#curl 'https://oapi.dingtalk.com/gettoken?corpid=xxx&corpsecret=xxx'
import json,urllib2,sys
appkey = '钉钉自建应用查看'
appsecret = '钉钉自建应用查看'
agentid = 钉钉自建应用查看'
touser = sys.argv[1]
content = sys.argv[2]
tockenurl = 'https://oapi.dingtalk.com/gettoken?corpid=' + appkey + "&corpsecret=" + appsecret
tockenresponse = urllib2.urlopen(tockenurl)
tockenresult = json.loads(tockenresponse.read().decode('utf-8'))
tocken = tockenresult['access_token']
sendurl = 'https://oapi.dingtalk.com/message/send?access_token=' + tocken
headers = {
'Content-Type':'application/json'
}
main_content = {
"touser": touser,
"toparty": "",
"agentid": agentid,
"msgtype": "text",
"text": {
"content": content
}
}
main_content = json.dumps(main_content)
req = urllib2.Request(sendurl,headers=headers)
response = urllib2.urlopen(req, main_content.encode('utf8'))
print(response.read().decode('utf-8'))
##测试
/usr/local/zabbix/alertscripts/zabbix_dd.py 填写钉钉用户ID 'disk > 90%'