分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

cos migration工具webhook推送

发布时间:2023-09-06 02:04责任编辑:郭大石关键词:暂无标签

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

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

主要的步骤如下:

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

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

webhook服务api

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

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

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

import sysimport requestsimport jsondef 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)

修改java代码

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

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

java调用shell命令行的方法

 1 ????public static void callShell(String[] shellString, boolean waitExit) { 2 ????????try { 3 ????????????Process process = Runtime.getRuntime().exec(shellString); 4 ????????????if(!waitExit){ 5 ????????????????return; 6 ????????????} 7 ????????????int exitValue = process.waitFor(); 8 ????????????if (0 != exitValue) { 9 ????????????????log.error("call shell failed. error code is :" + exitValue);10 ????????????}else{11 ????????????????log.info("succeed to call cmd ");12 ????????????}13 ????????} catch (Exception e) {14 ????????????log.error("call shell failed. " + e);15 ????????}16 ????}

 添加同步任务

1 ????????????printTaskStaticsInfo();2 ????????????sendInfo2Webhook(); ????// 同步任务之后,添加消息通知任务
 1 ????public void sendInfo2Webhook(){ 2 ????????if(TaskStatics.instance.getSuccessCnt() > 0){
??????????????// 同步文件数大于0时,消息推送 3 ????????????String noty_info = String.format("成功同步文件数 : %d", TaskStatics.instance.getSuccessCnt()); 4 ????????????String[] shellString = {"python3", "dingdingrobot.py", noty_info}; 5 ????????????SystemUtils.callShell(shellString, false); 6 ????????} 7
       // 查看error.log ,获取失败文件和原因 8 ????????String[] shell_str = {"nohup", "sh", "webhook_error_DING.sh", ">/dev/null 2>&1 & "}; 9 ????????SystemUtils.callShell(shell_str, false);10 ????}

 python代码

#  dingdingrobot.py  脚本

 1 import sys 2 import requests 3 import json 4 import datetime 5 ?6 7 def send_msg(msg): 8 ????now_time = datetime.datetime.now().strftime(‘%d{d}%H:%M{M}\n‘).format(d=‘号‘, M=‘分‘) 9 ????url = "https://oapi.dingtalk.com/robot/send?access_token=xxxx"10 ????headers = {‘content-type‘: ‘application/json; charset=UTF-8‘}11 ????data = {12 ?????"msgtype": "text",13 ?????"text": {14 ?????????"content": "%s*%s" % (now_time, msg)15 ?????}16 ????}17 ????conrest = requests.post(url, data=json.dumps(data, ensure_ascii=False).encode(‘utf8‘), headers=headers)18 ????print(conrest.text, end=‘ ?‘)19 ????print(conrest.status_code)20 21 22 if __name__ == ‘__main__‘:23 ????text = sys.argv[1]24 ????send_msg(text)

 shell脚本

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

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

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


java的重编译  

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

cos migration工具webhook推送

原文地址:https://www.cnblogs.com/duoxuan/p/9318381.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved