gen_integrations.py 20 KB

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