Python实现个人微信号自动监控告警的示例
作者:Sunny_Future 发布时间:2023-02-04 20:03:03
标签:Python,微信号,监控,告警
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
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,微信扫一扫
微信登录后,终端释放,提示成功,消息同时发送,并且web二维码失效
Login successfully as someone
微信点击确认
查看手机助手,消息已经收到!
监控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
2.httpd 服务关闭后,端口 80 不存在
[root@server1 ~]# systemctl stop httpd
[root@server1 ~]# sh /mnt/check.sh
3.httpd 服务再次开启,端口 80 存在
[root@server1 ~]# systemctl start httpd
[root@server1 ~]# sh /mnt/check.sh
添加任务计划自动监控进行告警
[root@server1 ~]# crontab -e
* 1 * * * sh /mnt/check.sh
投入使用
添加任务计划后,妥善修改脚本,避免频繁告警。
来源:https://blog.csdn.net/Sunny_Future/article/details/81545327


猜你喜欢
- QUICKSORT(A, p, r)是快速排序的子程序,调用划分程序对数组进行划分,然后递归地调用QUICKSORT(A, p, r),以完
- Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)
- 本文实例讲述了JavaScript内置对象math,global功能与用法。分享给大家供大家参考,具体如下:学习要点:1.Global对象2
- 大家好,使用 Python Flask 创建 URL 缩短器是一个有趣而简单的项目,可以帮助您深入了解 Web 开发的世界。Flask 是
- 背景:项目中有多个组件调用同一接口,为提高代码可维护性,需要封装公共方法直接return 接口调用的结果export function ge
- 使用[[v]*n]*n遇到的坑今天通过[[v]*n]*n快速创建列表, 当我使用索引修改值时, 本来是打算修改a[0][0]这一个元素的第一
- 众所周知,升级某个库(假设为 xxx),可以用pip install --upgrade xxx 命令,或者简写成pip install -
- 程序开始:<% Server.ScriptTimeout = &HE10 '&
- Python类的继承在OOP(ObjectOrientedProgramming)程序设计中,当我们定义一个class的时候,可以从某个现有
- 前言这是俺写的第一篇关于python的博客,分享一下我所整合的python中的随机数函数,就当作是自己的一个笔记,也可以供像我一样的pyth
- Python的命名空间是Python程序猿必须了解的内容,对Python命名空间的学习,将使我们在本质上掌握一些Python中的琐碎的规则。
- 1 re.search() 的作用:re.search会匹配整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None 从源
- 在学习傅里叶变换的时候遇到了求周期方波信号频谱图的例子,在书上和网上查阅了一些资料,发现大都是讨论的都是下图左边的周期信号的频谱,课程老师的
- MySQL数据库恢复到指定时间点时,我们必须通过MySQL全备+MySQL增量备份(可选)+MySQL的二进制日志(binlog)进行重放来
- Frame切换在本文中,将介绍如何使用 Frame tkraise() 方法在 Tkinter 应用程序中的Frame之间切换。1、Fram
- 代码实现:<!DOCTYPE html><html lang="en"><head>
- QQ登录Banner增加了剧情的概念之后,已经推出了春节和情人节两期。这之后设想能围绕Banner做的更加丰富,对传统文化的体现也能更为深入
- 目录概述模板模式生活案例策略模式涉及到两个角色UML总结示例概述模板方法模式定义了一个算法的步骤,并允许子类别为一个或多个步骤提供其实践方式
- 前做PPT要用到折线图,嫌弃EXCEL自带的看上去不好看,就用python写了一个画折线图的程序。import matplotlib.pyp
- 编写兼容IE和FireFox的脚本确定的件很烦人的事,今日又经历了一次。一、正式表达式问题试图用以下表达式提取中括号“]”后面的内容,连接调