#!/bin/bash
### script name weixin.sh
### send messages from weixin for zabbix monitor
### jack
### --
### usage: curl -s -G url 获取acessToken
### curl --data url 传送凭证调用企业号接口
### zabbix 会传送三个参数给脚本,$ 是消息接收账号,$ 报警标题, $ 报警内容 CropID=''
Secret=''
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F '"' '{print $4}')
now_time=$(date -d now)
now_timestamp=$(date -d "${now_time}" +%s)
options=$[**]
token_file='/tmp/token_file.txt'
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
LOG_FILE='/tmp/weixin_log.txt' function logMessageToFile(){
echo "[ $1 ] - ["$(date "+%Y-%m-%d %H:%M:%S")"] - $2" >> $LOG_FILE
} function create_tokenfile(){
echo "${now_time} > ${Gtoken}" > ${token_file}
} function get_Gtoken(){
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F '"' '{print $4}')
} function check_token(){
if [ -f "${token_file}" ]; then
file_token=$(awk -F '>' '{print $2}' ${token_file})
file_time=$(awk -F '>' '{print $1}' ${token_file})
file_timestamp=$(date -d "${file_time}" +%s)
time_difference=$[${now_timestamp}-${file_timestamp}]
if [ "${time_difference}" -gt "${options}" ]; then
get_Gtoken
create_tokenfile
logMessageToFile "INFO" "上次token是: ${file_token},到现在过了${time_difference} 秒, 新的token是:${Gtoken}"
else
logMessageToFile "INFO" "not need update the token, use old token ${file_token} 到现在过了${time_difference} 秒"
Gtoken=$(echo ${file_token})
fi
else
get_Gtoken
logMessageToFile "INFO" "the token_file : ${token_file} not exsits ,will create it . 到现在过了${time_difference} 秒"
create_tokenfile
fi } function body(){
local int AppID= # 企业号中的应用ID
local UserID="@all" # 部门成员id,zabbix中定义的微信接受者
local PartyID= # 部门id,定义了范围,组内成员都可接收到消息
local Msg=$(echo "$@" | cut -d " " -f3-) # 过滤出zabbix中传递的第三个参数
printf '{\n'
printf '\t"touser":"'"$UserID"\"",\n"
printf '\t"toparty":"'"$PartyID"\"",\n"
printf '\t"msgtype": "text",'"\n"
printf '\t"agentid":'$AppID\,"\n"
printf '\t"text":{\n'
printf '\t\t"content":"'"$Msg"\"
printf '\n\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
} check_token
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL

最新文章

  1. SPI基础知识
  2. jquery样式篇
  3. HTML5 与 CSS3 jQuery部分知识总结
  4. Myeclipse非正常关闭出现问题
  5. MySQL MHA配置常见问题
  6. script标签块的独立性与共享性
  7. ThinkPHP验证码刷新随机数
  8. [ACM] 最短路算法整理(bellman_ford , SPFA , floyed , dijkstra 思想,步骤及模板)
  9. Linux grep 命令中的正则表达式详解
  10. Struts1、Struts2的线程安全问题
  11. NHibernate - HQL - 添加和更改
  12. [Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer
  13. python之jieba库
  14. 简单比较init-method,afterPropertiesSet和BeanPostProcessor
  15. java中组合与聚合(聚集)的区别
  16. bzoj千题计划198:bzoj1084: [SCOI2005]最大子矩阵
  17. SQL 查找重复记录
  18. 3、第一个Python程序
  19. 阿里大于短信返回XML
  20. Ubuntu开机自动挂载分区

热门文章

  1. NLP项目
  2. Java中判断String对象是否为空的方法
  3. MAC终端命令行整理
  4. Ubuntu切换至root错误:su:Authentication failure解决
  5. treeList获取目录下的所有文件
  6. Headroom.js插件用法
  7. substr使用注意
  8. C++字符串操作二
  9. 后台运行命令:&和nohup command & 以及关闭、查看后台任务
  10. EC2的维护更新