ApplicationCDCMonitor.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. print(errrepo.json(),'---error---')
  30. message = repo.json()
  31. print(message)
  32. for json in message:
  33. task_id = json['id']
  34. tso = json['checkpoint_tso']
  35. error = json['error']
  36. if error is not None:
  37. requests.delete(url='http://{}:{}{}/{}'.format(self.cdc_url, self.cdc_port,
  38. self.cdc_changefeeds_url, task_id))
  39. status = os.system(
  40. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create --pd=http://192.168.0.184:2379 '
  41. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  42. '--changefeed-id={task_id} --start-ts {tso}'.format(tso=tso, task_id=task_id))
  43. print(status)
  44. backResult = requests.get(
  45. url='http://{}:{}{}/{}'.format(self.cdc_url, self.cdc_port, self.cdc_changefeeds_url, task_id),
  46. headers=headers)
  47. back = backResult.json()
  48. print(back)
  49. if back is None:
  50. os.system(
  51. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create --pd=http://192.168.0.184:2379 '
  52. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  53. '--changefeed-id={task_id} '.format(task_id=task_id))
  54. else:
  55. print(back, '--------2-------')
  56. if back['error'] is not None:
  57. requests.delete(url='http://{}:{}{}/{}'.format(self.cdc_url, self.cdc_port,
  58. self.cdc_changefeeds_url, task_id))
  59. os.system(
  60. 'cd /data/tidb-deploy/cdc-8300/bin; ./cdc cli changefeed create '
  61. '--pd=http://192.168.0.184:2379 '
  62. '--sink-uri="mysql://data:hcst@2021@139.186.27.96:3390" --config ../conf/{task_id}.toml '
  63. '--changefeed-id={task_id} '.format(task_id=task_id))
  64. else:
  65. logging.info('新创建没有问题')
  66. else:
  67. logging.info("无异常")
  68. if __name__ == '__main__':
  69. ApplicationCDCMonitor().Headler()