ApplicationCDCMonitor.py 3.9 KB

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