from __future__ import unicode_literals
import requests
import json
import subprocess
from PIL import Image, ImageDraw, ImageFont
from apscheduler.schedulers.blocking import BlockingScheduler
# 获取kafka的topic的信息
def get_Lag():
text = ""
# 使用脚本,并且转为str
p = subprocess.Popen(
'kafka-run-class kafka.admin.ConsumerGroupCommand --new-consumer --bootstrap-server kafka的ip:9092 --group web_owner_lzwl --describe',
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
while True:
line = p.stdout.readline()
if not line:
break
else:
print(line)
text += line
return text
# 监控日志,获取包含send message消息的数据,其中最主要的三个时间t0STime,t1RTime,t1STime
def get_news():
text = []
index = 0
t = True
p = subprocess.Popen('tail -F /opt/web_app/forward-service/logs/service.log', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
while True:
line = str(p.stdout.readline())
if not line:
break
elif "send message" in line and t:
for item in line.split(','):
if 't0STime' in item or 't1RTime' in item or 't1STime' in item:
index += 1
text[index] = item
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()
# 发送消费延迟的数据
# 1800 宽度,400:高度,255:底板颜色为白色
lagImage = Image.new('RGB', (1800, 400), (255, 255, 255))
draw = ImageDraw.Draw(lagImage)
# 字体设置,18字号设置
font = ImageFont.truetype("/usr/share/fonts/cjkuni-ukai/ukai.ttc", 18, encoding="unic")
draw.text((8, 8), lags, 'black', font)
# 将生成的图片保存本地
lagImage.save('/home/hadoop/lags.jpg')
url = getUrl(r'/home/hadoop/lags.jpg')
getType("topic延迟", url)
content = get_context()
print(content)
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")
draw.text((10, 10), content, 'black', font)
image.save('/home/hadoop/log.jpg')
url = getUrl(r'/home/hadoop/log.jpg')
getType("日志监控", url)
except:
print(u"今天消息发送失败了")
# 将图片发送到钉钉,access_token为钉钉机器人的标识
def getType(type, url):
webhook = "https://oapi.dingtalk.com/robot/send?access_token=钉钉机器人的access—token"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
datas = {
"msgtype": "markdown",
"markdown": {
"title": type,
"text": " ![screenshot](" + url + ")\n"
}
}
# 对请求的数据进行json封装
message_json = json.dumps(datas)
# 发送请求
info = requests.post(url=webhook, data=message_json, headers=header)
# 打印返回的结果
print(info.text)
# 将本地图片发送到免费服务器
def getUrl(picUrl):
LIST_URL = "https://sm.ms/api/list"
UPLOAD_URL = "https://sm.ms/api/upload"
CLEAR_URL = "https://sm.ms/api/clear"
data = {'smfile': open(picUrl, 'rb')}
# 上传本地图片
r = requests.post(UPLOAD_URL, files=data)
# 获取图片上传后的具体信息,然后得到图片的url地址,因为图片名称是随机生成
data = r.json()
url = data.get("data").get("url")
return url
#获取免费服务器的图片列表
def get_history():
params = {'ssl': 0, 'format': 'json'}
r = requests.get("https://sm.ms/api/list", params)
print(r.json())
#清空免费服务器的图片
def clear_history():
r = requests.get("https://sm.ms/api/clear");
print(r.json())
# 设置定时任务
if __name__ == "__main__":
# send_image()
scheduler = BlockingScheduler()
scheduler.add_job(send_image, 'cron', hour='10')
scheduler.add_job(send_image, 'cron', hour='15')
scheduler.start()