{"id":783,"date":"2022-06-29T18:38:58","date_gmt":"2022-06-29T10:38:58","guid":{"rendered":"http:\/\/www.rchen.top\/?p=783"},"modified":"2022-06-29T18:38:59","modified_gmt":"2022-06-29T10:38:59","slug":"zabbix%e5%91%8a%e8%ad%a6%e9%85%8d%e7%bd%ae-%e6%9c%ba%e5%99%a8%e4%ba%bawebhook%e6%8e%a8%e9%80%81%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/www.rchen.top\/?p=783","title":{"rendered":"zabbix\u544a\u8b66\u914d\u7f6e-\u673a\u5668\u4ebawebhook\u63a8\u9001\u914d\u7f6e"},"content":{"rendered":"<p><b>1<\/b><b>\u3001\u914d\u7f6e<\/b><b>zabbix<\/b><b>\u63a8\u9001\u811a\u672c<\/b><\/p>\n<p>\u627e\u5230zabbix\u811a\u672c\u76ee\u5f55alertscripts\uff0cfind \/ -name alertscripts,\u65b0\u5efapython\u811a\u672c<\/p>\n<p>vi \/usr\/lib\/zabbix\/alertscripts\/wxrobot.py<\/p>\n<p>#!\/usr\/bin\/python<\/p>\n<p>#-*- coding: utf-8 -*-<\/p>\n<p>import requests<\/p>\n<p>import json<\/p>\n<p>import sys<\/p>\n<p>import os<\/p>\n<p>headers = {&#8216;Content-Type&#8217;: &#8216;application\/json;charset=utf-8&#8217;}<\/p>\n<p>api_url = &#8220;<a href=\"https:\/\/qyapi.weixin.qq.com\/cgi-bin\/webhook\/send?key=70ce7757-8f9c-4da6-84c0-f76146215c86\">https:\/\/qyapi.weixin.qq.com\/cgi-bin\/webhook\/send?key=70ce7757-8f9c-4da6-84c0-f76146215c86<\/a>&#8221;<\/p>\n<p>def msg(text):<\/p>\n<p>json_text= {<\/p>\n<p>&#8220;msgtype&#8221;: &#8220;text&#8221;,<\/p>\n<p>&#8220;text&#8221;: {<\/p>\n<p>&#8220;content&#8221;: text<\/p>\n<p>},<\/p>\n<p>}<\/p>\n<p>print requests.post(api_url,json.dumps(json_text),headers=headers).content<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<\/p>\n<p>text = sys.argv[1]<\/p>\n<p>msg(text)<\/p>\n<p>\u5176\u4e2d &#8220;<a href=\"https:\/\/qyapi.weixin.qq.com\/cgi-bin\/webhook\/send?key=70ce7757-8f9c-4da6-84c0-f76146215c86\">https:\/\/qyapi.weixin.qq.com\/cgi-bin\/webhook\/send?key=70ce7757-8f9c-4da6-84c0-f76146215c86<\/a>&#8221; \u4e3a\u4f01\u4e1a\u5fae\u4fe1\u673a\u5668\u4eba\u83b7\u53d6\u7684webhook\u5730\u5740<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"451\" height=\"334\" class=\"alignnone wp-image-784 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/06\/Pasted-68.png\" \/><\/p>\n<p><b>2<\/b><b>\u3001\u8d4b\u4e88\u811a\u672c\u6267\u884c\u6743\u9650\uff0c\u5b89\u88c5<\/b><b>python<\/b><b>\u7ec4\u4ef6\uff0c\u6d4b\u8bd5<\/b><b>python<\/b><b>\u811a\u672c\u662f\u5426\u80fd\u6210\u529f\u53d1\u9001<\/b><\/p>\n<p>chmod +x \/usr\/lib\/zabbix\/alertscripts\/wxrobot.py<\/p>\n<p>yum\u00a0 install -y epel-release<\/p>\n<p>yum install -y python-pip<\/p>\n<p>yum install \u00a0-y python-requests<\/p>\n<p>ubuntu:<\/p>\n<p>chmod +x \/usr\/lib\/zabbix\/alertscripts\/wxrobot.py<\/p>\n<p>apt-get install python-pip<\/p>\n<p>pip install requests<\/p>\n<p><b>3<\/b><b>\u3001\u6d4b\u8bd5\u53d1\u9001\u6548\u679c<\/b><\/p>\n<p>python wxrobot.py ceshi<\/p>\n<p><b>4<\/b><b>\u3001<\/b><b>zabbix<\/b><b>\u9875\u9762\u914d\u7f6e<\/b><\/p>\n<p>4.1\u6dfb\u52a0\u4e1a\u52a1\u62a5\u8b66\u5a92\u4ecb<\/p>\n<p>{ALERT.SENDTO}<\/p>\n<p>{ALERT.SUBJECT}<\/p>\n<p>{ALERT.MESSAGE}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"288\" class=\"alignnone wp-image-785 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/06\/Pasted-69.png\" \/><\/p>\n<p>4.2\u6dfb\u52a0\u7528\u6237\u62a5\u8b66\u5a92\u4ecb<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"477\" height=\"210\" class=\"alignnone wp-image-786 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/06\/Pasted-70.png\" \/><\/p>\n<p>4.3 \u52a8\u4f5c\u5185\u6dfb\u52a0\u62a5\u8b66\u64cd\u4f5c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"290\" class=\"alignnone wp-image-787 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/06\/Pasted-71.png\" \/><\/p>\n<p><b>\u9489\u9489<\/b><b>webhook<\/b><b>\u914d\u7f6e<\/b><\/p>\n<p>vi \/usr\/lib\/zabbix\/alertscripts\/dingding.sh<\/p>\n<p>[root@zabbix alertscripts]# cat dingding.sh<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>to=$1<\/p>\n<p>subject=$2<\/p>\n<p>text=$3<\/p>\n<p>curl -i -X POST \\<\/p>\n<p>&#8216;<a href=\"https:\/\/oapi.dingtalk.com\/robot\/send?access_token=0cde2d606b2a3b919d783ec1cfa663aaafe765fa0d77191a620f16223372055a\">https:\/\/oapi.dingtalk.com\/robot\/send?access_token=0cde2d606b2a3b919d783ec1cfa663aaafe765fa0d77191a620f16223372055a<\/a>&#8216; \\<\/p>\n<p>-H &#8216;Content-type&#8217;:&#8217;application\/json&#8217; \\<\/p>\n<p>-d &#8216;<\/p>\n<p>{<\/p>\n<p>&#8220;msgtype&#8221;: &#8220;text&#8221;,<\/p>\n<p>&#8220;text&#8221;: {<\/p>\n<p>&#8220;content&#8221;: &#8220;&#8216;warning&#8221;&#8221;$text&#8221;&#8216;&#8221;<\/p>\n<p>},<\/p>\n<p>&#8220;at&#8221;:{<\/p>\n<p>&#8220;atMobiles&#8221;:[<\/p>\n<p>&#8220;&#8216;&#8221;$1&#8243;&#8216;&#8221;<\/p>\n<p>],<\/p>\n<p>&#8220;isAtAll&#8221;:false<\/p>\n<p>}<\/p>\n<p>}&#8217;<\/p>\n<p><b>\u8d4b\u4e88\u811a\u672c\u6267\u884c\u6743\u9650<\/b><\/p>\n<p>[root@zabbix alertscripts]# chmod +x dingding.sh<\/p>\n<p><b>\u6d4b\u8bd5<\/b><\/p>\n<p>[root@zabbix alertscripts]# sh dingding.sh 111<\/p>\n<p>HTTP\/1.1 200 OK<\/p>\n<p>Server: DingTalk\/1.0.0<\/p>\n<p>Date: Fri, 30 Apr 2021 01:16:39 GMT<\/p>\n<p>Content-Type: application\/json<\/p>\n<p>Content-Length: 27<\/p>\n<p>Connection: keep-alive<\/p>\n<p>Cache-Control: no-cache<\/p>\n<p><b>\u98de\u4e66<\/b><b>webhook<\/b><b>\u914d\u7f6e<\/b><\/p>\n<p>#!\/usr\/bin\/python<\/p>\n<p># -*- coding: utf-8 -*-<\/p>\n<p>import requests<\/p>\n<p>import json<\/p>\n<p>import sys<\/p>\n<p>import os<\/p>\n<p>import datetime<\/p>\n<p>url = &#8220;<a href=\"https:\/\/open.feishu.cn\/open-apis\/bot\/v2\/hook\/687333dd-e80a-4721-a7b2-be92da9bccea\">https:\/\/open.feishu.cn\/open-apis\/bot\/v2\/hook\/687333dd-e80a-4721-a7b2-be92da9bccea<\/a>&#8221;<\/p>\n<p>def send_message(message):<\/p>\n<p>payload_message = {<\/p>\n<p>&#8220;msg_type&#8221;: &#8220;text&#8221;,<\/p>\n<p>&#8220;content&#8221;: {<\/p>\n<p>&#8220;text&#8221;: message<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>headers = {<\/p>\n<p>&#8216;Content-Type&#8217;: &#8216;application\/json&#8217;<\/p>\n<p>}<\/p>\n<p>response = requests.request(&#8220;POST&#8221;, url, headers=headers, data=json.dumps(payload_message))<\/p>\n<p>return response<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<\/p>\n<p>text = sys.argv[1]<\/p>\n<p>send_message(text)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u914d\u7f6ezabbix\u63a8\u9001\u811a\u672c \u627e\u5230zabbix\u811a\u672c\u76ee\u5f55alertscripts\uff0cfind \/ -name al [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":698,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[102],"class_list":["post-783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-134","tag-zabbix"],"_links":{"self":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=783"}],"version-history":[{"count":1,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/media\/698"}],"wp:attachment":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}