使用python3调用wxpy模块监控linux日志并定时发送消息给群组或好友


Posted in Python onJune 05, 2019

使用python3调用wxpy模块,监控linux日志并定时发送消息给群组或好友,具体代码如下所示:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
import subprocess
import time
from PIL import Image, ImageDraw, ImageFont
from apscheduler.schedulers.blocking import BlockingScheduler
#cache_path=true 表示登陆一次之后,进行缓存,下次登陆只需要手机确认
bot = Bot(console_qr=2,cache_path=True)
#获取topic是否消费延迟
def get_Lag():
  text=""
  p = subprocess.Popen('kafka-consumer-offset-checker --zookeeper 192.168.1.116 --group t_sync --topic SYNC_DATABASE_UPDATE', shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True) #universal_newlines=True,表示输出为字符串,默认是byte
  while True:
    line =p.stdout.readline()
    if not line:
      break
    else:
      text+=line + '\n'
  return text
def get_news():
  text=[]
  count=0
  t=True
  p = subprocess.Popen('tail -F /home/hadoop/da.txt', shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
  while True:
    line =str(p.stdout.readline())
    if not line:
      break
    elif "send data" in line and t:
      for item in line.split(','):
        count +=1
        if count <= 200:
           text.append(item)
      break
  return text
def get_context():
  result=""
  word=get_news()
  for i in range(len(word)):
    if(i % 4 ==0):
      result= result+word[i]+"\n"
    else:
      result= result+word[i]+"  "
  return result
def send_image():
  try:
    lags=get_Lag()
    print(lags)
    content=get_context()
#发送消费延迟的数据
    lagImage= Image.new('RGB', (1000, 600),(255,255,255))
    draw = ImageDraw.Draw(lagImage)
    font = ImageFont.truetype("/usr/share/fonts/cjkuni-ukai/ukai.ttc", 18, encoding="unic")
    draw.text((10, 10), lags, 'black', font)
    lagImage.save('/home/hadoop/lags.jpg')
#发送日志消息
    image= Image.new('RGB', (1000, 810),(255,255,255))
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("/usr/share/fonts/cjkuni-ukai/ukai.ttc", 18, encoding="unic") #ukai.ttc 字体
    draw.text((10, 10), content, 'black', font)
    image.save('/home/hadoop/123.jpg')
 
 #发送群组
    group = bot.groups().search("大数据小组")[0]
    group.send_image('/home/hadoop/123.jpg')
 #发送好友
    my_friend = bot.friends().search(u'涛')[0]
    my_friend.send_image('/home/hadoop/lags.jpg')
    my_friend.send_image('/home/hadoop/123.jpg')
    # t = Timer(100, send_image)
    #t.start()
  except:
    my_friend.send(u"今天消息发送失败了")
if __name__ == "__main__":
  #send_image()
  scheduler = BlockingScheduler()
  scheduler.add_job(send_image, 'cron', hour='15', minute='01')
  scheduler.add_job(send_image, 'cron', hour='14', minute='58')
  scheduler.start()

总结

以上所述是小编给大家介绍的使用python3调用wxpy模块监控linux日志并定时发送消息给群组或好友,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python 文件操作技巧(File operation) 实例代码分析
Aug 11 Python
Python使用代理抓取网站图片(多线程)
Mar 14 Python
使用Python脚本将Bing的每日图片作为桌面的教程
May 04 Python
Python字符串处理之count()方法的使用
May 18 Python
Python selenium 父子、兄弟、相邻节点定位方式详解
Sep 15 Python
virtualenv实现多个版本Python共存
Aug 21 Python
对pandas的算术运算和数据对齐实例详解
Dec 22 Python
Python常用模块之requests模块用法分析
May 15 Python
Python3常见函数range()用法详解
Dec 30 Python
python 元组的使用方法
Jun 09 Python
python 实现德洛内三角剖分的操作
Apr 22 Python
关于的python五子棋的算法
May 02 Python
python3下载抖音视频的完整代码
Jun 05 #Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
Jun 04 #Python
Django集成搜索引擎Elasticserach的方法示例
Jun 04 #Python
python添加菜单图文讲解
Jun 04 #Python
Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解
Jun 04 #Python
Python自动化之数据驱动让你的脚本简洁10倍【推荐】
Jun 04 #Python
pandas DataFrame索引行列的实现
Jun 04 #Python
You might like
php中单个数据库字段多列显示(单字段分页、横向输出)
2014/07/28 PHP
jQuery向下滚动即时加载内容实现的瀑布流效果
2016/01/07 PHP
Apache无法自动跳转却显示目录的解决方法
2020/11/30 PHP
PHP基于面向对象实现的留言本功能实例
2018/04/04 PHP
List Installed Hot Fixes
2007/06/12 Javascript
asp.net+js 实现无刷新上传解析csv文件的代码
2010/05/17 Javascript
jQuery EasyUI API 中文文档 - Spinner微调器使用
2011/10/21 Javascript
在js(jquery)中获得文本框焦点和失去焦点的方法
2012/12/04 Javascript
Html5的placeholder属性(IE兼容)实现代码
2014/08/30 Javascript
js实现带缓冲效果的仿QQ面板折叠菜单代码
2015/09/06 Javascript
JQuery.validate在ie8下不支持的快速解决方法
2016/05/18 Javascript
微信小程序 数组中的push与concat的区别
2017/01/05 Javascript
Angular 输入框实现自定义验证功能
2017/02/19 Javascript
Vue的Class与Style绑定的方法
2017/09/01 Javascript
浅谈vue项目打包优化策略
2018/09/29 Javascript
微信小程序实现搜索功能并跳转搜索结果页面
2019/05/18 Javascript
JS去除字符串最后的逗号实例分析【四种方法】
2019/06/20 Javascript
在Vue中使用this.$store或者是$route一直报错的解决
2019/11/08 Javascript
urllib2自定义opener详解
2014/02/07 Python
Python打包可执行文件的方法详解
2016/09/19 Python
python使用mysql数据库示例代码
2017/05/21 Python
Python日志模块logging基本用法分析
2018/08/23 Python
Python实现清理微信僵尸粉功能示例【基于itchat模块】
2020/05/29 Python
python中random.randint和random.randrange的区别详解
2020/09/20 Python
canvas 基础之图像处理的使用
2020/04/10 HTML / CSS
联想中国官方商城:Lenovo China
2017/10/18 全球购物
英国皇室御用百货:福南梅森(Fortnum & Mason)
2017/12/03 全球购物
Eyeko美国:屡获殊荣的睫毛膏、眼线笔和眉妆
2018/07/05 全球购物
大学生农村教师实习自我鉴定
2013/09/21 职场文书
高一化学教学反思
2014/02/05 职场文书
老公爱的承诺书
2014/03/31 职场文书
三八妇女节活动总结
2014/05/04 职场文书
2014年党员自我剖析材料
2014/10/07 职场文书
2015年人事专员工作总结
2015/04/29 职场文书
爱国主题班会教案
2015/08/14 职场文书
浅谈音视频 pts dts基本概念及理解
2022/08/05 数码科技