注意:当前采用python3.6.8 版本为基础环境,创建钉钉机器人步骤此文章忽略
可以直接复制最后完整代码使用
一、定义执行系统命令的函数
此函数会把服务器运行的服务返回到一个列表中
def run_shell_command(command):
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
lines = result.stdout.split()
return [line.strip().decode('utf-8') for line in lines if line.strip()]
else:
print(f"Error executing command: {command}. Error message: {result.stderr}")
return []
二、定义判断的服务进程是否存在的函数
def judge():
command = "/usr/bin/jps | awk '{ print $2 }'" # 注意这里jps 命令一定要使用绝对路径
serverlist = ["HMaster", "HRegionServer", "NameNode", "DataNode"] # 定义要检查的服务列表
output_list = run_shell_command(command)
for s in serverlist:
if s in output_list:
pass
else:
webhook_msg(s)
三、定义钉钉发送消息报警
def webhook_msg(dmsg):
"""
"""
web_hook = "https://oapi.dingtalk.com/robot/send?access_token=431860b7a346163b940d36f5697aexxxxxxxxxxxxxa2ca93f1c9" # 替换自己的webhook
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
head = {'Content-Type': 'application/json'}
hostname = os.uname()[1] # 获取主机名
msg = hostname + '\n' + dmsg + "服务掉线请及时查看" + '\n' + now
text = {
"msgtype": "text",
"text": {
"content": msg
},
}
try:
r = requests.post(web_hook, data=json.dumps(text), headers=head)
except Exception as E:
return {'success': False, 'msg': str(E)}
if r.status_code != 200:
return {"success": False, "msg": r.text}
else:
return {"success": True, "msg": r.text}
四、完整脚本
import requests # 使用pip3 install requests 安装
import datetime
import json
import os
import subprocess
def run_shell_command(command):
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
lines = result.stdout.split()
return [line.strip().decode('utf-8') for line in lines if line.strip()]
else:
print(f"Error executing command: {command}. Error message: {result.stderr}")
return []
def judge():
command = "/usr/bin/jps | awk '{ print $2 }'"
serverlist = ["HMaster", "HRegionServer", "NameNode", "DataNode"]
output_list = run_shell_command(command)
for s in serverlist:
if s in output_list:
pass
else:
webhook_msg(s)
def webhook_msg(dmsg):
"""
"""
web_hook = "https://oapi.dingtalk.com/robot/send?access_token=431860b7a346163b940d36f5697xxxxxxxxx738a2ca93f1c9" # 替换自己的webhook
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
head = {'Content-Type': 'application/json'}
hostname = os.uname()[1]
msg = hostname + '\n' + dmsg + "服务掉线请及时查看" + '\n' + now
text = {
"msgtype": "text",
"text": {
"content": msg
},
}
try:
r = requests.post(web_hook, data=json.dumps(text), headers=head)
except Exception as E:
return {'success': False, 'msg': str(E)}
if r.status_code != 200:
return {"success": False, "msg": r.text}
else:
return {"success": True, "msg": r.text}
if __name__ == "__main__":
judge()