|
@@ -0,0 +1,97 @@
|
|
|
+#!/usr/bin/python2.7
|
|
|
+#-*- coding: utf-8 -*-
|
|
|
+#auth: robert_du
|
|
|
+
|
|
|
+from flask import Flask, make_response, jsonify
|
|
|
+from flask_restful import Api, Resource, reqparse
|
|
|
+from flask_apscheduler import APScheduler
|
|
|
+from datetime import datetime,timedelta
|
|
|
+from app.logger import logs
|
|
|
+import json
|
|
|
+
|
|
|
+app = Flask(__name__)
|
|
|
+app.config['JSON_AS_ASCII'] = False
|
|
|
+app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
|
|
|
+api = Api(app)
|
|
|
+
|
|
|
+from app.zbx_tools import zabbix_api
|
|
|
+
|
|
|
+Flags = {
|
|
|
+ "novelapi": {"name": "book-api-rs", "key_": "8080"},
|
|
|
+ "novel-newmedia": {"name": "wejuan-h5", "key_": "8080"},
|
|
|
+ "esbookweb": {"name": "esbook-webview", "key_": "8080"},
|
|
|
+ "novelMtouch": {"name": "book-touchrs", "key_": "8080"},
|
|
|
+ 'promote-server': {"name": "ec-media-tengine", "key_": "8058"},
|
|
|
+ "wxmsg-sender": {"name": "ec-media-redis","key_": "8058"},
|
|
|
+ "comic-web": {"name": "comic-webrs", "key_": "8080"},
|
|
|
+ "comic-api": {"name": "comic-apirs", "key_": "8080"},
|
|
|
+ "novelMtouch": {"name": "book-touchrs", "key_": "8080"},
|
|
|
+ "query-analyzer": {"name": "qd01-tech2-qs", "key_": "9005"},
|
|
|
+ "novel-hotword": {"name": "hotword-rs", "key_": "net.tcp.port[{HOST.IP},80]"},
|
|
|
+ "novel_search_front": {"name": "hotword-rs", "key_": "net.tcp.port[{HOST.IP},8080]"},
|
|
|
+ "novelsuggestion": {"name": "search-rs", "key_": "8081"},
|
|
|
+ "novelh5": {"name": "ec-h5", "key_": "8080"},
|
|
|
+ "wapage2web": {"name": "qd2-irs", "key_": "8080"},
|
|
|
+ }
|
|
|
+
|
|
|
+parser = reqparse.RequestParser()
|
|
|
+parser.add_argument('ident', type=str, required=True, help=u'ident can not be empty!\nfor example ident=novelapi')
|
|
|
+parser.add_argument('action', type=str, required=True, help=u'action can not be empty!\nfor example action=item_disable,action=item_enable')
|
|
|
+
|
|
|
+messages = {
|
|
|
+ 'status':0,
|
|
|
+ 'message':'',
|
|
|
+ }
|
|
|
+
|
|
|
+seduler = APScheduler()
|
|
|
+seduler.init_app(app)
|
|
|
+seduler.start()
|
|
|
+logger = logs()
|
|
|
+
|
|
|
+class Index(Resource):
|
|
|
+ def get(self):
|
|
|
+ return "hello world!"
|
|
|
+
|
|
|
+def schedule(ident):
|
|
|
+ name = Flags.get(ident)['name']
|
|
|
+ search_key = Flags.get(ident)['key_']
|
|
|
+ zbx = zabbix_api()
|
|
|
+ zbx.item_enable(name,search_key)
|
|
|
+
|
|
|
+def add_task(ident):
|
|
|
+ if seduler.get_job(ident):
|
|
|
+ seduler.remove_job(ident)
|
|
|
+ else:
|
|
|
+ dt = datetime.now()+timedelta(minutes=30)
|
|
|
+ logger.info("定时任务: %s,%s,%s,%s,%s,%s" % (dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second))
|
|
|
+ seduler.add_job(id=ident,func=schedule,trigger='date',run_date=datetime(dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second),replace_existing = True,args=[ident,])
|
|
|
+
|
|
|
+class ApiInterface(Resource):
|
|
|
+ def get(self):
|
|
|
+ data= parser.parse_args()
|
|
|
+ print data['action'],data['ident']
|
|
|
+ if not Flags.has_key(data['ident']):
|
|
|
+ return jsonify({"message": "object whose ident is %s was not found." % data['ident']})
|
|
|
+ if data['action'] == 'item_disable':
|
|
|
+ name = Flags.get(data['ident'])['name']
|
|
|
+ search_key = Flags.get(data['ident'])['key_']
|
|
|
+ zbx = zabbix_api()
|
|
|
+ #return zbx.item_disable(name,search_key)
|
|
|
+ result = zbx.item_disable(name,search_key)
|
|
|
+ add_task(data['ident'])
|
|
|
+ return result
|
|
|
+ elif data['action'] == 'item_enable':
|
|
|
+ name = Flags.get(data['ident'])['name']
|
|
|
+ search_key = Flags.get(data['ident'])['key_']
|
|
|
+ if seduler.get_job(data['ident']):
|
|
|
+ seduler.remove_job(data['ident'])
|
|
|
+ zbx = zabbix_api()
|
|
|
+ return zbx.item_enable(name,search_key)
|
|
|
+ else:
|
|
|
+ return {'status': 404, 'message': u'action parameter is invalid!'}
|
|
|
+
|
|
|
+api.add_resource(Index, '/index')
|
|
|
+api.add_resource(ApiInterface, '/api/')
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ app.run(host='0.0.0.0',port=8011,debug=True)
|