uptime-monitor/main.py

134 lines
3.9 KiB
Python
Raw Normal View History

2024-01-22 07:58:22 +00:00
import cv2
import numpy as np
import urllib.request
from datetime import datetime
2024-01-22 08:25:58 +00:00
import time
2024-01-23 18:57:34 +00:00
import os
2024-01-23 17:09:25 +00:00
recent_blog=[]
recent_cloud=[]
recent_code=[]
recent_git=[]
2024-01-23 16:47:09 +00:00
blog=[]
cloud=[]
code=[]
2024-01-23 17:09:25 +00:00
git=[]
2024-01-22 07:58:22 +00:00
width=720
2024-01-22 08:01:09 +00:00
height=50
2024-01-22 07:58:22 +00:00
red=(0x00,0x00,0xff)
orange=(0x00,0x66,0xff)
yellow=(0x00,0xcc,0xff)
light_green=(0x33,0xff,0xcc)
green=(0x00,0xcc,0x00)
2024-01-23 16:47:09 +00:00
imgpath="/tmp/uptime-graph/"
2024-01-23 18:57:34 +00:00
2024-01-23 17:21:32 +00:00
def update_history(history,recent):
2024-01-22 07:58:22 +00:00
average=0
for i in recent:
average+=i
average/=len(recent)
if len(history)>=144:
del(history[0])
history.append(average)
def fill(img,pos,color):
for i in range(5):
2024-01-22 08:01:09 +00:00
for j in range(height):
2024-01-22 07:58:22 +00:00
img[j,i+5*pos,:]=[color[0]%256,color[1]%256,color[2]%256]
2024-01-23 16:47:09 +00:00
def update_graph(filename,history):
2024-01-22 07:58:22 +00:00
img=np.ones([height,width,3],dtype=np.uint8)
for i,delay in enumerate(history):
if delay>=5000:
fill(img,i,red)
2024-01-22 13:35:10 +00:00
elif delay>=2000:
2024-01-23 03:56:30 +00:00
fill(img,i,orange)
2024-01-22 13:35:10 +00:00
elif delay>=1000:
2024-01-23 03:56:30 +00:00
fill(img,i,yellow)
elif delay>=500:
2024-01-22 07:58:22 +00:00
fill(img,i,light_green)
else:
fill(img,i,green)
2024-01-23 16:47:09 +00:00
cv2.imwrite(imgpath+filename, img)
2024-01-22 07:58:22 +00:00
def testConnect(url):
try:
time1=datetime.now().timestamp()
2024-01-23 17:38:34 +00:00
status_code=urllib.request.urlopen(url,timeout=5).getcode()
2024-01-22 07:58:22 +00:00
time2=datetime.now().timestamp()
delay=int((time2-time1)*1000)
except:
2024-01-23 17:38:34 +00:00
status_code=404
if status_code/100>=4:
2024-01-22 07:58:22 +00:00
delay=10000
2024-01-23 17:38:34 +00:00
return (status_code,delay)
2024-01-22 07:58:22 +00:00
if __name__=="__main__":
2024-01-23 18:57:34 +00:00
if not os.path.exists(imgpath):#如果路径不存在
os.makedirs(imgpath)
2024-01-22 07:58:22 +00:00
while True:
2024-01-23 18:57:34 +00:00
time.sleep(0.5)
if datetime.now().second==0:
2024-01-23 16:47:09 +00:00
time.sleep(1)
2024-01-23 17:38:34 +00:00
(status_code,delay)=testConnect("https://blog.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
2024-01-22 07:58:22 +00:00
print("connected",delay,"ms")
else:
print("error")
2024-01-23 17:09:25 +00:00
if len(recent_blog)<10:
recent_blog.append(delay)
2024-01-22 07:58:22 +00:00
else:
2024-01-23 17:21:32 +00:00
update_history(blog,recent_blog)
2024-01-23 16:47:09 +00:00
update_graph("blog.png",blog)
2024-01-23 17:09:25 +00:00
recent_blog=[]
recent_blog.append(delay)
2024-01-23 16:47:09 +00:00
2024-01-23 18:57:34 +00:00
elif datetime.now().second==10:
2024-01-23 16:47:09 +00:00
time.sleep(1)
2024-01-23 17:38:34 +00:00
(status_code,delay)=testConnect("https://cloud.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
2024-01-23 16:47:09 +00:00
print("connected",delay,"ms")
else:
print("error")
2024-01-23 17:09:25 +00:00
if len(recent_cloud)<10:
recent_cloud.append(delay)
2024-01-23 16:47:09 +00:00
else:
2024-01-23 17:21:32 +00:00
update_history(cloud,recent_cloud)
2024-01-23 16:47:09 +00:00
update_graph("cloud.png",cloud)
2024-01-23 17:09:25 +00:00
recent_cloud=[]
recent_cloud.append(delay)
2024-01-23 16:47:09 +00:00
2024-01-23 18:57:34 +00:00
elif datetime.now().second==20:
2024-01-23 16:47:09 +00:00
# if True:
time.sleep(1)
2024-01-23 17:38:34 +00:00
(status_code,delay)=testConnect("https://code.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
2024-01-23 16:47:09 +00:00
print("connected",delay,"ms")
else:
print("error")
2024-01-23 17:09:25 +00:00
if len(recent_code)<10:
recent_code.append(delay)
2024-01-23 16:47:09 +00:00
else:
2024-01-23 17:21:32 +00:00
update_history(code,recent_code)
2024-01-23 16:47:09 +00:00
update_graph("code.png",code)
2024-01-23 17:09:25 +00:00
recent_code=[]
recent_code.append(delay)
2024-01-23 16:47:09 +00:00
2024-01-23 18:57:34 +00:00
elif datetime.now().second==30:
2024-01-23 16:47:09 +00:00
# if True:
time.sleep(1)
2024-01-23 17:38:34 +00:00
(status_code,delay)=testConnect("https://git.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
2024-01-23 16:47:09 +00:00
print("connected",delay,"ms")
else:
print("error")
2024-01-23 17:09:25 +00:00
if len(recent_git)<10:
recent_git.append(delay)
2024-01-23 16:47:09 +00:00
else:
2024-01-23 17:21:32 +00:00
update_history(git,recent_git)
2024-01-23 16:47:09 +00:00
update_graph("git.png",git)
2024-01-23 17:09:25 +00:00
recent_git=[]
recent_git.append(delay)