#!/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

weixin报警脚本的更多相关文章

  1. linux下日志文件error监控报警脚本分享

    即对日志文件中的error进行监控,当日志文件中出现error关键字时,即可报警!(grep -i error 不区分大小写进行搜索"error"关键字,但是会将包含error大小 ...

  2. shell脚本编程学习笔记(三)编写邮件报警脚本

    一.shell编写邮件报警脚本 1.POSTFIX邮件服务器准备 a.首先卸载服务器上自带的sendmail rpm -qa sendmail* //查看安装的sendmail rpm -e send ...

  3. zabbix 微信报警脚本

    不知道是什么原因直接用Python脚本zabbix无法执行脚本,需要一个shell来启动 #! /bin/bash userid=$ content=$ python /data/zabbix/ale ...

  4. Zabbix微信报警脚本及写触发记录

    一.Zabbix微信报警 默认路径:/usr/lib/zabbix/scripts/weixin.py #!/usr/bin/python # -*- coding: utf-8 -*- import ...

  5. Zabbix-4.0-设置钉钉报警脚本

    问题:当服务器发生报错时,有一个信息能实现自动发送到我的手机或者应用上,以达到对服务器的实时的监控与处理.邮件与短信不能满足实时性,于是想到了钉钉的通知. 思路:在钉钉里面建一个群,群里面拉个机器人. ...

  6. zabbix邮件报警脚本(Python)

    #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...

  7. zabbix邮件报警脚本

    #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...

  8. MySQL主从复制邮件报警脚本

    #!/bin/shexport PATH=$PATH:/application/mysql/3306/binlogFile=`date +"%Y-%m-%d %H:%M:%S"`_ ...

  9. 通过脚本发送zabbix微信报警

    实现zabbix通过微信报警的方式也是通过脚本来实现,与邮件报警不同的是,脚本调用的微信的相关接口的获取相对复杂一点 1.申请一个微信公众号(企业号) 申请方法不多说,如果已申请请忽略 2.在微信企业 ...

随机推荐

  1. Jquery实现的图标抖动效果

    原文:http://www.webdm.cn/webcode/75de64a9-3fb4-473d-bc2c-97a0a063be79.html <!DOCTYPE html PUBLIC &q ...

  2. Spring 与 MyBatis 整合

    一.实验介绍 1.1 实验内容 本节课程将整合 Spring 和 MyBatis,并采用 Junit 进行单元测试. 1.2 实验知识点 Spring - MyBatis 整合 Junit 单元测试 ...

  3. FreeBSD 8

    FreeBSD 8.0的安装过程和7.2区别不大.先在FreeBSD官方网站上下载安装镜像,我一般都下载DVD的ISO,也有人爱好下最小的安装包,然后通过FTP或HTTP方式从网上下载各个程序包. 这 ...

  4. 微信公众平台SDK for node

    实现了下面特性: 1.开启开发人员模式 2.解析微信请求參数 3.验证消息来源 4.被动回复文字消息 5.被动回复图文消息 6.获取access_token 7.创建自己定义菜单 地址:wechat ...

  5. C++静态库与动态库深入研究

    什么是库 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说库是一种可执行代码的二进制形式,可以被操 ...

  6. 解题报告 之 HDU5288 OO&#39; s Sequence

    解题报告 之 HDU5288 OO' s Sequence Description OO has got a array A of size n ,defined a function f(l,r) ...

  7. PJSIP 调用的GUID库

    PJSIP库产生随机序列串用到GUID库,针对不同的平台使用的方式不同:Windows平台下使用的是Windows系统API CoCreateGuid,在方法 pj_generate_unique_s ...

  8. ubuntu14.04下CPU的caffe配置,不成功的朋友请与我(lee)联系,后面附带邮箱

    因广大朋友需求cpu的caffe配置.所以我(lee)在这份博客中对cpu配置caffe做出对应操作说明.希望能够解决大家对cpu配置caffe的困惑.少走弯路. 假设有安装不成功的朋友能够和我联系, ...

  9. PA-RISC

    http://baike.baidu.com/view/167703.htm PA-RISC处理器 编辑   HP(惠普)公司的RISC芯片PA-RISC于1986年问世. 第一款芯片的型号为PA-8 ...

  10. speechSynthesis,TTS语音合成。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...