使用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 除法小技巧
Sep 06 Python
python在Windows8下获取本机ip地址的方法
Mar 14 Python
Python Numpy 数组的初始化和基本操作
Mar 13 Python
Php多进程实现代码
May 07 Python
Windows系统下PhantomJS的安装和基本用法
Oct 21 Python
带你认识Django
Jan 15 Python
django query模块
Apr 20 Python
详解PyCharm+QTDesigner+PyUIC使用教程
Jun 13 Python
用python中的matplotlib绘制方程图像代码
Nov 21 Python
python实现实时视频流播放代码实例
Jan 11 Python
python实现输入三角形边长自动作图求面积案例
Apr 12 Python
Pycharm Plugins加载失败问题解决方案
Nov 28 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数组中删除元素的实现代码
2012/06/22 PHP
php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】
2016/12/07 PHP
PHP const定义常量及global定义全局常量实例解析
2020/05/28 PHP
jQuery 遍历json数组的实现代码
2020/09/22 Javascript
javascript动态添加表格数据行(ASP后台数据库保存例子)
2010/05/08 Javascript
基于JQuery模仿苹果桌面的Dock效果(初级版)
2012/10/15 Javascript
JS控制阿拉伯数字转为中文大写示例代码
2013/09/04 Javascript
Seajs的学习笔记
2014/03/04 Javascript
全面解析Bootstrap表单使用方法(表单控件)
2015/11/24 Javascript
JQuery移动页面开发之屏幕方向改变与滚屏的实现
2015/12/03 Javascript
每日十条JavaScript经验技巧(二)
2016/06/23 Javascript
关于Sequelize连接查询时inlude中model和association的区别详解
2017/02/27 Javascript
node.js将MongoDB数据同步到MySQL的步骤
2017/12/10 Javascript
用Axios Element实现全局的请求loading的方法
2018/03/15 Javascript
使用D3.js+Vue实现一个简单的柱形图
2018/08/05 Javascript
vue实现跳转接口push 转场动画示例
2019/11/01 Javascript
微信小程序静默登录的实现代码
2020/01/08 Javascript
如何利用Node.js与JSON搭建简单的动态服务器
2020/06/16 Javascript
JavaScript中如何调用Java方法
2020/09/16 Javascript
Python格式化css文件的方法
2015/03/10 Python
web.py在SAE中的Session问题解决方法(使用mysql存储)
2015/06/24 Python
详解Python编程中time模块的使用
2015/11/20 Python
python Pandas库基础分析之时间序列的处理详解
2019/07/13 Python
对Python函数设计规范详解
2019/07/19 Python
将python包发布到PyPI和制作whl文件方式
2019/12/25 Python
PyTorch在Windows环境搭建的方法步骤
2020/05/12 Python
Python下划线5种含义代码实例解析
2020/07/10 Python
全网最全python库selenium自动化使用详细教程
2021/01/12 Python
html5 兼容IE6结构的实现代码
2012/05/14 HTML / CSS
Farfetch阿联酋:奢侈品牌时尚购物平台
2019/07/26 全球购物
Hotels.com越南:酒店预订
2019/10/29 全球购物
应届毕业生个人求职信范文
2014/01/29 职场文书
2014年开学第一课活动方案
2014/03/06 职场文书
会计毕业生自荐书
2014/06/12 职场文书
干部作风纪律整顿心得体会
2016/01/23 职场文书
一文搞懂MySQL索引页结构
2022/02/28 MySQL