service.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/python2.7
  2. #-*- coding: utf-8 -*-
  3. #auth: robert_du
  4. from flask import Flask, make_response, jsonify
  5. from flask_restful import Api, Resource, reqparse
  6. from flask_apscheduler import APScheduler
  7. from datetime import datetime,timedelta
  8. from app.logger import logs
  9. import json
  10. app = Flask(__name__)
  11. app.config['JSON_AS_ASCII'] = False
  12. app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
  13. api = Api(app)
  14. from app.zbx_tools import zabbix_api
  15. Flags = {
  16. "novelapi": {"name": "book-apirs", "key_": "8080"},
  17. "novel-newmedia": {"name": "wejuan-h5", "key_": "8080"},
  18. "esbookweb": {"name": "esbook-webview", "key_": "8080"},
  19. "novelMtouch": {"name": "book-touchrs", "key_": "8080"},
  20. 'promote-server': {"name": "ec-media-tengine", "key_": "8058"},
  21. "wxmsg-sender": {"name": "ec-media-redis","key_": "8058"},
  22. "comic-web": {"name": "comic-webrs", "key_": "8080"},
  23. "comic-api": {"name": "comic-apirs", "key_": "8080"},
  24. "novelMtouch": {"name": "book-touchrs", "key_": "8080"},
  25. "query-analyzer": {"name": "qd01-tech2-qs", "key_": "9005"},
  26. "novel-hotword": {"name": "hotword-rs", "key_": "net.tcp.port[{HOST.IP},80]"},
  27. "novel_search_front": {"name": "hotword-rs", "key_": "net.tcp.port[{HOST.IP},8080]"},
  28. "novelsuggestion": {"name": "search-rs", "key_": "8081"},
  29. "novelh5": {"name": "ec-h5", "key_": "8080"},
  30. "wapage2web": {"name": "qd2-irs", "key_": "8080"},
  31. }
  32. parser = reqparse.RequestParser()
  33. parser.add_argument('ident', type=str, required=True, help=u'ident can not be empty!\nfor example ident=novelapi')
  34. parser.add_argument('action', type=str, required=True, help=u'action can not be empty!\nfor example action=item_disable,action=item_enable')
  35. messages = {
  36. 'status':0,
  37. 'message':'',
  38. }
  39. seduler = APScheduler()
  40. seduler.init_app(app)
  41. seduler.start()
  42. logger = logs()
  43. class Index(Resource):
  44. def get(self):
  45. return "hello world!"
  46. def schedule(ident):
  47. name = Flags.get(ident)['name']
  48. search_key = Flags.get(ident)['key_']
  49. zbx = zabbix_api()
  50. zbx.item_enable(name,search_key)
  51. def add_task(ident):
  52. if seduler.get_job(ident):
  53. seduler.remove_job(ident)
  54. else:
  55. dt = datetime.now()+timedelta(minutes=30)
  56. logger.info("定时任务: %s,%s,%s,%s,%s,%s" % (dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second))
  57. 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,])
  58. class ApiInterface(Resource):
  59. def get(self):
  60. data= parser.parse_args()
  61. print data['action'],data['ident']
  62. if not Flags.has_key(data['ident']):
  63. return jsonify({"message": "object whose ident is %s was not found." % data['ident']})
  64. if data['action'] == 'item_disable':
  65. name = Flags.get(data['ident'])['name']
  66. search_key = Flags.get(data['ident'])['key_']
  67. zbx = zabbix_api()
  68. #return zbx.item_disable(name,search_key)
  69. result = zbx.item_disable(name,search_key)
  70. add_task(data['ident'])
  71. return result
  72. elif data['action'] == 'item_enable':
  73. name = Flags.get(data['ident'])['name']
  74. search_key = Flags.get(data['ident'])['key_']
  75. if seduler.get_job(data['ident']):
  76. seduler.remove_job(data['ident'])
  77. zbx = zabbix_api()
  78. return zbx.item_enable(name,search_key)
  79. else:
  80. return {'status': 404, 'message': u'action parameter is invalid!'}
  81. api.add_resource(Index, '/index')
  82. api.add_resource(ApiInterface, '/api/')
  83. if __name__ == '__main__':
  84. app.run(host='0.0.0.0',port=8011,debug=True)