经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
使用python3调用wxpy模块监控linux日志并定时发送消息给群组或好友
来源:jb51  时间:2019/6/5 11:40:37  对本文有异议

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

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from __future__ import unicode_literals
  4. from threading import Timer
  5. from wxpy import *
  6. import requests
  7. import subprocess
  8. import time
  9. from PIL import Image, ImageDraw, ImageFont
  10. from apscheduler.schedulers.blocking import BlockingScheduler
  11. #cache_path=true 表示登陆一次之后,进行缓存,下次登陆只需要手机确认
  12. bot = Bot(console_qr=2,cache_path=True)
  13. #获取topic是否消费延迟
  14. def get_Lag():
  15. text=""
  16. 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
  17. while True:
  18. line =p.stdout.readline()
  19. if not line:
  20. break
  21. else:
  22. text+=line + '\n'
  23. return text
  24. def get_news():
  25. text=[]
  26. count=0
  27. t=True
  28. p = subprocess.Popen('tail -F /home/hadoop/da.txt', shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
  29. while True:
  30. line =str(p.stdout.readline())
  31. if not line:
  32. break
  33. elif "send data" in line and t:
  34. for item in line.split(','):
  35. count +=1
  36. if count <= 200:
  37. text.append(item)
  38. break
  39. return text
  40. def get_context():
  41. result=""
  42. word=get_news()
  43. for i in range(len(word)):
  44. if(i % 4 ==0):
  45. result= result+word[i]+"\n"
  46. else:
  47. result= result+word[i]+" "
  48. return result
  49. def send_image():
  50. try:
  51. lags=get_Lag()
  52. print(lags)
  53. content=get_context()
  54. #发送消费延迟的数据
  55. lagImage= Image.new('RGB', (1000, 600),(255,255,255))
  56. draw = ImageDraw.Draw(lagImage)
  57. font = ImageFont.truetype("/usr/share/fonts/cjkuni-ukai/ukai.ttc", 18, encoding="unic")
  58. draw.text((10, 10), lags, 'black', font)
  59. lagImage.save('/home/hadoop/lags.jpg')
  60. #发送日志消息
  61. image= Image.new('RGB', (1000, 810),(255,255,255))
  62. draw = ImageDraw.Draw(image)
  63. font = ImageFont.truetype("/usr/share/fonts/cjkuni-ukai/ukai.ttc", 18, encoding="unic") #ukai.ttc 字体
  64. draw.text((10, 10), content, 'black', font)
  65. image.save('/home/hadoop/123.jpg')
  66. #发送群组
  67. group = bot.groups().search("大数据小组")[0]
  68. group.send_image('/home/hadoop/123.jpg')
  69. #发送好友
  70. my_friend = bot.friends().search(u'涛')[0]
  71. my_friend.send_image('/home/hadoop/lags.jpg')
  72. my_friend.send_image('/home/hadoop/123.jpg')
  73. # t = Timer(100, send_image)
  74. #t.start()
  75. except:
  76. my_friend.send(u"今天消息发送失败了")
  77. if __name__ == "__main__":
  78. #send_image()
  79. scheduler = BlockingScheduler()
  80. scheduler.add_job(send_image, 'cron', hour='15', minute='01')
  81. scheduler.add_job(send_image, 'cron', hour='14', minute='58')
  82. scheduler.start()

总结

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号