使用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读取csv文件示例(python操作csv)
Mar 11 Python
python多重继承新算法C3介绍
Sep 28 Python
在Mac OS上使用mod_wsgi连接Python与Apache服务器
Dec 24 Python
python实践项目之监控当前联网状态详情
May 23 Python
Python中BeautifuSoup库的用法使用详解
Nov 15 Python
python查找特定名称文件并按序号、文件名分行打印输出的方法
Apr 24 Python
Spring @Enable模块驱动原理及使用实例
Jun 23 Python
Python爬虫之Spider类用法简单介绍
Aug 04 Python
Python读取xlsx数据生成图标代码实例
Aug 12 Python
几款好用的python工具库(小结)
Oct 20 Python
python不同版本的_new_不同点总结
Dec 09 Python
python实现定时发送邮件
Dec 23 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 empty()与isset()区别的详细介绍
2013/06/17 PHP
Javascript 学习书 推荐
2009/06/13 Javascript
Chrome中模态对话框showModalDialog返回值问题的解决方法
2010/05/25 Javascript
理解Javascript_10_对象模型
2010/10/16 Javascript
6款新颖的jQuery和CSS3进度条插件推荐
2013/03/05 Javascript
超漂亮的Bootstrap 富文本编辑器summernote
2016/04/05 Javascript
Ionic + Angular.js实现图片轮播的方法示例
2017/05/21 Javascript
js+html5实现页面可刷新的倒计时效果
2017/07/15 Javascript
JavaScript中立即执行函数实例详解
2017/11/04 Javascript
JS动态添加元素及绑定事件造成程序重复执行解决
2017/12/07 Javascript
react-router4按需加载(踩坑填坑)
2019/01/06 Javascript
JavaScript在web自动化测试中的作用示例详解
2019/08/25 Javascript
微信小程序地图绘制线段并且测量(实例代码)
2020/01/02 Javascript
JavaScript实现多个物体同时运动
2020/03/12 Javascript
vue+Element-ui实现登录注册表单
2020/11/17 Javascript
python实现一次创建多级目录的方法
2015/05/15 Python
python 环境变量和import模块导入方法(详解)
2017/07/11 Python
python中将函数赋值给变量时需要注意的一些问题
2017/08/18 Python
Python如何抓取天猫商品详细信息及交易记录
2018/02/23 Python
PyQt5每天必学之创建窗口居中效果
2018/04/19 Python
Python代码打开本地.mp4格式文件的方法
2019/01/03 Python
Python控制鼠标键盘代码实例
2020/12/08 Python
土耳其时尚购物网站:Morhipo
2017/09/04 全球购物
达拉斯牛仔官方商店:Dallas Cowboys Pro Shop
2018/02/10 全球购物
印度化妆品购物网站:Nykaa
2018/07/22 全球购物
纯净、自信、100%的羊绒服装:360Cashmere
2021/02/20 全球购物
运动会通讯稿400字
2014/01/28 职场文书
母婴店促销方案
2014/03/05 职场文书
求职信范文大全
2014/05/26 职场文书
五水共治一句话承诺
2014/05/30 职场文书
党员群众路线教育实践活动剖析材料
2014/10/10 职场文书
2015最新学生自我评价范文
2015/03/03 职场文书
2016清明节森林防火广播稿
2015/12/17 职场文书
如何在C++中调用Python
2021/05/21 Python
Python还能这么玩之用Python修改了班花的开机密码
2021/06/04 Python
Java生成日期时间存入Mysql数据库的实现方法
2022/03/03 Java/Android