gen_integrations.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import re
  5. import sys
  6. from copy import deepcopy
  7. from pathlib import Path
  8. from jsonschema import Draft7Validator, ValidationError
  9. from referencing import Registry, Resource
  10. from referencing.jsonschema import DRAFT7
  11. from ruamel.yaml import YAML, YAMLError
  12. AGENT_REPO = 'netdata/netdata'
  13. INTEGRATIONS_PATH = Path(__file__).parent
  14. TEMPLATE_PATH = INTEGRATIONS_PATH / 'templates'
  15. OUTPUT_PATH = INTEGRATIONS_PATH / 'integrations.js'
  16. JSON_PATH = INTEGRATIONS_PATH / 'integrations.json'
  17. CATEGORIES_FILE = INTEGRATIONS_PATH / 'categories.yaml'
  18. REPO_PATH = INTEGRATIONS_PATH.parent
  19. SCHEMA_PATH = INTEGRATIONS_PATH / 'schemas'
  20. DISTROS_FILE = REPO_PATH / '.github' / 'data' / 'distros.yml'
  21. METADATA_PATTERN = '*/metadata.yaml'
  22. COLLECTOR_SOURCES = [
  23. (AGENT_REPO, REPO_PATH / 'src' / 'collectors', True),
  24. (AGENT_REPO, REPO_PATH / 'src' / 'collectors' / 'charts.d.plugin', True),
  25. (AGENT_REPO, REPO_PATH / 'src' / 'collectors' / 'python.d.plugin', True),
  26. (AGENT_REPO, REPO_PATH / 'src' / 'go' / 'collectors' / 'go.d.plugin' / 'modules', True),
  27. ]
  28. DEPLOY_SOURCES = [
  29. (AGENT_REPO, INTEGRATIONS_PATH / 'deploy.yaml', False),
  30. ]
  31. EXPORTER_SOURCES = [
  32. (AGENT_REPO, REPO_PATH / 'src' / 'exporting', True),
  33. ]
  34. NOTIFICATION_SOURCES = [
  35. (AGENT_REPO, REPO_PATH / 'src' / 'health' / 'notifications', True),
  36. (AGENT_REPO, INTEGRATIONS_PATH / 'cloud-notifications' / 'metadata.yaml', False),
  37. ]
  38. COLLECTOR_RENDER_KEYS = [
  39. 'alerts',
  40. 'metrics',
  41. 'overview',
  42. 'related_resources',
  43. 'setup',
  44. 'troubleshooting',
  45. ]
  46. EXPORTER_RENDER_KEYS = [
  47. 'overview',
  48. 'setup',
  49. 'troubleshooting',
  50. ]
  51. NOTIFICATION_RENDER_KEYS = [
  52. 'overview',
  53. 'setup',
  54. 'troubleshooting',
  55. ]
  56. CUSTOM_TAG_PATTERN = re.compile('\\{% if .*?%\\}.*?\\{% /if %\\}|\\{%.*?%\\}', flags=re.DOTALL)
  57. FIXUP_BLANK_PATTERN = re.compile('\\\\\\n *\\n')
  58. GITHUB_ACTIONS = os.environ.get('GITHUB_ACTIONS', False)
  59. DEBUG = os.environ.get('DEBUG', False)
  60. def debug(msg):
  61. if GITHUB_ACTIONS:
  62. print(f':debug:{ msg }')
  63. elif DEBUG:
  64. print(f'>>> { msg }')
  65. else:
  66. pass
  67. def warn(msg, path):
  68. if GITHUB_ACTIONS:
  69. print(f':warning file={ path }:{ msg }')
  70. else:
  71. print(f'!!! WARNING:{ path }:{ msg }')
  72. def retrieve_from_filesystem(uri):
  73. path = SCHEMA_PATH / Path(uri)
  74. contents = json.loads(path.read_text())
  75. return Resource.from_contents(contents, DRAFT7)
  76. registry = Registry(retrieve=retrieve_from_filesystem)
  77. CATEGORY_VALIDATOR = Draft7Validator(
  78. {'$ref': './categories.json#'},
  79. registry=registry,
  80. )
  81. DEPLOY_VALIDATOR = Draft7Validator(
  82. {'$ref': './deploy.json#'},
  83. registry=registry,
  84. )
  85. EXPORTER_VALIDATOR = Draft7Validator(
  86. {'$ref': './exporter.json#'},
  87. registry=registry,
  88. )
  89. NOTIFICATION_VALIDATOR = Draft7Validator(
  90. {'$ref': './notification.json#'},
  91. registry=registry,
  92. )
  93. COLLECTOR_VALIDATOR = Draft7Validator(
  94. {'$ref': './collector.json#'},
  95. registry=registry,
  96. )
  97. _jinja_env = False
  98. def get_jinja_env():
  99. global _jinja_env
  100. if not _jinja_env:
  101. from jinja2 import Environment, FileSystemLoader, select_autoescape
  102. _jinja_env = Environment(
  103. loader=FileSystemLoader(TEMPLATE_PATH),
  104. autoescape=select_autoescape(),
  105. block_start_string='[%',
  106. block_end_string='%]',
  107. variable_start_string='[[',
  108. variable_end_string=']]',
  109. comment_start_string='[#',
  110. comment_end_string='#]',
  111. trim_blocks=True,
  112. lstrip_blocks=True,
  113. )
  114. _jinja_env.globals.update(strfy=strfy)
  115. return _jinja_env
  116. def strfy(value):
  117. if isinstance(value, bool):
  118. return "yes" if value else "no"
  119. if isinstance(value, str):
  120. return ' '.join([v.strip() for v in value.strip().split("\n") if v]).replace('|', '/')
  121. return value
  122. def get_category_sets(categories):
  123. default = set()
  124. valid = set()
  125. for c in categories:
  126. if 'id' in c:
  127. valid.add(c['id'])
  128. if c.get('collector_default', False):
  129. default.add(c['id'])
  130. if 'children' in c and c['children']:
  131. d, v = get_category_sets(c['children'])
  132. default |= d
  133. valid |= v
  134. return (default, valid)
  135. def get_collector_metadata_entries():
  136. ret = []
  137. for r, d, m in COLLECTOR_SOURCES:
  138. if d.exists() and d.is_dir() and m:
  139. for item in d.glob(METADATA_PATTERN):
  140. ret.append((r, item))
  141. elif d.exists() and d.is_file() and not m:
  142. if d.match(METADATA_PATTERN):
  143. ret.append(d)
  144. return ret
  145. def load_yaml(src):
  146. yaml = YAML(typ='safe')
  147. if not src.is_file():
  148. warn(f'{ src } is not a file.', src)
  149. return False
  150. try:
  151. contents = src.read_text()
  152. except (IOError, OSError):
  153. warn(f'Failed to read { src }.', src)
  154. return False
  155. try:
  156. data = yaml.load(contents)
  157. except YAMLError:
  158. warn(f'Failed to parse { src } as YAML.', src)
  159. return False
  160. return data
  161. def load_categories():
  162. categories = load_yaml(CATEGORIES_FILE)
  163. if not categories:
  164. sys.exit(1)
  165. try:
  166. CATEGORY_VALIDATOR.validate(categories)
  167. except ValidationError:
  168. warn(f'Failed to validate { CATEGORIES_FILE } against the schema.', CATEGORIES_FILE)
  169. sys.exit(1)
  170. return categories
  171. def load_collectors():
  172. ret = []
  173. entries = get_collector_metadata_entries()
  174. for repo, path in entries:
  175. debug(f'Loading { path }.')
  176. data = load_yaml(path)
  177. if not data:
  178. continue
  179. try:
  180. COLLECTOR_VALIDATOR.validate(data)
  181. except ValidationError:
  182. warn(f'Failed to validate { path } against the schema.', path)
  183. continue
  184. for idx, item in enumerate(data['modules']):
  185. item['meta']['plugin_name'] = data['plugin_name']
  186. item['integration_type'] = 'collector'
  187. item['_src_path'] = path
  188. item['_repo'] = repo
  189. item['_index'] = idx
  190. ret.append(item)
  191. return ret
  192. def _load_deploy_file(file, repo):
  193. ret = []
  194. debug(f'Loading { file }.')
  195. data = load_yaml(file)
  196. if not data:
  197. return []
  198. try:
  199. DEPLOY_VALIDATOR.validate(data)
  200. except ValidationError:
  201. warn(f'Failed to validate { file } against the schema.', file)
  202. return []
  203. for idx, item in enumerate(data):
  204. item['integration_type'] = 'deploy'
  205. item['_src_path'] = file
  206. item['_repo'] = repo
  207. item['_index'] = idx
  208. ret.append(item)
  209. return ret
  210. def load_deploy():
  211. ret = []
  212. for repo, path, match in DEPLOY_SOURCES:
  213. if match and path.exists() and path.is_dir():
  214. for file in path.glob(METADATA_PATTERN):
  215. ret.extend(_load_deploy_file(file, repo))
  216. elif not match and path.exists() and path.is_file():
  217. ret.extend(_load_deploy_file(path, repo))
  218. return ret
  219. def _load_exporter_file(file, repo):
  220. debug(f'Loading { file }.')
  221. data = load_yaml(file)
  222. if not data:
  223. return []
  224. try:
  225. EXPORTER_VALIDATOR.validate(data)
  226. except ValidationError:
  227. warn(f'Failed to validate { file } against the schema.', file)
  228. return []
  229. if 'id' in data:
  230. data['integration_type'] = 'exporter'
  231. data['_src_path'] = file
  232. data['_repo'] = repo
  233. data['_index'] = 0
  234. return [data]
  235. else:
  236. ret = []
  237. for idx, item in enumerate(data):
  238. item['integration_type'] = 'exporter'
  239. item['_src_path'] = file
  240. item['_repo'] = repo
  241. item['_index'] = idx
  242. ret.append(item)
  243. return ret
  244. def load_exporters():
  245. ret = []
  246. for repo, path, match in EXPORTER_SOURCES:
  247. if match and path.exists() and path.is_dir():
  248. for file in path.glob(METADATA_PATTERN):
  249. ret.extend(_load_exporter_file(file, repo))
  250. elif not match and path.exists() and path.is_file():
  251. ret.extend(_load_exporter_file(path, repo))
  252. return ret
  253. def _load_notification_file(file, repo):
  254. debug(f'Loading { file }.')
  255. data = load_yaml(file)
  256. if not data:
  257. return []
  258. try:
  259. NOTIFICATION_VALIDATOR.validate(data)
  260. except ValidationError:
  261. warn(f'Failed to validate { file } against the schema.', file)
  262. return []
  263. if 'id' in data:
  264. data['integration_type'] = 'notification'
  265. data['_src_path'] = file
  266. data['_repo'] = repo
  267. data['_index'] = 0
  268. return [data]
  269. else:
  270. ret = []
  271. for idx, item in enumerate(data):
  272. item['integration_type'] = 'notification'
  273. item['_src_path'] = file
  274. item['_repo'] = repo
  275. item['_index'] = idx
  276. ret.append(item)
  277. return ret
  278. def load_notifications():
  279. ret = []
  280. for repo, path, match in NOTIFICATION_SOURCES:
  281. if match and path.exists() and path.is_dir():
  282. for file in path.glob(METADATA_PATTERN):
  283. ret.extend(_load_notification_file(file, repo))
  284. elif not match and path.exists() and path.is_file():
  285. ret.extend(_load_notification_file(path, repo))
  286. return ret
  287. def make_id(meta):
  288. if 'monitored_instance' in meta:
  289. instance_name = meta['monitored_instance']['name'].replace(' ', '_')
  290. elif 'instance_name' in meta:
  291. instance_name = meta['instance_name']
  292. else:
  293. instance_name = '000_unknown'
  294. return f'{ meta["plugin_name"] }-{ meta["module_name"] }-{ instance_name }'
  295. def make_edit_link(item):
  296. item_path = item['_src_path'].relative_to(REPO_PATH)
  297. return f'https://github.com/{ item["_repo"] }/blob/master/{ item_path }'
  298. def sort_integrations(integrations):
  299. integrations.sort(key=lambda i: i['_index'])
  300. integrations.sort(key=lambda i: i['_src_path'])
  301. integrations.sort(key=lambda i: i['id'])
  302. def dedupe_integrations(integrations, ids):
  303. tmp_integrations = []
  304. for i in integrations:
  305. if ids.get(i['id'], False):
  306. first_path, first_index = ids[i['id']]
  307. warn(f'Duplicate integration ID found at { i["_src_path"] } index { i["_index"] } (original definition at { first_path } index { first_index }), ignoring that integration.', i['_src_path'])
  308. else:
  309. tmp_integrations.append(i)
  310. ids[i['id']] = (i['_src_path'], i['_index'])
  311. return tmp_integrations, ids
  312. def render_collectors(categories, collectors, ids):
  313. debug('Computing default categories.')
  314. default_cats, valid_cats = get_category_sets(categories)
  315. debug('Generating collector IDs.')
  316. for item in collectors:
  317. item['id'] = make_id(item['meta'])
  318. debug('Sorting collectors.')
  319. sort_integrations(collectors)
  320. debug('Removing duplicate collectors.')
  321. collectors, ids = dedupe_integrations(collectors, ids)
  322. clean_collectors = []
  323. idmap = {i['id']: i for i in collectors}
  324. for item in collectors:
  325. debug(f'Processing { item["id"] }.')
  326. item['edit_link'] = make_edit_link(item)
  327. clean_item = deepcopy(item)
  328. related = []
  329. for res in item['meta']['related_resources']['integrations']['list']:
  330. res_id = make_id(res)
  331. if res_id not in idmap.keys():
  332. warn(f'Could not find related integration { res_id }, ignoring it.', item['_src_path'])
  333. continue
  334. related.append({
  335. 'plugin_name': res['plugin_name'],
  336. 'module_name': res['module_name'],
  337. 'id': res_id,
  338. 'name': idmap[res_id]['meta']['monitored_instance']['name'],
  339. 'info': idmap[res_id]['meta']['info_provided_to_referring_integrations'],
  340. })
  341. item_cats = set(item['meta']['monitored_instance']['categories'])
  342. bogus_cats = item_cats - valid_cats
  343. actual_cats = item_cats & valid_cats
  344. if bogus_cats:
  345. warn(f'Ignoring invalid categories: { ", ".join(bogus_cats) }', item["_src_path"])
  346. if not item_cats:
  347. item['meta']['monitored_instance']['categories'] = list(default_cats)
  348. warn(f'{ item["id"] } does not list any caregories, adding it to: { default_cats }', item["_src_path"])
  349. else:
  350. item['meta']['monitored_instance']['categories'] = [x for x in item['meta']['monitored_instance']['categories'] if x in list(actual_cats)]
  351. for scope in item['metrics']['scopes']:
  352. if scope['name'] == 'global':
  353. scope['name'] = f'{ item["meta"]["monitored_instance"]["name"] } instance'
  354. for cfg_example in item['setup']['configuration']['examples']['list']:
  355. if 'folding' not in cfg_example:
  356. cfg_example['folding'] = {
  357. 'enabled': item['setup']['configuration']['examples']['folding']['enabled']
  358. }
  359. for key in COLLECTOR_RENDER_KEYS:
  360. if key in item.keys():
  361. template = get_jinja_env().get_template(f'{ key }.md')
  362. data = template.render(entry=item, related=related, clean=False)
  363. clean_data = template.render(entry=item, related=related, clean=True)
  364. if 'variables' in item['meta']['monitored_instance']:
  365. template = get_jinja_env().from_string(data)
  366. data = template.render(variables=item['meta']['monitored_instance']['variables'])
  367. template = get_jinja_env().from_string(clean_data)
  368. clean_data = template.render(variables=item['meta']['monitored_instance']['variables'])
  369. else:
  370. data = ''
  371. clean_data = ''
  372. item[key] = data
  373. clean_item[key] = clean_data
  374. for k in ['_src_path', '_repo', '_index']:
  375. del item[k], clean_item[k]
  376. clean_collectors.append(clean_item)
  377. return collectors, clean_collectors, ids
  378. def render_deploy(distros, categories, deploy, ids):
  379. debug('Sorting deployments.')
  380. sort_integrations(deploy)
  381. debug('Checking deployment ids.')
  382. deploy, ids = dedupe_integrations(deploy, ids)
  383. clean_deploy = []
  384. template = get_jinja_env().get_template('platform_info.md')
  385. for item in deploy:
  386. debug(f'Processing { item["id"] }.')
  387. item['edit_link'] = make_edit_link(item)
  388. clean_item = deepcopy(item)
  389. if item['platform_info']['group']:
  390. entries = [
  391. {
  392. 'version': i['version'],
  393. 'support': i['support_type'],
  394. 'arches': i.get('packages', {'arches': []})['arches'],
  395. 'notes': i['notes'],
  396. } for i in distros[item['platform_info']['group']] if i['distro'] == item['platform_info']['distro']
  397. ]
  398. else:
  399. entries = []
  400. data = template.render(entries=entries, clean=False)
  401. clean_data = template.render(entries=entries, clean=True)
  402. for method in clean_item['methods']:
  403. for command in method['commands']:
  404. command['command'] = CUSTOM_TAG_PATTERN.sub('', command['command'])
  405. command['command'] = FIXUP_BLANK_PATTERN.sub('', command['command'])
  406. item['platform_info'] = data
  407. clean_item['platform_info'] = clean_data
  408. if 'clean_additional_info' in item:
  409. clean_item['additional_info'] = item['clean_additional_info']
  410. del item['clean_additional_info'], clean_item['clean_additional_info']
  411. for k in ['_src_path', '_repo', '_index']:
  412. del item[k], clean_item[k]
  413. clean_deploy.append(clean_item)
  414. return deploy, clean_deploy, ids
  415. def render_exporters(categories, exporters, ids):
  416. debug('Sorting exporters.')
  417. sort_integrations(exporters)
  418. debug('Checking exporter ids.')
  419. exporters, ids = dedupe_integrations(exporters, ids)
  420. clean_exporters = []
  421. for item in exporters:
  422. item['edit_link'] = make_edit_link(item)
  423. clean_item = deepcopy(item)
  424. for key in EXPORTER_RENDER_KEYS:
  425. if key in item.keys():
  426. template = get_jinja_env().get_template(f'{ key }.md')
  427. data = template.render(entry=item, clean=False)
  428. clean_data = template.render(entry=item, clean=True)
  429. if 'variables' in item['meta']:
  430. template = get_jinja_env().from_string(data)
  431. data = template.render(variables=item['meta']['variables'], clean=False)
  432. template = get_jinja_env().from_string(clean_data)
  433. clean_data = template.render(variables=item['meta']['variables'], clean=True)
  434. else:
  435. data = ''
  436. clean_data = ''
  437. item[key] = data
  438. clean_item[key] = clean_data
  439. for k in ['_src_path', '_repo', '_index']:
  440. del item[k], clean_item[k]
  441. clean_exporters.append(clean_item)
  442. return exporters, clean_exporters, ids
  443. def render_notifications(categories, notifications, ids):
  444. debug('Sorting notifications.')
  445. sort_integrations(notifications)
  446. debug('Checking notification ids.')
  447. notifications, ids = dedupe_integrations(notifications, ids)
  448. clean_notifications = []
  449. for item in notifications:
  450. item['edit_link'] = make_edit_link(item)
  451. clean_item = deepcopy(item)
  452. for key in NOTIFICATION_RENDER_KEYS:
  453. if key in item.keys():
  454. template = get_jinja_env().get_template(f'{ key }.md')
  455. data = template.render(entry=item, clean=False)
  456. clean_data = template.render(entry=item, clean=True)
  457. if 'variables' in item['meta']:
  458. template = get_jinja_env().from_string(data)
  459. data = template.render(variables=item['meta']['variables'], clean=False)
  460. template = get_jinja_env().from_string(clean_data)
  461. clean_data = template.render(variables=item['meta']['variables'], clean=True)
  462. else:
  463. data = ''
  464. clean_data = ''
  465. item[key] = data
  466. clean_item[key] = clean_data
  467. for k in ['_src_path', '_repo', '_index']:
  468. del item[k], clean_item[k]
  469. clean_notifications.append(clean_item)
  470. return notifications, clean_notifications, ids
  471. def render_integrations(categories, integrations):
  472. template = get_jinja_env().get_template('integrations.js')
  473. data = template.render(
  474. categories=json.dumps(categories),
  475. integrations=json.dumps(integrations),
  476. )
  477. OUTPUT_PATH.write_text(data)
  478. def render_json(categories, integrations):
  479. JSON_PATH.write_text(json.dumps({
  480. 'categories': categories,
  481. 'integrations': integrations,
  482. }))
  483. def main():
  484. categories = load_categories()
  485. distros = load_yaml(DISTROS_FILE)
  486. collectors = load_collectors()
  487. deploy = load_deploy()
  488. exporters = load_exporters()
  489. notifications = load_notifications()
  490. collectors, clean_collectors, ids = render_collectors(categories, collectors, dict())
  491. deploy, clean_deploy, ids = render_deploy(distros, categories, deploy, ids)
  492. exporters, clean_exporters, ids = render_exporters(categories, exporters, ids)
  493. notifications, clean_notifications, ids = render_notifications(categories, notifications, ids)
  494. integrations = collectors + deploy + exporters + notifications
  495. render_integrations(categories, integrations)
  496. clean_integrations = clean_collectors + clean_deploy + clean_exporters + clean_notifications
  497. render_json(categories, clean_integrations)
  498. if __name__ == '__main__':
  499. sys.exit(main())