Nornir Worker
WatchDog(worker)
¤
Bases: WorkerWatchDog
Class to monitor Nornir worker performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worker
|
Worker
|
The worker instance that this NornirWorker will manage. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
worker |
Worker
|
The worker instance being monitored. |
connections_idle_timeout |
int
|
Timeout value for idle connections. |
connections_data |
dict
|
Dictionary to store connection use timestamps. |
started_at |
float
|
Timestamp when the watchdog was started. |
idle_connections_cleaned |
int
|
Counter for idle connections cleaned. |
dead_connections_cleaned |
int
|
Counter for dead connections cleaned. |
watchdog_tasks |
list
|
List of tasks for the watchdog to run in a given order. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
stats() -> Dict
¤
Collects and returns statistics about the worker.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict
|
A dictionary containing the following keys:
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
configuration() -> Dict
¤
Returns the configuration settings for the worker.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
A dictionary containing the configuration settings:
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
connections_get() -> Dict
¤
Retrieve the current connections data.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
A dictionary containing the current connections data. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
128 129 130 131 132 133 134 135 136 137 | |
connections_update(nr: Any, plugin: str) -> None
¤
Function to update connection use timestamps for each host
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nr
|
Any
|
Nornir object |
required |
plugin
|
str
|
connection plugin name |
required |
Source code in norfab\workers\nornir_worker\nornir_worker.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
connections_clean()
¤
Cleans up idle connections based on the configured idle timeout.
This method checks for connections that have been idle for longer than the
specified connections_idle_timeout and disconnects them. The behavior
varies depending on the value of connections_idle_timeout:
- If
connections_idle_timeoutis None, no connections are disconnected. - If
connections_idle_timeoutis 0, all connections are disconnected. - If
connections_idle_timeoutis greater than 0, only connections that have been idle for longer than the specified timeout are disconnected.
The method acquires a lock to ensure thread safety while modifying the
connections data. It logs the disconnection actions and updates the
idle_connections_cleaned counter.
Raises:
| Type | Description |
|---|---|
Exception
|
If an error occurs while attempting to disconnect idle connections, an error message is logged. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
connections_keepalive()
¤
Keepalive connections and clean up dead connections if any.
This method performs the following tasks:
- If
connections_idle_timeoutis 0, it returns immediately without performing any actions. - Attempts to acquire a lock on
worker.connections_lockto ensure thread safety. - Logs a debug message indicating that the keepalive process is running.
- Uses
HostsKeepaliveto check and clean up dead connections, updating thedead_connections_cleanedcounter. - Removes connections that are no longer present in the Nornir inventory.
- Removes hosts from
connections_dataif they have no remaining connections. - Updates the keepalive statistics for each connection plugin, including the last keepalive time and keepalive count.
- Logs an error message if an exception occurs during the keepalive process.
- Releases the lock on
worker.connections_lockin thefinallyblock to ensure it is always released.
Raises:
| Type | Description |
|---|---|
Exception
|
If an error occurs during the keepalive process, it is logged as an error. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
NornirWorker(inventory: str, broker: str, worker_name: str, exit_event=None, init_done_event=None, log_level: str = None, log_queue: object = None)
¤
Bases: NFPWorker, TaskTask, CliTask, CfgTask, TestTask, NetworkTask, RuntimeInventoryTask, ParseTask, FileCopyTask, NetconfTask
NornirWorker class for managing Nornir Service tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inventory
|
str
|
Path to the inventory file. |
required |
broker
|
str
|
Broker address. |
required |
worker_name
|
str
|
Name of the worker. |
required |
exit_event
|
Event
|
Event to signal worker exit. Defaults to None. |
None
|
init_done_event
|
Event
|
Event to signal initialization completion. Defaults to None. |
None
|
log_level
|
str
|
Logging level. Defaults to None. |
None
|
log_queue
|
object
|
Queue for logging. Defaults to None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
init_done_event |
Event
|
Event to signal initialization completion. |
tf_base_path |
str
|
Base path for files folder saved using |
connections_lock |
Lock
|
Lock for managing connections. |
nornir_inventory |
dict
|
Inventory data for Nornir. |
watchdog |
WatchDog
|
Watchdog instance for monitoring. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
worker_exit()
¤
Executes all functions registered under the "nornir-exit" hook in the inventory.
This method iterates through the list of hooks associated with the "nornir-exit" key in the inventory's hooks.
For each hook, it calls the function specified in the hook, passing the current
instance (self) as the first argument, followed by any additional positional
and keyword arguments specified in the hook.
Source code in norfab\workers\nornir_worker\nornir_worker.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
init_nornir(inventory: dict) -> None
¤
Initializes the Nornir automation framework with the provided inventory.
This method first closes any existing Nornir connections if present, optionally emitting a progress event. It then creates a new Nornir instance using the supplied inventory dictionary, which should contain configuration for logging, runner, hosts, groups, defaults, and user-defined settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inventory
|
dict
|
A dictionary containing Nornir inventory and configuration options. |
required |
Source code in norfab\workers\nornir_worker\nornir_worker.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
filter_hosts_and_validate(kwargs: Dict[str, Any], ret: Result) -> Tuple[Any, Result]
¤
Helper method to filter hosts and validate results.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[Any, Result]
|
(filtered_nornir, Result) where Result status set to
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
refresh_nornir(job: Job, progress: bool = False) -> Result
¤
Refreshes the Nornir instance by reloading the inventory from configured sources.
This method performs the following steps:
1. Loads the inventory configuration from the broker.
2. If Netbox is specified in the inventory, pulls inventory data from Netbox.
3. If Containerlab is specified in the inventory, pulls inventory data from Containerlab.
4. Initializes the Nornir instance with the refreshed inventory.
5. Optionally emits progress events at each stage if `progress` is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata |
required |
progress
|
bool
|
If True, emits progress events during the refresh process. Defaults to False. |
False
|
The inventory configuration is expected to be a dictionary with the following keys:
- "logging": A dictionary specifying logging configuration (default: {"enabled": False}).
- "runner": A dictionary specifying runner options (default: {}).
- "hosts": A dictionary specifying host details (default: {}).
- "groups": A dictionary specifying group details (default: {}).
- "defaults": A dictionary specifying default values (default: {}).
- "user_defined": A dictionary specifying user-defined options (default: {}).
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
A Result object indicating the outcome of the refresh operation. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
nornir_inventory_load_netbox(job: Job, progress: bool = False) -> Result
¤
Queries inventory data from Netbox Service and merges it into the Nornir inventory.
This function checks if there is Netbox data in the inventory and retrieves it if available. It handles retries and timeout configurations, and ensures that necessary filters or devices are specified. The retrieved inventory data is then merged into the existing Nornir inventory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata |
required |
Logs
- Critical: If the inventory has no hosts, filters, or devices defined.
- Error: If no inventory data is returned from Netbox.
- Warning: If the Netbox instance returns no hosts data.
Source code in norfab\workers\nornir_worker\nornir_worker.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
nornir_inventory_load_containerlab(job: Job, lab_name: str = None, groups: Union[None, list] = None, clab_workers: str = 'all', use_default_credentials: bool = True, progress: bool = False, dry_run: bool = False, re_init_nornir: bool = True) -> Result
¤
Pulls the Nornir inventory from a Containerlab lab instance and merges it with the existing Nornir inventory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
NorFab Job object containing relevant metadata |
required |
lab_name
|
str
|
The name of the Containerlab lab to retrieve the inventory from. |
None
|
groups
|
list
|
A list of group names to include into the hosts' inventory. |
None
|
use_default_credentials
|
bool
|
Whether to use default credentials for the hosts. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
A Result object indicating the success or failure of the operation. If successful, the Nornir inventory is updated with the retrieved data. |
Notes
- The method retrieves inventory data from a Containerlab lab using a client job.
- If the retrieved inventory contains host data, it is merged into the existing
Nornir inventory using the
merge_recursivelyfunction. - If no inventory or host data is returned, the method logs an error and marks the operation as failed.
- After successful merging of inventory, Nornir instance is re-initialized with the updated inventory.
Source code in norfab\workers\nornir_worker\nornir_worker.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
load_job_data(job_data: Any) -> Dict
¤
Helper function to download job data YAML files and load it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_data
|
str
|
job data NorFab file path to download and load using YAML. |
required |
Returns:dict data: The job data loaded from the YAML string.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the job data is a URL and the file download fails. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
jinja2_network_hosts(network: str, pfxlen: bool = False) -> list
¤
Custom Jinja2 filter that return a list of hosts for a given IP network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
str
|
The network address in CIDR notation. |
required |
pfxlen
|
bool
|
If True, include the prefix length in the returned host addresses. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
A list of host addresses as strings. If pfxlen is True, each address will include the prefix length. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 | |
jinja2_nb_create_ip(prefix: str, device: str = None, interface: str = None, **kwargs) -> str
¤
Jinja2 filter to get or create next available IP address from prefix using Netbox service.
Source code in norfab\workers\nornir_worker\nornir_worker.py
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
jinja2_nb_create_prefix(parent: str, description: str, prefixlen: int = 30, **kwargs) -> str
¤
Jinja2 filter to get or create next available prefix from parent prefix using Netbox service.
Source code in norfab\workers\nornir_worker\nornir_worker.py
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | |
jinja2_call_netbox(netbox_task: str) -> callable
¤
Returns a callable function to execute arbitrary NetBox service task.
Source code in norfab\workers\nornir_worker\nornir_worker.py
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | |
add_jinja2_netbox() -> Dict
¤
Aggregates Jinja2 NetBox-related methods and functions into a dictionary for the ease of use within Jinja2 templates.
Source code in norfab\workers\nornir_worker\nornir_worker.py
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 | |
add_jinja2_filters() -> Dict
¤
Adds custom filters for use in Jinja2 templates using | syntaxis.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict
|
A dictionary where the keys are the names of the filters and the values are the corresponding filter functions.
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 | |
get_nornir_hosts(details: bool = False, **kwargs: dict) -> Result
¤
Retrieve a list of Nornir hosts managed by this worker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
details
|
bool
|
If True, returns detailed information about each host. |
False
|
**kwargs
|
dict
|
Hosts filters to apply when retrieving hosts. |
{}
|
Returns:
| Type | Description |
|---|---|
Result
|
List[Dict]: A list of hosts with optional detailed information. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 | |
get_inventory(**kwargs: dict) -> Result
¤
Retrieve running Nornir inventory for requested hosts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
dict
|
Fx filters used to filter the inventory. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Result
|
A dictionary representation of the filtered inventory. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | |
get_version() -> Result
¤
Retrieve the versions of various libraries and system information.
This method collects the version information of a predefined set of libraries and system details such as the Python version and platform. It attempts to import each library and fetch its version. If a library is not found, it is skipped.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
a dictionary with the library names as keys and their respective version numbers as values. If a library is not found, its value will be an empty string. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 | |
get_watchdog_connections() -> Result
¤
Retrieve the list of connections currently managed by watchdog.
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
An instance of the Result class containing the current watchdog connections. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 | |