ApplicationCDCMonitor.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/10/20 3:39 下午
  4. # @Site :
  5. # @File : ApplicationCDCMonitor.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import logging
  10. from Apietl.constant.ConfConstant import ConfConstant
  11. import requests
  12. import os
  13. class ApplicationCDCMonitor:
  14. def __init__(self):
  15. self.cdc_url = ConfConstant.CDC_URL
  16. self.pd_url = ConfConstant.TIDB_HOST
  17. self.cdc_port = 8300
  18. self.pd_port = 2379
  19. self.cdc_job = 'application'
  20. self.cdc_status_url = '/api/v1/status'
  21. self.cdc_health_url = 'GET /api/v1/health'
  22. self.cdc_changefeeds_url = '/api/v1/changefeeds'
  23. def Headler(self):
  24. headers = {'Content-Type': 'application/json'}
  25. repo = requests.get(url='http://{}:{}{}?'.format(self.cdc_url, self.cdc_port, self.cdc_changefeeds_url),
  26. headers=headers)
  27. errrepo = requests.get(url='http://{}:{}{}?state=error'.format(self.cdc_url, self.cdc_port, self.cdc_changefeeds_url),
  28. headers=headers)
  29. errtask = errrepo.json()
  30. if errtask is not None:
  31. os.system(
  32. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create --pd=http://192.168.0.184:2379 '
  33. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  34. '--changefeed-id={task_id} '.format(task_id=errtask['id']))
  35. message = repo.json()
  36. #print(message)
  37. for json in message:
  38. task_id = json['id']
  39. tso = json['checkpoint_tso']
  40. error = json['error']
  41. if error is not None:
  42. requests.delete(url='http://{}:{}{}/{}'.format(self.cdc_url, self.cdc_port,
  43. self.cdc_changefeeds_url, task_id))
  44. status = os.system(
  45. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create --pd=http://192.168.0.184:2379 '
  46. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  47. '--changefeed-id={task_id} --start-ts {tso}'.format(tso=tso, task_id=task_id))
  48. #print(status)
  49. backResult = requests.get(
  50. url='http://{}:{}{}/{}'.format(self.cdc_url, self.cdc_port, self.cdc_changefeeds_url, task_id),
  51. headers=headers)
  52. back = backResult.json()
  53. print(back)
  54. if back is None:
  55. os.system(
  56. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create --pd=http://192.168.0.184:2379 '
  57. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  58. '--changefeed-id={task_id} '.format(task_id=task_id))
  59. else:
  60. print(back, '--------2-------')
  61. if back['error'] is not None:
  62. requests.delete(url='http://{}:{}{}/{}'.format(self.cdc_url, self.cdc_port,
  63. self.cdc_changefeeds_url, task_id))
  64. os.system(
  65. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create '
  66. '--pd=http://192.168.0.184:2379 '
  67. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  68. '--changefeed-id={task_id} '.format(task_id=task_id))
  69. else:
  70. logging.info('新创建没有问题')
  71. else:
  72. logging.info("无异常")
  73. if __name__ == '__main__':
  74. ApplicationCDCMonitor().Headler()