cos migration工具webhook推送

时间:2023-03-09 00:04:31
cos migration工具webhook推送

上一篇讲了腾讯云同步工具的使用,这篇主要是补充如何将同步结果主动消息通知。

因为cos migration 工具是java语言,并在github开源的,所以可以直接修改源码,添加webhook推送代码。

主要的步骤如下:

  1. 在群聊中添加自定义机器人,获取webhook地址。
  2. 修改cos migration工具的源码,在同步任务后添加通知任务,向webhook地址post数据

上述步骤完成后群聊中就可以显示同步结果了,有成功的文件数,还有失败的文件路径及原因

webhook服务api

这里使用钉钉的群聊自定义机器人,当然可以用自己的服务器搭建,但最后还是希望可以通知到终端用户,使用现成的平台更改方便和高效。钉钉机器人的文档已经很完备了。

  1. 创建群聊
  2. 添加机器人,获取webhook地址

使用python简单测试下接口,官方文档也有java和php的测试例子

import sys
import requests
import json def send_msg(msg):
url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
headers = {'content-type': 'application/json; charset=UTF-8'}
data = {
"msgtype": "text",
"text": {
"content": msg
},
"at": {
"atMobiles": [
"13XXXXXXXXX"
],
"isAtAll": False
}
}
conrest = requests.post(url, data=json.dumps(data, ensure_ascii=False).encode('utf8'), headers=headers)
print(conrest.text, end=' ')
print(conrest.status_code) if __name__ == '__main__':
# text = sys.argv[1]
text = '你好,阿里云的钉钉,我是腾讯的cos migration'
send_msg(text)

cos migration工具webhook推送

修改java代码

因为对java不太熟,所以这边使用python脚本写https post的功能。java中调用终端执行python脚本。

同步成功跟同步失败的日志都通过python脚本发送消息。

java调用shell命令行的方法

     public static void callShell(String[] shellString, boolean waitExit) {
try {
Process process = Runtime.getRuntime().exec(shellString);
if(!waitExit){
return;
}
int exitValue = process.waitFor();
if (0 != exitValue) {
log.error("call shell failed. error code is :" + exitValue);
}else{
log.info("succeed to call cmd ");
}
} catch (Exception e) {
log.error("call shell failed. " + e);
}
}

添加同步任务

             printTaskStaticsInfo();
sendInfo2Webhook(); // 同步任务之后,添加消息通知任务
     public void sendInfo2Webhook(){
if(TaskStatics.instance.getSuccessCnt() > 0){
// 同步文件数大于0时,消息推送
String noty_info = String.format("成功同步文件数 : %d", TaskStatics.instance.getSuccessCnt());
String[] shellString = {"python3", "dingdingrobot.py", noty_info};
SystemUtils.callShell(shellString, false);
}

       // 查看error.log ,获取失败文件和原因
String[] shell_str = {"nohup", "sh", "webhook_error_DING.sh", ">/dev/null 2>&1 & "};
SystemUtils.callShell(shell_str, false);
}

 python代码

#  dingdingrobot.py  脚本

 import sys
import requests
import json
import datetime def send_msg(msg):
now_time = datetime.datetime.now().strftime('%d{d}%H:%M{M}\n').format(d='号', M='分')
url = "https://oapi.dingtalk.com/robot/send?access_token=xxxx"
headers = {'content-type': 'application/json; charset=UTF-8'}
data = {
"msgtype": "text",
"text": {
"content": "%s*%s" % (now_time, msg)
}
}
conrest = requests.post(url, data=json.dumps(data, ensure_ascii=False).encode('utf8'), headers=headers)
print(conrest.text, end=' ')
print(conrest.status_code) if __name__ == '__main__':
text = sys.argv[1]
send_msg(text)

 shell脚本

用于检索error.log是否有localpath的失败文件路径,有则调用python脚本通知

 #!/bin/bash
sleep
if [ `grep -i "localpath" cos_migrate_tool_v5-master/log/error.log|wc -l` -gt ];then
DATA="`cat cos_migrate_tool_v5-master/log/error.log | grep "localpath" |sort|uniq`"
python3 dingdingrobot.py "$DATA"
: > cos_migrate_tool_v5-master/log/error.log # 清除之前的错误信息
fi

基本上述代码就可以实现同步结果的钉钉消息推送了。


java的重编译  

腾讯云的start_migrate脚本是运行dep下的cos_migrate_tool-1.0-jar-with-dependencies.jar,同时工程使用Maven进行编译的,所以修改过java代码后,需要使用maven进行重编译。