使用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内置的字符串处理函数详细整理(覆盖日常所用)
Aug 19 Python
python通过wxPython打开一个音频文件并播放的方法
Mar 25 Python
Tornado协程在python2.7如何返回值(实现方法)
Jun 22 Python
对变量赋值的理解--Pyton中让两个值互换的实现方法
Nov 29 Python
Python编程求质数实例代码
Jan 31 Python
Python实现一个服务器监听多个客户端请求
Apr 12 Python
mac下pycharm设置python版本的图文教程
Jun 13 Python
详解利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击
Oct 09 Python
Django应用程序入口WSGIHandler源码解析
Aug 05 Python
Python shutil模块用法实例分析
Oct 02 Python
python求前n个阶乘的和实例
Apr 02 Python
python中的unittest框架实例详解
Feb 05 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过滤危险html代码的函数
2008/07/22 PHP
thinkPHP的Html模板标签使用方法
2012/11/13 PHP
关于二级目录拖拽排序的实现(源码示例下载)
2013/04/26 PHP
从性能方面考虑PHP下载远程文件的3种方法
2015/12/29 PHP
PHP面向对象程序设计模拟一般面向对象语言中的方法重载(overload)示例
2019/06/13 PHP
使用JQuery快速实现Tab的AJAX动态载入(实例讲解)
2013/12/11 Javascript
JS实现点击链接取消跳转效果的方法
2014/01/24 Javascript
js 针对html DOM元素操作等经验累积
2014/03/11 Javascript
jQuery的图片滑块焦点图插件整理推荐
2014/12/07 Javascript
简单实现js页面切换功能
2021/01/10 Javascript
javascript实现瀑布流动态加载图片原理
2016/08/12 Javascript
原生js实现对Ajax的封装(仿jquery)
2017/01/22 Javascript
原生JS实现垂直手风琴效果
2017/02/19 Javascript
js实现数组和对象的深浅拷贝
2017/09/30 Javascript
浅析Vue实例以及生命周期
2018/08/14 Javascript
vue v-for循环重复数据无法添加问题解决方法【加track-by='索引'】
2019/03/15 Javascript
微信小程序模板消息推送的两种实现方式
2019/08/27 Javascript
Python实现去除代码前行号的方法
2015/03/10 Python
python访问类中docstring注释的实现方法
2015/05/04 Python
浅析Python编写函数装饰器
2016/03/18 Python
Pandas:Series和DataFrame删除指定轴上数据的方法
2018/11/10 Python
python爬虫之urllib库常用方法用法总结大全
2018/11/14 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
2018/12/12 Python
使用python对文件中的单词进行提取的方法示例
2018/12/21 Python
python爬虫 批量下载zabbix文档代码实例
2019/08/21 Python
如何基于windows实现python定时爬虫
2020/05/01 Python
html5 input元素新特性_动力节点Java学院整理
2017/07/06 HTML / CSS
DHC中国官方购物网站:日本通信销售No.1化妆品
2016/08/20 全球购物
sleep()方法和wait()方法的区别是什么
2012/11/17 面试题
ktv中秋节活动方案
2014/01/30 职场文书
历史专业学生的自我评价
2014/02/28 职场文书
公司中层干部的自我评价分享
2014/03/01 职场文书
长城英文导游词
2015/01/30 职场文书
用Python实现Newton插值法
2021/04/17 Python
MYSQL主从数据库同步备份配置的方法
2021/05/26 MySQL
python实现双向链表原理
2022/05/25 Python