Compare commits

..

11 Commits

Author SHA1 Message Date
tianyi 0a4179b056 Update README.md 2024-01-27 16:12:22 +00:00
tianyi 1b49bf39a2 Update README.md 2024-01-24 16:17:54 +00:00
ClF3 a780c8e2e3 edit 2024-01-24 02:57:34 +08:00
ClF3 df995bf9fe edit 2024-01-24 01:38:34 +08:00
ClF3 0aeb620679 edit 2024-01-24 01:21:32 +08:00
ClF3 2a80c87d22 edit 2024-01-24 01:09:25 +08:00
ClF3 2d11336736 monitor multiple sites 2024-01-24 00:47:09 +08:00
ClF3 ff4659b1c1 modified bounds 2024-01-23 11:56:30 +08:00
ClF3 c50affcc8a Merge branch 'main' of https://git.clf3.org/tianyi/uptime-monitor 2024-01-22 21:35:16 +08:00
ClF3 5188a37184 modified the bound 2024-01-22 21:35:10 +08:00
tianyi d268076141 Update README.md 2024-01-22 09:42:01 +00:00
2 changed files with 90 additions and 24 deletions

View File

@ -1,4 +1,11 @@
# uptime-monitor # uptime-monitor
A simple script to send http/https requests to your own sites to check if they are working.
example: example:
![UPtime](http://hk.clf3.org/image.png)
![blog](http://hk.clf3.org/blog.png)
![cloud](http://hk.clf3.org/cloud.png)
![code](http://hk.clf3.org/code.png)
![git](http://hk.clf3.org/git.png)

105
main.py
View File

@ -3,8 +3,15 @@ import numpy as np
import urllib.request import urllib.request
from datetime import datetime from datetime import datetime
import time import time
recent=[] import os
history=[] recent_blog=[]
recent_cloud=[]
recent_code=[]
recent_git=[]
blog=[]
cloud=[]
code=[]
git=[]
width=720 width=720
height=50 height=50
red=(0x00,0x00,0xff) red=(0x00,0x00,0xff)
@ -12,7 +19,9 @@ 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,recent):
average=0 average=0
for i in recent: for i in recent:
average+=i average+=i
@ -24,7 +33,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,39 +46,89 @@ 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)
def testConnect(url): def testConnect(url):
try: try:
time1=datetime.now().timestamp() time1=datetime.now().timestamp()
code=urllib.request.urlopen(url,timeout=5).getcode() status_code=urllib.request.urlopen(url,timeout=5).getcode()
time2=datetime.now().timestamp() time2=datetime.now().timestamp()
delay=int((time2-time1)*1000) delay=int((time2-time1)*1000)
except: except:
code=404 status_code=404
if code/100>=4: if status_code/100>=4:
delay=10000 delay=10000
return (code,delay) return (status_code,delay)
if __name__=="__main__": if __name__=="__main__":
lastminute=-1 if not os.path.exists(imgpath):#如果路径不存在
os.makedirs(imgpath)
while True: while True:
if datetime.now().second==0 and datetime.now().minute!=lastminute: time.sleep(0.5)
# if True: if datetime.now().second==0:
time.sleep(0.5) time.sleep(1)
(code,delay)=testConnect("https://blog.clf3.org") (status_code,delay)=testConnect("https://blog.clf3.org")
print(code,end=" ") print(status_code,end=" ")
if code/100<=3: if status_code/100<=3:
print("connected",delay,"ms") print("connected",delay,"ms")
else: else:
print("error") print("error")
if len(recent)<10: if len(recent_blog)<10:
recent.append(delay) recent_blog.append(delay)
else: else:
update_history() update_history(blog,recent_blog)
update_graph() update_graph("blog.png",blog)
recent=[] recent_blog=[]
recent.append(delay) recent_blog.append(delay)
lastminute=datetime.now().minute
elif datetime.now().second==10:
time.sleep(1)
(status_code,delay)=testConnect("https://cloud.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
print("connected",delay,"ms")
else:
print("error")
if len(recent_cloud)<10:
recent_cloud.append(delay)
else:
update_history(cloud,recent_cloud)
update_graph("cloud.png",cloud)
recent_cloud=[]
recent_cloud.append(delay)
elif datetime.now().second==20:
# if True:
time.sleep(1)
(status_code,delay)=testConnect("https://code.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
print("connected",delay,"ms")
else:
print("error")
if len(recent_code)<10:
recent_code.append(delay)
else:
update_history(code,recent_code)
update_graph("code.png",code)
recent_code=[]
recent_code.append(delay)
elif datetime.now().second==30:
# if True:
time.sleep(1)
(status_code,delay)=testConnect("https://git.clf3.org")
print(status_code,end=" ")
if status_code/100<=3:
print("connected",delay,"ms")
else:
print("error")
if len(recent_git)<10:
recent_git.append(delay)
else:
update_history(git,recent_git)
update_graph("git.png",git)
recent_git=[]
recent_git.append(delay)