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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
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
150 151 152 153 154 155 156 157 158 159 | |
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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
connections_clean() -> None
¤
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
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
connections_keepalive() -> None
¤
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
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
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, InventoryTasks, ParseTask, FileCopyTask, NetconfTask, SnmpTask
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
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
worker_exit() -> None
¤
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
381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
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
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
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
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
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
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
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
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | |
jinja2_nb_create_ip(prefix: str, device: str = None, interface: str = None, **kwargs: object) -> 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
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
jinja2_nb_create_prefix(parent: str, description: str, prefixlen: int = 30, **kwargs: object) -> 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
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
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
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
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
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
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
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | |
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
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 | |
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
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 | |
refresh_nornir(job: Job) -> 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.
Args: job: NorFab Job object containing relevant metadata
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
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 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 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 | |