一.環境信息
OS: Centos 6.5
Python 3.5
二.由於CentOS 6.5系統自帶python2.6 ,故需要先安裝python 3.5而后在進行設置
1.安裝python 3.5.6
#wget https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tgz
#tar -zxf Python-3.5.6.tgz
#cd Python-3.5.6
#./configure --prefix=/usr/local/python3
#make
#make install
2.修改鏈接文件
#ln -s /usr/local/python3/bin/python3 /usr/bin/python3
3.添加環境變量
echo $PATH
/usr/local/java/bin:/usr/local/java/bin:/usr/local/java/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3/bin
4.執行py腳本時,如遇到報錯
Python ImportError: No module named 'requests'
需要安裝requests模塊,前提是通過pip install requests安裝,如沒有pip
#wget https://files.pythonhosted.org/packages/69/81/52b68d0a4de760a2f1979b0931ba7889202f302072cc7a0d614211bc7579/pip-18.0.tar.gz
# tar -xzvf pip-18.0.tar.gz
# cd pip-18.0
# python3 setup.py install
5.安裝requests模塊
#pip3 install requests
6.執行py腳本
#python3 alert.py "hello guys too"
b'{"errmsg":"ok","errcode":0}'
三. zabbix py腳本
#!/usr/bin/python3
#-*- coding: utf-8 -*-
#Author:DingLin
import requests
import json
import sys
import os
headers = {'Content-Type':'application/json;charset=utf-8'}
api_url = "https://oapi.dingtalk.com/robot/send?access_token=ad3172fe72faf277548d4db6ada1ed02c3d3c3e0a0xxxxxxxxxx"
def msg(text):
json_text= {
"msgtype": "text",
"at": {
"atMobiles": [
"1xxxxxxxx0"
],
"isAtAll": "false"
},
"text": {
"content": text
}
}
print (requests.post(api_url,json.dumps(json_text),headers=headers).content)
if __name__ == '__main__':
text = sys.argv[1]
msg(text)
釘釘API Reference: https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
