summaryrefslogtreecommitdiffstats
path: root/integrations/check_collector_metadata.py
blob: 4e610b068dd0306d4da2d24dfd8dd3503ab6f3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3

import sys

from pathlib import Path

from jsonschema import ValidationError

from gen_integrations import (CATEGORIES_FILE, SINGLE_PATTERN, MULTI_PATTERN, SINGLE_VALIDATOR, MULTI_VALIDATOR,
                              load_yaml, get_category_sets)


def main():
    if len(sys.argv) != 2:
        print(':error:This script takes exactly one argument.')
        return 2

    check_path = Path(sys.argv[1])

    if not check_path.is_file():
        print(f':error file={ check_path }:{ check_path } does not appear to be a regular file.')
        return 1

    if check_path.match(SINGLE_PATTERN):
        variant = 'single'
        print(f':debug:{ check_path } appears to be single-module metadata.')
    elif check_path.match(MULTI_PATTERN):
        variant = 'multi'
        print(f':debug:{ check_path } appears to be multi-module metadata.')
    else:
        print(f':error file={ check_path }:{ check_path } does not match required file name format.')
        return 1

    categories = load_yaml(CATEGORIES_FILE)

    if not categories:
        print(':error:Failed to load categories file.')
        return 2

    _, valid_categories = get_category_sets(categories)

    data = load_yaml(check_path)

    if not data:
        print(f':error file={ check_path }:Failed to load data from { check_path }.')
        return 1

    check_modules = []

    if variant == 'single':
        try:
            SINGLE_VALIDATOR.validate(data)
        except ValidationError as e:
            print(f':error file={ check_path }:Failed to validate { check_path } against the schema.')
            raise e
        else:
            check_modules.append(data)
    elif variant == 'multi':
        try:
            MULTI_VALIDATOR.validate(data)
        except ValidationError as e:
            print(f':error file={ check_path }:Failed to validate { check_path } against the schema.')
            raise e
        else:
            for item in data['modules']:
                item['meta']['plugin_name'] = data['plugin_name']
                check_modules.append(item)
    else:
        print(':error:Internal error encountered.')
        return 2

    failed = False

    for idx, module in enumerate(check_modules):
        invalid_cats = set(module['meta']['monitored_instance']['categories']) - valid_categories

        if invalid_cats:
            print(f':error file={ check_path }:Invalid categories found in module { idx } in { check_path }: { ", ".joiin(invalid_cats) }.')
            failed = True

    if failed:
        return 1
    else:
        print('{ check_path } is a valid collector metadata file.')
        return 0


if __name__ == '__main__':
    sys.exit(main())