本文接上文
python3 钉钉 加签名 钉钉群机器人告警 脚本
https://blog.csdn.net/frdevolcqzyxynjds/article/details/128455191
cat dd-os-t1.py
import json
import sys
from hdfs import *
import json
import hashlib
import base64
import hmac
import time
import requests
from urllib.parse import quote_plus
from datetime import datetime
from subprocess import call
import subprocess
timestamp = str(round(time.time() * 1000))
url = 'https://oapi.dingtalk.com/robot/send?access_token=你的webhook'
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 = quote_plus(base64.b64encode(hmac_code))
class Send_Message:
def __init__(self, text):
self.text = text
def send_message(self):
headers = {'Content-Type': 'application/json'}
webhook = url + '×tamp=' + timestamp + '&sign=' + sign
data = {
"msgtype": "text",
"text": {
"content": "%s" % (self.text)
}
}
value = json.dumps(data)
r = requests.post(webhook, value, headers=headers)
return r.text
if __name__ == "__main__":
py_path = sys.executable
cmd = """echo '----';
date +%F-%H:%M:%S;
echo '';
echo 'node info: ';hostname -f;
echo '';
echo 'kernel info: ';uname -a;
echo '';
echo 'os info: ';cat /etc/system-release;
echo '';
echo 'arch: ';hostnamectl;
echo '';
echo 'cpu info: ';lscpu;
echo '';
echo 'mem info: ';free -h;
echo '';
echo 'disk info: ';lsblk;echo;df -hT;
echo '';
echo 'ip info: ';hostname -I;
echo '';
date +%F-%H:%M:%S;
echo '----';
""".format(py_path, py_path)
result = subprocess.getoutput(cmd)
s = Send_Message(result + "\n\n巡检成功!" + "\n\n通知时间:{}".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
s.send_message()
sys.exit(0)
打完收工!
我们下期见!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)