master
py 101 lines 3.16 KB
Raw
1 #!/usr/bin/env python3
2
3 import argparse
4 import sys
5 from pathlib import Path
6
7 from ruamel.yaml import YAML
8
9 from _common import load_yaml
10
11
12 def contexts_from_metadata(metadata, module_name):
13 modules = [
14 module for module in metadata.get('modules', [])
15 if module.get('meta', {}).get('module_name') == module_name
16 ]
17 contexts = []
18 for module in modules:
19 for scope in module.get('metrics', {}).get('scopes', []):
20 for metric in scope.get('metrics', []):
21 name = metric.get('name')
22 if name and name not in contexts:
23 contexts.append(name)
24 return contexts
25
26
27 def default_module_name(metadata):
28 modules = metadata.get('modules', [])
29 if len(modules) != 1:
30 return None
31 return modules[0]['meta']['module_name']
32
33
34 def monitored_title(metadata, module_name):
35 for module in metadata.get('modules', []):
36 if module.get('meta', {}).get('module_name') == module_name:
37 return module.get('meta', {}).get('monitored_instance', {}).get('name', module_name)
38 return module_name
39
40
41 def build_seed(metadata_path, module_name, section_id, placement_id, icon):
42 metadata = load_yaml(metadata_path)
43 if not metadata:
44 raise SystemExit(f'Failed to load {metadata_path}')
45
46 if not module_name:
47 module_name = default_module_name(metadata)
48 if not module_name:
49 raise SystemExit('metadata.yaml has multiple modules; pass --module-name explicitly.')
50
51 contexts = contexts_from_metadata(metadata, module_name)
52 if not contexts:
53 raise SystemExit(f'No metric contexts found for module {module_name}.')
54
55 if not placement_id:
56 placement_id = module_name.replace('_', '-')
57 title = monitored_title(metadata, module_name)
58
59 placement = {
60 'id': placement_id,
61 'section_id': section_id,
62 'title': title,
63 'items': contexts,
64 }
65 if icon:
66 placement['icon'] = icon
67
68 return {
69 'taxonomy_version': 1,
70 'plugin_name': metadata['plugin_name'],
71 'module_name': module_name,
72 'placements': [placement],
73 }
74
75
76 def main():
77 parser = argparse.ArgumentParser(description='Seed a collector taxonomy.yaml from metadata.yaml contexts.')
78 parser.add_argument('metadata_yaml', type=Path)
79 parser.add_argument('--module-name', help='metadata.yaml module name to seed.')
80 parser.add_argument('--section-id', default='TODO.section', help='Initial section_id value.')
81 parser.add_argument('--placement-id', help='Initial placement id. Defaults to module-name with underscores replaced.')
82 parser.add_argument('--icon', help='Optional icon id.')
83 parser.add_argument('--output', type=Path, help='Write to this path instead of stdout.')
84 args = parser.parse_args()
85
86 seed = build_seed(args.metadata_yaml, args.module_name, args.section_id, args.placement_id, args.icon)
87 yaml = YAML()
88 yaml.default_flow_style = False
89 yaml.indent(mapping=2, sequence=4, offset=2)
90
91 if args.output:
92 with args.output.open('w') as fp:
93 yaml.dump(seed, fp)
94 else:
95 yaml.dump(seed, sys.stdout)
96
97 return 0
98
99
100 if __name__ == '__main__':
101 sys.exit(main())