|
| 1 | +# Extending Node Scraper |
| 2 | + |
| 3 | +This guide covers how to integrate Nodescraper into another Python tool and how to create and use external plugins. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | +- [nodescraper integration](#nodescraper-integration) |
| 7 | +- [external plugins](#external-plugins) |
| 8 | + |
| 9 | +## nodescraper integration |
| 10 | +Nodescraper can be integrated inside another Python tool by leveraging its classes and functionality. |
| 11 | +See below for a comprehensive example on how to create plugins and run the associated data |
| 12 | +collection and analysis. |
| 13 | +Sample run command: |
| 14 | +```sh |
| 15 | +python3 sample.py |
| 16 | +``` |
| 17 | + |
| 18 | +Sample.py file: |
| 19 | +```python |
| 20 | +import logging |
| 21 | +import sys |
| 22 | +from nodescraper.plugins.inband.bios.bios_plugin import BiosPlugin |
| 23 | +from nodescraper.plugins.inband.bios.analyzer_args import BiosAnalyzerArgs |
| 24 | +from nodescraper.plugins.inband.kernel.kernel_plugin import KernelPlugin |
| 25 | +from nodescraper.plugins.inband.kernel.analyzer_args import KernelAnalyzerArgs |
| 26 | +from nodescraper.plugins.inband.os.os_plugin import OsPlugin |
| 27 | +from nodescraper.plugins.inband.os.analyzer_args import OsAnalyzerArgs |
| 28 | +from nodescraper.models.systeminfo import SystemInfo, OSFamily |
| 29 | +from nodescraper.enums import EventPriority, SystemLocation |
| 30 | +from nodescraper.resultcollators.tablesummary import TableSummary |
| 31 | +from nodescraper.connection.inband.inbandmanager import InBandConnectionManager |
| 32 | +from nodescraper.connection.inband.sshparams import SSHConnectionParams |
| 33 | +from nodescraper.pluginregistry import PluginRegistry |
| 34 | +from nodescraper.models.pluginconfig import PluginConfig |
| 35 | +from nodescraper.pluginexecutor import PluginExecutor |
| 36 | + |
| 37 | +def main(): |
| 38 | + |
| 39 | + #setting up my custom logger |
| 40 | + log_level = "INFO" |
| 41 | + handlers = [logging.StreamHandler(stream=sys.stdout)] |
| 42 | + logging.basicConfig( |
| 43 | + force=True, |
| 44 | + level=log_level, |
| 45 | + format="%(asctime)25s %(levelname)10s %(name)25s | %(message)s", |
| 46 | + datefmt="%Y-%m-%d %H:%M:%S %Z", |
| 47 | + handlers=handlers, |
| 48 | + encoding="utf-8", |
| 49 | + ) |
| 50 | + logging.root.setLevel(logging.INFO) |
| 51 | + logging.getLogger("paramiko").setLevel(logging.ERROR) |
| 52 | + logger = logging.getLogger("nodescraper") |
| 53 | + |
| 54 | + #setting up system info |
| 55 | + system_info = SystemInfo(name="test_host", |
| 56 | + platform="X", |
| 57 | + os_familty=OSFamily.LINUX, |
| 58 | + sku="some_sku") |
| 59 | + |
| 60 | + #initiate plugins |
| 61 | + bios_plugin = BiosPlugin(system_info=system_info, logger=logger) |
| 62 | + kernel_plugin = KernelPlugin(system_info=system_info, logger=logger) |
| 63 | + |
| 64 | + #launch data collection |
| 65 | + _ = bios_plugin.collect() |
| 66 | + _ = kernel_plugin.collect() |
| 67 | + |
| 68 | + #launch data analysis |
| 69 | + bios_plugin.analyze(analysis_args=BiosAnalyzerArgs(exp_bios_version="XYZ")) |
| 70 | + kernel_plugin.analyze(analysis_args=KernelAnalyzerArgs(exp_kernel="ABC")) |
| 71 | + |
| 72 | + #log plugin data models |
| 73 | + logger.info(kernel_plugin.data.model_dump()) |
| 74 | + logger.info(bios_plugin.data.model_dump()) |
| 75 | + |
| 76 | + #alternate method |
| 77 | + all_res = [] |
| 78 | + |
| 79 | + #launch plugin collection & analysis |
| 80 | + bios_result = bios_plugin.run(analysis_args={"exp_bios_version":"ABC"}) |
| 81 | + all_res.append(bios_result) |
| 82 | + table_summary = TableSummary() |
| 83 | + table_summary.collate_results(all_res, None) |
| 84 | + |
| 85 | + #remote connection |
| 86 | + system_info.location=SystemLocation.REMOTE |
| 87 | + ssh_params = SSHConnectionParams(hostname="my_system", |
| 88 | + port=22, |
| 89 | + username="my_username", |
| 90 | + key_filename="/home/user/.ssh/ssh_key") |
| 91 | + conn_manager = InBandConnectionManager(system_info=system_info, connection_args=ssh_params) |
| 92 | + os_plugin = OsPlugin(system_info=system_info, logger=logger, connection_manager=conn_manager) |
| 93 | + os_plugin.run(analysis_args=OsAnalyzerArgs(exp_os="DEF")) |
| 94 | + |
| 95 | + #run multiple plugins through a queue |
| 96 | + system_info.location=SystemLocation.LOCAL |
| 97 | + config_dict = { |
| 98 | + "global_args": { |
| 99 | + "collection" : 1, |
| 100 | + "analysis" : 1 |
| 101 | + }, |
| 102 | + "plugins": { |
| 103 | + "BiosPlugin": { |
| 104 | + "analysis_args": { |
| 105 | + "exp_bios_version": "123", |
| 106 | + } |
| 107 | + }, |
| 108 | + "KernelPlugin": { |
| 109 | + "analysis_args": { |
| 110 | + "exp_kernel": "ABC", |
| 111 | + } |
| 112 | + } |
| 113 | + }, |
| 114 | + "result_collators": {}, |
| 115 | + "name": "plugin_config", |
| 116 | + "desc": "Auto generated config" |
| 117 | + } |
| 118 | + |
| 119 | + config1 = PluginConfig(**config_dict) |
| 120 | + plugin_executor = PluginExecutor( |
| 121 | + logger=logger, |
| 122 | + plugin_configs=[config1], |
| 123 | + system_info=system_info |
| 124 | + ) |
| 125 | + results = plugin_executor.run_queue() |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
| 131 | +``` |
| 132 | + |
| 133 | +## external plugins |
| 134 | +External plugins can be added and installed in the same env as node-scraper plugins. See -> [docs/node-scraper-external/README.md](docs/node-scraper-external/README.md) |
0 commit comments