monitor multiple sites

This commit is contained in:
ClF3 2024-01-24 00:47:09 +08:00
parent ff4659b1c1
commit 2d11336736
1 changed files with 62 additions and 8 deletions

70
main.py
View File

@ -4,7 +4,10 @@ import urllib.request
from datetime import datetime from datetime import datetime
import time import time
recent=[] recent=[]
history=[] blog=[]
git=[]
cloud=[]
code=[]
width=720 width=720
height=50 height=50
red=(0x00,0x00,0xff) red=(0x00,0x00,0xff)
@ -12,7 +15,8 @@ orange=(0x00,0x66,0xff)
yellow=(0x00,0xcc,0xff) yellow=(0x00,0xcc,0xff)
light_green=(0x33,0xff,0xcc) light_green=(0x33,0xff,0xcc)
green=(0x00,0xcc,0x00) green=(0x00,0xcc,0x00)
def update_history(): imgpath="/tmp/uptime-graph/"
def update_history(history):
average=0 average=0
for i in recent: for i in recent:
average+=i average+=i
@ -24,7 +28,7 @@ def fill(img,pos,color):
for i in range(5): for i in range(5):
for j in range(height): for j in range(height):
img[j,i+5*pos,:]=[color[0]%256,color[1]%256,color[2]%256] img[j,i+5*pos,:]=[color[0]%256,color[1]%256,color[2]%256]
def update_graph(): def update_graph(filename,history):
img=np.ones([height,width,3],dtype=np.uint8) img=np.ones([height,width,3],dtype=np.uint8)
for i,delay in enumerate(history): for i,delay in enumerate(history):
if delay>=5000: if delay>=5000:
@ -37,7 +41,7 @@ def update_graph():
fill(img,i,light_green) fill(img,i,light_green)
else: else:
fill(img,i,green) fill(img,i,green)
cv2.imwrite("/tmp/uptime-graph/image.png", img) cv2.imwrite(imgpath+filename, img)
@ -58,7 +62,7 @@ if __name__=="__main__":
while True: while True:
if datetime.now().second==0 and datetime.now().minute!=lastminute: if datetime.now().second==0 and datetime.now().minute!=lastminute:
# if True: # if True:
time.sleep(0.5) time.sleep(1)
(code,delay)=testConnect("https://blog.clf3.org") (code,delay)=testConnect("https://blog.clf3.org")
print(code,end=" ") print(code,end=" ")
if code/100<=3: if code/100<=3:
@ -68,8 +72,58 @@ if __name__=="__main__":
if len(recent)<10: if len(recent)<10:
recent.append(delay) recent.append(delay)
else: else:
update_history() update_history(blog)
update_graph() update_graph("blog.png",blog)
recent=[] recent=[]
recent.append(delay) recent.append(delay)
lastminute=datetime.now().minute
elif datetime.now().second==10 and datetime.now().minute!=lastminute:
# if True:
time.sleep(1)
(code,delay)=testConnect("https://cloud.clf3.org")
print(code,end=" ")
if code/100<=3:
print("connected",delay,"ms")
else:
print("error")
if len(recent)<10:
recent.append(delay)
else:
update_history(cloud)
update_graph("cloud.png",cloud)
recent=[]
recent.append(delay)
elif datetime.now().second==20 and datetime.now().minute!=lastminute:
# if True:
time.sleep(1)
(code,delay)=testConnect("https://code.clf3.org")
print(code,end=" ")
if code/100<=3:
print("connected",delay,"ms")
else:
print("error")
if len(recent)<10:
recent.append(delay)
else:
update_history(code)
update_graph("code.png",code)
recent=[]
recent.append(delay)
elif datetime.now().second==30 and datetime.now().minute!=lastminute:
# if True:
time.sleep(1)
(code,delay)=testConnect("https://git.clf3.org")
print(code,end=" ")
if code/100<=3:
print("connected",delay,"ms")
else:
print("error")
if len(recent)<10:
recent.append(delay)
else:
update_history(git)
update_graph("git.png",git)
recent=[]
recent.append(delay)