Python实现个人微信号自动监控告警的示例


Posted in Python onJuly 03, 2019

wechat_sender 是基于 wxpy 和 tornado 实现的一个可以将你的网站、爬虫、脚本等其他应用中各种消息 (日志、报警、运行结果等) 发送到微信的工具。

运行环境

Python 2.7 及以上 Python 3 及以上

实现过程

安装 pip 工具

[root@server1 ~]# wget https://bootstrap.pypa.io/get-pip.py
[root@server1 ~]# python get-pip.py

Python实现个人微信号自动监控告警的示例

pip 安装模块

##安装依赖软件
[root@server1 ~]# yum install -y gcc python-devel
##安装
[root@server1 ~]# pip install wechat_sender

Web登录微信发送消息

安装web服务器

[root@server1 ~]# yum install -y httpd
[root@server1 ~]# systemctl start http
[root@server1 ~]# systemctl stop firewalld
[root@server1 ~]# cat /var/www/html/index.html
<html>
<head><meta http-equiv="refresh" content="2"></head>
<style>
  body {
    width: 35em;
    margin: 0 auto;
    font-family: Tahoma, Verdana, Arial, sans-serif;
  }
</style>
<body>
<img src="/qr.png">
</body>
</html>

python脚本代码

[root@server1 ~]# cat /var/www/html/sender.py
#!/bin/bash/env python
#coding:utf-8

from wxpy import *
from wechat_sender import *
from wechat_sender import Sender

#bot = Bot()  ##windows直接扫
#下面这个是服务器版(Linux)
#bot = Bot(qr_path="qr.png")
#避免重复登录重复扫二维码
bot = Bot(qr_path="qr.png",cache_path=True)
##通过文件助手给登录的微信号发消息
bot.file_helper.send('Hello world!')

web登录微信

##执行python脚本,占用终端,web登录后会有提示
[root@server1 ~]# cd /var/www/html/
[root@server1 ~]# python sender.py 
Getting uuid of QR code.
Downloading QR code.
xdg-open: no method available for opening 'qr.png'
Please scan the QR code to log in.

另一方面,打开浏览器输入 ip or localhost,微信扫一扫

Python实现个人微信号自动监控告警的示例

微信登录后,终端释放,提示成功,消息同时发送,并且web二维码失效

Login successfully as someone

微信点击确认

Python实现个人微信号自动监控告警的示例

查看手机助手,消息已经收到!

Python实现个人微信号自动监控告警的示例

监控80端口,自动告警

若是web服务也是80端口,请先登录成功后,如下操作。

shell脚本

[root@server1 ~]# cat /var/www/html/check_80.sh 
#!/bin/sh

x=$(netstat -antlp | grep '\<80\>'|awk -F' ' '{print $4}'|awk -F: '{print $2}')

if [ "$x" != 80 ];then
 python /var/www/html/check_80.py &
else
 python /var/www/html/check01_80.py &
fi
##添加执行权限
[root@server1 ~]# chomd +x /var/www/html/check_80.sh
[root@server1 ~]# cat /var/www/html/check01_80.py 
#!/bin/sh/env python
#coding:utf-8

from wxpy import *
from wechat_sender import *
from wechat_sender import Sender

bot = Bot(qr_path="qr.png",cache_path=True)
##通过文件助手给登录的微信号发消息
bot.file_helper.send('port 80 nice!')
[root@server1 ~]# cat /var/www/html/check_80.py 
#!/bin/sh/env python
#coding:utf-8

from wxpy import *
from wechat_sender import *
from wechat_sender import Sender

bot = Bot(qr_path="qr.png",cache_path=True)
##通过文件助手给登录的微信号发消息
bot.file_helper.send('port 80 error!')
[root@server1 ~]# cat /mnt/check.sh 
#!/bin/sh

cd /var/www/html
sh check_80.sh
[root@server1 ~]# chmod +x /mnt/check.sh

测试脚本

1.httpd 服务开启时,端口 80 存在

[root@server1 ~]# sh /mnt/check.sh

Python实现个人微信号自动监控告警的示例

2.httpd 服务关闭后,端口 80 不存在

[root@server1 ~]# systemctl stop httpd
[root@server1 ~]# sh /mnt/check.sh

Python实现个人微信号自动监控告警的示例

3.httpd 服务再次开启,端口 80 存在

[root@server1 ~]# systemctl start httpd
[root@server1 ~]# sh /mnt/check.sh

Python实现个人微信号自动监控告警的示例

添加任务计划自动监控进行告警

[root@server1 ~]# crontab -e
* 1 * * * sh /mnt/check.sh

投入使用

添加任务计划后,妥善修改脚本,避免频繁告警。

以上这篇Python实现个人微信号自动监控告警的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 提取dict转换为xml/json/table并输出的实现代码
Aug 28 Python
Python如何实现守护进程的方法示例
Feb 08 Python
详解python3中socket套接字的编码问题解决
Jul 01 Python
Flask和Django框架中自定义模型类的表名、父类相关问题分析
Jul 19 Python
python+numpy+matplotalib实现梯度下降法
Aug 31 Python
浅谈python中拼接路径os.path.join斜杠的问题
Oct 23 Python
python+pyqt5实现图片批量缩放工具
Mar 18 Python
Python3.5装饰器典型案例分析
Apr 30 Python
如何更优雅地写python代码
Jul 02 Python
对Python 中矩阵或者数组相减的法则详解
Aug 26 Python
python argparse传入布尔参数false不生效的解决
Apr 20 Python
Ubuntu中配置TensorFlow使用环境的方法
Apr 21 Python
python pandas模块基础学习详解
Jul 03 #Python
python将excel转换为csv的代码方法总结
Jul 03 #Python
pandas实现to_sql将DataFrame保存到数据库中
Jul 03 #Python
python实现控制COM口的示例
Jul 03 #Python
python pandas时序处理相关功能详解
Jul 03 #Python
在linux下实现 python 监控usb设备信号
Jul 03 #Python
django-allauth入门学习和使用详解
Jul 03 #Python
You might like
php桥接模式应用案例分析
2019/10/23 PHP
JavaScript CSS 修改学习第四章 透明度设置
2010/02/19 Javascript
JSON.parse 解析字符串出错的解决方法
2010/07/08 Javascript
JS实现随机数生成算法示例代码
2013/08/08 Javascript
JS控制日期显示的小例子
2013/11/23 Javascript
借助javascript代码判断网页是静态还是伪静态
2014/05/05 Javascript
AngularJS优雅的自定义指令
2016/07/01 Javascript
微信小程序 高德地图SDK详解及简单实例(源码下载)
2017/01/11 Javascript
jQuery实现获取h1-h6标题元素值的方法
2017/03/06 Javascript
easyui简介_动力节点Java学院整理
2017/07/14 Javascript
es6 filter() 数组过滤方法总结
2019/04/03 Javascript
浅析js实现网页截图的两种方式
2019/11/01 Javascript
40行代码把Vue3的响应式集成进React做状态管理
2020/05/20 Javascript
JavaScript中CreateTextFile函数
2020/08/30 Javascript
JS如何生成动态列表
2020/09/22 Javascript
从表单校验看JavaScript策略模式的使用详解
2020/10/17 Javascript
[01:00:26]Ti4主赛事胜者组第一天 EG vs NEWBEE 1
2014/07/19 DOTA
python中__call__方法示例分析
2014/10/11 Python
Python的__builtin__模块中的一些要点知识
2015/05/02 Python
Python Json模块中dumps、loads、dump、load函数介绍
2018/05/15 Python
Python实现string字符串连接的方法总结【8种方式】
2018/07/06 Python
对python requests发送json格式数据的实例详解
2018/12/19 Python
python 文本单词提取和词频统计的实例
2018/12/22 Python
python列表的逆序遍历实现
2020/04/20 Python
Pyinstaller加密打包应用的示例代码
2020/06/11 Python
keras model.fit 解决validation_spilt=num 的问题
2020/06/19 Python
CSS 说明横向进度条最后显示文字的实现代码
2020/11/10 HTML / CSS
详解快速开发基于 HTML5 网络拓扑图应用
2018/01/08 HTML / CSS
销售所有的狗狗产品:Dog.com
2016/10/13 全球购物
春秋航空官方网站:Spring Airlines
2017/09/27 全球购物
施华洛世奇中国官网:SWAROVSKI中国
2020/06/16 全球购物
酒吧总经理岗位职责
2013/12/10 职场文书
我们的节日中秋节活动总结
2015/03/23 职场文书
2015年前台文员工作总结
2015/05/18 职场文书
「天才王子的赤字国家重生术」妮妮姆·拉雷粘土人开订
2022/03/21 日漫
MySQL 执行数据库更新update操作的时候数据库卡死了
2022/05/02 MySQL