check_collector_metadata.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. import sys
  3. from pathlib import Path
  4. from jsonschema import ValidationError
  5. from gen_integrations import (CATEGORIES_FILE, SINGLE_PATTERN, MULTI_PATTERN, SINGLE_VALIDATOR, MULTI_VALIDATOR,
  6. load_yaml, get_category_sets)
  7. def main():
  8. if len(sys.argv) != 2:
  9. print(':error:This script takes exactly one argument.')
  10. return 2
  11. check_path = Path(sys.argv[1])
  12. if not check_path.is_file():
  13. print(f':error file={ check_path }:{ check_path } does not appear to be a regular file.')
  14. return 1
  15. if check_path.match(SINGLE_PATTERN):
  16. variant = 'single'
  17. print(f':debug:{ check_path } appears to be single-module metadata.')
  18. elif check_path.match(MULTI_PATTERN):
  19. variant = 'multi'
  20. print(f':debug:{ check_path } appears to be multi-module metadata.')
  21. else:
  22. print(f':error file={ check_path }:{ check_path } does not match required file name format.')
  23. return 1
  24. categories = load_yaml(CATEGORIES_FILE)
  25. if not categories:
  26. print(':error:Failed to load categories file.')
  27. return 2
  28. _, valid_categories = get_category_sets(categories)
  29. data = load_yaml(check_path)
  30. if not data:
  31. print(f':error file={ check_path }:Failed to load data from { check_path }.')
  32. return 1
  33. check_modules = []
  34. if variant == 'single':
  35. try:
  36. SINGLE_VALIDATOR.validate(data)
  37. except ValidationError as e:
  38. print(f':error file={ check_path }:Failed to validate { check_path } against the schema.')
  39. raise e
  40. else:
  41. check_modules.append(data)
  42. elif variant == 'multi':
  43. try:
  44. MULTI_VALIDATOR.validate(data)
  45. except ValidationError as e:
  46. print(f':error file={ check_path }:Failed to validate { check_path } against the schema.')
  47. raise e
  48. else:
  49. for item in data['modules']:
  50. item['meta']['plugin_name'] = data['plugin_name']
  51. check_modules.append(item)
  52. else:
  53. print(':error:Internal error encountered.')
  54. return 2
  55. failed = False
  56. for idx, module in enumerate(check_modules):
  57. invalid_cats = set(module['meta']['monitored_instance']['categories']) - valid_categories
  58. if invalid_cats:
  59. print(f':error file={ check_path }:Invalid categories found in module { idx } in { check_path }: { ", ".joiin(invalid_cats) }.')
  60. failed = True
  61. if failed:
  62. return 1
  63. else:
  64. print('{ check_path } is a valid collector metadata file.')
  65. return 0
  66. if __name__ == '__main__':
  67. sys.exit(main())