Python實現(xiàn)個人微信號自動監(jiān)控告警的示例
wechat_sender 是基于 wxpy 和 tornado 實現(xiàn)的一個可以將你的網(wǎng)站、爬蟲、腳本等其他應(yīng)用中各種消息 (日志、報警、運(yùn)行結(jié)果等) 發(fā)送到微信的工具。
運(yùn)行環(huán)境
Python 2.7 及以上 Python 3 及以上
實現(xiàn)過程
安裝 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登錄微信發(fā)送消息
安裝web服務(wù)器
[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直接掃
#下面這個是服務(wù)器版(Linux)
#bot = Bot(qr_path="qr.png")
#避免重復(fù)登錄重復(fù)掃二維碼
bot = Bot(qr_path="qr.png",cache_path=True)
##通過文件助手給登錄的微信號發(fā)消息
bot.file_helper.send('Hello world!')
web登錄微信
##執(zhí)行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,微信掃一掃

微信登錄后,終端釋放,提示成功,消息同時發(fā)送,并且web二維碼失效
Login successfully as someone
微信點擊確認(rèn)

查看手機(jī)助手,消息已經(jīng)收到!

監(jiān)控80端口,自動告警
若是web服務(wù)也是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
##添加執(zhí)行權(quán)限
[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)
##通過文件助手給登錄的微信號發(fā)消息
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)
##通過文件助手給登錄的微信號發(fā)消息
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 服務(wù)開啟時,端口 80 存在
[root@server1 ~]# sh /mnt/check.sh

2.httpd 服務(wù)關(guān)閉后,端口 80 不存在
[root@server1 ~]# systemctl stop httpd
[root@server1 ~]# sh /mnt/check.sh

3.httpd 服務(wù)再次開啟,端口 80 存在
[root@server1 ~]# systemctl start httpd
[root@server1 ~]# sh /mnt/check.sh

添加任務(wù)計劃自動監(jiān)控進(jìn)行告警
[root@server1 ~]# crontab -e * 1 * * * sh /mnt/check.sh
投入使用
添加任務(wù)計劃后,妥善修改腳本,避免頻繁告警。
以上這篇Python實現(xiàn)個人微信號自動監(jiān)控告警的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Plotly Dash進(jìn)行儀表板設(shè)計的步驟和技巧
Plotly Dash 是一個基于 Python 的開源框架,可以幫助你快速而靈活地構(gòu)建交互式儀表板,本文將介紹使用 Plotly Dash 創(chuàng)建儀表板的步驟和一些技巧,并附上代碼實例來演示每個步驟,需要的朋友可以參考下2024-05-05
python curl2pyreqs 生成接口腳本實戰(zhàn)教程
這篇文章主要介紹了python curl2pyreqs 生成接口腳本實戰(zhàn)教程,首先下載 curl2pyreqs 庫,打開調(diào)試模式,在Network這里獲取接口的cURL,需要的朋友可以參考下2023-10-10
Python 用Redis簡單實現(xiàn)分布式爬蟲的方法
本篇文章主要介紹了Python 用Redis簡單實現(xiàn)分布式爬蟲的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

