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

使用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日志并定时发送消息给群组或好友,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • python实现监控linux性能及进程消耗性能的方法

    本文以实例形式实现了python监控linux性能以及进程消耗性能的方法,具体实现代码如下: # -*- coding: utf-8 -*- """ Created on Tue Jun 10 10:20:13 2014 @author: lifeix """ from collections import OrderedDict import time import os def cpuinfo(): lines = open('/proc/s

  • Linux下用Python脚本监控目录变化代码分享

    #!/usr/bin/env python #coding=utf-8 import os from pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE,IN_MODIFY wm = WatchManager() mask = IN_DELETE | IN_CREATE |IN_MODIFY # watched events class PFilePath(ProcessEvent): def p

  • 使用Python脚本对Linux服务器进行监控的教程

    目前 Linux 下有一些使用 Python 语言编写的 Linux 系统监控工具 比如 inotify-sync(文件系统安全监控软件).glances(资源监控工具)在实际工作中,Linux 系统管理员可以根据自己使用的服务器的具体情况编写一下简单实用的脚本实现对 Linux 服务器的监控. 本文介绍一下使用 Python 脚本实现对 Linux 服务器 CPU 内存 网络的监控脚本的编写. Python 版本说明 Python 是由 Guido van Rossum 开发的.可免费获得的.

  • python监控linux内存并写入mongodb(推荐)

    (需要安装psutil 用来获取服务器资源,以及pymongo驱动)#pip install psutil #pip install pymongo #vim memory_monitory.py 文件内容如下 #!/usr/bin/env python # -*- coding: UTF-8 -*- import psutil import socket import time from pymongo import MongoClient mongodbIp = '192.168.200.1

  • linux系统使用python监控apache服务器进程脚本分享

    crtrl.py监控Apache服务器进程的Python 脚本 复制代码 代码如下: !/usr/bin/env Python import os, sys, time while True: time.sleep(4) try: ret = os.popen('ps -C apache -o pid,cmd').readlines() if len(ret) < 2: print "apache 进程异常退出, 4 秒后重新启动" time.sleep(3) os.system

  • Linux 发邮件磁盘空间监控(python)

    核心代码: #!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib import os import commands,time from email.mime.text import MIMEText #from email import MIMEText disk_free=os.popen('df -lh') list_disk=disk_free.read() mailto_list=["2880329185@qq.com&quo

  • Python wxpython模块响应鼠标拖动事件操作示例

    本文实例讲述了Python wxpython模块响应鼠标拖动事件操作.分享给大家供大家参考,具体如下: wxpython鼠标拖动事件小案例: #coding:UTF-8 import wx app = wx.App() def dragEVT(event): if event.ButtonDown(): panel1.SetPosition(event.GetPosition()) elif event.Dragging(): panel1.SetPosition(event.GetPositi

  • python和shell监控linux服务器的详细代码

    本文实例为大家分享了python和shell监控linux服务器的具体代码,供大家参考,具体内容如下 1. shell监控负载 监控原理:使用uptime来获取负载的信息,然后通过字符串截取的方式来获取load值来获取单个核心的负载,在将负载与阈值比较确定是否报警. loard_monitor.sh脚本: #!/bin/bash #使用uptime命令监控linux系统负载变化 #提取本服务器的IP地址信息 IP=`ifconfig eth0 | grep "inet addr" |

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

    使用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 Ima

  • python3+pyqt5+itchat微信定时发送消息的方法

    编这个程序是想过节过年,一些重要的纪念日,给亲戚好友发祝福之类的,但要凌晨0点才显得比较有诚意,可我又比较贪睡,常常忘了,所以就有了编个微信定时发送消息小程序. 运行环境: python 3.x,不支持python2 准备工作 由于我用到了微信的接口,所以引入itchat 界面用了pyqt5 安装命令如下: pip install PyQt5 pip install itchat 代码部分 # -*- coding: utf-8 -*- # @Time : 2018/9/25 11:06 # @

  • python3中超级好用的日志模块-loguru模块使用详解

    目录 一. 使用logging模块时 二. loguru模块的基础使用 三. logurr详细使用 3.1 add 方法的定义 3.2 基本参数 3.3 删除 sink 3.4 rotation 配置 3.5 retention 配置 3.6 compression 配置 3.7 字符串格式化 3.8 Traceback 记录 一. 使用logging模块时 用python写代码时,logging模块最基本的几行配置,如下: import logging logging.basicConfig(

  • python3通过subprocess模块调用脚本并和脚本交互的操作

    因工作需要,需实现如题所示功能.查阅网上博客,资料,大多都是针对python2的,而且很多地方不明所以,所以自己整理了一下查阅的结果,重新写一篇博客. 预备知识 1.python3的默认字符串类型 Python 2.x 同时支持ASCII和 Unicode字符串,默认情况下是ASCII编码.而 Python 3中这种支持刚好调换:Unicode现在变成了默认类型,而 ASCII 字符串现在称为 bytes. bytes 数据结构包含字节值,并且它 不应该再被视为一个字符串,因为它是一个包含数据的

  • Python利用watchdog模块监控文件变化

    目录 1.准备 2.基本使用 3.监控文件变化 假设现在有一个应用场景,需要对文件系统进行监控,发生变化时产生日志,对新增的文件做一些相应的操作. 比如说应用到我们之前的音乐高潮提取器:若当前文件夹下增加了一个音乐文件,监控器就调用音乐高潮提取器,自动提取该音乐文件的高潮部分. 这样的监控器写起来也不难,但是很花时间,有许多情况要考虑.不过幸好我们是写Python的,有许多轮子可以使用,本文介绍的就是一个名为 watchdog 的模块,它能帮助我们实现上述功能. 1.准备 开始之前,你要确保Py

  • 基于python调用psutil模块过程解析

    这篇文章主要介绍了基于python调用psutils模块过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 用Python来编写脚本简化日常的运维工作是Python的一个重要用途.在Linux下,有许多系统命令可以让我们时刻监控系统运行的状态,如ps,top,free等等.要获取这些系统信息,Python可以通过subprocess模块调用并获取结果.但这样做显得很麻烦,尤其是要写很多解析代码. 在Python中获取系统信息的另一个好办法是

  • python实时监控logstash日志代码

    实时读取logstash日志,有异常错误keywork即触发报警. # /usr/bin/env python3 # -*- coding: utf-8 -*- # __author__ = caozhi # create_time 2018-11-12,update_time 2018-11-15 # version = 1.0 # 录像高可用报警 # 1 读取日志 使用游标移动 # 2 线上业务日志文件会切割,切割后,读取上一个切割的日志 import os import sys impor

  • python3调用c语言代码的全过程记录

    开发环境 linux: python3.5.2 + ubuntu-gnome-16.04-desktop-amd64 windows:cygwin + powershell + python3.6 胶水语言python为什么要调用c代码? c与python对比python不擅长"大量运算"任务,python程序优势在于:编写简单,适合"IO密集型作业"(比如打开文件,下载图片,运行脚本).python作为知名的"胶水语言",快速实现"计

  • Centos安装python3与scapy模块的问题及解决方法

    目录 一.CentOS安装Python3 二.安装scapy模块 一.CentOS安装Python3 centos7自带版本是python2.7 如果要用的3.0以上的版本需要手动安装,下载地址:https://www.python.org/ftp/python/ 1.先查看系统python的位置在哪儿 whereis python python2.7默认安装是在 /usr/bin目录中,切换到/usr/bin/ cd /usr/bin/ ll python* 从下面的图中我们可以看到,pyth

  • shell脚本监控linux系统内存使用情况的方法(不使用nagios监控linux)

    一.安装linux下面的一个邮件客户端msmtp软件(类似于一个foxmail的工具) 1.下载安装: 复制代码 代码如下: # tar jxvf msmtp-1.4.16.tar.bz2# cd msmtp-1.4.16# ./configure --prefix=/usr/local/msmtp# make# make install 2.创建msmtp配置文件和日志文件(host为邮件域名,邮件用户名test,密码123456) 复制代码 代码如下: # vim ~/.msmtprcacc

随机推荐