要做什么    

1,向指定URL发送请求: requests
2,将状态码保存在redis数据库中 : redis
3,定时启动 : celerybeat
4,返回给前端,以便生成图表: flask
5,注意有时需要附加头部信息: HTTPAuthBasic
6,一次性返回最近24小时数据


程序结构:
demo
├── celerybeat.pid
├── celerybeat-schedule
├── demo2.py
├── make_celery.py
└── monitor.py


make_celery.py #用于帮助配置 monitor 中的 celery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from __future__ import absolute_import
from celery import Celery
import redis
def make_celery(app):
celery = Celery(app.import_name,broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self,*args,**kwargs):
with app.app_context():
return TaskBase.__call__(self,*args,**kwargs)
celery.Task = ContextTask
return celery

monitor.py #监控程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#coding:utf-8
from __future__ import absolute_import
import requests
import base64
import redis
from requests.auth import HTTPBasicAuth
from celery import Celery
from flask import Flask,jsonify
from celery.schedules import crontab
from datetime import timedelta
from os import sys,path
from make_celery import make_celery
from flask_script import Manager
#每次检查间隔时间
TIME_EVERY_CHECK=10
#连接redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1)
r = redis.StrictRedis(connection_pool=pool)
#图书馆头部信息
Passlib ="2016000000:123456"
b64Vallib = base64.b64encode(Passlib)
#初始化APP
app = Flask(__name__)
url01="https://taobao.com"
url02="https://ccnubox.muxixyz.com/api/lib/login/"
#配置
app.config.update(
CELERY_BROKER_URL='redis://127.0.0.1:6379',
CELERY_RESULT_BACKEND='redis://127.0.0.1:6379/0',
#Timezone
CELERY_TIMEZONE = 'Asia/Shanghai',
#schedules
CELERYBEAT_SCHEDULE = {
'request_taobao':{
'task': 'login_xinximenhu',
'schedule': timedelta(seconds = TIME_EVERY_CHECK),
},
'login_library':{
'task':'login_lib',
'schedule':timedelta(seconds = TIME_EVERY_CHECK),
}
})
celery = make_celery(app)
#访问淘宝
@celery.task(name='request_taobao')
def request_taobao():
resp01 = requests.get(url01)
statu01 = resp01.status_code
r.set(url01,statu01)
#登录CCNU图书馆
@celery.task(name='login_lib')
def login_lib():
resp02= requests.get(url02,headers = {"Authorization": "Basic %s" %b64Vallib})
statu02 = resp02.status_code
r.set(url02,statu02)
@app.route("/")
def index():
return jsonify({
url01:r.get(url01),
url02:r.get(url02)
})
if __name__ =='__main__':
app.run(debug=True)

demo2.py #一次返回最近XXX时间内的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#coding:utf-8
from __future__ import absolute_import
import requests
import base64
import redis
from requests.auth import HTTPBasicAuth
from celery import Celery
from flask import Flask,jsonify
from celery.schedules import crontab
from datetime import timedelta
from os import sys,path
from make_celery import make_celery
from flask_script import Manager
#每次检查间隔时间
TIME_EVERY_CHECK=3
#Control loop arg
i = 0
#返回数据的总数
TOTAL = 20
#redis链接池
pool01 = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1)
pool02 = redis.ConnectionPool(host='127.0.0.1', port=6379, db=2)
r01 = redis.StrictRedis(connection_pool=pool01)
r02 = redis.StrictRedis(connection_pool=pool02)
#图书馆头部信息
Passlib ="2016210942:123456"
b64Vallib = base64.b64encode(Passlib)
#初始化APP
app = Flask(__name__)
url01="https://taobao.com"
url02="https://ccnubox.muxixyz.com/api/lib/login/"
#配置
app.config.update(
CELERY_BROKER_URL='redis://127.0.0.1:6379',
CELERY_RESULT_BACKEND='redis://127.0.0.1:6379/0',
#Timezone
CELERY_TIMEZONE = 'Asia/Shanghai',
#schedules
CELERYBEAT_SCHEDULE = {
'product':{
'task': 'product',
'schedule': timedelta(seconds = TIME_EVERY_CHECK),
},
'login_library':{
'task':'login_lib',
'schedule':timedelta(seconds = TIME_EVERY_CHECK),
},
'controli':{
'task':'controli',
'schedule':timedelta(seconds = TIME_EVERY_CHECK),
}
})
app.config['JSON_AS_ASCII'] = False
celery = make_celery(app)
#木犀产品展示
@celery.task(name='product')
def product():
resp01 = requests.get(url01)
statu01 = resp01.status_code
r01.set(i,statu01)
#登录CCNU图书馆
@celery.task(name='login_lib')
def login_lib():
resp02= requests.get(url02,headers = {"Authorization": "Basic %s" %b64Vallib})
statu02 = resp02.status_code
r02.set(i,statu02)
@celery.task(name='controli')
def controli():
global i
if i < TOTAL-1:
i = i+1
elif i == TOTAL-1:
i = 0
@app.route("/")
def index():
return jsonify({
"木犀产品展示":[r01.get(k) for k in range(TOTAL)],
"登录图书馆":[r02.get(k) for k in range(TOTAL)]
})
if __name__ =='__main__':
app.run(debug=True)


程序运行步骤

1,flask project: python monitor.py runserver
2,redis: redis-server
3,celery(main process): celery worker - -app monitor.celery - -loglevel=info
4,celery(beat): celery beat - -app monitor.celery - -loglevel=info


运行效果
效果图.png

本文地址: http://Humbertzhang.github.io/2017/03/25/URL状态监控/