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